TTY – tables, frames and colours made of characters
The output below a cell is like a terminal: text, line by line, in a font where every character is equally wide. Command-line programs draw with exactly that – tables, frames and trees made of lines, plus colours. Piotr Murach's TTY toolkit is a family of about twenty small gems, each doing one job: tty-table, tty-box, tty-prompt … and pastel for colours. All pure Ruby. Let's start with colour:
install_gem "pastel"
require "pastel"
pastel = Pastel.new(enabled: true) # this is no terminal: colours on
puts pastel.green("12 tests, 0 failures")
puts pastel.red.bold("2 failures!")
puts pastel.black.on_yellow(" Chunky ") + " " + pastel.white.on_blue(" Bacon ")
pastel.red("bacon")The colours are not in the text, they are commands to the terminal. The => line shows what pastel.red("bacon") really returns: "\e[31mbacon\e[0m". \e is the escape character, [31m means “red from here on”, [0m “back to normal”. These ANSI escape codes date from the 1970s, and every terminal understands them – so does this page.
The methods chain: red.bold is red and bold, on_yellow colours the background. And enabled: true? Normally pastel checks whether its output goes to a terminal. Redirect it into a file (ruby bacon.rb > log.txt) and the codes would only be gibberish, so pastel leaves them out. A cell's output is no real terminal, so we switch the colours on ourselves.
Next, a table:
install_gem "tty-table"
require "tty-table"
table = TTY::Table.new(
header: ["Snack", "Price", "Stock"],
rows: [["Bacon", 4.5, 12], ["Pretzel", 2.0, 30], ["Cheese", 6.25, 5]]
)
# lines from Unicode box drawing, columns aligned left or right
puts table.render(:unicode, alignments: [:left, :right, :right], padding: [0, 1])
puts table.render(:ascii)TTY::Table.new gets the header and the rows as arrays. render draws them in one of three styles: :basic (no lines), :ascii (from +, - and | – any terminal, however old, can do that) and :unicode (with the box-drawing characters ┌─┐). alignments aligns each column – numbers to the right, so the digits line up –, padding: [0, 1] leaves no room above and below and one character left and right.
tty-table measures the width of each column itself, by its longest value. That is harder than it sounds: ä can be one character or two (a plus the dots), and 日本 takes twice as much room in a terminal as ab. The unicode-display_width gem, which tty-table builds on, counts that.
Frames around a text come from tty-box:
install_gem "tty-box"
require "tty-box"
puts TTY::Box.frame("Order received!", "3 strips of bacon",
title: { top_left: " Kiosk " }, padding: [0, 2], align: :center)
puts TTY::Box.frame("Sold out", border: :thick, padding: [0, 1])frame takes one or more lines, title puts a heading into the frame (also top_right, bottom_left …), border: picks the line – :light, :thick or :ascii. Whole folder trees come from tty-tree, like the tree command – here the layout of your project from lesson 105 on:
install_gem "tty-tree"
require "tty-tree"
# a folder is a hash, a file a string
tree = TTY::Tree.new("timelog" => [
"Gemfile",
{ "lib" => ["entry.rb", "timesheet.rb"] },
{ "test" => ["entry_test.rb"] }
])
puts tree.renderAnd for a grand entrance, say when your program starts, tty-font writes letters made of letters (FIGlet fonts: :doom, :standard, :block, :straight …):
install_gem "tty-font"
require "tty-font"
puts TTY::Font.new(:doom).write("Bacon")Some TTY gems need a real terminal, with a keyboard and a cursor that can jump back – which is why they do not run here. tty-prompt asks questions, also as a menu you choose from with the arrow keys:
require "tty-prompt"
prompt = TTY::Prompt.new
name = prompt.ask("What's your name?")
snack = prompt.select("What would you like?", %w[Bacon Pretzel Cheese])
more = prompt.yes?("Anything else?")tty-spinner spins a little wheel while something takes time, tty-progressbar shows a progress bar, and tty-screen tells you how wide the terminal is. gem install tty gets you all of them at once. For tests there is TTY::Prompt::Test, which reads the answers from a string.
Item and Qty, below it Bacon with 3 and Pretzel with 2.# shopping list: Item | Qty, Bacon 3, Pretzel 2 – with Unicode lines