Class: Luck::Display

Inherits:
Object
  • Object
show all
Defined in:
lib/luck/display.rb

Constant Summary collapse

BUTTONS =
{
  0 => :left,
  1 => :middle,
  2 => :right,
  3 => :release,
  64 => :scrollup,
  65 => :scrolldown
}

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(client) ⇒ Display

Returns a new instance of Display.



5
6
7
8
9
10
11
# File 'lib/luck/display.rb', line 5

def initialize client
  @client = client
  @panes = {}
  @dirty = true
  
  @driver = ANSIDriver.new
end

Instance Attribute Details

#active_controlObject

Returns the value of attribute active_control.



3
4
5
# File 'lib/luck/display.rb', line 3

def active_control
  @active_control
end

#active_paneObject

Returns the value of attribute active_pane.



3
4
5
# File 'lib/luck/display.rb', line 3

def active_pane
  @active_pane
end

#clientObject

Returns the value of attribute client.



3
4
5
# File 'lib/luck/display.rb', line 3

def client
  @client
end

#dirtyObject

Returns the value of attribute dirty.



3
4
5
# File 'lib/luck/display.rb', line 3

def dirty
  @dirty
end

#driverObject

Returns the value of attribute driver.



3
4
5
# File 'lib/luck/display.rb', line 3

def driver
  @driver
end

Returns the value of attribute modal.



3
4
5
# File 'lib/luck/display.rb', line 3

def modal
  @modal
end

#panesObject

Returns the value of attribute panes.



3
4
5
# File 'lib/luck/display.rb', line 3

def panes
  @panes
end

Instance Method Details

#[](pane, control = nil) ⇒ Object



39
40
41
42
43
44
45
# File 'lib/luck/display.rb', line 39

def [] pane, control=nil
  if control
    @panes[pane].controls[control]
  else
    @panes[pane]
  end
end

#alert(name, *args, &blck) ⇒ Object



27
28
29
# File 'lib/luck/display.rb', line 27

def alert name, *args, &blck
  @panes[name] = Alert.new(self, *args, &blck)
end

#closeObject



19
20
21
# File 'lib/luck/display.rb', line 19

def close
  @driver.undo_modes
end

#color(c) ⇒ Object



17
# File 'lib/luck/display.rb', line 17

def color c; @driver.color c; end

#cycle_controlsObject



89
90
91
92
93
94
95
96
97
98
99
# File 'lib/luck/display.rb', line 89

def cycle_controls
  index = @active_pane.controls.keys.index @active_pane.controls.index(@active_control)
  begin
    index += 1
    index = 0 if index >= @active_pane.controls.size
  end until @active_pane.controls[@active_pane.controls.keys[index]].respond_to? :handle_char
  old = @active_control
  @active_control = @active_pane.controls[@active_pane.controls.keys[index]]
  old.redraw
  @active_control.redraw
end

#cycle_controls_backObject



101
102
103
104
105
106
107
108
109
110
111
# File 'lib/luck/display.rb', line 101

def cycle_controls_back
  index = @active_pane.controls.keys.index @active_pane.controls.key(@active_control)
  begin
    index -= 1
    index = @active_pane.controls.size - 1 if index < 0
  end until @active_pane.controls[@active_pane.controls.keys[index]].respond_to? :handle_char
  old = @active_control
  @active_control = @active_pane.controls[@active_pane.controls.keys[index]]
  old.redraw
  @active_control.redraw
end

#dirty!(pane = nil) ⇒ Object



31
32
33
34
35
36
37
# File 'lib/luck/display.rb', line 31

def dirty! pane=nil
  if pane
    @panes[pane].dirty!
  else
    @dirty = true
  end
end

#focus(*path) ⇒ Object



47
48
49
# File 'lib/luck/display.rb', line 47

def focus *path
  self.active_control = self[*path]
end

#handleObject



51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
# File 'lib/luck/display.rb', line 51

def handle
  handle_stdin
  
  if @driver.resized?
    dirty!
  end
  
  if @dirty
    redraw
    @dirty = false
  else
    panes = @panes.values.select {|pane| pane.dirty? && pane.visible? }
    redraw panes if panes.any?
  end
  
  @panes.each_value {|pane| pane.dirty = false }
end

#handle_stdinObject

