Module: WhirledPeas::Utils::Ansi

Defined in:
lib/whirled_peas/utils/ansi.rb

Overview

Helper module for working with ANSI escape codes. The most useful ANSI escape codes relate to text formatting.

Constant Summary collapse

ESC =
"\033"
BOLD =

Text formatting constants

1
UNDERLINE =
4
BLACK =

Text and background color constants

30
RED =
31
GREEN =
32
YELLOW =
33
BLUE =
34
MAGENTA =
35
CYAN =
36
WHITE =
37
BRIGHT_OFFSET =

Bright colors are offset by this much from their standard versions

60

Class Method Summary collapse

Class Method Details

.clearObject



58
59
60
# File 'lib/whirled_peas/utils/ansi.rb', line 58

def clear
  esc_seq(END_FORMATTING)
end

.clear_downObject



54
55
56
# File 'lib/whirled_peas/utils/ansi.rb', line 54

def clear_down
  "#{ESC}[J"
end

.cursor_pos(left:, top:) ⇒ Object



46
47
48
# File 'lib/whirled_peas/utils/ansi.rb', line 46

def cursor_pos(left:, top:)
  "#{ESC}[#{top + 1};#{left + 1}H"
end

.cursor_visible(visible) ⇒ Object



50
51
52
# File 'lib/whirled_peas/utils/ansi.rb', line 50

def cursor_visible(visible)
  visible ? "#{ESC}[?25h" : "#{ESC}[?25l"
end

.esc_seq(code) ⇒ Object



62
63
64
# File 'lib/whirled_peas/utils/ansi.rb', line 62

def esc_seq(code)
  "#{ESC}[#{code}m"
end

.with_screen(output = STDOUT, width: nil, height: nil, &block) ⇒ Object



31
32
33
34
35
36
37
38
39
40
41
42
43
44
# File 'lib/whirled_peas/utils/ansi.rb', line 31

def with_screen(output=STDOUT, width: nil, height: nil, &block)
  require 'highline'
  unless width && height
    width, height = HighLine.new.terminal.terminal_size
  end
  output.print cursor_visible(false)
  output.flush
  yield width, height
ensure
  output.print clear
  output.print cursor_pos(left: 0, top: height - 1)
  output.print cursor_visible(true)
  output.flush
end