### Installation Instructions Source: https://github.com/gurgeous/table_tennis/blob/main/README.md Shows how to install the TableTennis gem via command line or by adding it to a Gemfile. ```bash gem install table_tennis ``` ```ruby gem "table_tennis" ``` -------------------------------- ### Supported Data Structures Source: https://github.com/gurgeous/table_tennis/blob/main/README.md Examples of various data formats supported by TableTennis, including arrays of hashes, ActiveRecord collections, structs, and arrays of arrays. ```ruby puts TableTennis.new([{a: "hello", b: "world"}, {a: "foo", b: "bar"}]) puts TableTennis.new(Recipe.all.to_a) puts TableTennis.new(array_of_structs) puts TableTennis.new([[1,2],[3,4]]) puts TableTennis.new(authors[0]) ``` -------------------------------- ### Specify Custom Column Headers Source: https://github.com/gurgeous/table_tennis/blob/main/README.md Use the `headers` option to provide custom names for your table columns. This is particularly helpful for making the table more readable by using user-friendly names instead of internal field names. For example, you can map `user_id` to `Customer`. ```ruby headers: {user_id: "Customer", email: "Email Address"} ``` -------------------------------- ### Create Basic Table with Array of Hashes (Ruby) Source: https://context7.com/gurgeous/table_tennis/llms.txt Demonstrates the fundamental usage of TableTennis by creating a styled table from an array of hashes. It shows how to initialize the library and render the table to standard output. ```ruby require "table_tennis" # Basic usage with array of hashes data = [ { name: "Luke Skywalker", height: 172, homeworld: "Tatooine" }, { name: "Darth Vader", height: 202, homeworld: "Tatooine" }, { name: "Leia Organa", height: 150, homeworld: "Alderaan" } ] puts TableTennis.new(data) ``` -------------------------------- ### Initialize and Print a Table Source: https://github.com/gurgeous/table_tennis/blob/main/README.md Demonstrates how to initialize a TableTennis object with data and display it in the terminal. It uses a hash for configuration options such as titles, zebra striping, and color scaling. ```ruby require "table_tennis" options = { title: "Star Wars People", zebra: true, color_scale: :height } puts TableTennis.new(Starwars.all, options) ``` -------------------------------- ### Configure TableTennis Instances Source: https://github.com/gurgeous/table_tennis/blob/main/README.md Shows different ways to configure table settings, including global defaults, constructor arguments, and block-based initialization. ```ruby TableTennis.defaults = { title: "All Tables Have This Name" } TableTennis.new(rows, title: "An Amazing Title") TableTennis.new do |t| t.title = "Yet Another Way To Set Things Up" end ``` -------------------------------- ### Configure Table Options (Ruby) Source: https://context7.com/gurgeous/table_tennis/llms.txt Illustrates how to configure TableTennis tables using an options hash or a block. It covers common options like title, zebra stripes, row numbers, column selection, date formatting, and titleizing headers. Also shows how to set global defaults. ```ruby require "table_tennis" data = [ { name: "Han Solo", commission: 5234.567, bday: Date.new(1977, 5, 25) }, { name: "Chewbacca", commission: 8912.123, bday: Date.new(1977, 5, 25) } ] # Using options hash options = { title: "Employees", # Add title to table zebra: true, # Enable zebra stripes row_numbers: true, # Show row numbers columns: %i[name commission bday], # Specify columns to display digits: 2, # Format floats to 2 decimal places strftime: "%Y-%m-%d", # Date format titleize: true # Titleize column headers } puts TableTennis.new(data, options) # Using block configuration TableTennis.new(data) do |t| t.title = "Employees" t.zebra = true t.row_numbers = true end # Setting global defaults TableTennis.defaults = { zebra: true, row_numbers: true } puts TableTennis.new(data, title: "All Tables Inherit Defaults") ``` -------------------------------- ### Customizing Table Headers and Titles Source: https://context7.com/gurgeous/table_tennis/llms.txt Demonstrates how to set custom headers for table columns, auto-titleize headers, and combine these with column selection. It shows how to map specific data keys to display names and leverage titleization for cleaner output. ```ruby puts TableTennis.new(data, columns: %i[user_id first_name email], headers: { user_id: "ID", first_name: "Name", email: "Email Address" } ) ``` ```ruby puts TableTennis.new(data, titleize: true) ``` ```ruby puts TableTennis.new(data, columns: %i[first_name last_name created_at], headers: { created_at: "Joined" }, titleize: true ) ``` -------------------------------- ### Customizing Column Headers and Selection Source: https://context7.com/gurgeous/table_tennis/llms.txt Demonstrates how to filter and reorder columns using the columns option and provide custom labels for headers. ```ruby require "table_tennis" data = [ { user_id: 1, first_name: "Alice", last_name: "Smith", email: "alice@example.com", created_at: "2024-01-15" }, { user_id: 2, first_name: "Bob", last_name: "Jones", email: "bob@example.com", created_at: "2024-02-20" } ] # Select specific columns in custom order puts TableTennis.new(data, columns: %i[first_name last_name email]) ``` -------------------------------- ### Rendering Tables to Various Outputs Source: https://context7.com/gurgeous/table_tennis/llms.txt Shows how to render table data to strings, stdout, stderr, files, or StringIO buffers. The render method is recommended for large datasets to optimize memory usage. ```ruby require "table_tennis" require "stringio" data = [ { id: 1, status: "active" }, { id: 2, status: "pending" } ] table = TableTennis.new(data, title: "Status Report") # Convert to string (best for small tables) output = table.to_s puts output # Render directly to stdout (preferred for large tables) table.render # Render to stderr table.render($stderr) # Render to a file File.open("/tmp/output.txt", "w") do |file| table.render(file) end # Render to StringIO for testing buffer = StringIO.new table.render(buffer) captured_output = buffer.string ``` -------------------------------- ### Exporting Tables to CSV Files Source: https://context7.com/gurgeous/table_tennis/llms.txt Explains how to export table data to CSV format either during initialization or by calling the save method. Note that markdown links are automatically stripped during the export process. ```ruby require "table_tennis" data = [ { name: "Alice", email: "alice@example.com", score: 95.5 }, { name: "Bob", email: "bob@example.com", score: 87.3 }, { name: "Carol", email: "carol@example.com", score: 92.1 } ] # Auto-save to CSV when table is created puts TableTennis.new(data, save: "/tmp/scores.csv", title: "Scores") # Or save explicitly after creation table = TableTennis.new(data) table.save("/tmp/scores_backup.csv") # Data with markdown links - links are stripped in CSV linked_data = [ { site: "[Google](https://google.com)", visits: 1000 }, { site: "[GitHub](https://github.com)", visits: 500 } ] puts TableTennis.new(linked_data, save: "/tmp/sites.csv") ``` -------------------------------- ### Controlling Table Layout and Column Widths Source: https://context7.com/gurgeous/table_tennis/llms.txt Explains how to manage the layout of TableTennis tables. Options include auto-layout fitting the terminal, full-width columns, fixed widths for all columns, and custom widths for specific columns. It also shows how to disable separators. ```ruby require "table_tennis" data = [ { id: 1, description: "This is a very long description that might need truncation", status: "active" }, { id: 2, description: "Short desc", status: "pending" } ] # Auto-layout (default) - fits terminal width puts TableTennis.new(data, layout: true) ``` ```ruby # No layout - full width columns (may overflow terminal) puts TableTennis.new(data, layout: false) ``` ```ruby # Fixed width for all columns puts TableTennis.new(data, layout: 15) ``` ```ruby # Custom widths per column puts TableTennis.new(data, layout: { id: 5, description: 30, status: 10 }) ``` ```ruby # Disable separators for a more compact look puts TableTennis.new(data, separators: false) ``` -------------------------------- ### Advanced Table Configuration Source: https://github.com/gurgeous/table_tennis/blob/main/README.md Illustrates complex configuration options for TableTennis, including custom column selection, row marking logic, search filtering, and exporting to CSV. ```ruby options = { color_scales: :commission, columns: %i[ name commission bday phone ], mark: -> { _1[:name] =~ /jane|john/i }, row_numbers: true, save: "/tmp/people.csv", search: "february", title: "Employees", zebra: true } ``` -------------------------------- ### Highlighting and Searching Rows in TableTennis Source: https://context7.com/gurgeous/table_tennis/llms.txt Demonstrates how to use the mark option with lambdas for row highlighting and the search option for text or regex-based cell matching. These features allow for dynamic visual identification of specific data points. ```ruby require "table_tennis" characters = [ { name: "R2-D2", species: "Droid", homeworld: "Naboo" }, { name: "C-3PO", species: "Droid", homeworld: "Tatooine" }, { name: "Luke Skywalker", species: "Human", homeworld: "Tatooine" }, { name: "Yoda", species: "Unknown", homeworld: "Unknown" } ] # Mark rows where species is Droid (highlighted with background color) puts TableTennis.new(characters, mark: ->(row) { row[:species] == "Droid" }) # Mark with regex pattern puts TableTennis.new(characters, mark: ->(row) { row[:name] =~ /^[RC]/ }) # Search and highlight text matches puts TableTennis.new(characters, search: "Tatooine") # Search with regex (case insensitive) puts TableTennis.new(characters, search: /droid|human/i) # Combine mark and search puts TableTennis.new(characters, mark: ->(row) { row[:species] == "Droid" }, search: "Tatooine", zebra: true, row_numbers: true ) ``` -------------------------------- ### Highlight Rows and Search Text with TableTennis Source: https://github.com/gurgeous/table_tennis/blob/main/README.md Shows how to use the 'mark' option to highlight specific rows based on a condition and the 'search' option to highlight text within cells. Also includes enabling row numbers and zebra striping for better readability. ```ruby puts TableTennis.new(rows, mark: ->(row) { row[:homeworld] =~ /droids/i }) puts TableTennis.new(rows, search: /hope.*empire/i }) puts TableTennis.new(rows, row_numbers: true, zebra: true) ``` -------------------------------- ### Apply Color Scales for Numeric Visualization (Ruby) Source: https://context7.com/gurgeous/table_tennis/llms.txt Shows how to use the `:color_scales` option in TableTennis to apply conditional formatting to numeric columns. It covers applying a single scale to multiple columns, and defining custom color schemes per column. ```ruby require "table_tennis" sales_data = [ { product: "Widget A", price: 29.99, quantity: 150, margin: 0.15 }, { product: "Widget B", price: 49.99, quantity: 89, margin: 0.32 }, { product: "Widget C", price: 19.99, quantity: 203, margin: 0.08 }, { product: "Widget D", price: 99.99, quantity: 42, margin: 0.45 } ] # Single column color scale (defaults to blue gradient) puts TableTennis.new(sales_data, color_scales: :price) # Multiple columns with same scale puts TableTennis.new(sales_data, color_scales: [:price, :quantity]) # Custom colors per column # Available scales: g, y, r, b, gw, yw, rw, bw, rg, gr, gyr puts TableTennis.new(sales_data, color_scales: { price: :b, # blue gradient quantity: :g, # green gradient margin: :gyr # green-yellow-red gradient }) ``` -------------------------------- ### Setting Table Themes and Colors Source: https://context7.com/gurgeous/table_tennis/llms.txt Details how to control the visual theme and color output of TableTennis tables. It covers automatic theme detection based on terminal background, forcing specific themes (dark, light, ansi), disabling colors, and forcing colors when redirecting output. It also shows how to retrieve theme information. ```ruby require "table_tennis" data = [ { name: "Item 1", value: 100 }, { name: "Item 2", value: 200 } ] # Auto-detect theme based on terminal background (default) puts TableTennis.new(data) ``` ```ruby # Force dark theme puts TableTennis.new(data, theme: :dark) ``` ```ruby # Force light theme puts TableTennis.new(data, theme: :light) ``` ```ruby # Use ANSI theme (terminal's default colors) puts TableTennis.new(data, theme: :ansi) ``` ```ruby # Disable colors entirely puts TableTennis.new(data, color: false) ``` ```ruby # Force colors even when redirecting to file puts TableTennis.new(data, color: true) ``` ```ruby # Check theme detection info require "table_tennis" info = TableTennis::Theme.info puts "Detected theme: #{info[:detect_theme]}" puts "Terminal dark?: #{info[:terminal_dark?]}" ``` -------------------------------- ### Render Hyperlinks in Terminal Tables Source: https://github.com/gurgeous/table_tennis/blob/main/README.md Demonstrates how to include markdown-style links in table cells. TableTennis automatically detects and renders these using the OSC 8 terminal standard. ```ruby puts TableTennis.new([{site: "[search](https://google.com)"}]) ``` -------------------------------- ### Advanced Formatting Options Source: https://github.com/gurgeous/table_tennis/blob/main/README.md Configure advanced formatting and display options for tables. ```APIDOC ## Advanced Formatting Options ### Description This section details advanced options for customizing the appearance and behavior of tables generated by Table Tennis. ### Options Table | Option | Default | Details | | ------------- | ------- | --------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------- | | `coerce` | `true` | If true, attempts to coerce strings into numbers where possible for formatting with `digits`. Disable if you have pre-formatted numbers you don't want altered. | | `color` | `─` | Enables ANSI colors. Set to `true` or `false`, or leave `nil` for autodetection (disables color when redirecting to a file). `ENV["FORCE_COLOR"]` forces color on, `ENV["NO_COLOR"]` forces it off. | | `delims` | `true` | Formats integers and floats with comma delimiters (e.g., 123,456). | | `digits` | `3` | Formats floats to the specified number of digits. Works on `Float` cells or string representations of floats. | | `layout` | `true` | Controls column widths. `true` or `nil` enables autolayout (shrinks table to terminal width). `false` disables layout (columns use full width). An integer sets a fixed width for all columns. A hash can set widths for specific columns. | | `placeholder` | `”—` | String to display in empty cells. | | `save` | `─` | If set to a file path, saves the table as a CSV file in addition to displaying it. | | `strftime` | `see →` | `strftime` string for formatting `Date`/`Time` objects. Default is `"%Y-%m-%d"` (e.g., `2025-04-21`). | | `theme` | `─` | Sets the color theme. Autodetects based on terminal background, defaulting to `:dark` if detection fails. Manually set to `:dark`, `:light`, or `:ansi`. Has no effect if colors are disabled. | ``` -------------------------------- ### Table Tennis - Popular Options Source: https://github.com/gurgeous/table_tennis/blob/main/README.md This section outlines common options for customizing table output in the Table Tennis gem. ```APIDOC ## Table Tennis Popular Options ### Description This document describes popular options for configuring the Table Tennis gem to customize table output. ### Options - **`color_scales`** (─) - Color code a column of floats, similar to conditional formatting in spreadsheets. See [docs below](#color-scales). - **`columns`** (─) - Manually set which columns to include. Leave unset to show all columns. - **`headers`** (─) - Specify some or all column headers. For example, `{user_id: "Customer"}`. When unset, headers are inferred. - **`mark`** (─) - Highlight specific columns with color. For example, use `mark: ->(row) { row[:planet] == "tatooine" }` to highlight those rows. Your lambda can also return a specific background color or Paint color array. - **`row_numbers`** (`false`) - Show row numbers in the table. - **`search`** (─) - String or regex to highlight in the output. - **`separators`** (`true`) - Include column and header separators in the output. - **`title`** (─) - Add a title line to the table. - **`titleize`** (─) - Titleize column headers, so `person_id` becomes `Person`. - **`zebra`** (`false`) - Turn on zebra stripes for rows. ``` -------------------------------- ### Output Table Data to Streams Source: https://github.com/gurgeous/table_tennis/blob/main/README.md Provides methods for outputting table data. Options include standard puts, row-by-row rendering for large datasets, and writing to custom I/O streams. ```ruby # Uses `to_s`, so there can be a pause before output shows up. Best for small tables. puts TableTennis.new(rows) # Write to `$stdout` one row at a time. Prefer this for tables over 10,000 rows. TableTennis.new(rows).render # Render to any I/O stream ($stdout/$stderr, an open file, StringIO...) TableTennis.new(rows).render(io) ``` -------------------------------- ### Configure Color Scales in TableTennis Source: https://github.com/gurgeous/table_tennis/blob/main/README.md Demonstrates how to apply color scales to numeric columns for visualization. Supports single column, array of columns, or a hash mapping columns to specific color scales. The default scale is blue (:b). ```ruby puts TableTennis.new(rows, color_scales: :price) puts TableTennis.new(rows, color_scales: [ :price, :quantity ]) puts TableTennis.new(rows, color_scales: { price: :b, quantity: :r }) ``` -------------------------------- ### Manually Set Column Headers Source: https://github.com/gurgeous/table_tennis/blob/main/README.md The `columns` option allows you to explicitly define which columns to include in the table. If this option is left unset, all columns will be displayed by default. This is useful for controlling the order and selection of data presented. ```ruby columns: [:user_id, :name, :email] ``` -------------------------------- ### Handling Diverse Data Input Formats Source: https://context7.com/gurgeous/table_tennis/llms.txt Illustrates how TableTennis supports various Ruby data types including arrays of hashes, single hashes, arrays of arrays, Structs, and Data objects. ```ruby require "table_tennis" # Array of hashes (most common) hash_data = [{ a: 1, b: 2 }, { a: 3, b: 4 }] puts TableTennis.new(hash_data) # Single hash (converted to key/value rows) single_hash = { name: "Config", version: "1.0", enabled: true } puts TableTennis.new(single_hash) # Array of arrays (columns indexed as 0, 1, 2...) array_data = [[1, 2, 3], [4, 5, 6], [7, 8, 9]] puts TableTennis.new(array_data) # Struct objects Person = Struct.new(:name, :age, :city, keyword_init: true) people = [ Person.new(name: "Alice", age: 30, city: "NYC"), Person.new(name: "Bob", age: 25, city: "LA") ] puts TableTennis.new(people) # Ruby 3.2+ Data objects Point = Data.define(:x, :y, :label) points = [ Point.new(x: 0, y: 0, label: "origin"), Point.new(x: 10, y: 20, label: "target") ] puts TableTennis.new(points) ``` -------------------------------- ### Formatting Numbers and Dates in Tables Source: https://context7.com/gurgeous/table_tennis/llms.txt Explains the automatic formatting capabilities of TableTennis for numeric and date values. It covers controlling float precision with `digits`, date formatting with `strftime`, and numeric delimiters with `delims`. The `coerce` option's effect on string-to-number conversion and the `placeholder` option for empty cells are also demonstrated. ```ruby require "table_tennis" data = [ { item: "Sale 1", amount: 1234567.89123, date: Date.new(2024, 3, 15) }, { item: "Sale 2", amount: 9876.5, date: Date.new(2024, 4, 20) }, { item: "Sale 3", amount: 123.456789, date: Time.new(2024, 5, 25, 14, 30) } ] # Default formatting (3 decimal places, delimiters, YYYY-MM-DD dates) puts TableTennis.new(data) ``` ```ruby # Custom float precision puts TableTennis.new(data, digits: 2) ``` ```ruby # No decimal places puts TableTennis.new(data, digits: 0) ``` ```ruby # Custom date format puts TableTennis.new(data, strftime: "%B %d, %Y") # "March 15, 2024" ``` ```ruby # Disable numeric delimiters (no commas in large numbers) puts TableTennis.new(data, delims: false) ``` ```ruby # Disable coercion (keep string numbers as-is) string_data = [{ value: "001234" }, { value: "005678" }] puts TableTennis.new(string_data, coerce: false) # Preserves leading zeros ``` ```ruby # Custom placeholder for empty cells (default is em-dash) sparse_data = [{ a: 1, b: nil }, { a: nil, b: 2 }] puts TableTennis.new(sparse_data, placeholder: "N/A") ``` -------------------------------- ### Highlight Rows with Mark Option Source: https://github.com/gurgeous/table_tennis/blob/main/README.md The `mark` option allows you to highlight specific rows in the table based on a condition. You can provide a lambda function that evaluates each row. This function can return a boolean to simply highlight or not, or it can return a specific background color or a Paint color array for more advanced styling. ```ruby mark: ->(row) { row[:planet] == "tatooine" } ``` -------------------------------- ### Color Scales Source: https://github.com/gurgeous/table_tennis/blob/main/README.md Visualize numeric columns using predefined color scales. ```APIDOC ## Color Scales ### Description Color scales are a powerful way to visually represent numeric data within your tables. You can apply a single scale to multiple columns or specify different scales for different columns. ### Usage The `:color_scales` option can accept: - A single column name (symbol or string). - An array of column names. - A hash mapping column names to color scale symbols. ### Examples ```ruby puts TableTennis.new(rows, color_scales: :price) puts TableTennis.new(rows, color_scales: [ :price, :quantity ]) puts TableTennis.new(rows, color_scales: { price: :b, quantity: :r }) ``` ### Supported Color Scales Color scale names are abbreviations. The available scales include: - `:g` (green) - `:y` (yellow) - `:r` (red) - `:b` (blue) - `:gw` (green-white) - `:yw` (yellow-white) - `:rw` (red-white) - `:bw` (blue-white) - `:rg` (red-green) - `:gr` (green-red) - `:gyr` (green-yellow-red) For example, `:gyr` creates a gradient from green to yellow to red. ``` -------------------------------- ### Highlighting Rows and Text Source: https://github.com/gurgeous/table_tennis/blob/main/README.md Highlight specific rows or search for text within the table. ```APIDOC ## Highlighting Rows and Text ### Description Table Tennis provides options to highlight specific rows based on a condition or to search and highlight occurrences of text within the table. ### Highlighting Rows with `:mark` Use the `:mark` option with a lambda or proc to define a condition for highlighting rows. The lambda should accept a row hash and return `true` if the row should be highlighted. **Example:** Highlight rows where the 'homeworld' contains 'droids' (case-insensitive). ```ruby puts TableTennis.new(rows, mark: ->(row) { row[:homeworld] =~ /droids/i }) ``` ![droids](./screenshots/droids.png) ### Searching and Highlighting Text with `:search` Use the `:search` option with a regular expression to highlight all occurrences of a pattern within the table. **Example:** Highlight rows containing 'hope' followed by 'empire' (case-insensitive). ```ruby puts TableTennis.new(rows, search: /hope.*empire/i) ``` ![hope](./screenshots/hope.png) ### Row Numbers and Zebra Stripes Enable row numbers and zebra striping for improved readability. **Example:** ```ruby puts TableTennis.new(rows, row_numbers: true, zebra: true) ``` ![row numbers](./screenshots/row_numbers.png) ``` -------------------------------- ### Using Markdown Hyperlinks in Table Cells Source: https://context7.com/gurgeous/table_tennis/llms.txt Illustrates how to embed markdown-style hyperlinks within table cell data. These links are rendered as clickable URLs in compatible terminals (using OSC 8). The gem automatically strips links to plain text when exporting to CSV. ```ruby require "table_tennis" # Markdown-style links in data resources = [ { name: "Documentation", url: "[Read the Docs](https://docs.example.com)" }, { name: "Source Code", url: "[GitHub](https://github.com/example/repo)" }, { name: "Bug Tracker", url: "[Issues](https://github.com/example/repo/issues)" } ] # Links render as clickable in supporting terminals puts TableTennis.new(resources) ``` ```ruby # Links in any column work products = [ { product: "[Widget Pro](https://example.com/widget-pro)", price: 29.99 }, { product: "[Gadget Plus](https://example.com/gadget-plus)", price: 49.99 } ] puts TableTennis.new(products, title: "Products") ``` === COMPLETE CONTENT === This response contains all available snippets from this library. No additional content exists. Do not make further requests.