module Dev::UI::ANSI

Constants

ESC

Public Class Methods

control(args, cmd) click to toggle source

Returns an ANSI control sequence

Attributes

  • args - Argument to pass to the ANSI control sequence

  • cmd - ANSI control sequence Command

# File lib/dev/ui/ansi.rb, line 47
def self.control(args, cmd)
  ESC + "[" + args + cmd
end
cursor_back(n = 1) click to toggle source

Move the cursor back n columns

Attributes

  • n - number of columns by which to move the cursor back

# File lib/dev/ui/ansi.rb, line 97
def self.cursor_back(n = 1)
  return '' if n.zero?
  control(n.to_s, 'D')
end
cursor_down(n = 1) click to toggle source

Move the cursor down n lines

Attributes

  • n - number of lines by which to move the cursor down

# File lib/dev/ui/ansi.rb, line 75
def self.cursor_down(n = 1)
  return '' if n.zero?
  control(n.to_s, 'B')
end
cursor_forward(n = 1) click to toggle source

Move the cursor forward n columns

Attributes

  • n - number of columns by which to move the cursor forward

# File lib/dev/ui/ansi.rb, line 86
def self.cursor_forward(n = 1)
  return '' if n.zero?
  control(n.to_s, 'C')
end
cursor_horizontal_absolute(n = 1) click to toggle source

Move the cursor to a specific column

Attributes

  • n - The column to move to

# File lib/dev/ui/ansi.rb, line 108
def self.cursor_horizontal_absolute(n = 1)
  control(n.to_s, 'G')
end
cursor_restore() click to toggle source

Restore the saved cursor position

# File lib/dev/ui/ansi.rb, line 132
def self.cursor_restore
  control('', 'u')
end
cursor_save() click to toggle source

Save the cursor position

# File lib/dev/ui/ansi.rb, line 126
def self.cursor_save
  control('', 's')
end
cursor_up(n = 1) click to toggle source

Move the cursor up n lines

Attributes

  • n - number of lines by which to move the cursor up

# File lib/dev/ui/ansi.rb, line 64
def self.cursor_up(n = 1)
  return '' if n.zero?
  control(n.to_s, 'A')
end
end_of_line() click to toggle source

Move to the end of the line

# File lib/dev/ui/ansi.rb, line 150
def self.end_of_line
  control("\033[", 'C')
end
hide_cursor() click to toggle source

Hide the cursor

# File lib/dev/ui/ansi.rb, line 120
def self.hide_cursor
  control('', "?25l")
end
next_line() click to toggle source

Move to the next line

# File lib/dev/ui/ansi.rb, line 138
def self.next_line
  cursor_down + control('1', 'G')
end
previous_line() click to toggle source

Move to the previous line

# File lib/dev/ui/ansi.rb, line 144
def self.previous_line
  cursor_up + control('1', 'G')
end
printing_width(str) click to toggle source

ANSI escape sequences (like x1b[31m) have zero width. when calculating the padding width, we must exclude them. This also implements a basic version of utf8 character width calculation like we could get for real from something like utf8proc.

# File lib/dev/ui/ansi.rb, line 13
def self.printing_width(str)
  zwj = false
  strip_codes(str).codepoints.reduce(0) do |acc, cp|
    if zwj
      zwj = false
      next acc
    end
    case cp
    when 0x200d # zero-width joiner
      zwj = true
      acc
    else
      acc + 1
    end
  end
end
sgr(params) click to toggle source

en.wikipedia.org/wiki/ANSI_escape_code#graphics

# File lib/dev/ui/ansi.rb, line 52
def self.sgr(params)
  control(params.to_s, 'm')
end
show_cursor() click to toggle source

Show the cursor

# File lib/dev/ui/ansi.rb, line 114
def self.show_cursor
  control('', "?25h")
end
strip_codes(str) click to toggle source

Strips ANSI codes from a str

Attributes

  • str - The string from which to strip codes

# File lib/dev/ui/ansi.rb, line 36
def self.strip_codes(str)
  str.gsub(/\x1b\[[\d;]+[A-z]|\r/, '')
end