Class: Achoo::Term

Inherits:
Object
  • Object
show all
Defined in:
lib/achoo/term.rb,
lib/achoo/term/menu.rb,
lib/achoo/term/table.rb

Defined Under Namespace

Classes: Menu, Table

Constant Summary collapse

BOLD =
1
UNDERLINE =
4
RED =
31
YELLOW =
33

Class Method Summary collapse

Class Method Details

.ask(question = '') ⇒ Object



30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
# File 'lib/achoo/term.rb', line 30

def self.ask(question='')
  answer = nil
  loop do
    print bold("#{question}> ")
    $stdout.flush
    answer = gets
    
    # Answer is nil if user hits C-d on an empty input
    if answer.nil?
      puts
      exit
    end
    
    answer.strip! unless answer.nil?

    # FIX move this to achoo.rb?
    unless $stdin.tty?
      puts answer
    end
    break unless a_little_something(answer)
  end
  answer
end

.bold(text) ⇒ Object



18
# File 'lib/achoo/term.rb', line 18

def self.bold(text);      effect(BOLD,      text); end

.choose(question, entries, special = nil, additional_valid_answers = []) ⇒ Object



54
55
56
57
# File 'lib/achoo/term.rb', line 54

def self.choose(question, entries, special=nil, additional_valid_answers=[])
  menu = Menu.new(question, entries, special, additional_valid_answers)
  menu.print_ask_and_validate
end

.effect(code, text) ⇒ Object



16
# File 'lib/achoo/term.rb', line 16

def self.effect(code, text); "\e[#{code}m#{text}\e[0m"; end

.fatal(text) ⇒ Object



21
# File 'lib/achoo/term.rb', line 21

def self.fatal(text);     effect(RED,       text); end

.passwordObject



23
24
25
26
27
28
# File 'lib/achoo/term.rb', line 23

def self.password
  `stty -echo`
  pas = ask('Password')
  `stty echo`
  pas
end

.underline(text) ⇒ Object



19
# File 'lib/achoo/term.rb', line 19

def self.underline(text); effect(UNDERLINE, text); end

.warn(text) ⇒ Object



20
# File 'lib/achoo/term.rb', line 20

def self.warn(text);      effect(YELLOW,    text); end