### Install TTY::Table Gem Source: https://github.com/piotrmurach/tty-table/blob/master/README.md Instructions on how to install the TTY::Table gem using Bundler or directly via the gem command. This is the first step to using the table formatting capabilities. ```ruby gem "tty-table" ``` ```bash $ bundle ``` ```bash $ gem install tty-table ``` -------------------------------- ### Configure TTY::Table Rendering Options Using a Block Source: https://context7.com/piotrmurach/tty-table/llms.txt This example shows how to configure multiple TTY::Table rendering options, such as alignment, border separators, border styles, and padding, using a convenient block syntax. It also demonstrates creating a reusable renderer instance for consistent table formatting. ```ruby require "tty-table" table = TTY::Table.new( ["Product", "Price", "Qty", "Total"], [ ["Widget A", "$10.00", "5", "$50.00"], ["Widget B", "$25.00", "2", "$50.00"], ["Widget C", "$15.00", "3", "$45.00"] ] ) puts table.render(:unicode) do |renderer| renderer.alignments = [:left, :right, :center, :right] renderer.border.separator = :each_row renderer.border.style = :blue renderer.padding = [0, 1] renderer.filter = ->(val, row, col) { col == 3 && row > 0 ? val.gsub("$", "USD ") : val } end # Creating reusable renderer renderer = TTY::Table::Renderer::Unicode.new(table, { multiline: true, padding: [0, 2], alignments: [:left, :right, :center, :right] }) # Render same table multiple times with same config puts renderer.render ``` -------------------------------- ### Control TTY::Table Width and Enable Terminal Resizing Source: https://context7.com/piotrmurach/tty-table/llms.txt This example demonstrates how to manage the width of TTY tables, including enabling automatic resizing to fit terminal dimensions. It shows rendering with `resize: true`, setting a fixed `width`, specifying individual `column_widths`, and combining resizing with padding. ```ruby require "tty-table" header = ["Column 1", "Column 2", "Column 3"] rows = [ ["Short", "Medium text", "A very long text that might need wrapping"], ["A", "B", "C"] ] table = TTY::Table.new(header, rows) # Resize to fit terminal width automatically puts table.render(:ascii, resize: true) # Set specific width puts table.render(:ascii, width: 40, resize: true) # Specify column widths manually puts table.render(:ascii, column_widths: [10, 15, 20]) # Combine resize with padding puts table.render(:ascii, resize: true, padding: [0, 1]) ``` -------------------------------- ### Querying Table Size in TTY-Table (Ruby) Source: https://github.com/piotrmurach/tty-table/blob/master/README.md Provides methods to retrieve the dimensions of a TTY::Table. You can get the number of rows, the number of columns, or both as an array. ```ruby table.rows_size # return row size table.columns_size # return column size table.size # return an array of [row_size, column_size] ``` -------------------------------- ### Define Custom TTY-Table Border Class Source: https://github.com/piotrmurach/tty-table/blob/master/README.md Provides an example of creating a custom border by subclassing `TTY::Table::Border` and defining border characters using an internal DSL. The custom border can then be applied to the table. ```ruby class MyBorder < TTY::Table::Border def_border do left "$" center "$" right "$" bottom " " bottom_mid "*" bottom_left "*" bottom_right "*" end end ``` ```ruby table = TTY::Table.new ["header1", "header2"], [["a1", "a2"], ["b1", "b2"]] table.render_with MyBorder ``` -------------------------------- ### Filter Table Fields for Formatting - Ruby Source: https://github.com/piotrmurach/tty-table/blob/master/README.md Explains how to apply filters to modify individual TTY::Table field values before rendering. This example capitalizes fields in the second column, excluding the header. ```ruby table = TTY::Table.new(["header1", "header2"], [["a1", "a2"], ["b1", "b2"]]) table.render do |renderer| renderer.filter = ->(val, row_index, col_index) do if col_index == 1 and !(row_index == 0) val.capitalize else val end end end ``` -------------------------------- ### Render TTY::Table with ASCII Borders in Ruby Source: https://context7.com/piotrmurach/tty-table/llms.txt Renders a TTY::Table using ASCII border characters for a classic terminal look. The 'tty-table' gem must be installed. This method allows for clear visual separation of table elements. ```ruby require "tty-table" table = TTY::Table.new( ["Name", "Role", "Department"], [ ["Alice", "Engineer", "R&D"], ["Bob", "Designer", "UX"], ["Charlie", "Manager", "Ops"] ] ) puts table.render(:ascii) # => # +-------+--------+----------+ # |Name |Role |Department| # +-------+--------+----------+ # |Alice |Engineer|R&D | # |Bob |Designer|UX | # |Charlie|Manager |Ops | # +-------+--------+----------+ # Using renderer class directly renderer = TTY::Table::Renderer::ASCII.new(table) puts renderer.render ``` -------------------------------- ### Filter Table Fields with Conditional Coloring - Ruby Source: https://github.com/piotrmurach/tty-table/blob/master/README.md Shows how to use a filter with the Pastel gem to conditionally color table fields. This example colors even-indexed columns red on a green background. ```ruby pastel = Pastel.new table.render do |renderer| renderer.filter = ->(val, row_index, col_index) do col_index % 2 == 1 ? pastel.red.on_green(val) : val end end ``` -------------------------------- ### Create and Render Table (Ruby) Source: https://github.com/piotrmurach/tty-table/blob/master/README.md Demonstrates the basic usage of TTY::Table by creating a table with headers and rows, and then rendering it to the console using the :ascii border style. This shows the core functionality of the gem. ```ruby table = TTY::Table.new(["header1","header2"], [["a1", "a2"], ["b1", "b2"]]) puts table.render(:ascii) ``` -------------------------------- ### Configure TTY-Table Rendering Options Source: https://github.com/piotrmurach/tty-table/blob/master/README.md Demonstrates how to pass rendering options to the TTY-Table's `render` method, either as a hash or within a block. Options control aspects like alignment, borders, and padding. ```ruby table.render(:basic, alignments: [:left, :center]) ``` ```ruby table.render(:basic) do |renderer| renderer.alignments = [:left, :center] end ``` -------------------------------- ### Initialize TTY::Table (Ruby) Source: https://github.com/piotrmurach/tty-table/blob/master/README.md Illustrates various ways to initialize a TTY::Table object. This includes direct initialization with 2D arrays, using a block, and specifying headers and rows separately or within a hash. ```ruby table = TTY::Table[["a1", "a2"], ["b1", "b2"]] table = TTY::Table.new([["a1", "a2"], ["b1", "b2"]]) table = TTY::Table.new(rows: [["a1", "a2"], ["b1", "b2"]]) ``` ```ruby table = TTY::Table.new do |t| t << ["a1", "a2"] t << ["b1", "b2"] end ``` ```ruby table = TTY::Table.new table << ["a1","a2"] table << ["b1","b2"] ``` ```ruby table = TTY::Table.new(["h1", "h2"], [["a1", "a2"], ["b1", "b2"]]) table = TTY::Table.new(header: ["h1", "h2"], rows: [["a1", "a2"], ["b1", "b2"]]) ``` ```ruby table = TTY::Table.new([{"h1" => ["a1", "a2"], "h2" => ["b1", "b2"]}]) ``` -------------------------------- ### Configuring TTY-Table Renderers with Options (Ruby) Source: https://github.com/piotrmurach/tty-table/blob/master/README.md Explains how to customize the rendering of a TTY::Table by passing rendering options. Options can be provided as a hash to the `render` method or configured within a block passed to the renderer. ```ruby table = TTY::Table.new [["a1", "a2"], ["b1", "b2"]] # Using hash for options table.render(:basic, alignments: [:left, :center]) # Using a block for options table.render(:basic) do |renderer| renderer.alignments= [:left, :center] end ``` -------------------------------- ### Create TTY-Table Unicode Border Renderer Source: https://github.com/piotrmurach/tty-table/blob/master/README.md Shows how to explicitly create and use a `TTY::Table::Renderer::Unicode` instance to render the table with a Unicode border. ```ruby renderer = TTY::Table::Renderer::Unicode.new(table) renderer.render ``` -------------------------------- ### Create TTY::Table Objects in Ruby Source: https://context7.com/piotrmurach/tty-table/llms.txt Demonstrates various ways to create TTY::Table objects, including using 2D arrays, headers and rows, keyword arguments, block-based initialization, and dynamic row addition. Requires the 'tty-table' gem. ```ruby require "tty-table" # Simple 2D array creation table = TTY::Table [["a1", "a2"], ["b1", "b2"]] # With headers and rows table = TTY::Table.new(["Name", "Age", "City"], [ ["Alice", "30", "NYC"], ["Bob", "25", "LA"], ["Charlie", "35", "Chicago"] ]) # Using keyword arguments table = TTY::Table.new( header: ["Product", "Price", "Stock"], rows: [ ["Laptop", "$999", "15"], ["Phone", "$599", "42"], ["Tablet", "$399", "28"] ] ) # Block-based initialization table = TTY::Table.new(header: ["ID", "Status"]) do |t| t << ["001", "Active"] t << ["002", "Pending"] t << ["003", "Completed"] end # Adding rows dynamically table = TTY::Table.new table << ["First", "Row"] table << ["Second", "Row"] puts table.render(:ascii) # => # +------+----+ # |First |Row | # |Second|Row | # +------+----+ ``` -------------------------------- ### Create Table with Separators - Ruby Source: https://github.com/piotrmurach/tty-table/blob/master/README.md Demonstrates how to create a TTY::Table with custom row separators using an array of rows and a separator index. This allows for more structured table layouts. ```ruby table = TTY::Table.new ["header1", "header2"], [["a1", "a2"], ["b1", "b2"], ["c1", "c2"]] table.render do |renderer| renderer.border.separator = [0, 2] end ``` -------------------------------- ### Rendering TTY-Table with ASCII Borders (Ruby) Source: https://github.com/piotrmurach/tty-table/blob/master/README.md Illustrates how to render a TTY::Table using ASCII characters to create borders. This can be done by calling `render(:ascii)` on the table instance or by creating and rendering an `TTY::Table::Renderer::ASCII` object. ```ruby table = TTY::Table.new [["a1", "a2"], ["b1", "b2"]] # Using table instance table.render(:ascii) # => # +-------+-------+ # |header1|header2| # +-------+-------+ # |a1 |a2 | # |b1 |b2 | # +-------+-------+ # Using ASCII Renderer renderer = TTY::Table::Renderer::ASCII.new(table) renderer.render # => # +-------+-------+ # |header1|header2| # +-------+-------+ # |a1 |a2 | # |b1 |b2 | # +-------+-------+ ``` -------------------------------- ### Align All TTY-Table Columns Uniformly Source: https://github.com/piotrmurach/tty-table/blob/master/README.md Illustrates aligning all columns to the same setting using the `:alignment` option. This is a simpler way to apply a single alignment style across the entire table. ```ruby table.render(:ascii, alignment: [:center]) ``` -------------------------------- ### Create Table with Keyword Separators - Ruby Source: https://github.com/piotrmurach/tty-table/blob/master/README.md Illustrates creating a TTY::Table by inserting separators using the :separator symbol within the row data or by pushing them onto the table. This provides an alternative way to define separator positions. ```ruby table = TTY::Table.new ["header1", "header2"], [:separator, ["a1", "a2"], ["b1", "b2"]] table << :separator << ["c1", "c2"] # you can push separators on too! table.render ``` -------------------------------- ### Creating Custom Renderers for TTY-Table (Ruby) Source: https://github.com/piotrmurach/tty-table/blob/master/README.md Details how to create custom renderers for TTY::Table by inheriting from `TTY::Table::Renderer`. This allows for flexible formatting of tabular data, with built-in renderers like Basic, ASCII, and Unicode. ```ruby table = TTY::Table.new [["a1", "a2"], ["b1", "b2"]] # Creating a renderer instance with multiline option multi_renderer = TTY::Table::Renderer::Basic.new(table, multiline: true) multi_renderer.render # Using Basic Renderer directly renderer = TTY::Table::Renderer::Basic.new(table) renderer.render ``` -------------------------------- ### Controlling TTY-Table Column Width and Resizing Source: https://github.com/piotrmurach/tty-table/blob/master/README.md Explains how to control table column sizes using 'width' and 'resize' options. 'resize: true' forces the table to match terminal width or a custom 'width', while 'width' alone enforces vertical rotation if content overflows. ```ruby header = ["h1", "h2", "h3"] rows = [["aaa1", "aa2", "aaaaaaa3"], ["b1", "b2", "b3"]] table = TTY::Table.new(header, rows) table.render(width: 80, resize: true) ``` -------------------------------- ### Multiline Content Rendering in TTY-Table Source: https://github.com/piotrmurach/tty-table/blob/master/README.md Demonstrates how to render tables with multiline content. The 'multiline: true' option is crucial for rows to span multiple lines, and padding can be applied to these multiline rows. ```ruby table = TTY::Table.new(header: ["head1", "head2"]) table << ["Multi\nLine", "Text\nthat\nwraps"] table << ["Some\nother\ntext", "Simple"] table.render(:ascii, multiline: true, padding: [1,2,1,2]) ``` -------------------------------- ### Apply Padding to Table - Ruby Source: https://github.com/piotrmurach/tty-table/blob/master/README.md Shows how to apply padding to a TTY::Table using the `padding` option. The padding can be specified as an array defining top, right, bottom, and left spacing. ```ruby table.render(:ascii, padding: [1,2,1,2]) ``` -------------------------------- ### Align TTY-Table Columns Individually Source: https://github.com/piotrmurach/tty-table/blob/master/README.md Shows how to set individual column alignments using the `:alignments` option in the `render` method. This allows for different alignments per column. ```ruby table.render(:ascii, alignments: [:center, :right]) ``` -------------------------------- ### Resizing TTY-Table to Full Terminal Width Source: https://github.com/piotrmurach/tty-table/blob/master/README.md Shows how to force a TTY-Table to occupy the full width of the terminal using the ':resize' option during rendering. ```ruby table.render(:ascii, resize: true) ``` -------------------------------- ### Create Table with Dynamic Separators - Ruby Source: https://github.com/piotrmurach/tty-table/blob/master/README.md Shows how to use a proc to dynamically determine where separators should be placed in a TTY::Table. This enables conditional separation based on row index. ```ruby table = TTY::Table.new ["header1", "header2"], [["a1", "a2"], ["b1", "b2"], ["c1", "c2"], ["d1", "d2"]] table.render do |renderer| renderer.border.separator = ->(row) { row == 0 || (row+1) % 2 == 0} # separate every two rows end ``` -------------------------------- ### Accessing Table Rows and Elements in TTY-Table (Ruby) Source: https://github.com/piotrmurach/tty-table/blob/master/README.md Demonstrates how to access specific rows and individual elements within a TTY::Table object using direct indexing and dedicated methods. Supports positive and negative indexing for rows, and row/column indexing for elements. Raises IndexError for out-of-bounds access. ```ruby table = TTY::Table.new [["a1","a2"], ["b1","b2"]] table[0] # => ["a1","a2"] table.row(0) # => ["a1","a2"] table.row(i) { |row| ... } # return array for row(i) table[-1] # => ["b1","b2"] table[i, j] # return element at row(i) and column(j) table[0,0] # => "a1" table.column(j) { ... } # return array for column(j) table.column(0) # => ["a1","b1"] table.column(name) # return array for column(name), name of header ``` -------------------------------- ### Align Columns in TTY::Table in Ruby Source: https://context7.com/piotrmurach/tty-table/llms.txt Demonstrates how to align columns in a TTY::Table using left, center, or right alignment. Supports per-column, global, and per-cell alignment options. Requires the 'tty-table' gem. ```ruby require "tty-table" table = TTY::Table.new( header: ["Left", "Center", "Right"], rows: [ ["abc", "def", "123"], ["x", "y", "456"], ["hello", "world", "789"] ] ) # Per-column alignment puts table.render(:ascii, alignments: [:left, :center, :right]) # => # +-----+------+-----+ # |Left |Center|Right| # +-----+------+-----+ # |abc | def | 123| # |x | y | 456| # |hello|world | 789| # +-----+------+-----+ # Global alignment for all columns puts table.render(:ascii, alignment: :center) # => # +-----+------+-----+ # |Left |Center|Right| # +-----+------+-----+ # | abc | def | 123 | # | x | y | 456 | # |hello|world | 789 | # +-----+------+-----+ # Per-cell alignment table = TTY::Table.new(header: ["Col1", "Col2"]) table << [{value: "right", alignment: :right}, "left"] table << ["left", {value: "center", alignment: :center}] puts table.render(:ascii) # => # +-----+------+ # |Col1 |Col2 | # +-----+------+ # |right|left | # |left |center| # +-----+------+ ``` -------------------------------- ### Render TTY-Table with Row Separators Source: https://github.com/piotrmurach/tty-table/blob/master/README.md Demonstrates how to force TTY-Table to render a separator line on each row by setting `renderer.border.separator = :each_row` within a render block. ```ruby table = TTY::Table.new ["header1", "header2"], [["a1", "a2"], ["b1", "b2"]] table.render do |renderer| renderer.border.separator = :each_row end ``` -------------------------------- ### Customize TTY-Table Border Parts Source: https://github.com/piotrmurach/tty-table/blob/master/README.md Illustrates how to customize individual border parts of a TTY-Table within a block passed to the `render` method. This allows for fine-tuning the border's appearance. ```ruby table = TTY::Table.new ["header1", "header2"], [["a1", "a2"], ["b1", "b2"]] table.render do |renderer| renderer.border do mid "=" mid_mid " " end end ``` -------------------------------- ### Apply Color and Separator Styles to TTY::Table Borders Source: https://context7.com/piotrmurach/tty-table/llms.txt This code illustrates how to style TTY::Table borders using colors from the Pastel gem and control border separators. It shows applying a green border with ASCII rendering, a blue border with row separators using Unicode rendering, and combining border styling with header coloring using Pastel. ```ruby require "tty-table" table = TTY::Table.new( ["Name", "Value"], [["foo", "100"], ["bar", "200"]] ) # Green border puts table.render(:ascii) do |renderer| renderer.border.style = :green end # Blue border with separator puts table.render(:unicode) do |renderer| renderer.border.style = :blue renderer.border.separator = :each_row end # Combined with pastel for header coloring require "pastel" pastel = Pastel.new header = [pastel.bold("Name"), pastel.bold("Value")] table = TTY::Table.new(header, [["foo", "100"], ["bar", "200"]]) puts table.render(:ascii) do |renderer| renderer.border.style = :cyan end ``` -------------------------------- ### Render TTY::Table with Basic (Whitespace) Formatting in Ruby Source: https://context7.com/piotrmurach/tty-table/llms.txt Renders a TTY::Table using basic whitespace-delimited output without borders. This is the default rendering method if no renderer is specified. Requires the 'tty-table' gem. ```ruby require "tty-table" table = TTY::Table.new( ["header1", "header2"], [["a1", "a2"], ["b1", "b2"]] ) # Basic whitespace rendering puts table.render(:basic) # => # header1 header2 # a1 a2 # b1 b2 # Equivalent: default render without arguments puts table.render # => # header1 header2 # a1 a2 # b1 b2 ``` -------------------------------- ### Style Table Borders with Color - Ruby Source: https://github.com/piotrmurach/tty-table/blob/master/README.md Demonstrates how to change the color of a TTY::Table's borders using the `border.style` option. It leverages the Pastel gem for color support. ```ruby table.render do |renderer| renderer.border.style = :green end ``` -------------------------------- ### Render Table with Multiline Content - Ruby Source: https://github.com/piotrmurach/tty-table/blob/master/README.md Demonstrates enabling multiline rendering in TTY::Table by setting the `multiline` option to `true`. This allows content with line breaks to wrap within cells. ```ruby table = TTY::Table.new([["First", "1"], ["Multi\nLine\nContent", "2"], ["Third", "3"]]) table.render(:ascii, multiline: true) ``` -------------------------------- ### Apply Padding to Table Cells (Ruby) Source: https://context7.com/piotrmurach/tty-table/llms.txt Demonstrates how to add padding around cell content in a TTY-Table using CSS-style padding values. Padding can be specified as an array for [top, right, bottom, left], [vertical, horizontal], or a single value for uniform padding. ```ruby require "tty-table" table = TTY::Table.new( ["Column 1", "Column 2"], [["A", "B"], ["C", "D"]] ) # Padding: [top, right, bottom, left] puts table.render(:ascii, padding: [1, 2, 1, 2]) # Symmetric padding: [vertical, horizontal] puts table.render(:ascii, padding: [0, 2]) # Uniform padding puts table.render(:ascii, padding: 1) ``` -------------------------------- ### Manipulate Table Rows (Ruby) Source: https://github.com/piotrmurach/tty-table/blob/master/README.md Shows how to add rows to a TTY::Table using the `<<` operator and how to chain row assignments. It also demonstrates iterating over the table rows using `each` and `each_with_index`. ```ruby table << ["a1", "a2", "a3"] table << ["b1", "b2", "b3"] table << ["a1", "a2"] << ["b1", "b2"] ``` ```ruby table.each { |row| ... } # iterate over rows table.each_with_index { |row, index| ... } # iterate over rows with an index ``` -------------------------------- ### Customize Table Borders (Ruby) Source: https://context7.com/piotrmurach/tty-table/llms.txt Shows how to create custom border styles for TTY-Table. This can be done by inline customization of border characters or by defining a custom border class that inherits from `TTY::Table::Border`. ```ruby require "tty-table" table = TTY::Table.new( ["header1", "header2"], [["a1", "a2"], ["b1", "b2"]] ) # Inline border customization puts table.render do |renderer| renderer.border do mid "=" mid_mid " " end end # Custom border class class MyBorder < TTY::Table::Border def_border do left "$" center "$" right "$" bottom " " bottom_mid "*" bottom_left "*" bottom_right "*" end end puts table.render_with(MyBorder) ``` -------------------------------- ### Align Individual TTY-Table Fields Source: https://github.com/piotrmurach/tty-table/blob/master/README.md Explains how to align specific cells within a row by providing an `alignment` option within the cell's value hash. This offers the most granular control over alignment. ```ruby table = TTY::Table.new(header: ["header1", "header2"]) table << [{value: "a1", alignment: :right}, "a2"] table << ["b1", {value: "b2", alignment: :center}] ``` ```ruby table.render(:ascii) ``` -------------------------------- ### Render Table with Separators After Specific Rows (Ruby) Source: https://context7.com/piotrmurach/tty-table/llms.txt Demonstrates how to render a TTY::Table with custom separators placed after specific rows. This is useful for visually grouping data within the table. The `renderer.border.separator` option accepts an array of row indices. ```ruby require "tty-table" table = TTY::Table.new( ["Name", "Score"], [["Alice", "95"], ["Bob", "87"], ["Charlie", "92"], ["Diana", "88"]] ) puts table.render(:ascii) do |renderer| renderer.border.separator = [0, 2] end ``` -------------------------------- ### Render Table with Conditional Row Separators (Ruby) Source: https://context7.com/piotrmurach/tty-table/llms.txt Shows how to use a lambda function to define conditional separators for table rows in TTY-Table. The lambda receives the row index and can return true to insert a separator. This allows for dynamic separator placement, such as every N rows. ```ruby require "tty-table" table = TTY::Table.new( ["Name", "Score"], [["Alice", "95"], ["Bob", "87"], ["Charlie", "92"], ["Diana", "88"]] ) puts table.render(:ascii) do |renderer| renderer.border.separator = ->(row) { row == 0 || (row + 1) % 2 == 0 } end ``` -------------------------------- ### Handle Multiline Content in Table Cells (Ruby) Source: https://context7.com/piotrmurach/tty-table/llms.txt Explains how to enable multiline rendering for cell content in TTY-Table, allowing text with newline characters to wrap across multiple lines. This feature is controlled by the `multiline` option and works in conjunction with padding. ```ruby require "tty-table" table = TTY::Table.new([ ["First", "1"], ["Multi\nLine\nContent", "2"], ["Third", "3"] ]) # With multiline enabled puts table.render(:ascii, multiline: true) # Without multiline (escapes newlines) puts table.render(:ascii, multiline: false) # Multiline with padding table = TTY::Table.new(header: ["Title", "Description"]) table << ["Product", "A great\nproduct\nfor everyone"] table << ["Service", "24/7\nsupport"] puts table.render(:ascii, multiline: true, padding: [1, 2, 1, 2]) ``` -------------------------------- ### Render Table with Inline Separators (Ruby) Source: https://context7.com/piotrmurach/tty-table/llms.txt Illustrates using the `:separator` symbol directly within the data array to insert horizontal separators in a TTY-Table. This method provides a concise way to add separators between specific rows or groups of data. ```ruby require "tty-table" table = TTY::Table.new( ["Category", "Value"], [:separator, ["Group A", "100"], ["Item 1", "50"], :separator, ["Group B", "200"]] ) table << :separator << ["Total", "300"] puts table.render(:ascii) ``` -------------------------------- ### Render TTY::Table with Unicode Borders in Ruby Source: https://context7.com/piotrmurach/tty-table/llms.txt Renders a TTY::Table using Unicode border characters for a modern terminal appearance. The 'tty-table' gem is required. This provides a visually appealing table suitable for terminals with Unicode support. ```ruby require "tty-table" table = TTY::Table.new( ["Item", "Qty", "Price"], [ ["Apple", "10", "$2.50"], ["Banana", "15", "$1.75"], ["Orange", "8", "$3.00"] ] ) puts table.render(:unicode) # => # ┌──────┬───┬─────┐ # │Item │Qty│Price│ # ├──────┼───┼─────┤ # │Apple │10 │$2.50│ # │Banana│15 │$1.75│ # │Orange│8 │$3.00│ # └──────┴───┴─────┘ # Using renderer class directly renderer = TTY::Table::Renderer::Unicode.new(table) puts renderer.render ``` -------------------------------- ### Transform TTY::Table Orientation Between Horizontal and Vertical Source: https://context7.com/piotrmurach/tty-table/llms.txt This code demonstrates how to change the orientation of a TTY::Table from its default horizontal layout to a vertical one. It shows the rendered output for both orientations using ASCII formatting. ```ruby require "tty-table" table = TTY::Table.new( header: ["Column 1", "Column 2", "Column 3"], rows: [ ["r1 c1", "r1 c2", "r1 c3"], ["r2 c1", "r2 c2", "r2 c3"], ["r3 c1", "r3 c2", "r3 c3"] ] ) # Horizontal (default) puts table.render(:ascii) # => # +--------+--------+--------+ # |Column 1|Column 2|Column 3| # +--------+--------+--------+ # |r1 c1 |r1 c2 |r1 c3 | # |r2 c1 |r2 c2 |r2 c3 | # |r3 c1 |r3 c2 |r3 c3 | # +--------+--------+--------+ # Switch to vertical orientation table.orientation = :vertical puts table.render(:ascii) # => # +--------+-----+-----+-----+ # |Column 1|r1 c1|r2 c1|r3 c1| # |Column 2|r1 c2|r2 c2|r3 c2| # |Column 3|r1 c3|r2 c3|r3 c3| # +--------+-----+-----+-----+ ``` -------------------------------- ### Define Custom Double-Line Unicode Border for TTY::Table Source: https://context7.com/piotrmurach/tty-table/llms.txt This snippet demonstrates how to define a custom border style for TTY::Table using double-line Unicode characters. It involves subclassing TTY::Table::Border and defining each border character within the `def_border` block. The custom border is then applied during table rendering. ```ruby class DoubleBorder < TTY::Table::Border def_border do top "═" top_left "╔" top_right "╗" top_mid "╦" left "║" center "║" right "║" bottom "═" bottom_left "╚" bottom_right "╝" bottom_mid "╩" mid "═" mid_left "╠" mid_right "╣" mid_mid "╬" end end puts table.render_with(DoubleBorder) ``` -------------------------------- ### Access and Iterate Over TTY::Table Data Source: https://context7.com/piotrmurach/tty-table/llms.txt This snippet explains how to access rows, columns, and individual cells within a TTY::Table object. It also covers iterating over the table data using various enumerable methods and retrieving table dimensions. ```ruby require "tty-table" table = TTY::Table.new( header: [:name, :age, :city], rows: [ ["Alice", "30", "NYC"], ["Bob", "25", "LA"], ["Charlie", "35", "Chicago"] ] ) # Access rows by index table[0] # => ["Alice", "30", "NYC"] table.row(0) # => ["Alice", "30", "NYC"] table[-1] # => ["Charlie", "35", "Chicago"] # Access specific cell table[0, 1] # => "30" table[1, 2] # => "LA" # Access columns by index or header name table.column(0) # => ["Alice", "Bob", "Charlie"] table.column(:age) # => ["30", "25", "35"] table.column(:city) # => ["NYC", "LA", "Chicago"] # Iteration table.each do |row| puts row.to_a.join(", ") end table.each_with_index do |row, index| puts "Row #{index}: #{row.to_a.join(', ')}" end # Iterate over column table.column(0) { |name| puts name } # Table dimensions table.rows_size # => 3 table.columns_size # => 3 table.size # => [3, 3] table.empty? # => false table.width # => total character width ``` -------------------------------- ### Apply Custom Filters to Cell Values (Ruby) Source: https://context7.com/piotrmurach/tty-table/llms.txt Demonstrates how to use a filter function with TTY-Table to apply custom transformations to cell values during rendering. The filter lambda receives the value, row index, and column index, allowing conditional formatting or modification. ```ruby require "tty-table" table = TTY::Table.new( ["Name", "Status"], [["alice", "active"], ["bob", "inactive"], ["charlie", "pending"]] ) # Capitalize second column (skip header row at index 0) puts table.render(:ascii) do |renderer| renderer.filter = ->(val, row_index, col_index) do if col_index == 1 && row_index != 0 val.upcase else val end end end # Using Pastel for colored output require "pastel" pastel = Pastel.new puts table.render(:ascii) do |renderer| renderer.filter = ->(val, row_index, col_index) do if col_index == 1 && row_index != 0 case val when "active" then pastel.green(val) when "inactive" then pastel.red(val) else pastel.yellow(val) end else val end end end ``` -------------------------------- ### Render Table with Escaped Multiline Content - Ruby Source: https://github.com/piotrmurach/tty-table/blob/master/README.md Illustrates disabling multiline rendering in TTY::Table by setting `multiline` to `false`. Line break characters are escaped, and content may be truncated if column widths are defined. ```ruby table = TTY::Table.new [["First", "1"], ["Multiline\nContent", "2"], ["Third", "3"]] table.render :ascii, multiline: false ``` -------------------------------- ### Add Row Separators in TTY::Table in Ruby Source: https://context7.com/piotrmurach/tty-table/llms.txt Adds separators between rows in a TTY::Table for enhanced readability. This feature is configured within the renderer options. Requires the 'tty-table' gem. ```ruby require "tty-table" table = TTY::Table.new( ["Name", "Score"], [["Alice", "95"], ["Bob", "87"], ["Charlie", "92"]] ) # Separator after each row puts table.render(:ascii) do |renderer| renderer.border.separator = :each_row end # => # +-------+-----+ # |Name |Score| # +-------+-----+ # |Alice |95 | # +-------+-----+ # |Bob |87 | # +-------+-----+ # |Charlie|92 | # +-------+-----+ ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.