Class: RawLine::Terminal

Inherits:
Object
  • Object
show all
Includes:
HighLine::SystemExtensions
Defined in:
lib/rawline/terminal.rb

Overview

The Terminal class defines character codes and code sequences which can be bound to actions by editors. An OS-dependent subclass of RawLine::Terminal is automatically instantiated by RawLine::Editor.

Direct Known Subclasses

VT220Terminal, WindowsTerminal

Defined Under Namespace

Classes: CursorPosition

Instance Attribute Summary collapse

Instance Method Summary collapse

Constructor Details

#initialize(input, output) ⇒ Terminal

Create an instance of RawLine::Terminal.



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
70
71
72
73
74
75
76
77
# File 'lib/rawline/terminal.rb', line 35

def initialize(input, output)
  @input = input
  @output = output
  @snapshotted_tty_attrs = []
  @keys =
    {
    :tab => [?\t.ord],
    :return => [?\r.ord],
    :newline => [?\n.ord],
    :escape => [?\e.ord],
    :space => [32],

    :ctrl_a => [?\C-a.ord],
    :ctrl_b => [?\C-b.ord],
    :ctrl_c => [?\C-c.ord],
    :ctrl_d => [?\C-d.ord],
    :ctrl_e => [?\C-e.ord],
    :ctrl_f => [?\C-f.ord],
    :ctrl_g => [?\C-g.ord],
    :ctrl_h => [?\C-h.ord],
    :ctrl_i => [?\C-i.ord],
    :ctrl_j => [?\C-j.ord],
    :ctrl_k => [?\C-k.ord],
    :ctrl_l => [?\C-l.ord],
    :ctrl_m => [?\C-m.ord],
    :ctrl_n => [?\C-n.ord],
    :ctrl_o => [?\C-o.ord],
    :ctrl_p => [?\C-p.ord],
    :ctrl_q => [?\C-q.ord],
    :ctrl_r => [?\C-r.ord],
    :ctrl_s => [?\C-s.ord],
    :ctrl_t => [?\C-t.ord],
    :ctrl_u => [?\C-u.ord],
    :ctrl_v => [?\C-v.ord],
    :ctrl_w => [?\C-w.ord],
    :ctrl_x => [?\C-x.ord],
    :ctrl_y => [?\C-y.ord],
    :ctrl_z => [?\C-z.ord]
    }
  @escape_codes = []
  @escape_sequences = []
  update
end

Instance Attribute Details

#escape_codesObject

Returns the value of attribute escape_codes.



29
30
31
# File 'lib/rawline/terminal.rb', line 29

def escape_codes
  @escape_codes
end

#escape_sequencesObject (readonly)

Returns the value of attribute escape_sequences.



30
31
32
# File 'lib/rawline/terminal.rb', line 30

def escape_sequences
  @escape_sequences
end

#inputObject

Returns the value of attribute input.



29
30
31
# File 'lib/rawline/terminal.rb', line 29

def input
  @input
end

#keysObject (readonly)

Returns the value of attribute keys.



30
31
32
# File 'lib/rawline/terminal.rb', line 30

def keys
  @keys
end

#outputObject

Returns the value of attribute output.



29
30
31
# File 'lib/rawline/terminal.rb', line 29

def output
  @output
end

Instance Method Details

#clear_screenObject



131
132
133
# File 'lib/rawline/terminal.rb', line 131

def clear_screen
  term_info.control "clear"
end

#clear_screen_downObject



135
136
137
# File 'lib/rawline/terminal.rb', line 135

def clear_screen_down
  term_info.control "ed"
end

#clear_to_beginning_of_lineObject



127
128
129
# File 'lib/rawline/terminal.rb', line 127

def clear_to_beginning_of_line
  term_info.control "el1"
end

#cooked!Object



85
86
87
# File 'lib/rawline/terminal.rb', line 85

def cooked!
  @input.cooked!
end

#cursor_positionObject



114
115
116
117
118
119
120
121
122
123
124
125
# File 'lib/rawline/terminal.rb', line 114

def cursor_position
  res = ''
  $stdin.raw do |stdin|
    $stdout << "\e[6n"
    $stdout.flush
    while (c = stdin.getc) != 'R'
      res << c if c
    end
  end
  m = res.match /(?<row>\d+);(?<column>\d+)/
  CursorPosition.new(Integer(m[:column]), Integer(m[:row]))
