Module: Dex::UI::ANSI

Defined in:
lib/dex/ui/ansi.rb

Constant Summary collapse

ESC =
"\x1b"

Class Method Summary collapse

Class Method Details

.control(args, cmd) ⇒ Object



18
19
20
# File 'lib/dex/ui/ansi.rb', line 18

def self.control(args, cmd)
  ESC + "[" + args + cmd
end

.cursor_back(n = 1) ⇒ Object



42
43
44
45
# File 'lib/dex/ui/ansi.rb', line 42

def self.cursor_back(n = 1)
  return '' if n.zero?
  control(n.to_s, 'D')
end

.cursor_down(n = 1) ⇒ Object



32
33
34
35
# File 'lib/dex/ui/ansi.rb', line 32

def self.cursor_down(n = 1)
  return '' if n.zero?
  control(n.to_s, 'B')
end

.cursor_forward(n = 1) ⇒ Object



37
38
39
40
# File 'lib/dex/ui/ansi.rb', line 37

def self.cursor_forward(n = 1)
  return '' if n.zero?
  control(n.to_s, 'C')
end

.cursor_up(n = 1) ⇒ Object



27
28
29
30
# File 'lib/dex/ui/ansi.rb', line 27

def self.cursor_up(n = 1)
  return '' if n.zero?
  control(n.to_s, 'A')
end

.printing_width(str) ⇒ Object

ANSI escape sequences (like x1b[31m) have zero width. when calculating the padding width, we must exclude them.



10
11
12
# File 'lib/dex/ui/ansi.rb', line 10

def self.printing_width(str)
  strip_codes(str).size
end

.sgr(params) ⇒ Object



23
24
25
# File 'lib/dex/ui/ansi.rb', line 23

def self.sgr(params)
  control(params.to_s, 'm')
end

.strip_codes(str) ⇒ Object



14
15
16
# File 'lib/dex/ui/ansi.rb', line 14

def self.strip_codes(str)
  str.gsub(/\x1b\[[\d;]+[A-z]|\r/, '')
end