Class: RawLine::Terminal
- Inherits:
-
Object
- Object
- RawLine::Terminal
- 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
Defined Under Namespace
Classes: CursorPosition
Instance Attribute Summary collapse
-
#escape_codes ⇒ Object
Returns the value of attribute escape_codes.
-
#escape_sequences ⇒ Object
readonly
Returns the value of attribute escape_sequences.
-
#input ⇒ Object
Returns the value of attribute input.
-
#keys ⇒ Object
readonly
Returns the value of attribute keys.
-
#output ⇒ Object
Returns the value of attribute output.
Instance Method Summary collapse
- #clear_screen ⇒ Object
- #clear_screen_down ⇒ Object
- #clear_to_beginning_of_line ⇒ Object
- #cooked! ⇒ Object
- #cursor_position ⇒ Object
- #height ⇒ Object
-
#initialize(input, output) ⇒ Terminal
constructor
Create an instance of RawLine::Terminal.
- #move_down_n_rows(n) ⇒ Object
- #move_left ⇒ Object
- #move_left_n_characters(n) ⇒ Object
- #move_right_n_characters(n) ⇒ Object
- #move_to_beginning_of_row ⇒ Object
- #move_to_column(n) ⇒ Object
- #move_to_column_and_row(column, row) ⇒ Object
- #move_up_n_rows(n) ⇒ Object
- #preserve_cursor(&blk) ⇒ Object
- #pseudo_cooked! ⇒ Object
- #puts(*args) ⇒ Object
- #raw! ⇒ Object
- #restore_tty_attrs ⇒ Object
- #snapshot_tty_attrs ⇒ Object
- #term_info ⇒ Object
-
#update ⇒ Object
Update the terminal escape sequences.
- #width ⇒ Object
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_codes ⇒ Object
Returns the value of attribute escape_codes.
29 30 31 |
# File 'lib/rawline/terminal.rb', line 29 def escape_codes @escape_codes end |
#escape_sequences ⇒ Object (readonly)
Returns the value of attribute escape_sequences.
30 31 32 |
# File 'lib/rawline/terminal.rb', line 30 def escape_sequences @escape_sequences end |
#input ⇒ Object
Returns the value of attribute input.
29 30 31 |
# File 'lib/rawline/terminal.rb', line 29 def input @input end |
#keys ⇒ Object (readonly)
Returns the value of attribute keys.
30 31 32 |
# File 'lib/rawline/terminal.rb', line 30 def keys @keys end |
#output ⇒ Object
Returns the value of attribute output.
29 30 31 |
# File 'lib/rawline/terminal.rb', line 29 def output @output end |
Instance Method Details
#clear_screen ⇒ Object
132 133 134 |
# File 'lib/rawline/terminal.rb', line 132 def clear_screen term_info.control "clear" end |
#clear_screen_down ⇒ Object
136 137 138 |
# File 'lib/rawline/terminal.rb', line 136 def clear_screen_down term_info.control "ed" end |
#clear_to_beginning_of_line ⇒ Object
128 129 130 |
# File 'lib/rawline/terminal.rb', line 128 def clear_to_beginning_of_line term_info.control "el1" end |
#cooked! ⇒ Object
86 87 88 |
# File 'lib/rawline/terminal.rb', line 86 def cooked! @input.cooked! end |
#cursor_position ⇒ Object
115 116 117 118 119 120 121 122 123 124 125 126 |
# File 'lib/rawline/terminal.rb', line 115 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 |
#height ⇒ Object
189 190 191 |
# File 'lib/rawline/terminal.rb', line 189 def height terminal_size[1] end |
#move_down_n_rows(n) ⇒ Object
168 169 170 |
# File 'lib/rawline/terminal.rb', line 168 def move_down_n_rows(n) n.times { term_info.control "cud1" } end |
#move_left ⇒ Object
144 145 146 |
# File 'lib/rawline/terminal.rb', line 144 def move_left move_left_n_characters 1 end |
#move_left_n_characters(n) ⇒ Object
148 149 150 |
# File 'lib/rawline/terminal.rb', line 148 def move_left_n_characters(n) n.times { term_info.control "cub1" } end |
#move_right_n_characters(n) ⇒ Object
152 153 154 |
# File 'lib/rawline/terminal.rb', line 152 def move_right_n_characters(n) n.times { term_info.control "cuf1" } end |
#move_to_beginning_of_row ⇒ Object
140 141 142 |
# File 'lib/rawline/terminal.rb', line 140 def move_to_beginning_of_row move_to_column 0 end |
#move_to_column(n) ⇒ Object
160 161 162 |
# File 'lib/rawline/terminal.rb', line 160 def move_to_column(n) term_info.control "hpa", n end |
#move_to_column_and_row(column, row) ⇒ Object
156 157 158 |
# File 'lib/rawline/terminal.rb', line 156 def move_to_column_and_row(column, row) term_info.control "cup", column, row end |
#move_up_n_rows(n) ⇒ Object
164 165 166 |
# File 'lib/rawline/terminal.rb', line 164 def move_up_n_rows(n) n.times { term_info.control "cuu1" } end |
#preserve_cursor(&blk) ⇒ Object
178 179 180 181 182 183 |
# File 'lib/rawline/terminal.rb', line 178 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
90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 |
# File 'lib/rawline/terminal.rb', line 90 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
172 173 174 175 176 |
# File 'lib/rawline/terminal.rb', line 172 def puts(*args) @output.cooked do @output.puts(*args) end end |
#raw! ⇒ Object
82 83 84 |
# File 'lib/rawline/terminal.rb', line 82 def raw! @input.raw! end |
#restore_tty_attrs ⇒ Object
111 112 113 |
# File 'lib/rawline/terminal.rb', line 111 def restore_tty_attrs Termios::tcsetattr(@input, Termios::TCSANOW, @snapshotted_tty_attrs.pop) end |
#snapshot_tty_attrs ⇒ Object
107 108 109 |
# File 'lib/rawline/terminal.rb', line 107 def snapshot_tty_attrs @snapshotted_tty_attrs << Termios.tcgetattr(@input) end |
#term_info ⇒ Object
210 211 212 |
# File 'lib/rawline/terminal.rb', line 210 def term_info @term_info ||= TermInfo.new(ENV["TERM"], $stdout) end |
#update ⇒ Object
Update the terminal escape sequences. This method is called automatically by RawLine::Editor#bind().
201 202 203 204 205 206 207 208 |
# File 'lib/rawline/terminal.rb', line 201 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 |
#width ⇒ Object
185 186 187 |
# File 'lib/rawline/terminal.rb', line 185 def width terminal_size[0] end |