Class: Rexec::Terminal

Inherits:
Object
  • Object
show all
Defined in:
lib/rexec/terminal.rb

Overview

WebSocket terminal connection.

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(ws) ⇒ Terminal

Returns a new instance of Terminal.



10
11
12
13
14
15
16
17
18
# File 'lib/rexec/terminal.rb', line 10

def initialize(ws)
  @ws = ws
  @closed = false
  @data_handlers = []
  @close_handlers = []
  @error_handlers = []

  setup_handlers
end

Instance Attribute Details

#closedObject (readonly) Also known as: closed?

Returns the value of attribute closed.



8
9
10
# File 'lib/rexec/terminal.rb', line 8

def closed
  @closed
end

Instance Method Details

#closeObject

Close the terminal connection.



62
63
64
65
66
67
# File 'lib/rexec/terminal.rb', line 62

def close
  return if @closed

  @closed = true
  @ws.close
end

#on_close { ... } ⇒ Object

Register a handler for connection close.

Yields:

  • Block called when connection closes



50
51
52
# File 'lib/rexec/terminal.rb', line 50

def on_close(&block)
  @close_handlers << block
end

#on_data {|data| ... } ⇒ Object

Register a handler for incoming data.

Yields:

  • (data)

    Block called with each chunk of data



43
44
45
# File 'lib/rexec/terminal.rb', line 43

def on_data(&block)
  @data_handlers << block
end

#on_error {|error| ... } ⇒ Object

Register a handler for errors.

Yields:

  • (error)

    Block called with error



57
58
59
# File 'lib/rexec/terminal.rb', line 57

def on_error(&block)
  @error_handlers << block
end

#resize(cols, rows) ⇒ Object

Resize the terminal.

Parameters:

  • cols (Integer)

    Number of columns

  • rows (Integer)

    Number of rows

Raises:



33
34
35
36
37
38
# File 'lib/rexec/terminal.rb', line 33

def resize(cols, rows)
  raise TerminalClosedError if @closed

  msg = { type: "resize", cols: cols, rows: rows }.to_json
  @ws.send(msg)
end

#write(data) ⇒ Object

Send data to the terminal.

Parameters:

  • data (String)

    Data to send

Raises:



23
24
25
26
27
# File 'lib/rexec/terminal.rb', line 23

def write(data)
  raise TerminalClosedError if @closed

  @ws.send(data)
end