1
0
mirror of https://github.com/bitwarden/help synced 2025-12-26 05:03:21 +00:00

Promote to Master (#748)

* initial commit

* adding quotes for the array error

* Create Gemfile

* Create Gemfile.lock

* add .nvmrc and .node-version

* removed /article from URL

* update links to work with netlify

* more fixed links

* link fixes

* update bad links

* Update netlify.toml

toml test for redirects

* article redirect

* link fixes

* Update index.html

* Update netlify.toml

* Update _config.yml

* Update netlify.toml

* Update netlify.toml

* Update netlify.toml

* Update netlify.toml

* Update netlify.toml

* add article back into URL for launch

* Update netlify.toml

* Update netlify.toml

* add order to categories front matter

* Update netlify.toml

* update

* sidemenu update

* Revert "sidemenu update"

This reverts commit 5441c3d35c.

* update order prop

* Navbar updates per Gary and compiler warnings

* font/style tweaks

* Update sidebar.html

* Stage Release Documentation (#739)

* initial drafts

* rewrite Custom Fields article to prioritize new context-menu option & better organize ancillary information

* edit

* edit

* Custom Field Context Menu & CAPTCHA item in release notes

* SSO relink event

* update rn

* small edits

* improve release notes titles

* fix side menu

* Edits courtest of mportune!

* update order

* link fixes

* link cleanup

* image updates and a link

* fix trailing slash

Co-authored-by: DanHillesheim <79476558+DanHillesheim@users.noreply.github.com>
This commit is contained in:
fred_the_tech_writer
2021-09-21 13:21:11 -04:00
committed by GitHub
parent 63f78e8979
commit 906e2ca0dd
3304 changed files with 386714 additions and 8864 deletions

View File

@@ -0,0 +1,94 @@
require 'unicode/display_width/no_string_ext'
module Terminal
class Table
class Cell
##
# Cell value.
attr_reader :value
##
# Column span.
attr_reader :colspan
##
# Initialize with _options_.
def initialize options = nil
@value, options = options, {} unless Hash === options
@value = options.fetch :value, value
@alignment = options.fetch :alignment, nil
@colspan = options.fetch :colspan, 1
@width = options.fetch :width, @value.to_s.size
@index = options.fetch :index
@table = options.fetch :table
end
def alignment?
!@alignment.nil?
end
def alignment
@alignment || @table.style.alignment || :left
end
def alignment=(val)
supported = %w(left center right)
if supported.include?(val.to_s)
@alignment = val
else
raise "Aligment must be one of: #{supported.join(' ')}"
end
end
def align(val, position, length)
positions = { :left => :ljust, :right => :rjust, :center => :center }
val.public_send(positions[position], length)
end
def lines
@value.to_s.split(/\n/)
end
##
# Render the cell.
def render(line = 0)
left = " " * @table.style.padding_left
right = " " * @table.style.padding_right
display_width = Unicode::DisplayWidth.of(escape(lines[line]))
render_width = lines[line].to_s.size - display_width + width
align("#{left}#{lines[line]}#{right}", alignment, render_width + @table.cell_padding)
end
alias :to_s :render
##
# Returns the longest line in the cell and
# removes all ANSI escape sequences (e.g. color)
def value_for_column_width_recalc
lines.map{ |s| escape(s) }.max_by{ |s| Unicode::DisplayWidth.of(s) }
end
##
# Returns the width of this cell
def width
padding = (colspan - 1) * @table.cell_spacing
inner_width = (1..@colspan).to_a.inject(0) do |w, counter|
w + @table.column_width(@index + counter - 1)
end
inner_width + padding
end
##
# removes all ANSI escape sequences (e.g. color)
def escape(line)
line.to_s.gsub(/\x1b(\[|\(|\))[;?0-9]*[0-9A-Za-z]/, '').
gsub(/\x1b(\[|\(|\))[;?0-9]*[0-9A-Za-z]/, '').
gsub(/(\x03|\x1a)/, '')
end
end
end
end

View File

@@ -0,0 +1,3 @@
require 'terminal-table' #required as some people require this file directly from their Gemfiles
include Terminal::Table::TableHelper

View File

@@ -0,0 +1,52 @@
module Terminal
class Table
class Row
##
# Row cells
attr_reader :cells
attr_reader :table
##
# Initialize with _width_ and _options_.
def initialize table, array = []
@cell_index = 0
@table = table
@cells = []
array.each { |item| self << item }
end
def add_cell item
options = item.is_a?(Hash) ? item : {:value => item}
cell = Cell.new(options.merge(:index => @cell_index, :table => @table))
@cell_index += cell.colspan
@cells << cell
end
alias << add_cell
def [] index
cells[index]
end
def height
cells.map { |c| c.lines.count }.max || 0
end
def render
y = @table.style.border_y
(0...height).to_a.map do |line|
y + cells.map do |cell|
cell.render(line)
end.join(y) + y
end.join("\n")
end
def number_of_columns
@cells.collect(&:colspan).inject(0, &:+)
end
end
end
end

View File

@@ -0,0 +1,14 @@
module Terminal
class Table
class Separator < Row
def render
arr_x = (0...@table.number_of_columns).to_a.map do |i|
@table.style.border_x * (@table.column_width(i) + @table.cell_padding)
end
border_i = @table.style.border_i
border_i + arr_x.join(border_i) + border_i
end
end
end
end

View File

@@ -0,0 +1,79 @@
module Terminal
class Table
# A Style object holds all the formatting information for a Table object
#
# To create a table with a certain style, use either the constructor
# option <tt>:style</tt>, the Table#style object or the Table#style= method
#
# All these examples have the same effect:
#
# # by constructor
# @table = Table.new(:style => {:padding_left => 2, :width => 40})
#
# # by object
# @table.style.padding_left = 2
# @table.style.width = 40
#
# # by method
# @table.style = {:padding_left => 2, :width => 40}
#
# To set a default style for all tables created afterwards use Style.defaults=
#
# Terminal::Table::Style.defaults = {:width => 80}
#
class Style
@@defaults = {
:border_x => "-", :border_y => "|", :border_i => "+",
:border_top => true, :border_bottom => true,
:padding_left => 1, :padding_right => 1,
:margin_left => '',
:width => nil, :alignment => nil,
:all_separators => false
}
attr_accessor :border_x
attr_accessor :border_y
attr_accessor :border_i
attr_accessor :border_top
attr_accessor :border_bottom
attr_accessor :padding_left
attr_accessor :padding_right
attr_accessor :margin_left
attr_accessor :width
attr_accessor :alignment
attr_accessor :all_separators
def initialize options = {}
apply self.class.defaults.merge(options)
end
def apply options
options.each { |m, v| __send__ "#{m}=", v }
end
class << self
def defaults
@@defaults
end
def defaults= options
@@defaults = defaults.merge(options)
end
end
def on_change attr
method_name = :"#{attr}="
old_method = method method_name
define_singleton_method(method_name) do |value|
old_method.call value
yield attr.to_sym, value
end
end
end
end
end

View File

@@ -0,0 +1,344 @@
require 'unicode/display_width/no_string_ext'
module Terminal
class Table
attr_reader :title
attr_reader :headings
##
# Generates a ASCII table with the given _options_.
def initialize options = {}, &block
@headings = []
@rows = []
@column_widths = []
self.style = options.fetch :style, {}
self.headings = options.fetch :headings, []
self.rows = options.fetch :rows, []
self.title = options.fetch :title, nil
yield_or_eval(&block) if block
style.on_change(:width) { require_column_widths_recalc }
end
##
# Align column _n_ to the given _alignment_ of :center, :left, or :right.
def align_column n, alignment
r = rows
column(n).each_with_index do |col, i|
cell = r[i][n]
next unless cell
cell.alignment = alignment unless cell.alignment?
end
end
##
# Add a row.
def add_row array
row = array == :separator ? Separator.new(self) : Row.new(self, array)
@rows << row
require_column_widths_recalc unless row.is_a?(Separator)
end
alias :<< :add_row
##
# Add a separator.
def add_separator
self << :separator
end
def cell_spacing
cell_padding + style.border_y.length
end
def cell_padding
style.padding_left + style.padding_right
end
##
# Return column _n_.
def column n, method = :value, array = rows
array.map { |row|
# for each cells in a row, find the column with index
# just greater than the required one, and go back one.
index = col = 0
row.cells.each do |cell|
break if index > n
index += cell.colspan
col += 1
end
cell = row[col - 1]
cell && method ? cell.__send__(method) : cell
}.compact
end
##
# Return _n_ column including headings.
def column_with_headings n, method = :value
column n, method, headings_with_rows
end
##
# Return columns.
def columns
(0...number_of_columns).map { |n| column n }
end
##
# Return length of column _n_.
def column_width n
column_widths[n] || 0
end
alias length_of_column column_width # for legacy support
##
# Return total number of columns available.
def number_of_columns
headings_with_rows.map { |r| r.number_of_columns }.max || 0
end
##
# Set the headings
def headings= arrays
arrays = [arrays] unless arrays.first.is_a?(Array)
@headings = arrays.map do |array|
row = Row.new(self, array)
require_column_widths_recalc
row
end
end
##
# Render the table.
def render
separator = Separator.new(self)
buffer = style.border_top ? [separator] : []
unless @title.nil?
buffer << Row.new(self, [title_cell_options])
buffer << separator
end
@headings.each do |row|
unless row.cells.empty?
buffer << row
buffer << separator
end
end
if style.all_separators
buffer += @rows.product([separator]).flatten
else
buffer += @rows
buffer << separator if style.border_bottom
end
buffer.map { |r| style.margin_left + r.render.rstrip }.join("\n")
end
alias :to_s :render
##
# Return rows without separator rows.
def rows
@rows.reject { |row| row.is_a? Separator }
end
def rows= array
@rows = []
array.each { |arr| self << arr }
end
def style=(options)
style.apply options
end
def style
@style ||= Style.new
end
def title=(title)
@title = title
require_column_widths_recalc
end
##
# Check if _other_ is equal to self. _other_ is considered equal
# if it contains the same headings and rows.
def == other
if other.respond_to? :render and other.respond_to? :rows
self.headings == other.headings and self.rows == other.rows
end
end
private
def columns_width
column_widths.inject(0) { |s, i| s + i + cell_spacing } + style.border_y.length
end
def recalc_column_widths
@require_column_widths_recalc = false
n_cols = number_of_columns
space_width = cell_spacing
return if n_cols == 0
# prepare rows
all_rows = headings_with_rows
all_rows << Row.new(self, [title_cell_options]) unless @title.nil?
# DP states, dp[colspan][index][split_offset] => column_width.
dp = []
# prepare initial value for DP.
all_rows.each do |row|
index = 0
row.cells.each do |cell|
cell_value = cell.value_for_column_width_recalc
cell_width = Unicode::DisplayWidth.of(cell_value.to_s)
colspan = cell.colspan
# find column width from each single cell.
dp[colspan] ||= []
dp[colspan][index] ||= [0] # add a fake cell with length 0.
dp[colspan][index][colspan] ||= 0 # initialize column length to 0.
# the last index `colspan` means width of the single column (split
# at end of each column), not a width made up of multiple columns.
single_column_length = [cell_width, dp[colspan][index][colspan]].max
dp[colspan][index][colspan] = single_column_length
index += colspan
end
end
# run DP.
(1..n_cols).each do |colspan|
dp[colspan] ||= []
(0..n_cols-colspan).each do |index|
dp[colspan][index] ||= [1]
(1...colspan).each do |offset|
# processed level became reverse map from width => [offset, ...].
left_colspan = offset
left_index = index
left_width = dp[left_colspan][left_index].keys.first
right_colspan = colspan - left_colspan
right_index = index + offset
right_width = dp[right_colspan][right_index].keys.first
dp[colspan][index][offset] = left_width + right_width + space_width
end
# reverse map it for resolution (max width and short offset first).
rmap = {}
dp[colspan][index].each_with_index do |width, offset|
rmap[width] ||= []
rmap[width] << offset
end
# sort reversely and store it back.
dp[colspan][index] = Hash[rmap.sort.reverse]
end
end
resolve = lambda do |colspan, full_width, index = 0|
# stop if reaches the bottom level.
return @column_widths[index] = full_width if colspan == 1
# choose best split offset for partition, or second best result
# if first one is not dividable.
candidate_offsets = dp[colspan][index].collect(&:last).flatten
offset = candidate_offsets[0]
offset = candidate_offsets[1] if offset == colspan
# prepare for next round.
left_colspan = offset
left_index = index
left_width = dp[left_colspan][left_index].keys.first
right_colspan = colspan - left_colspan
right_index = index + offset
right_width = dp[right_colspan][right_index].keys.first
# calculate reference column width, give remaining spaces to left.
total_non_space_width = full_width - (colspan - 1) * space_width
ref_column_width = total_non_space_width / colspan
remainder = total_non_space_width % colspan
rem_left_width = [remainder, left_colspan].min
rem_right_width = remainder - rem_left_width
ref_left_width = ref_column_width * left_colspan +
(left_colspan - 1) * space_width + rem_left_width
ref_right_width = ref_column_width * right_colspan +
(right_colspan - 1) * space_width + rem_right_width
# at most one width can be greater than the reference width.
if left_width <= ref_left_width and right_width <= ref_right_width
# use refernce width (evenly partition).
left_width = ref_left_width
right_width = ref_right_width
else
# the wider one takes its value, shorter one takes the rest.
if left_width > ref_left_width
right_width = full_width - left_width - space_width
else
left_width = full_width - right_width - space_width
end
end
# run next round.
resolve.call(left_colspan, left_width, left_index)
resolve.call(right_colspan, right_width, right_index)
end
full_width = dp[n_cols][0].keys.first
unless style.width.nil?
new_width = style.width - space_width - style.border_y.length
if new_width < full_width
raise "Table width exceeds wanted width " +
"of #{style.width} characters."
end
full_width = new_width
end
resolve.call(n_cols, full_width)
end
##
# Return headings combined with rows.
def headings_with_rows
@headings + rows
end
def yield_or_eval &block
return unless block
if block.arity > 0
yield self
else
self.instance_eval(&block)
end
end
def title_cell_options
{:value => @title, :alignment => :center, :colspan => number_of_columns}
end
def require_column_widths_recalc
@require_column_widths_recalc = true
end
def column_widths
recalc_column_widths if @require_column_widths_recalc
@column_widths
end
end
end

View File

@@ -0,0 +1,9 @@
module Terminal
class Table
module TableHelper
def table headings = [], *rows, &block
Terminal::Table.new :headings => headings.to_a, :rows => rows, &block
end
end
end
end

View File

@@ -0,0 +1,5 @@
module Terminal
class Table
VERSION = '2.0.0'
end
end