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
78
# File 'lib/rawline/terminal.rb', line 35

def initialize(input, output)
  @input = input
  @output = output
  @snapshotted_tty_attrs = []
  @keys =
    {
    :tab => [?\t.ord],
    :shift_tab => [27, 91, 90],
    :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



141
142
143
# File 'lib/rawline/terminal.rb', line 141

def clear_screen
  term_info.control "clear"
end

#clear_screen_downObject



145
146
147
# File 'lib/rawline/terminal.rb', line 145

def clear_screen_down
  term_info.control "ed"
end

#clear_to_beginning_of_lineObject



133
134
135
# File 'lib/rawline/terminal.rb', line 133

def clear_to_beginning_of_line
  term_info.control "el1"
end

#clear_to_end_of_lineObject



137
138
139
# File 'lib/rawline/terminal.rb', line 137

def clear_to_end_of_line
  term_info.control "el"
end

#cooked!Object



87
88
89
90
# File 'lib/rawline/terminal.rb', line 87

def cooked!
  return unless @input.isatty
  @input.cooked!
end

#cursor_positionObject



120
121
122
123
124
125
126
127
128
129
130
131
# File 'lib/rawline/terminal.rb', line 120

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



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

def height
  terminal_size[1]
end

#hide_cursorObject



149
150
151
# File 'lib/rawline/terminal.rb', line 149

def hide_cursor
  term_info.control_string "civis"
end

#move_down_n_rows(n) ⇒ Object



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

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

#move_leftObject



161
162
163
# File 'lib/rawline/terminal.rb', line 161

def move_left
  move_left_n_characters 1
end

#move_left_n_characters(n) ⇒ Object



165
166
167
# File 'lib/rawline/terminal.rb', line 165

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

#move_right_n_characters(n) ⇒ Object



169
170
171
# File 'lib/rawline/terminal.rb', line 169

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

#move_to_beginning_of_rowObject



157
158
159
# File 'lib/rawline/terminal.rb', line 157

def move_to_beginning_of_row
  move_to_column 0
end

#move_to_column(n) ⇒ Object



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

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

#move_to_column_and_row(column, row) ⇒ Object



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

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

#move_up_n_rows(n) ⇒ Object



181
182
183
# File 'lib/rawline/terminal.rb', line 181

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

#preserve_cursor(&blk) ⇒ Object



197
198
199
200
201
202
# File 'lib/rawline/terminal.rb', line 197

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



92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
# File 'lib/rawline/terminal.rb', line 92

def pseudo_cooked!
  return unless @input.isatty

  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



189
190
191
192
193
194
195
# File 'lib/rawline/terminal.rb', line 189

def puts(*args)
  if @output.isatty
    @output.cooked { @output.puts(*args) }
  else
    @output.puts(*args)
  end
end

#raw!Object



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

def raw!
  return unless @input.isatty
  @input.raw!
end

#restore_tty_attrsObject



115
116
117
118
# File 'lib/rawline/terminal.rb', line 115

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

#show_cursorObject



153
154
155
# File 'lib/rawline/terminal.rb', line 153

def show_cursor
  term_info.control_string "cnorm"
end

#snapshot_tty_attrsObject



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

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

#term_infoObject



229
230
231
# File 'lib/rawline/terminal.rb', line 229

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().



220
221
222
223
224
225
226
227
# File 'lib/rawline/terminal.rb', line 220

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



204
205
206
# File 'lib/rawline/terminal.rb', line 204

def width
  terminal_size[0]
end