Class: ScripTTY::Term::XTerm

Inherits:
Object
  • Object
show all
Defined in:
lib/scriptty/term/xterm.rb

Constant Summary collapse

PARSER_DEFINITION =
File.read(File.join(File.dirname(__FILE__), "xterm/xterm-escapes.txt"))
DEFAULT_FLAGS =
{
  :insert_mode => false,
  :wraparound_mode => false,
}.freeze

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(height = 24, width = 80) ⇒ XTerm

Returns a new instance of XTerm.



39
40
41
42
43
44
45
46
47
48
# File 'lib/scriptty/term/xterm.rb', line 39

def initialize(height=24, width=80)
  @parser_fsm = Util::FSM.new(:definition => PARSER_DEFINITION,
    :callback => self, :callback_method => :send)

  @height = height
  @width = width

  on_unknown_sequence :error
  reset_to_initial_state!
end

Instance Attribute Details

#heightObject (readonly)

width and height of the display buffer



37
38
39
# File 'lib/scriptty/term/xterm.rb', line 37

def height
  @height
end

#widthObject (readonly)

width and height of the display buffer



37
38
39
# File 'lib/scriptty/term/xterm.rb', line 37

def width
  @width
end

Instance Method Details

#cursor_posObject

Return the cursor position, as an array of [row, column].

0,0

represents the topmost, leftmost position.



130
131
132
# File 'lib/scriptty/term/xterm.rb', line 130

def cursor_pos
  [@cursor.row, @cursor.column]
end

#cursor_pos=(v) ⇒ Object

Set the cursor position to [row, column].

0,0

represents the topmost, leftmost position.



137
138
139
# File 'lib/scriptty/term/xterm.rb', line 137

def cursor_pos=(v)
  @cursor.pos = v
end

#feed_byte(byte) ⇒ Object

Feed the specified byte to the terminal. Returns a string of bytes that should be transmitted (e.g. for TELNET negotiation).

Raises:

  • (ArgumentError)


85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
# File 'lib/scriptty/term/xterm.rb', line 85

def feed_byte(byte)
  raise ArgumentError.new("input should be single byte") unless byte.is_a?(String) and byte.length == 1
  begin
    @parser_fsm.process(byte)
  rescue Util::FSM::NoMatch => e
    @parser_fsm.reset!
    if @on_unknown_sequence == :error
      raise
    elsif @on_unknown_sequence == :ignore
      # do nothing
    elsif !@on_unknown_sequence.is_a?(Symbol)   # @on_unknown_sequence is a Proc
      @on_unknown_sequence.call(e.input_sequence.join)
    else
      raise "BUG"
    end
  end
  ""
end

#feed_bytes(bytes) ⇒ Object

Convenience method: Feeds several bytes to the terminal. Returns a string of bytes that should be transmitted (e.g. for TELNET negotiation).



107
108
109
110
111
112
113
# File 'lib/scriptty/term/xterm.rb', line 107

def feed_bytes(bytes)
  retvals = []
  bytes.split(//n).each do |byte|
    retvals << feed_byte(byte)
  end
  retvals.join
end

#inspectObject

:nodoc:



78
79
80
81
# File 'lib/scriptty/term/xterm.rb', line 78

def inspect # :nodoc:
  # The default inspect method shows way too much information.  Simplify it.
  "#<#{self.class.name}:#{sprintf('0x%0x', object_id)} h=#{@height.inspect} w=#{@width.inspect} cursor=#{cursor_pos.inspect}>"
end

#on_unknown_sequence(mode = nil, &block) ⇒ Object

Set the behaviour of the terminal when an unknown escape sequence is found.

This method takes either a symbol or a block.

When a block is given, it is executed whenever an unknown escape sequence is received. The block is passed the escape sequence as a single string.

When a symbol is given, it may be one of the following:

:error

(default) Raise a ScripTTY::Util::FSM::NoMatch exception.

:ignore

Ignore the unknown escape sequence.



64
65
66
67
68
69
70
71
72
73
74
75
76
# File 'lib/scriptty/term/xterm.rb', line 64

def on_unknown_sequence(mode=nil, &block)
  if !block and !mode
    raise ArgumentError.new("No mode specified and no block given")
  elsif block and mode
    raise ArgumentError.new("Block and mode are mutually exclusive, but both were given")
  elsif block
    @on_unknown_sequence = block
  elsif [:error, :ignore].include?(mode)
    @on_unknown_sequence = mode
  else
    raise ArgumentError.new("Invalid mode #{mode.inspect}")
  end
end

#text(copy = true) ⇒ Object

Return an array of strings representing the lines of text on the screen

NOTE: If passing copy=false, do not modify the return value or the strings inside it.



119
120
121
122
123
124
125
# File 'lib/scriptty/term/xterm.rb', line 119

def text(copy=true)
  if copy
    @glyphs.content.map{|line| line.dup}
  else
    @glyphs.content
  end
end

#text=(a) ⇒ Object

Replace the text on the screen with the specified text.

NOTE: This is API is very likely to change in the future.



144
145
146
147
148
# File 'lib/scriptty/term/xterm.rb', line 144

def text=(a)
  @glyphs.clear!
  @glyphs.replace_at(0, 0, a)
  a
end