Class: Interface

Inherits:
Object
  • Object
show all
Defined in:
lib/live_sql/interface.rb

Overview

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

Instance Attribute Summary collapse

Instance Method Summary collapse

Instance Attribute Details

#stringObject

Returns the value of attribute string.



6
7
8
# File 'lib/live_sql/interface.rb', line 6

def string
  @string
end

Instance Method Details

#get_keyboard_inputObject

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



26
27
28
29
30
31
32
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
# File 'lib/live_sql/interface.rb', line 26

def get_keyboard_input
  c = read_char

  case c
  # when " "
  #   "SPACE"
  # when "\t"
  #   :query
  when "\r"
    :error
  # when "\n"
  #   "LINE FEED"
  when "\e"
    :abort
  # when "\e[A"
  #   "UP ARROW"
  #   :error
  # when "\e[B"
  #   "DOWN ARROW"
  #   :error
  when "\e[C"
    "RIGHT ARROW"
    :right
  when "\e[D"
    "LEFT ARROW"
    :left
  when "\177"
    :backspace
  # when "\004"
  #   "DELETE"
  # when "\e[3~"
  #   "ALTERNATE DELETE"
  # when "\u0003"
  #   "CONTROL-C"
  #   exit 0
  when /^.$/
    c
  else
    :error
  end
end

#read_charObject



8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# File 'lib/live_sql/interface.rb', line 8

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