alt-anything => e*KEY* (same as Esc, key) alt-[ would become e[ which is an ANSI escape

ctrl-stuff becomes weird stuff, i.e. ctrl-space = x00, ctrl-a = x01, ctrl-b = x02

super is not sent?



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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
# File 'lib/luck/display.rb', line 128

def handle_stdin
  @escapes ||= 0
  @ebuff ||= ''
  
  $stdin.read_nonblock(1024).each_char do |chr|
  
    if @escapes == 0
      if chr == "\e"
        @escapes = 1
      elsif chr == "\t"
        cycle_controls
      elsif chr == "\177"
        route_key :backspace
      else
        route_key chr
      end
      
    elsif @escapes == 1 && chr == '['
      @escapes = 2
    elsif @escapes == 1 && chr == 'O'
      @escapes = 5
      
    elsif @escapes == 2
      if chr == 'A'
        route_key :up
      elsif chr == 'B'
        route_key :down
      elsif chr == 'C'
        route_key :right
      elsif chr == 'D'
        route_key :left
      elsif chr == 'E'
        route_key :center
      elsif chr == 'Z'
        cycle_controls_back
      else
        @ebuff = chr
        @escapes = 3
      end
      @escapes = 0 if @escapes == 2
      
    elsif @escapes == 3
      if chr == '~' && @ebuff.to_i.to_s == @ebuff
        route_key case @ebuff.to_i
          when 2; :insert
          when 3; :delete
          when 5; :pageup
          when 6; :pagedown
          when 15; :f5
          when 17; :f6
          when 18; :f7
          when 19; :f8
          when 20; :f9
          when 24; :f12
          else; raise @ebuff.inspect
        end
      elsif @ebuff[0,1] == 'M' && @ebuff.size == 3
        @ebuff += chr
        info, x, y = @ebuff.unpack('xCCC').map{|i| i - 32}
        modifiers = []
        modifiers << :shift if info & 4 == 4
        modifiers << :meta if info & 8 == 8
        modifiers << :control if info & 16 == 16
        pane = pane_at(x, y)
        
        unless modal && modal != pane
          pane.handle_click BUTTONS[info & 71], modifiers, x, y if pane
        end
      elsif @ebuff.size > 10
        raise "long ebuff #{@ebuff.inspect} - #{chr.inspect}"
      else
        @ebuff += chr
        @escapes = 4
      end
      @escapes = 0 if @escapes == 3
      @escapes = 3 if @escapes == 4
      @ebuff = '' if @escapes == 0
      
    elsif @escapes == 5
      if chr == 'H'
        route_key :home
      elsif chr == 'F'
        route_key :end
      elsif chr == 'Q'
        route_key :f2
      elsif chr == 'R'
        route_key :f3
      elsif chr == 'S'
        route_key :f4
      else
        raise "escape 5 #{chr.inspect}"
      end
      @escapes = 0
      
    else
      @escapes = 0
    end
  end
  
  @driver.flush
  
rescue Errno::EAGAIN
rescue EOFError
end

#heightObject



14
# File 'lib/luck/display.rb', line 14

def height; @driver.height; end

#pane(name, *args, &blck) ⇒ Object



23
24
25
# File 'lib/luck/display.rb', line 23

def pane name, *args, &blck
  @panes[name] = Pane.new(self, *args, &blck)
end

#pane_at(x, y) ⇒ Object



233
234
235
236
237
238
# File 'lib/luck/display.rb', line 233

def pane_at x, y
  @panes.values.reverse.each do |pane|
    return pane if pane.visible? && (pane.x1..pane.x2).include?(x) && (pane.y1..pane.y2).include?(y)
  end
  nil
end

#place(x, y, t) ⇒ Object



16
# File 'lib/luck/display.rb', line 16

def place x,y,t; @driver.put x,y,t; end

#redraw(panes = nil) ⇒ Object



69
70
71
72
73
74
75
76
77
78
79
80
81
82
# File 'lib/luck/display.rb', line 69

def redraw panes=nil
  unless panes
    @driver.clear
    @driver.cursor_to_home
  end
  
  panes ||= @panes.values
  panes.each {|pane| pane.redraw if pane.visible? }
  modal.redraw if modal
  @panes.each_value {|pane| pane.draw_title if pane.visible? }
  
  @driver.restore_cursor
  @driver.flush
end

#route_key(chr) ⇒ Object



240
241
242
# File 'lib/luck/display.rb', line 240

def route_key chr
  @active_control.handle_char chr if @active_control
end

#widthObject



13
# File 'lib/luck/display.rb', line 13

def width; @driver.width; end