Module: Colsole
- Defined in:
- lib/colsole.rb,
lib/colsole/compat.rb,
lib/colsole/version.rb
Overview
This file contains methods that are called by the main Colsole module for compatibility with older versions of colsole. Do not use these methods directly.
Constant Summary collapse
- ANSI_COLORS =
{ 'n' => '', # no color 'k' => "\e[30m", # black 'r' => "\e[31m", # red 'g' => "\e[32m", # green 'y' => "\e[33m", # yellow 'b' => "\e[34m", # blue 'm' => "\e[35m", # magenta 'c' => "\e[36m", # cyan 'w' => "\e[37m", # white }
- ANSI_STYLES =
{ 'b' => "\e[1m", # bold 'u' => "\e[4m", # underlined 'i' => "\e[7m", # inverted 'z' => "\e[0m", # terminate }
- VERSION =
'0.8.1'
Instance Method Summary collapse
-
#colorize(string) ⇒ Object
Convert color markers to ansi colors.
- #colors ⇒ Object
-
#command_exist?(command) ⇒ Boolean
Returns true if the command exists in the path.
- #detect_terminal_size(*args) ⇒ Object
- #old_colorize(text) ⇒ Object
- #old_strip_colors(text) ⇒ Object
- #resay(text) ⇒ Object
-
#say(text, replace: false) ⇒ Object
Output a string with optional color markers to stdout.
-
#say!(text, replace: false) ⇒ Object
Output a string with optional color markers to stderr.
- #say_status(status, message = nil, color = nil) ⇒ Object
-
#strip_colors(string) ⇒ Object
Remove color markers.
-
#terminal?(stream = $stdout) ⇒ Boolean
Returns true if stdout/stderr are a terminal.
-
#terminal_height ⇒ Object
Returns the rows part of the ‘#terminal_size`.
-
#terminal_size(default = [80, 30]) ⇒ Object
Returns the terminal size as [columns, rows].
-
#terminal_width ⇒ Object
Returns the columns part of the ‘#terminal_size`.
-
#use_colors?(stream = $stdout) ⇒ Boolean
Returns true if we can and should use colors in this stream.
-
#word_wrap(text, length = nil) ⇒ Object
Converts a long string to be wrapped keeping words in tact.
Instance Method Details
#colorize(string) ⇒ Object
Convert color markers to ansi colors.
105 106 107 108 109 110 111 112 |
# File 'lib/colsole.rb', line 105 def colorize(string) # compatibility later compat_string = old_colorize string process_color_markers compat_string do |color, styles, text| "#{styles}#{color}#{text}#{ANSI_STYLES['z']}" end end |
#colors ⇒ Object
35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 |
# File 'lib/colsole/compat.rb', line 35 def colors @colors ||= begin esc = 27.chr pattern = "#{esc}[%{decor};%{fg}m" decors = { txt: 0, bld: 1, und: 4, rev: 7 } color_codes = { blk: 0, red: 1, grn: 2, ylw: 3, blu: 4, pur: 5, cyn: 6, wht: 7 } colors = {} decors.each do |dk, dv| color_codes.each do |ck, cv| key = "#{dk}#{ck}" val = pattern % { decor: dv, fg: "3#{cv}" } colors[key] = val end end colors['txtbld'] = "#{esc}[1m" colors['txtund'] = "#{esc}[4m" colors['txtrev'] = "#{esc}[7m" colors['txtrst'] = "#{esc}[0m" colors end end |
#command_exist?(command) ⇒ Boolean
Returns true if the command exists in the path
49 50 51 52 53 |
# File 'lib/colsole.rb', line 49 def command_exist?(command) ENV['PATH'].split(File::PATH_SEPARATOR).any? do |dir| File.exist?(File.join dir, command) or File.exist?(File.join dir, "#{command}.exe") end end |
#detect_terminal_size(*args) ⇒ Object
5 6 7 |
# File 'lib/colsole/compat.rb', line 5 def detect_terminal_size(*args) terminal_size(*args) end |
#old_colorize(text) ⇒ Object
9 10 11 12 13 14 15 16 17 18 19 20 |
# File 'lib/colsole/compat.rb', line 9 def old_colorize(text) reset = colors['txtrst'] reset_called_last = true out = text.gsub(/!([a-z]{6})!/) do reset_called_last = $1 == 'txtrst' colors[$1] end reset_called_last or out = "#{out}#{reset}" out end |
#old_strip_colors(text) ⇒ Object
22 23 24 |
# File 'lib/colsole/compat.rb', line 22 def old_strip_colors(text) text.gsub(/!([a-z]{6})!/, '') end |
#resay(text) ⇒ Object
26 27 28 |
# File 'lib/colsole/compat.rb', line 26 def resay(text) say text, replace: true end |
#say(text, replace: false) ⇒ Object
Output a string with optional color markers to stdout. If text is ended with a white space, you can call again with replace: true to replace that line
28 29 30 |
# File 'lib/colsole.rb', line 28 def say(text, replace: false) internal_say text, $stdout, replace: replace end |
#say!(text, replace: false) ⇒ Object
Output a string with optional color markers to stderr. Behaves similarly to ‘#say`.
34 35 36 |
# File 'lib/colsole.rb', line 34 def say!(text, replace: false) internal_say text, $stderr, replace: replace end |
#say_status(status, message = nil, color = nil) ⇒ Object
30 31 32 33 |
# File 'lib/colsole/compat.rb', line 30 def say_status(status, = nil, color = nil) color ||= ( ? :txtgrn : :txtblu) say "!#{color}!#{status.to_s.rjust 12} !txtrst! #{}".strip end |
#strip_colors(string) ⇒ Object
Remove color markers.
115 116 117 118 119 120 |
# File 'lib/colsole.rb', line 115 def strip_colors(string) # compatibility layer compat_string = old_strip_colors string process_color_markers(compat_string) { |_color, _styles, text| text } end |
#terminal?(stream = $stdout) ⇒ Boolean
Returns true if stdout/stderr are a terminal
39 40 41 42 43 44 45 46 |
# File 'lib/colsole.rb', line 39 def terminal?(stream = $stdout) case ENV['TTY'] when 'on' then true when 'off' then false else stream.tty? end end |
#terminal_height ⇒ Object
Returns the rows part of the ‘#terminal_size`
82 83 84 |
# File 'lib/colsole.rb', line 82 def terminal_height terminal_size[1] end |
#terminal_size(default = [80, 30]) ⇒ Object
Returns the terminal size as [columns, rows]. Environment variables can be used to cheat.
62 63 64 65 66 67 68 69 70 71 72 73 74 |
# File 'lib/colsole.rb', line 62 def terminal_size(default = [80, 30]) result = if (ENV['COLUMNS'] =~ /^\d+$/) && (ENV['LINES'] =~ /^\d+$/) [ENV['COLUMNS'].to_i, ENV['LINES'].to_i] else safe_get_tty_size default end unless result[0].is_a?(Integer) && result[1].is_a?(Integer) && result[0].positive? && result[1].positive? result = default end result end |
#terminal_width ⇒ Object
Returns the columns part of the ‘#terminal_size`
77 78 79 |
# File 'lib/colsole.rb', line 77 def terminal_width terminal_size[0] end |
#use_colors?(stream = $stdout) ⇒ Boolean
Returns true if we can and should use colors in this stream
56 57 58 |
# File 'lib/colsole.rb', line 56 def use_colors?(stream = $stdout) ENV['FORCE_COLOR'] || (!ENV['NO_COLOR'] && terminal?(stream)) end |
#word_wrap(text, length = nil) ⇒ Object
Converts a long string to be wrapped keeping words in tact. If the string starts with one or more spaces, they will be preserved in all subsequent lines (i.e., remain indented).
89 90 91 92 93 94 95 96 97 98 99 100 101 102 |
# File 'lib/colsole.rb', line 89 def word_wrap(text, length = nil) length ||= terminal_width lead = text[/^\s*/] text.strip! length -= lead.length text.split("\n").collect! do |line| if line.length > length line.gsub!(/([^\s]{#{length}})([^\s$])/, '\\1 \\2') line.gsub(/(.{1,#{length}})(\s+|$)/, "#{lead}\\1\n").rstrip else "#{lead}#{line}" end end * "\n" end |