Class: VER::View::Terminal

Inherits:
Object
  • Object
show all
Defined in:
lib/ver/view/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/view/term.rb', line 6

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

Instance Method Details

#ansi2sym(ansi) ⇒ Object



236
237
238
# File 'lib/ver/view/term.rb', line 236

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

#bench(name, &block) ⇒ Object



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

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

#color(fg, bg = nil) ⇒ Object



172
173
174
175
176
177
178
179
180
181
182
183
184
# File 'lib/ver/view/term.rb', line 172

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



245
246
247
248
# File 'lib/ver/view/term.rb', line 245

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

#destroyObject



39
40
41
42
# File 'lib/ver/view/term.rb', line 39

def destroy
  @text.destroy
  @parent.focus
end

#font_for(options) ⇒ Object



231
232
233
234
# File 'lib/ver/view/term.rb', line 231

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

#insert(index, chars) ⇒ Object



240
241
242
243
# File 'lib/ver/view/term.rb', line 240

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 sequences:

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



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
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
# File 'lib/ver/view/term.rb', line 103

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



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
# File 'lib/ver/view/term.rb', line 189

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



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/ver/view/term.rb', line 44

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

      loop do
        c = r_pty.sysread(1 << 15)
        on_chunk(c) if c
      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/view/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