CLI::UI hidden gems ๐Ÿ’Ž

Explore the hidden gems of CLI::UI. Adding links, printing tables & separating frames for better command-line interface experience.

Published ยท Revised ยท 89 words ยท View as Markdown


Contents
  1. Adding links ๐Ÿ”—
  2. Printing tables
  3. Seperating frames

CLI::UI is a Ruby gem designed to enhance command-line interfaces (CLIs) by providing a framework for formatting and styling text output. It integrates seamlessly with other Ruby gems like Thor, expanding its capabilities further.

There are a couple of cool undocumented features, here are a couple of them ๐Ÿ‘‡

CLI::UI.link
Ruby
puts CLI::UI.link('https://logs.panos.codes', 'My blog')

Printing tables#

Thor CLI print_table

CLI::UI formating can be combiented with the Thor gem, that give us access to the print_table method

Ruby
   row_1 = [
    CLI::UI.fmt("{{red:Header}}"),
    CLI::UI.fmt("{{red:Value}}"),
  ]

  row_2 = [
    CLI::UI.fmt('{{info:Accept-Encoding:}} '),  
    CLI::UI.fmt('{{white:gzip, deflate, br, zstd}}')
  ]

  row_3 = [
    CLI::UI.fmt('{{info:Accept:}} '),  
    CLI::UI.fmt('{{white:text/html,application/xhtml+xml,application/xml;q=0.9,image/avif,image/webp,image/apng,*/*;q=0.8}}')
  ]

  row_4 = [
    CLI::UI.fmt('{{info:Cache-Control:}} '),  
    CLI::UI.fmt('{{white:no-cache}}')
  ]

  Thor::Shell::Basic.new.print_table([ row_1, row_2, row_3, row_4]) 

Seperating frames#

CLI::UI Frame seperation

CLI::UI.Frame gives us a convitient .divider method

Ruby
CLI::UI::Frame.open(CLI::UI.link('https://logs.panos.codes', 'My blog'), timing: false) do
  CLI::UI::Frame.divider('Headers', frame_style: :bracket)
  Thor::Shell::Basic.new.print_table([ row_1, row_2, row_3, row_4]) 

  CLI::UI::Frame.divider('Response')
  puts "<html>...</html>"
end