Module: Obfusk::Util::Term

Defined in:
lib/obfusk/util/term.rb

Constant Summary collapse

TERM_COLOUR_CODES =

ansi colour codes

{                                         # {{{1
  black:      '0;30', dark_gray:    '1;30',
  blue:       '0;34', light_blue:   '1;34',
  green:      '0;32', light_green:  '1;32',
  cyan:       '0;36', light_cyan:   '1;36',
  red:        '0;31', light_red:    '1;31',
  purple:     '0;35', light_purple: '1;35',
  brown:      '0;33', yellow:       '1;33',
  light_gray: '0;37', white:        '1;37',
  none:       '0'
}
TERM_COLOUR_ALIASES =

some colour aliases

{                                       # {{{1
  bla: :black,      dgr: :dark_gray,
  blu: :blue,       lbl: :light_blue,
  grn: :green,      lgn: :light_green,
  cyn: :cyan,       lcy: :light_cyan,   lrd: :light_red,
  pur: :purple,     lpu: :light_purple,
  bro: :brown,      yel: :yellow,
  lgr: :light_gray, whi: :white,        non: :none,
}
TERM_COLOUR_ESCAPES =

ansi colour escapes

(->(;a,b) {
a = Hash[TERM_COLOUR_CODES.map   { |k,v| [k,"\e[#{v}m"] }]
b = Hash[TERM_COLOUR_ALIASES.map { |k,v| [k,    a[v]  ]
GET_COLS =
'TERM=${TERM:-dumb} tput cols'
GET_LINES =
'TERM=${TERM:-dumb} tput lines'

Class Method Summary collapse

Class Method Details

.colour(x, what = :out) ⇒ Object

colour code (or '' if not tty)



54
55
56
57
58
# File 'lib/obfusk/util/term.rb', line 54

def self.colour(x, what = :out)
  c = TERM_COLOUR_ESCAPES[x] or raise ArgumentError,
        "No such colour: #{x}"
  tty?(what) ? c : ''
end

.colour_e(x) ⇒ Object

colour code for $stderr



61
62
63
# File 'lib/obfusk/util/term.rb', line 61

def self.colour_e(x)
  colour x, :err
end

.columnsObject

terminal columns



68
69
70
# File 'lib/obfusk/util/term.rb', line 68

def self.columns
  %x[#{GET_COLS}].to_i
end

.linesObject

terminal lines



73
74
75
# File 'lib/obfusk/util/term.rb', line 73

def self.lines
  %x[#{GET_LINES}].to_i
end

.prompt(prompt, hide = false) ⇒ Object

prompt for line; optionally hide input



85
86
87
88
89
90
# File 'lib/obfusk/util/term.rb', line 85

def self.prompt(prompt, hide = false)
  print prompt; $stdout.flush
  line = hide ? $stdin.noecho { |i| i.gets } .tap { puts } :
                $stdin.gets
  line && line.chomp
end

.tty?(what = :out) ⇒ Boolean

is $stdout (or $stderr) a tty?

Returns:

  • (Boolean)


78
79
80
# File 'lib/obfusk/util/term.rb', line 78

def self.tty?(what = :out)
  (what == :out ? $stdout : $stderr).isatty
end