Movatterモバイル変換


[0]ホーム

URL:


Skip to content

Navigation Menu

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Sign up

Declarative API for drawing unicode/ascii character tables in crystal lang

License

NotificationsYou must be signed in to change notification settings

epoch/tallboy

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

63 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Generate prettyUnicode,ASCII orMarkdown tables on the terminal for your command line programs.

tallboy is a DSL for quickly creating text based tables inCrystal.

Quick start

table=Tallboy.tabledo  header ["name","hex"]  row ["mistyrose","#ffe4e1"]  row ["darkolivegreen","#556b2f"]  row ["papayawhip","#ffefd5"]endputs table
┌────────────────┬─────────┐│ name           │ hex     │├────────────────┼─────────┤│ mistyrose      │ #ffe4e1 ││ darkolivegreen │ #556b2f ││ papayawhip     │ #ffefd5 │└────────────────┴─────────┘
# full APItable=Tallboy.tabledo# define 3 columns. set first column width to 12 & align right  columnsdo     add"size",width:12,align::right    add"http method"    add"path"end# add header with multiple lines  header"good\nfood\nhunting",align::right# add header with column span on one cell  headerdo    cell""    cell"routes",span:2end# add header inferred from column definitions# [size, http method, path]  header  rows [    ["207 B","post","/dishes"],    ["1.3 kB","get","/dishes"],    ["910 B","patch","/dishes/:id"],    ["10.2 kB","delete","/dishes/:id"],  ]endputs table
┌────────────────────────────────────────┐│                                   good ││                                   food ││                                hunting │├────────────┬───────────────────────────┤│            │ routes                    │├────────────┼─────────────┬─────────────┤│       size │ http method │ path        │├────────────┼─────────────┼─────────────┤│      207 B │ post        │ /dishes     ││     1.3 kB │ get         │ /dishes     ││      910 B │ patch       │ /dishes/:id ││    10.2 kB │ delete      │ /dishes/:id │└────────────┴─────────────┴─────────────┘# draw border joints correctly even with different span sizes :)

Top Features

  • spanning cells across muliple columns and entire rows
  • simple, readable and flexible API
  • text alignment (left, right, center)
  • set width and alignment for entire columns with column definitions
  • static type checking for almost all DSL options
  • support multi-line cells with the newline character
  • full custom styling or choose from multiple border styles including ascii, unicode and markdown
  • render directly into IO for better performance

Install it as a shard

  1. Add the dependency to yourshard.yml:
dependencies:tallboy:github:epoch/tallboy
  1. Runshards install

Simple tutorial

  1. create a table withTallboy.table
table=Tallboy.tabledoend
  1. define columns. here we will define a 4 column table withcolumns.
table=Tallboy.tabledo  columnsdo    add"id"    add"name"    add"description"    add"price  endend
  1. add rows. you can add single row withrow or nested arrays withrows. values can beany object that has ato_s method.
table=Tallboy.tabledo  columnsdo    add"id"    add"name"    add"description"    add"price"end  rows [    [1,"cake","goes well with pudding",3.4],    [2,"pudding","so good with cake!",12.5],    [3,"burger","from the reburgulator",22.9],    [4,"chips","wait you mean fries?",5],  ]end
  1. add header. we can manually add header withheader with arguments or pass no arguments to inferred from column definitions. header is just a row with a border below.
table=Tallboy.tabledo  columnsdo    add"id"    add"name"    add"description"    add"price"end  header  rows [    [1,"cake","goes well with pudding",3.4],    [2,"pudding","so good with cake!",12.5],    [3,"burger","from the reburgulator",22.9],    [4,"chips","wait you mean fries?",5],  ]end
  1. add footer. we can add footer withfooter. footer is a row with border on top. If we pass a string instead of an array it will auto span all 4 columns based on the other rows defined in this table. nice! :)
table=Tallboy.tabledo  columnsdo    add"id"    add"name"    add"description"    add"price"end  header  rows [    [1,"cake","goes well with pudding",3.4],    [2,"pudding","so good with cake!",12.5],    [3,"burger","from the reburgulator",22.9],    [4,"chips","wait you mean fries?",5],  ]  footer"43.8"end
  1. set column span, widths and aligments.header,row andfooter also take blocks. here we can set column span on a cell within the footer.
table=Tallboy.tabledo  columnsdo    add"id"    add"name"    add"description"    add"price"end  header  rows [    [1,"cake","goes well with pudding",3.4],    [2,"pudding","so good with cake!",12.5],    [3,"burger","from the reburgulator",22.9],    [4,"chips","wait you mean fries?",5],  ]  footerdo    cell"total",span:3    cell"43.8"endend
  1. render with different border styles.
puts table.render# defaults to unicodeputs table.render(:ascii)# classic look# markdown does not support column spans and outer edge borders# turning off top and bottom border with border set to `:none`table=Tallboy.table(border::none)do  header ["name","hex"]  row ["mistyrose","#ffe4e1"]  row ["darkolivegreen","#556b2f"]  row ["papayawhip","#ffefd5"]endputs table.render(:markdown)
| name           | hex     ||----------------|---------|| mistyrose      | #ffe4e1 || darkolivegreen | #556b2f || papayawhip     | #ffefd5 |
  1. tallboy supports rendering into custom IO
table.render(IO::Memory.new)puts(Tallboy.tabledo    row [1,2,3]end)

How it works

Most components in tallboy can be invoked separately. The design philosophy is inspired by how web browsers renders HTML.

┌───────────────────────────────────────────────────────────┐│                  web browser vs tallboy                   │├───────────────────────────────────────────────────────────┤│ HTML ──> Document Object Model ──> render tree ──> pixels ││ DSL  ──> Table Object Model    ──> render tree ──> text   │└───────────────────────────────────────────────────────────┘
data= [  [1,2,3],  [4,5,6]]# TableBuilder is the DSL that returns an object modeltable_object_model=Tallboy::TableBuilder.newdo   rows(data)end# ComputedTableBuilder takes the object model and calculate widths for each cellcomputed_table=Tallboy::ComputedTableBuilder.new(table_object_model).build# RenderTreeBuilder work out borders, spans and organize into nodes to renderingrender_tree=Tallboy::RenderTreeBuilder.new(computed_table).build# render into output with unicode border styleio=Tallboy::Renderer.new(render_tree).render

API

for more examples checkouttallboy_spec.cr

Contributing

Issues and pull requests are welcome on GitHub at (https://github.com/epoch/tallboy)

About

Declarative API for drawing unicode/ascii character tables in crystal lang

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published

Languages


[8]ページ先頭

©2009-2025 Movatter.jp