{"id":13463123,"url":"https://github.com/arches/table_print","last_synced_at":"2025-03-25T06:31:33.079Z","repository":{"id":52341444,"uuid":"1595553","full_name":"arches/table_print","owner":"arches","description":"The best data slicer! Watch a 3 minute screencast at http://tableprintgem.com","archived":false,"fork":false,"pushed_at":"2023-03-21T03:55:03.000Z","size":408,"stargazers_count":901,"open_issues_count":30,"forks_count":43,"subscribers_count":19,"default_branch":"master","last_synced_at":"2024-10-29T13:50:29.099Z","etag":null,"topics":[],"latest_commit_sha":null,"homepage":"","language":"Ruby","has_issues":true,"has_wiki":null,"has_pages":null,"mirror_url":null,"source_name":null,"license":"mit","status":null,"scm":"git","pull_requests_enabled":true,"icon_url":"https://github.com/arches.png","metadata":{"files":{"readme":"README.rdoc","changelog":null,"contributing":null,"funding":null,"license":"LICENSE.txt","code_of_conduct":null,"threat_model":null,"audit":null,"citation":null,"codeowners":null,"security":null,"support":null,"governance":null}},"created_at":"2011-04-10T17:20:44.000Z","updated_at":"2024-10-26T01:21:50.000Z","dependencies_parsed_at":"2023-10-20T17:31:26.753Z","dependency_job_id":null,"html_url":"https://github.com/arches/table_print","commit_stats":{"total_commits":263,"total_committers":17,"mean_commits":"15.470588235294118","dds":0.1901140684410646,"last_synced_commit":"591affe5283973474ce89cdcc70c1b42b4cf6352"},"previous_names":[],"tags_count":10,"template":false,"template_full_name":null,"repository_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arches%2Ftable_print","tags_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arches%2Ftable_print/tags","releases_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arches%2Ftable_print/releases","manifests_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories/arches%2Ftable_print/manifests","owner_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners/arches","download_url":"https://codeload.github.com/arches/table_print/tar.gz/refs/heads/master","host":{"name":"GitHub","url":"https://github.com","kind":"github","repositories_count":244721652,"owners_count":20498987,"icon_url":"https://github.com/github.png","version":null,"created_at":"2022-05-30T11:31:42.601Z","updated_at":"2022-07-04T15:15:14.044Z","host_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub","repositories_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repositories","repository_names_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/repository_names","owners_url":"https://repos.ecosyste.ms/api/v1/hosts/GitHub/owners"}},"keywords":[],"created_at":"2024-07-31T13:00:46.445Z","updated_at":"2025-03-25T06:31:32.737Z","avatar_url":"https://github.com/arches.png","language":"Ruby","funding_links":[],"categories":["Developer Tools","Ruby","CLI Utilities"],"sub_categories":["irb Tools"],"readme":"= table_print\n\n{\u003cimg src=\"https://travis-ci.org/arches/table_print.svg?branch=master\" /\u003e}[http://travis-ci.org/arches/table_print]\n{\u003cimg src=\"https://codeclimate.com/github/arches/table_print.png\" /\u003e}[https://codeclimate.com/github/arches/table_print]\n\nTablePrint shows objects in nicely formatted columns for easy reading. It even lets you nest other tables\nof related objects, contextualizing data across tables. It's incredibly flexible, yet simple, making it easy to\nsee exactly the data you care about.\n\nThis {three minute screencast}[http://tableprintgem.com] will give you a quick overview of table_print's most\npowerful features and how to use them.\n\nhttp://arches.io/tp_example.gif\n\n== Installation\n\n  # Install as a standalone gem\n  $ gem install table_print\n\n  # Or install within rails\n  In your Gemfile: gem \"table_print\"\n  $ bundle install\n\n== Usage\n\n  # Outside rails\n  $ irb\n  \u003e require 'table_print'\n  \u003e tp array_of_objects, options\n\n  # Inside rails, the gem has already been required by your Gemfile so all you need to do is\n  $ rails c\n  \u003e tp array_of_objects, options\n\nYou should see something like this:\n\n  \u003e tp Book.all\n  AUTHOR            | SUMMARY                         | TITLE\n  ------------------|---------------------------------|------------------\n  Michael Connelly  | Another book by Michael Con...  | The Fifth Witness\n  Manning Marable   | From acclaimed historian Ma...  | Malcolm X\n  Tina Fey          | Worth it. -Trees                | Bossypants\n\nTablePrint tries to use sensible defaults to choose the columns to show. If you're inspecting ActiveRecord objects, it\nuses the ActiveRecord column names. You can customize the output to show fewer columns, or show other methods you've written\non your model. Use symbols or strings to reference the columns.\n\n  # Maybe you store a user's hourly rate but you want to see their yearly income\n  tp User.limit(30), :include =\u003e :yearly_income, :except =\u003e :hourly_rate\n\n  # Maybe all you care about is their mailing info\n  tp User.limit(30), :address, 'city', 'state', :zip\n\nIf you're not using ActiveRecord, the TablePrint default is to show all the methods defined directly on your object (nothing\nfrom superclasses/mixins).\n\nYou can reference nested objects with the method chain required to reach them. Say you had some users who wrote books, and those\nbooks had photos.\n\n  \u003e tp Author.limit(3), \"name\", \"books.title\", \"books.photos.caption\"\n  NAME              | BOOKS.TITLE       | BOOKS.PHOTOS.CAPTION\n  ------------------|-------------------|----------------------------------\n  Michael Connelly  | The Fifth Witness | Susan was running, fast, away...\n                    |                   | Along came a spider.\n                    | Malcolm X         |\n                    | Bossypants        | Yes! Yes! A thousand times ye...\n                    |                   | Don't see many like you aroun...\n  Carrot Top        |                   |\n  Milton Greene     | How I Learned     | Once upon a time, I was a sma...\n                    |                   | Lemons are yellow, limes are ...\n                    |                   | Never as a woman her age. I l...\n                    | Desperados        | Avast.\n                    |                   | Giraffes lived a peaceful exi...\n\n\n=== Column options\n\nPass options to individual columns through the options hash by using the display method as the hash key. Eg, if you wanted\na skinny email column, set the width explicitly:\n\n  tp User.all, :email =\u003e {:width =\u003e 12}\n  tp User.all, :id, {:email =\u003e {:width =\u003e 12}}, :age\n\nAvailable column options:\n\n* *display_method* - string/symbol/proc - used to populate the column. Can be a method name or a proc. See below.\n* *formatters* - array of objects - each will be called in turn as the cells are printed. Must define a 'format' method. See below.\n* *time_format* - string - passed to strftime[http://www.ruby-doc.org/core-1.9.3/Time.html#method-i-strftime], only affects time columns\n* *width* - integer - how wide you want your column.\n* *display_name* - string - useful if you want spaces in your column name\n* *separator* - string - default is vertical bar for console and markdown, change it to a comma for CSV output\n\n==== Display method\n\nColumns are named after hash keys. To rename a column, use the name you want as the key, and pass the method as an option.\n\n  tp User.all, :active =\u003e {:display_method =\u003e :active_in_the_past_six_months} # the User class defines active_in_the_past_six_months method\n\n==== Lambdas\n\nYou can pass a proc as the display_method for a column:\n\n  tp User.all, :email, :monthly_payment, yearly_payment: lambda{|u| u.monthly_payment * 12}\n\nOr if you want to pass other options along with the lambda:\n\n  tp User.all, :yearly_payment =\u003e {:display_method =\u003e lambda{|u| u.monthly_payment * 12}, :width =\u003e 10}\n\nMake sure to add curly braces if you have more than one column with a lambda or if this column isn't the last one.\n\n  tp User.all, :email, { daily_payment: lambda { |u| u.monthly_payment / 30} }, :monthly_payment, { yearly_payment: lambda { |u| u.monthly_payment * 12} }\n\n==== Column formatters\n\nSimilar to a lambda column, you can use a column formatter to reuse code across prints. Any object with a 'format' method\ncan be used to filter a column. This could also be used for coloring output.\n\n  class NoNewlineFormatter\n    def format(value)\n      value.to_s.gsub(/\\r\\n/, \" \")\n    end\n  end\n\n  tp User.all, :bio =\u003e {:formatters =\u003e [NoNewlineFormatter.new]} # strip newlines from user bios\n\n=== HTML Output\n\nCurrently the best way to show table_print output on an HTML page is using a \u003cpre\u003e tag. You can create a helper\nmethod to send table_print output directly into your \u003cpre\u003e tag:\n\n  def tp_pre data, options={}\n    content_tag :pre, TablePrint::Printer.new(data, options).table_print\n  end\n\n=== CSV Output\n\nSet the column separator to get an output you can save as a CSV:\n\n  tp.set :separator, \",\"\n\nSince your CSV viewer probably already handles column widths for you, setting +max_width+ to something very large will help you export your full data set.  Otherwise, this CSV output will still be truncated to the default max_width of 30 characters.\n\n=== Config\n\nUse tp.set and tp.clear to set options on a class-by-class basis.\n\n  tp.set User, :id, :email # now whenever you print users, the only columns shown will be id and email\n  \n  \u003e tp User.first\n  ID | EMAIL\n  ---|------------\n  1  | foo@bar.com\n\n  # the config sets a 'baseline' for each print. You can still include/except columns:\n  \u003e tp User.first, :except =\u003e :email\n  ID\n  --\n  17\n\n  # a specific set of columns will entirely override the baseline config:\n  \u003e tp User.first, :first_name\n  FIRST_NAME\n  ----------\n  Phooey\n\n  tp.clear User # back to square one - print all the columns we can find\n\nTo see the config for a given object, use tp.config_for.\n\n  \u003e tp.set User, :id, :email\n  \u003e tp.config_for User\n  [:id, :email]\n\nYou can also set global options:\n\n  tp.set :max_width, 10 # columns won't exceed 10 characters\n  tp.set :time_format, \"%Y\" # date columns will only show the year\n  tp.set :capitalize_headers, false # don't capitalize column headers\n  \nYou can redirect output:\n\n  f = File.open(\"users.txt\", \"w\")\n  tp.set :io, f\n  tp User.all # written to users.txt instead of STDOUT\n  tp.clear :io\n  tp User.all # writing to STDOUT again\n  \n=== Multibyte\n\nUnfortunately, the current approach to finding the width of multibyte strings is too slow. If you're going to be\nworking with a lot of multibyte characters, set the multibyte option first to enable the slower yet more precise\nwidth calculation:\n\n  \u003e tp.set :multibyte, true\n\n== Contributing to table_print\n\n* Check out the latest master to make sure the feature hasn't been implemented or the bug hasn't been fixed yet\n* Check out the issue tracker to make sure someone already hasn't requested it and/or contributed it\n* Fork the project\n* Start a feature/bugfix branch\n* Commit and push until you are happy with your contribution\n* Make sure to add tests for it. This is important so I don't break it in a future version unintentionally.\n* Please try not to mess with the Rakefile, version, or history. If you want to have your own version, or is otherwise necessary, that is fine, but please isolate to its own commit so I can cherry-pick around it.\n\n== Copyright\n\nCopyright (c) 2013 Chris Doyle. See LICENSE.txt for further details.\n\n","project_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farches%2Ftable_print","html_url":"https://awesome.ecosyste.ms/projects/github.com%2Farches%2Ftable_print","lists_url":"https://awesome.ecosyste.ms/api/v1/projects/github.com%2Farches%2Ftable_print/lists"}