Class: ScripTTY::Net::Console

Inherits:
Object
  • Object
show all
Defined in:
lib/scriptty/net/console.rb

Constant Summary collapse

IAC_WILL_ECHO =
"\377\373\001"
IAC_WONT_ECHO =
"\377\374\001"
IAC_DO_ECHO =
"\377\375\001"
IAC_DONT_ECHO =
"\377\376\001"
IAC_WILL_SUPPRESS_GA =
"\377\373\003"
IAC_WONT_SUPPRESS_GA =
"\377\374\003"
IAC_DO_SUPPRESS_GA =
"\377\375\003"
IAC_DONT_SUPPRESS_GA =
"\377\376\003"

Instance Method Summary collapse

Constructor Details

#initialize(conn, app) ⇒ Console

Returns a new instance of Console.



14
15
16
17
18
19
20
21
22
23
24
25
# File 'lib/scriptty/net/console.rb', line 14

def initialize(conn, app)
  @conn = conn
  @app = app
  conn.on_receive_bytes { |bytes| handle_receive_bytes(bytes) }
  conn.on_close { |bytes| handle_close }
  conn.write(IAC_WILL_ECHO + IAC_DONT_ECHO)   # turn echoing off
  conn.write(IAC_WILL_SUPPRESS_GA + IAC_DO_SUPPRESS_GA)   # turn line-buffering off (RFC 858)
  conn.write("\ec") # reset terminal (clear screen; reset attributes)
  @refresh_in_progress = false
  @need_another_refresh = false
  @prompt_input = ""
end

Instance Method Details

#refresh!Object



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
67
68
69
# File 'lib/scriptty/net/console.rb', line 27

def refresh!
  if @refresh_in_progress
    @need_another_refresh = true
    return
  end
  screen_lines = []
  screen_lines << "# #{@prompt_input}"  # prompt
  if @app.term
    term_width = @app.term.width
    screen_lines << "Cursor position: #{@app.term.cursor_pos.inspect}"
    screen_lines << "+" + "-"*term_width + "+"
    @app.term.text.each do |line|
      screen_lines << "|#{line}|"
    end
    screen_lines << "+" + "-"*term_width + "+"
  else
    term_width = 80
    screen_lines << "[ No terminal ]"
  end
  if @app.respond_to?(:log_messages)
    screen_lines << ""
    @app.log_messages.each do |line|
      if line.length > term_width
        line = line[0,term_width-1]
        line += ">"
      end
      screen_lines << ":#{line}"
    end
  end
  output = []
  output << "\e[H"
  output << screen_lines.map{|line| line + "\e[K" + "\r\n"}.join   # erase to end of line after each line
  output << "\e[;#{3+@prompt_input.length}H" # return to prompt
  @refresh_in_progress = true
  @conn.write(output.join) {
    @refresh_in_progress = false
    if @need_another_refresh
      @need_another_refresh = false
      refresh!
    end
  }
  nil
end