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



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

def self.ask(question='')
  answer = nil
  loop do
    answer = Readline.readline(bold("#{question}> "), true)
    
    # 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



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

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

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



58
59
60
61
# File 'lib/achoo/term.rb', line 58

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

.clearscreenObject



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

def self.clearscreen
  print "\e[2J\e[H"
  $stdout.flush
end

.effect(code, text) ⇒ Object



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

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

.fatal(text) ⇒ Object



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

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

.passwordObject



29
30
31
32
33
34
# File 'lib/achoo/term.rb', line 29

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

.underline(text) ⇒ Object



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

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

.warn(text) ⇒ Object



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

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