Module: Colsole
- Defined in:
- lib/colsole.rb,
lib/colsole/version.rb
Overview
Colsole - Colorful Console Applications
This class provides several utility functions for console application developers.
-
#colorize string - return a colorized strings
-
#say string - print a string with colors
-
#say! string - print a string with colors to stderr
-
#resay string - same as say, but overwrite current line
-
#say_status symbol, string [, color] - print a message with status
-
#word_wrap string - wrap a string and maintain indentation
-
#detect_terminal_size
Credits: terminal width detection by Gabrial Horner github.com/cldwalker
Constant Summary collapse
- VERSION =
"0.4.2"
Instance Method Summary collapse
-
#colorize(text = nil, force_color = false, stream = :stdout) ⇒ Object
Parses and returns a color-flagged string.
-
#command_exist?(command) ⇒ Boolean
Determines if a shell command exists.
-
#detect_terminal_size(default = [80,30]) ⇒ Object
Returns [width, height] of terminal when detected, or a default value otherwise.
-
#err_terminal? ⇒ Boolean
Returns true if stderr is interactive terminal.
-
#out_terminal? ⇒ Boolean
Returns true if stdout is interactive terminal.
-
#resay(text, force_color = false) ⇒ Object
Erase the current output line, and say a new string.
-
#say(text, force_color = false) ⇒ Object
Prints a color-flagged string.
-
#say!(text, force_color = false) ⇒ Object
Prints a color-flagged string to STDERR Use color flags (like !txtred!) to change color in the string.
-
#say_status(status, message, color = :txtgrn) ⇒ Object
Prints a line with a colored status and message.
-
#terminal?(stream = :stdout) ⇒ Boolean
Returns true if stdout/stderr is interactive terminal.
-
#terminal_width ⇒ Object
Returns terminal width with re-asking.
-
#word_wrap(text, length = nil) ⇒ Object
Converts a long string to be wrapped keeping words in tact.
Instance Method Details
#colorize(text = nil, force_color = false, stream = :stdout) ⇒ Object
Parses and returns a color-flagged string. Respects pipe and auto terminates colored strings. Call without text to see a list/demo of all available colors.
108 109 110 111 112 |
# File 'lib/colsole.rb', line 108 def colorize(text=nil, force_color=false, stream=:stdout) return show_color_demo if text.nil? return strip_color_markers(text) unless terminal?(stream) || force_color colorize! text end |
#command_exist?(command) ⇒ Boolean
Determines if a shell command exists.
68 69 70 |
# File 'lib/colsole.rb', line 68 def command_exist?(command) ENV['PATH'].split(File::PATH_SEPARATOR).any? {|d| File.exist? File.join(d, command) } end |
#detect_terminal_size(default = [80,30]) ⇒ Object
Returns [width, height] of terminal when detected, or a default value otherwise.
74 75 76 77 78 79 80 81 82 83 84 85 |
# File 'lib/colsole.rb', line 74 def detect_terminal_size(default=[80,30]) if (ENV['COLUMNS'] =~ /^\d+$/) && (ENV['LINES'] =~ /^\d+$/) [ENV['COLUMNS'].to_i, ENV['LINES'].to_i] elsif (RUBY_PLATFORM =~ /java/ || (!STDIN.tty? && ENV['TERM'])) && command_exist?('tput') [`tput cols`.to_i, `tput lines`.to_i] elsif STDIN.tty? && command_exist?('stty') result = `stty size`.scan(/\d+/).map { |s| s.to_i }.reverse result == [0,0] ? default : result else default end end |
#err_terminal? ⇒ Boolean
Returns true if stderr is interactive terminal
63 64 65 |
# File 'lib/colsole.rb', line 63 def err_terminal? STDERR.tty? end |
#out_terminal? ⇒ Boolean
Returns true if stdout is interactive terminal
58 59 60 |
# File 'lib/colsole.rb', line 58 def out_terminal? STDOUT.tty? end |
#resay(text, force_color = false) ⇒ Object
Erase the current output line, and say a new string. This should be used after a space terminated say().
40 41 42 43 |
# File 'lib/colsole.rb', line 40 def resay(text, force_color=false) terminal? and text = "\033[2K\r#{text}" say text, force_color end |
#say(text, force_color = false) ⇒ Object
Prints a color-flagged string. Use color flags (like !txtred!) to change color in the string. Space terminated strings will leave the cursor at the same line.
23 24 25 26 27 28 29 30 |
# File 'lib/colsole.rb', line 23 def say(text, force_color=false) last = text[-1, 1] if last == ' ' or last == '\t' print colorize(text, force_color) else print colorize("#{text}\n", force_color) end end |
#say!(text, force_color = false) ⇒ Object
Prints a color-flagged string to STDERR Use color flags (like !txtred!) to change color in the string.
34 35 36 |
# File 'lib/colsole.rb', line 34 def say!(text, force_color=false) $stderr.puts colorize(text, force_color, :stderr) end |
#say_status(status, message, color = :txtgrn) ⇒ Object
Prints a line with a colored status and message. Status can be a symbol or a string. Color is optional, defaulted to green (:txtgrn).
48 49 50 |
# File 'lib/colsole.rb', line 48 def say_status(status, , color=:txtgrn) say "!#{color}!#{status.to_s.rjust 12} !txtrst! #{message}" end |
#terminal?(stream = :stdout) ⇒ Boolean
Returns true if stdout/stderr is interactive terminal
53 54 55 |
# File 'lib/colsole.rb', line 53 def terminal?(stream=:stdout) stream == :stdout ? out_terminal? : err_terminal? end |
#terminal_width ⇒ Object
Returns terminal width with re-asking
88 89 90 |
# File 'lib/colsole.rb', line 88 def terminal_width @terminal_width ||= detect_terminal_size[0] 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).
95 96 97 98 99 100 101 102 103 |
# File 'lib/colsole.rb', line 95 def word_wrap(text, length=nil) length ||= terminal_width lead = text[/^\s*/] text.strip! length -= lead.length text.split("\n").collect! do |line| line.length > length ? line.gsub(/(.{1,#{length}})(\s+|$)/, "#{lead}\\1\n").rstrip : "#{lead}#{line}" end * "\n" end |