end

#heightObject



188
189
190
# File 'lib/rawline/terminal.rb', line 188

def height
  terminal_size[1]
end

#move_down_n_rows(n) ⇒ Object



167
168
169
# File 'lib/rawline/terminal.rb', line 167

def move_down_n_rows(n)
  n.times { term_info.control "cud1" }
end

#move_leftObject



143
144
145
# File 'lib/rawline/terminal.rb', line 143

def move_left
  move_left_n_characters 1
end

#move_left_n_characters(n) ⇒ Object



147
148
149
# File 'lib/rawline/terminal.rb', line 147

def move_left_n_characters(n)
  n.times { term_info.control "cub1" }
end

#move_right_n_characters(n) ⇒ Object



151
152
153
# File 'lib/rawline/terminal.rb', line 151

def move_right_n_characters(n)
  n.times { term_info.control "cuf1" }
end

#move_to_beginning_of_rowObject



139
140
141
# File 'lib/rawline/terminal.rb', line 139

def move_to_beginning_of_row
  move_to_column 0
end

#move_to_column(n) ⇒ Object



159
160
161
# File 'lib/rawline/terminal.rb', line 159

def move_to_column(n)
  term_info.control "hpa", n
end

#move_to_column_and_row(column, row) ⇒ Object



155
156
157
# File 'lib/rawline/terminal.rb', line 155

def move_to_column_and_row(column, row)
  term_info.control "cup", column, row
end

#move_up_n_rows(n) ⇒ Object



163
164
165
# File 'lib/rawline/terminal.rb', line 163

def move_up_n_rows(n)
  n.times { term_info.control "cuu1" }
end

#preserve_cursor(&blk) ⇒ Object



177
178
179
180
181
182
# File 'lib/rawline/terminal.rb', line 177

def preserve_cursor(&blk)
  term_info.control "sc" # store cursor position
  blk.call
ensure
  term_info.control "rc" # restore cursor position
end

#pseudo_cooked!Object



89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
# File 'lib/rawline/terminal.rb', line 89

def pseudo_cooked!
  old_tty_attrs = Termios.tcgetattr(@input)
  new_tty_attrs = old_tty_attrs.dup


  new_tty_attrs.cflag |= Termios::BRKINT | Termios::ISTRIP | Termios::ICRNL | Termios::IXON

  new_tty_attrs.iflag |= Termios::ICRNL | Termios::IGNBRK

  new_tty_attrs.oflag |= Termios::OPOST

  new_tty_attrs.lflag &= ~Termios::ECHONL
  new_tty_attrs.lflag |= Termios::ECHO | Termios::ECHOE | Termios::ECHOK | Termios::ICANON | Termios::ISIG | Termios::IEXTEN

  Termios::tcsetattr(@input, Termios::TCSANOW, new_tty_attrs)
end

#puts(*args) ⇒ Object



171
172
173
174
175
# File 'lib/rawline/terminal.rb', line 171

def puts(*args)
  @output.cooked do
    @output.puts(*args)
  end
end

#raw!Object



81
82
83
# File 'lib/rawline/terminal.rb', line 81

def raw!
  @input.raw!
end

#restore_tty_attrsObject



110
111
112
# File 'lib/rawline/terminal.rb', line 110

def restore_tty_attrs
  Termios::tcsetattr(@input, Termios::TCSANOW, @snapshotted_tty_attrs.pop)
end

#snapshot_tty_attrsObject



106
107
108
# File 'lib/rawline/terminal.rb', line 106

def snapshot_tty_attrs
  @snapshotted_tty_attrs << Termios.tcgetattr(@input)
end

#term_infoObject



209
210
211
# File 'lib/rawline/terminal.rb', line 209

def term_info
  @term_info ||= TermInfo.new(ENV["TERM"], $stdout)
end

#updateObject

Update the terminal escape sequences. This method is called automatically by RawLine::Editor#bind().



200
201
202
203
204
205
206
207
# File 'lib/rawline/terminal.rb', line 200

def update
  @keys.each_value do |k|
    l = k.length
    if  l > 1 then
      @escape_sequences << k unless @escape_sequences.include? k
    end
  end
end

#widthObject



184
185
186
# File 'lib/rawline/terminal.rb', line 184

def width
  terminal_size[0]
end