Module: DialogTui::UserAction

Included in:
Dialog
Defined in:
lib/dialog_tui/user_action.rb,
lib/dialog_tui/user_action/action.rb,
lib/dialog_tui/user_action/read_char.rb

Defined Under Namespace

Classes: Action, UserActions

Class Method Summary collapse

Class Method Details

.read_charObject

Reads keypresses from the user including 2 and 3 escape character sequences.



15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
# File 'lib/dialog_tui/user_action/read_char.rb', line 15

def read_char
  STDIN.echo = false
  STDIN.raw!
 
  input = STDIN.getc.chr
  if input == "\e" then
    input << STDIN.read_nonblock(3) rescue nil
    input << STDIN.read_nonblock(2) rescue nil
  end
ensure
  STDIN.echo = true
  STDIN.cooked!
 
  return input
end

.show_single_keyObject

oringal case statement from: www.alecjacobson.com/weblog/?p=75



33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
# File 'lib/dialog_tui/user_action/read_char.rb', line 33

def show_single_key
  c = read_char
 
  case c
  when " "
    puts "SPACE"
  when "\t"
    puts "TAB"
  when "\r"
    puts "RETURN"
  when "\n"
    puts "LINE FEED"
  when "\e"
    puts "ESCAPE"
  when "\e[A"
    puts "UP ARROW"
  when "\e[B"
    puts "DOWN ARROW"
  when "\e[C"
    puts "RIGHT ARROW"
  when "\e[D"
    puts "LEFT ARROW"
  when "\177"
    puts "BACKSPACE"
  when "\004"
    puts "DELETE"
  when "\e[3~"
    puts "ALTERNATE DELETE"
  when "\u0003"
    puts "CONTROL-C"
    exit 0
  when /^.$/
    puts "SINGLE CHAR HIT: #{c.inspect}"
  else
    puts "SOMETHING ELSE: #{c.inspect}"
  end
end

.user_action(&block) ⇒ Object



12
13
14
# File 'lib/dialog_tui/user_action.rb', line 12

def user_action &block
  UserActions.run &block
end