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

  • #terminal_width

Credits: terminal width detection by Gabrial Horner github.com/cldwalker

Constant Summary collapse

VERSION =
"0.7.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.



119
120
121
122
123
# File 'lib/colsole.rb', line 119

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.

Returns:

  • (Boolean)


71
72
73
74
75
# File 'lib/colsole.rb', line 71

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(default = [80,30]) ⇒ Object

Returns [width, height] of terminal when detected, or a default value otherwise.



79
80
81
82
83
84
85
86
87
88
89
90
91
# File 'lib/colsole.rb', line 79

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

Returns:

  • (Boolean)


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

def err_terminal?
  ENV['TTY'] == 'on' ? true : ENV['TTY'] == 'off' ? false : $stderr.tty?
end

#out_terminal?Boolean

Returns true if stdout is interactive terminal

Returns:

  • (Boolean)


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

def out_terminal?
  ENV['TTY'] == 'on' ? true : ENV['TTY'] == 'off' ? false : $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().



41
42
43
44
# File 'lib/colsole.rb', line 41

def resay(text, force_color = false)
  text = "\033[2K\r#{text}" if terminal?
  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.



24
25
26
27
28
29
30
31
# File 'lib/colsole.rb', line 24

def say(text, force_color = false)
  last = text[-1, 1]
  if terminal? and (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.



35
36
37
# File 'lib/colsole.rb', line 35

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



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

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

Returns:

  • (Boolean)


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

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

#terminal_widthObject

Returns terminal width



94
95
96
# File 'lib/colsole.rb', line 94

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



101
102
103
104
105
106
107
108
109
110
111
112
113
114
# File 'lib/colsole.rb', line 101

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