Class: Nodule::Console

Inherits:
Base
  • Object
show all
Defined in:
lib/nodule/console.rb

Overview

a simple colored output resource

e.g. Nodule::Console.new(:fg => :green) Nodule::Console.new(:fg => :green, :bg => :white)

Constant Summary collapse

COLORS =
{
  :black   => "\x1b[30m",
  :red     => "\x1b[31m",
  :green   => "\x1b[32m",
  :yellow  => "\x1b[33m",
  :blue    => "\x1b[34m",
  :magenta => "\x1b[35m",
  :cyan    => "\x1b[36m",
  :white   => "\x1b[37m",
  :dkgray  => "\x1b[1;30m",
  :dkred   => "\x1b[1;31m",
  :reset   => "\x1b[0m",
}.freeze

Instance Attribute Summary

Attributes inherited from Base

#prefix, #read_count, #readers, #running, #topology

Instance Method Summary collapse

Methods inherited from Base

#add_reader, #add_readers, #clear!, #done?, #join_topology!, #output, #output!, #output?, #read_until, #require_read_count, #run, #run_readers, #stop, #stop!, #verbose, #wait, #wait_with_backoff

Constructor Details

#initialize(opts = {}) ⇒ Console

Create a new console handler. Defaults to printing to STDERR without color. Color output is automatically disabled on non-tty devices. It can be force-enabled with the CLICOLOR_FORCE environment variable. The list of valid colors is in Nodule::Console::COLORS.

Parameters:

  • opts (Hash) (defaults to: {})
  • [Symbol] (Hash)

    a customizable set of options

  • [IO] (Hash)

    a customizable set of options



36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
# File 'lib/nodule/console.rb', line 36

def initialize(opts={})
  super(opts)
  @fg = opts[:fg]
  @bg = opts[:bg]

  if @fg and not COLORS.has_key?(@fg)
    raise ArgumentError.new "fg :#{@fg} is not a valid color"
  end

  if @bg and not COLORS.has_key?(@bg)
    raise ArgumentError.new "bg :#{@bg} is not a valid color"
  end

  # IO handle to use as the console
  @io = opts[:io] || STDERR

  # from https://github.com/sickill/rainbow/blob/master/lib/rainbow.rb
  @enabled = @io.tty? && ENV['TERM'] != 'dumb' || ENV['CLICOLOR_FORCE'] == '1'

  add_reader { |line,src| display(src, line) }
end

Instance Method Details

#bg(str) ⇒ Object



63
64
65
66
# File 'lib/nodule/console.rb', line 63

def bg(str)
  return str unless @enabled
  "#{COLORS[@bg]}#{str}"
end

#display(src, line) ⇒ Object

Write to stdout using puts, but append a prefix if it’s defined.

Parameters:

  • src (Object)

    if this responds to :prefix, :prefix will be prepended to the output

  • line (String)

    the data to write to stdout



78
79
80
81
82
83
84
# File 'lib/nodule/console.rb', line 78

def display(src, line)
  if src.respond_to? :prefix
    @io.print "#{reset('')}#{src.prefix}#{reset(bg(fg(line)))}\n"
  else
    @io.print "#{reset(bg(fg(line)))}\n"
  end
end

#fg(str) ⇒ Object



58
59
60
61
# File 'lib/nodule/console.rb', line 58

def fg(str)
  return str unless @enabled
  "#{COLORS[@fg]}#{str}"
end

#reset(str) ⇒ Object



68
69
70
71
# File 'lib/nodule/console.rb', line 68

def reset(str)
  return str unless @enabled
  "#{str}#{COLORS[:reset]}"
end