Module: NattyUI::Ansi
- Defined in:
- lib/natty-ui/ansi.rb,
lib/natty-ui/ansi/constants.rb
Overview
Helper module for ANSI escape codes.
Constant Summary collapse
- CURSOR_SHOW =
"\e[?25h"
- CURSOR_HIDE =
"\e[?25l"
- CURSOR_HOME =
cursor_pos(nil, nil)
- CURSOR_FIRST_ROW =
cursor_pos(1).freeze
- CURSOR_FIRST_COLUMN =
cursor_column(1).freeze
- CURSOR_SAFE_POS_SCO =
"\e[s"
- CURSOR_SAFE_POS_DEC =
"\e7"
- CURSOR_SAFE_POS =
CURSOR_SAFE_POS_DEC
- CURSOR_RESTORE_POS_SCO =
"\e[u"
- CURSOR_RESTORE_POS_DEC =
"\e8"
- CURSOR_RESTORE_POS =
CURSOR_RESTORE_POS_DEC
- SCREEN_ERASE =
screen_erase(:entire)
- SCREEN_SAVE =
"\e[?47h"
- SCREEN_RESTORE =
"\e[?47l"
- SCREEN_ALTERNATE =
"\e[?1049h"
- SCREEN_ALTERNATE_OFF =
"\e[?1049l"
- LINE_PREVIOUS =
cursor_previous_line(1).freeze
- LINE_NEXT =
cursor_next_line(1).freeze
- LINE_ERASE =
line_erase(:entire)
- RESET =
self[:reset].freeze
- BOLD =
self[:bold].freeze
- BOLD_OFF =
self[:bold].freeze
- FAINT =
self[:faint].freeze
- FAINT_OFF =
self[:faint_off].freeze
- ITALIC =
self[:italic].freeze
- ITALIC_OFF =
self[:italic_off].freeze
- STRIKE =
self[:strike].freeze
- STRIKE_OFF =
self[:strike_off].freeze
- UNDERLINE =
self[:underline].freeze
- DOUBLE_UNDERLINE =
self[:double_underline].freeze
- UNDERLINE_OFF =
self[:underline_off].freeze
- CURLY_UNDERLINE =
self[:curly_underline].freeze
- CURLY_UNDERLINE_OFF =
self[:curly_underline_off].freeze
- BLINK =
self[:blink].freeze
- BLINK_OFF =
self[:blink_off].freeze
- INVERT =
self[:invert].freeze
- INVERT_OFF =
self[:invert_off].freeze
- HIDE =
self[:hide].freeze
- REVEAL =
self[:reveal].freeze
Class Attribute Summary collapse
-
.attribute_names ⇒ Array<Symbol>
readonly
Supported attribute names.
-
.color_names ⇒ Array<Symbol>
readonly
Supported color names.
Control functions collapse
-
.cursor_back(columns = 1) ⇒ String
Move cursor given colums back.
-
.cursor_column(column = 1) ⇒ String
Move cursor to given column in the current row.
-
.cursor_down(lines = 1) ⇒ String
Move cursor given lines down.
-
.cursor_forward(columns = 1) ⇒ String
Move cursor given colums forward.
-
.cursor_next_line(lines = 1) ⇒ String
Move cursor of beginning of the given next line.
-
.cursor_pos(row, column = nil) ⇒ String
Move cursor to given row and column counting from the top left corner.
-
.cursor_previous_line(lines = 1) ⇒ String
(also: cursor_prev_line)
Move cursor of beginning of the given previous line.
-
.cursor_up(lines = 1) ⇒ String
Move cursor given lines up.
-
.line_erase(part = :entire) ⇒ String
Clear part of current line.
-
.screen_erase(part = :entire) ⇒ String
Erase screen.
-
.scroll_down(lines = 1) ⇒ String
Scroll window given lines down.
-
.scroll_up(lines = 1) ⇒ String
Scroll window given lines up.
-
.tab_title(title) ⇒ String
Change tab title.
-
.window_title(title) ⇒ String
Change window title.
Tool functions collapse
-
.[](*attributes) ⇒ String
Combine given ANSI Ansi.attribute_names, Ansi.color_names and color codes.
-
.blemish(str) ⇒ String
Remove ANSI functions, attributes and colors from given string.
-
.embellish(str, *attributes, reset: true) ⇒ String
Decorate given
str
with ANSI attributes and colors. -
.rainbow(str, type: :foreground, frequence: 0.3, spread: 0.8, seed: 1.1) ⇒ String
Fancy text.
-
.try_convert(attributes) ⇒ String?
Try to combine given ANSI attributes and colors.
Class Attribute Details
.attribute_names ⇒ Array<Symbol> (readonly)
Supported attribute names.
14 |
# File 'lib/natty-ui/ansi.rb', line 14 def attribute_names = SATTR.keys.sort! |
.color_names ⇒ Array<Symbol> (readonly)
Supported color names.
21 |
# File 'lib/natty-ui/ansi.rb', line 21 def color_names = SCLR.keys |
Class Method Details
.[](*attributes) ⇒ String
Combine given ANSI attribute_names, color_names and color codes.
Colors can specified by their name for ANSI 3-bit and 4-bit colors.
For 8-bit ANSI colors use 2-digit hexadecimal values 00
...ff
.
To use RGB ANSI colors (24-bit colors) specify 3-digit or 6-digit
hexadecimal values 000
...fff
or 000000
...ffffff
.
This represent the RRGGBB
values (or RGB
for short version) like you
may known from CSS color notation.
To use a color as background color prefix the color attribute with bg_
or on_
.
To use a color as underline color prefix the color attribute with ul_
.
To clarify that a color attribute have to be used as foreground
color use the prefix fg_
.
198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 |
# File 'lib/natty-ui/ansi.rb', line 198 def [](*attributes) return '' if attributes.empty? "\e[#{ attributes .map do |arg| case arg when Symbol SATTR[arg] || SCLR[arg] || color(arg.to_s) || invalid_argument(arg) when String ATTR[arg] || CLR[arg] || color(arg) || invalid_argument(arg) when (0..255) "38;5;#{arg}" when (256..511) "48;5;#{arg - 256}" when (512..767) "58;5;#{arg - 512}" else invalid_argument(arg) end end .join(';') }m" end |
.blemish(str) ⇒ String
Remove ANSI functions, attributes and colors from given string.
242 |
# File 'lib/natty-ui/ansi.rb', line 242 def blemish(str) = str.to_s.gsub(ESC, '') |
.cursor_back(columns = 1) ⇒ String
Move cursor given colums back.
47 |
# File 'lib/natty-ui/ansi.rb', line 47 def cursor_back(columns = 1) = "\e[#{columns}D" |
.cursor_column(column = 1) ⇒ String
Move cursor to given column in the current row.
66 |
# File 'lib/natty-ui/ansi.rb', line 66 def cursor_column(column = 1) = "\e[#{column}G" |
.cursor_down(lines = 1) ⇒ String
Move cursor given lines down.
35 |
# File 'lib/natty-ui/ansi.rb', line 35 def cursor_down(lines = 1) = "\e[#{lines}B" |
.cursor_forward(columns = 1) ⇒ String
Move cursor given colums forward.
41 |
# File 'lib/natty-ui/ansi.rb', line 41 def cursor_forward(columns = 1) = "\e[#{columns}C" |
.cursor_next_line(lines = 1) ⇒ String
Move cursor of beginning of the given next line.
53 |
# File 'lib/natty-ui/ansi.rb', line 53 def cursor_next_line(lines = 1) = "\e[#{lines}E" |
.cursor_pos(row, column = nil) ⇒ String
Move cursor to given row and column counting from the top left corner.
73 74 75 76 |
# File 'lib/natty-ui/ansi.rb', line 73 def cursor_pos(row, column = nil) return column ? "\e[#{row};#{column}H" : "\e[#{row}H" if row column ? "\e[;#{column}H" : "\e[H" end |
.cursor_previous_line(lines = 1) ⇒ String Also known as: cursor_prev_line
Move cursor of beginning of the given previous line.
59 |
# File 'lib/natty-ui/ansi.rb', line 59 def cursor_previous_line(lines = 1) = "\e[#{lines}F" |
.cursor_up(lines = 1) ⇒ String
Move cursor given lines up.
29 |
# File 'lib/natty-ui/ansi.rb', line 29 def cursor_up(lines = 1) = "\e[#{lines}A" |
.embellish(str, *attributes, reset: true) ⇒ String
Decorate given str
with ANSI attributes and colors.
231 232 233 234 |
# File 'lib/natty-ui/ansi.rb', line 231 def embellish(str, *attributes, reset: true) attributes = self[*attributes] attributes.empty? ? str.to_s : "#{attributes}#{str}#{"\e[0m" if reset}" end |
.line_erase(part = :entire) ⇒ String
Clear part of current line.
95 96 97 98 99 |
# File 'lib/natty-ui/ansi.rb', line 95 def line_erase(part = :entire) return "\e[0K" if part == :to_end return "\e[1K" if part == :to_start "\e[2K" end |
.rainbow(str, type: :foreground, frequence: 0.3, spread: 0.8, seed: 1.1) ⇒ String
Returns fancy text.
274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 |
# File 'lib/natty-ui/ansi.rb', line 274 def rainbow( str, type: :foreground, frequence: 0.3, spread: 0.8, seed: 1.1 ) type = color_type(type) pos = -1 str .to_s .chars .map! do |char| i = (seed + ((pos += 1) / spread)) * frequence "\e[#{type};2;#{(Math.sin(i) * 255).abs.to_i};" \ "#{(Math.sin(i + PI2_THIRD) * 255).abs.to_i};" \ "#{(Math.sin(i + PI4_THIRD) * 255).abs.to_i}m#{char}" end .join end |
.screen_erase(part = :entire) ⇒ String
Erase screen.
84 85 86 87 88 89 |
# File 'lib/natty-ui/ansi.rb', line 84 def screen_erase(part = :entire) return "\e[0J" if part == :below return "\e[1J" if part == :above return "\e[3J" if part == :scrollback "\e[2J" end |
.scroll_down(lines = 1) ⇒ String
Scroll window given lines down.
115 |
# File 'lib/natty-ui/ansi.rb', line 115 def scroll_down(lines = 1) = "\e[;#{lines}T" |
.scroll_up(lines = 1) ⇒ String
Scroll window given lines up.
109 |
# File 'lib/natty-ui/ansi.rb', line 109 def scroll_up(lines = 1) = "\e[;#{lines}S" |
.tab_title(title) ⇒ String
Change tab title. This is not widely supported but by iTerm.
141 |
# File 'lib/natty-ui/ansi.rb', line 141 def tab_title(title) = "\e]0;#{title}\a" |
.try_convert(attributes) ⇒ String?
Try to combine given ANSI attributes and colors. The attributes and colors have to be seperated by space char (" ").
259 260 261 262 263 264 265 266 |
# File 'lib/natty-ui/ansi.rb', line 259 def try_convert(attributes) return if !attributes || (attributes = attributes.to_s.split).empty? "\e[#{ attributes .map! { ATTR[_1] || CLR[_1] || color(_1) || return } .join(';') }m" end |
.window_title(title) ⇒ String
Change window title. This is not widely supported but by XTerm and iTerm.
134 |
# File 'lib/natty-ui/ansi.rb', line 134 def window_title(title) = "\e]2;#{title}\e\\" |