Class: Listener

Inherits:
Object
  • Object
show all
Defined in:
lib/linmeric/Listener.rb

Overview

This class works on the command line interface to allow the user to move the cursor, modify the input string in each point and retrieve commands from the history

Author

Massimiliano Dal Mas ([email protected])

License

Distributed under MIT license

Constant Summary collapse

CSI =
"\e["
LETTERS =
Tool.letters
NUMBERS =
"0123456789"
SPECIAL_CHARS =
"|!$%&/()=?'^~+-*<>-_,:;. " + '"'
CHARS =
LETTERS + LETTERS.upcase + SPECIAL_CHARS + NUMBERS

Instance Method Summary collapse

Constructor Details

#initializeListener

Creates the main variables



23
24
25
26
27
28
29
# File 'lib/linmeric/Listener.rb', line 23

def initialize
  @final_string = ""
  @i = 0
  @s = 0
  @exit = false
  @list = Archive.new
end

Instance Method Details

#collect_single_keyObject

according to the collected key decides what to do: moving the cursor, surfing the history, or simply store chars in a variable



57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
# File 'lib/linmeric/Listener.rb', line 57

def collect_single_key()
  c = read_char
  case c
    
    # "TAB"
    when "\t"        
    
    # "RETURN"
    when "\r"
      puts c
      @exit = true
    when "\n"
      # "LINE FEED"
    when "\e"
      # "ESCAPE"
  
    # "UP ARROW"
    when "\e[A"      	
      temp = @list.previous
      unless temp == ""
        clear_line
        @final_string = temp
        @i = @final_string.size
        print @final_string
      end
      
    # "DOWN ARROW"    
    when "\e[B" 
        clear_line
        @final_string = @list.next_
        @i = @final_string.size
        print @final_string

    # "RIGHT ARROW"
    when "\e[C"
      @list.top
      unless @i == @final_string.size
        $stdout.write "#{CSI}1C"
        @i += 1
      end 
    
    # "LEFT ARROW"  
    when "\e[D"
      @list.top
      unless @i == 0
        $stdout.write "#{CSI}1D" 
        @i -= 1
      end
   
    # Delete char 
    when "\177" 
      @list.top
      unless @i == 0
        $stdout.write "#{CSI}1D"
        for i in (@i)...@final_string.size do
          $stdout.write "#{@final_string[i]}"
        end
        $stdout.write " "
        @final_string = @final_string.remove(@i-1)
        for i in (@i-2)...@final_string.size do
          $stdout.write "#{CSI}1D"
        end
        @i -= 1
      end

    when "\004"
      # "DELETE"
    when "\e[3~"
      # "ALTERNATE DELETE"
    
    # "Ctrl + C"
    when "\u0003"        
#        puts
#        exit 0
    when /^.$/
      @list.top
      if CHARS.include? c
        if @i < @final_string.size then
          last_i = @i + 2
          @final_string.insert @i, c
          clear_line
          $stdout.write "#{@final_string}"
          cursor_to(last_i)
          @i = last_i - 1
        else
          print c
          @final_string += c
          @i += 1
        end
      elsif c == "\f" then
        @i = 0
        print "\e[H\e[2J"
        print "Linmeric-main> "
      end
  end
end

#getsObject

main function that can be used. It returns the stream the user wrote on the command line after they pressed return.



156
157
158
159
160
161
162
# File 'lib/linmeric/Listener.rb', line 156

def gets()
  collect_single_key while(!@exit)
  @list.store(@final_string)
  return @final_string.clone
ensure
  reset
end

#read_charObject

Reads a single char from the command line



39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
# File 'lib/linmeric/Listener.rb', line 39

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

#resetObject

reinitializates three main variables



32
33
34
35
36
# File 'lib/linmeric/Listener.rb', line 32

def reset
  @final_string = ""
  @i = 0
  @exit = false
end