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.5.2"

Instance Method Summary collapse

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.



111
112
113
114
115
# File 'lib/colsole.rb', line 111

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.



70
71
72
# File 'lib/colsole.rb', line 70

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.



76
77
78
79
80
81
82
83
84
85
86
87
88
# File 'lib/colsole.rb', line 76

def detect_terminal_size(default=[80,30])
  if (ENV['COLUMNS'] =~ /^\d+$/) && (ENV['LINES'] =~ /^\d+$/)
    result = [ENV['COLUMNS'].to_i, ENV['LINES'].to_i]
  elsif (RUBY_PLATFORM =~ /java/ || (!STDIN.tty? && ENV['TERM'])) && command_exist?('tput')
    result = [`tput cols 2>&1`.to_i, `tput lines 2>&1`.to_i]
  elsif STDIN.tty? && command_exist?('stty')
    result = `stty size 2>&1`.scan(/\d+/).map { |s| s.to_i }.reverse
  else
    result = default
  end
  result = default unless result[0].is_a? Integer and result[1].is_a? Integer and result[0] > 0 and result[1] > 0
  result
end

#err_terminal?Boolean

Returns true if stderr is interactive terminal



65
66
67
# File 'lib/colsole.rb', line 65

def err_terminal?
  STDERR.tty?
end

#out_terminal?Boolean

Returns true if stdout is interactive terminal



60
61
62
# File 'lib/colsole.rb', line 60

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 = nil, color = nil) ⇒ Object

Prints a line with a colored status and message. Status can be a symbol or a string. Color is optional, defaults to green (:txtgrn) when there is a message, and to blue (:txtblu) when there is only a status



49
50
51
52
# File 'lib/colsole.rb', line 49

def say_status(status, message=nil, color=nil)
  color ||= (message ? :txtgrn : :txtblu)
  say "!#{color}!#{status.to_s.rjust 12} !txtrst! #{message}".strip
end

#terminal?(stream = :stdout) ⇒ Boolean

Returns true if stdout/stderr is interactive terminal



55
56
57
# File 'lib/colsole.rb', line 55

def terminal?(stream=:stdout)
  stream == :stdout ? out_terminal? : err_terminal?
end

#terminal_widthObject

Returns terminal width



91
92
93
# File 'lib/colsole.rb', line 91

def 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).



98
99
100
101
102
103
104
105
106
# File 'lib/colsole.rb', line 98

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