Class: VER::Buffer::Terminal

Inherits:
Object
  • Object
show all
Defined in:
lib/ver/buffer/term.rb

Constant Summary collapse

ANSI_CODES =
[]
FG_COLORS =
[:black, :red, :green, :yellow, :blue, :magenta, :cyan, :white]
BG_COLORS =
[:on_black, :on_red, :on_green, :on_yellow, :on_blue, :on_magenta, :on_cyan, :on_white]

Instance Method Summary collapse

Constructor Details

#initialize(parent) ⇒ Terminal

Returns a new instance of Terminal.



6
7
8
9
10
# File 'lib/ver/buffer/term.rb', line 6

def initialize(parent)
  @parent = parent
  setup_widgets
  pty
end

Instance Method Details

#ansi2sym(ansi) ⇒ Object



242
243
244
# File 'lib/ver/buffer/term.rb', line 242

def ansi2sym(ansi)
  COLORS[ansi.to_i]
end

#bench(name, &block) ⇒ Object



12
13
14
15
# File 'lib/ver/buffer/term.rb', line 12

def bench(name, &block)
  require 'benchmark'
  p name => Benchmark.realtime(&block)
end

#color(fg, bg = nil) ⇒ Object



178
179
180
181
182
183
184
185
186
187
188
189
190
# File 'lib/ver/buffer/term.rb', line 178

def color(fg, bg = nil)
  if bg
    fg, bg = ANSI_CODES[fg.to_i], ANSI_CODES[bg.to_i]

    @tag = [:ansi, fg, bg].join('_')
    @text.tag_configure @tag, options_for(@tag, fg, bg)
  else
    fg = ANSI_CODES[fg.to_i]

    @tag = [:ansi, fg].join('_')
    @text.tag_configure @tag, options_for(@tag, fg)
  end
end

#delete(*indices) ⇒ Object



251
252
253
254
# File 'lib/ver/buffer/term.rb', line 251

def delete(*indices)
  # p delete: indices
  @text.delete(*indices)
end

#destroyObject



39
40
41
42
43
44
# File 'lib/ver/buffer/term.rb', line 39

def destroy
  @text.destroy
rescue Errno::EIO, PTY::ChildExited
ensure
  @parent.focus
end

#font_for(options) ⇒ Object



237
238
239
240
# File 'lib/ver/buffer/term.rb', line 237

def font_for(options)
  @fonts ||= {}
  @fonts[options] ||= Tk::Font.new(options)
end

#insert(index, chars) ⇒ Object



246
247
248
249
# File 'lib/ver/buffer/term.rb', line 246

def insert(index, chars)
  # p insert: [index, chars]
  @text.insert(index, chars, @tag)
end

#on_chunk(chunk) ⇒ Object

Trying to build a little dictionary of escape patterns:

| Sequence | Meaning | | e]0; | reset fg/bg | | e[?1034h | ??? | | e[01;34mbine | fg: 1, bg: 34 |



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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
# File 'lib/ver/buffer/term.rb', line 109

def on_chunk(chunk)
  @buffer ||= ''
  @buffer << chunk
  s = StringScanner.new(@buffer)

  until s.eos?
    pos = s.pos

    case s.peek(1)
    when "\e"
      if    s.scan(/\e\](\d+);/)
        color s[1]
      elsif s.scan(/\e\[(\d+)([A-Z])/)
        color s[1]
      elsif s.scan(/\e\[\?(\d+h)/)
        color s[1]
      elsif s.scan(/\e\[(\d+);(\d+)m/) # \e[01;34m
        color s[1], s[2]
      elsif s.scan(/\e\[(\d+)[m;]/)
        color s[1]
      elsif s.scan(/\e\[([A-Z])/)
        color s[1]
      elsif s.scan(/\e\[m/)
        # nothing?
      elsif s.scan(/\e=\r/)
        delete 'insert linestart', 'insert lineend'
      else
        p s.rest
      end
    when "\r"
      if s.scan(/\r\n/)
        insert :end, "\n"
      elsif s.scan(/\r\r/)
        delete 'insert linestart', 'insert lineend'
      elsif s.scan(/\r[^\n\r]/)
        p :no_rn => s.matched
      else
        p :r_fail => s.rest
      end
    when "\x07"
      s.pos += 1
      delete 'insert linestart', 'insert lineend'
    when "\x08"
      s.pos += 1
      delete 'insert - 1 chars'
    else
      if s.scan(/\A[^\e\r\x08\x07]+/)
        insert :end, s.matched
      end
    end
   #  p s.matched

    if s.pos == pos
      warn("Scanner stopped at: %p" % [s])
      @buffer = s.rest
      return
    end
  end

  @buffer = ''
  @text.see :end
rescue StringScanner::Error => ex
  VER.error(ex)
rescue => ex
  puts "#{ex.class}: #{ex}", *ex.backtrace
  @buffer = ''
  destroy
end

#options_for(tag, fg, bg = nil) ⇒ Object



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
232
233
234
235
# File 'lib/ver/buffer/term.rb', line 195

def options_for(tag, fg, bg = nil)

  if found = @option_cache[tag]
    return found
  end

  options = {}

  case fg
  when nil
  when :reset
    options[:font] = @font
    options[:background] = nil
    options[:foreground] = nil
  when :bold
    options[:font] = font_for(@font_actual.merge(weight: :bold))
  when *FG_COLORS
    options[:foreground] = fg
  when *BG_COLORS
    options[:background] = bg.to_s.sub(/on_/, '')
  else
    p fg: fg
  end

  case bg
  when nil
  when :reset
    options[:font] = @font
    options[:background] = nil
    options[:foreground] = nil
  when *FG_COLORS
    options[:foreground] = bg
  when *BG_COLORS
    options[:background] = bg.to_s.sub(/on_/, '')
  else
    p bg: bg
  end

  @option_cache[options] = options
  options
end

#ptyObject



46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
# File 'lib/ver/buffer/term.rb', line 46

def pty
  @pty_queue = queue = Queue.new

  Thread.new do
    shell = ENV['SHELL'] || 'bash'

    PTY.spawn(shell) do |r_pty, w_pty, pid|
      Thread.new do
        while chunk = queue.shift
          w_pty.print chunk
          w_pty.flush
        end
      end

      begin
        loop do
          c = r_pty.sysread(1 << 15)
          on_chunk(c) if c
        end
      rescue Errno::EIO, PTY::ChildExited
        destroy
      end
    end
  end
rescue Errno::EIO, PTY::ChildExited
  destroy
end

#setup_widgetsObject



17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
# File 'lib/ver/buffer/term.rb', line 17

def setup_widgets
  Tk.set_palette('black') # maybe use themes instead?

  @option_cache ||= {}
  @font ||= VER.options[:font]
  @font_actual ||= @font.actual_hash

  @text = Tk::Text.new(font: @font, insertofftime: 1)
  @text.pack expand: true, fill: :both
  @text.focus

  @text.bind('<Key>'){|event|
    @pty_queue << event.unicode
    Tk.callback_break
  }

  @text.bind('<Return>'){|event|
    @pty_queue << event.unicode
    # Tk.callback_break
  }
end