Module: Colorator

Defined in:
lib/colorator.rb

Constant Summary collapse

VERSION =
"1.1.0"
ANSI_MATCHR =

/\x1b.*?[jkmsuABGKH]/
ANSI_COLORS =
{
  :black   => 30,
  :red     => 31,
  :green   => 32,
  :yellow  => 33,
  :blue    => 34,
  :magenta => 35,
  :cyan    => 36,
  :white   => 37,
  :bold    => 1
}
CORE_METHODS =

(
  public_methods - Object.methods
)

Class Method Summary collapse

Class Method Details

.ansi_jump(str, num) ⇒ Object


Jump the cursor, moving it up and then back down to it’s spot, allowing you to do fancy things like multiple output (downloads) the way that Docker does them in an async way without breaking term.




38
39
40
41
42
# File 'lib/colorator.rb', line 38

def ansi_jump(str, num)
  "\x1b[#{num}A#{clear_line(str)}\x1b[#{
    num
  }B"
end

.clear_line(str = "") ⇒ Object




54
55
56
57
58
# File 'lib/colorator.rb', line 54

def clear_line(str = "")
  "\x1b[2K\r#{
    str
  }"
end

.clear_screen(str = "") ⇒ Object


Clear the screen’s current view, so the user gets a clean output.




74
75
76
77
78
# File 'lib/colorator.rb', line 74

def clear_screen(str = "")
  "\x1b[H\x1b[2J#{
    str
  }"
end

.colorize(str = "", color) ⇒ Object




82
83
84
# File 'lib/colorator.rb', line 82

def colorize(str = "", color)
  "\x1b[#{color}m#{str}\x1b[0m"
end

.has_ansi?(str) ⇒ Boolean Also known as: has_color?


Allows you to check if a string currently has ansi.


Returns:

  • (Boolean)


26
27
28
29
30
# File 'lib/colorator.rb', line 26

def has_ansi?(str)
  str.match(ANSI_MATCHR).is_a?(
    MatchData
  )
end

.reset_ansi(str = "") ⇒ Object Also known as: reset_color




46
47
48
49
50
# File 'lib/colorator.rb', line 46

def reset_ansi(str = "")
  "\x1b[0m#{
    str
  }"
end

.strip_ansi(str) ⇒ Object Also known as: strip_color


Strip ANSI from the current string, making it just a normal string.




64
65
66
67
68
# File 'lib/colorator.rb', line 64

def strip_ansi(str)
  str.gsub(
    ANSI_MATCHR, ""
  )
end