Terminal.rb version

Terminal access with support for ANSI control codes and BBCode-like embedded text attribute syntax.

Features

  • terminal emulator application detection
  • ANSI support detection
  • color depth detection
  • NO_COLOR convention support
  • markup of text attributes and colors using a BBCode-like syntax
  • calculation for correct display width of strings containing Unicdode chars inclusive emojis
  • generation of word-by-word wrapped text
  • supports CSIu protocol

Examples

Check whether ANSI is supported

Terminal.ansi?
# => true when current terminal emulator supports ANSI control codes
Terminal.puts "[red on_bright_yellow bold]Hello World![/]"
# when terminal supports ANSI
# => prints  "\e[31;103;1mHello World!\e[m"
# when terminal does not support ANSI
# => prints  "Hello World"

Calculate display width of a string

Terminal::Text.width('ライブラリは中国語、日本語、韓国語のテキストをサポートしています')
# => 64

Split text word-by-word and limit line width to 30 chars

Terminal::Text.each_line(<<~TEXT, limit: 30).to_a
  [b]Hello Ruby World![/]
  This gem provides terminal access with support for [i]ANSI[/] control codes
  and [i]BBCode-like[/] embedded text attribute syntax.
TEXT
# => ["\e[1mHello Ruby World!\e[m",
# =>  "This gem provides terminal",
# =>  "access with support for \e[3mANSI\e[m",
# =>  "control codes",
# =>  "and \e[3mBBCode-like\e[m embedded text",
# =>  "attribute syntax."]

Have a look at the examples directory to learn from code.

Installation

You can install the gem in your system with

gem install terminal_rb

or you can use Bundler to add Terminal.rb to your own project:

bundle add terminal_rb

After that you only need one line of code to have everything together

require 'terminal'