Class: Charming::Internal::Terminal::TTYBackend
- Inherits:
-
Object
- Object
- Charming::Internal::Terminal::TTYBackend
- Includes:
- Adapter
- Defined in:
- lib/charming/internal/terminal/tty_backend.rb
Constant Summary collapse
- ALT_SCREEN_ON =
"\e[?1049h"- ALT_SCREEN_OFF =
"\e[?1049l"- CTRL_KEY_PATTERN =
/\Actrl_(?<key>.+)\z/- MOUSE_SGR_PATTERN =
/\e\[<(\d+);(\d+);(\d+)([HmMhCc]?)(M|m)/- MOUSE_LEGACY_PATTERN =
/\e\[M(.{3})/- MOUSE_BUTTON_MAP =
{ 0 => :left, 1 => :middle, 2 => :right, 3 => :release, 64 => :scroll_up, 65 => :scroll_down, 66 => :scroll_up, 67 => :scroll_down }.freeze
Instance Method Summary collapse
- #clear ⇒ Object
- #disable_mouse_tracking ⇒ Object
- #enable_mouse_tracking ⇒ Object
- #enter_alt_screen ⇒ Object
- #hide_cursor ⇒ Object
-
#initialize(input: $stdin, output: $stdout, reader: nil, cursor: TTY::Cursor) ⇒ TTYBackend
constructor
A new instance of TTYBackend.
- #install_focus_handler ⇒ Object
- #install_resize_handler ⇒ Object
- #leave_alt_screen ⇒ Object
- #mouse_enabled? ⇒ Boolean
- #move_cursor(row, column) ⇒ Object
- #notify_resize ⇒ Object
- #read_event(timeout: nil) ⇒ Object
- #restore_focus_handler ⇒ Object
- #restore_resize_handler ⇒ Object
- #show_cursor ⇒ Object
- #size ⇒ Object
- #write_frame(frame) ⇒ Object
- #write_lines(line_changes) ⇒ Object
Constructor Details
#initialize(input: $stdin, output: $stdout, reader: nil, cursor: TTY::Cursor) ⇒ TTYBackend
Returns a new instance of TTYBackend.
24 25 26 27 28 29 30 31 32 |
# File 'lib/charming/internal/terminal/tty_backend.rb', line 24 def initialize(input: $stdin, output: $stdout, reader: nil, cursor: TTY::Cursor) @input = input @output = output @reader = reader || TTY::Reader.new(input: input, output: output) @cursor = cursor @resized = false @previous_winch_handler = nil @mouse_enabled = false end |
Instance Method Details
#clear ⇒ Object
119 120 121 |
# File 'lib/charming/internal/terminal/tty_backend.rb', line 119 def clear write_control(@cursor.clear_screen) end |
#disable_mouse_tracking ⇒ Object
76 77 78 79 80 81 82 83 84 |
# File 'lib/charming/internal/terminal/tty_backend.rb', line 76 def disable_mouse_tracking return unless @mouse_enabled write_control("\e[?1000l") write_control("\e[?1002l") write_control("\e[?1003l") write_control("\e[?1006l") @mouse_enabled = false end |
#enable_mouse_tracking ⇒ Object
67 68 69 70 71 72 73 74 |
# File 'lib/charming/internal/terminal/tty_backend.rb', line 67 def enable_mouse_tracking return if @mouse_enabled write_control("\e[?1000h") write_control("\e[?1002h") write_control("\e[?1006h") @mouse_enabled = true end |
#enter_alt_screen ⇒ Object
103 104 105 |
# File 'lib/charming/internal/terminal/tty_backend.rb', line 103 def enter_alt_screen write_control(ALT_SCREEN_ON) end |
#hide_cursor ⇒ Object
115 116 117 |
# File 'lib/charming/internal/terminal/tty_backend.rb', line 115 def hide_cursor write_control(@cursor.hide) end |
#install_focus_handler ⇒ Object
51 52 53 54 55 |
# File 'lib/charming/internal/terminal/tty_backend.rb', line 51 def install_focus_handler # Terminal focus change: some terminals send a special sequence # when focus changes. We use this to throttle rendering. @previous_focus_handler = Signal.trap("INFO") { @focused = true } end |
#install_resize_handler ⇒ Object
47 48 49 |
# File 'lib/charming/internal/terminal/tty_backend.rb', line 47 def install_resize_handler @previous_winch_handler = Signal.trap("WINCH") { @resized = true } end |
#leave_alt_screen ⇒ Object
107 108 109 |
# File 'lib/charming/internal/terminal/tty_backend.rb', line 107 def leave_alt_screen write_control(ALT_SCREEN_OFF) end |
#mouse_enabled? ⇒ Boolean
86 87 88 |
# File 'lib/charming/internal/terminal/tty_backend.rb', line 86 def mouse_enabled? @mouse_enabled end |
#move_cursor(row, column) ⇒ Object
123 124 125 |
# File 'lib/charming/internal/terminal/tty_backend.rb', line 123 def move_cursor(row, column) write_control(@cursor.move_to(column - 1, row - 1)) end |
#notify_resize ⇒ Object
90 91 92 |
# File 'lib/charming/internal/terminal/tty_backend.rb', line 90 def notify_resize @resized = true end |
#read_event(timeout: nil) ⇒ Object
34 35 36 37 38 39 40 41 42 43 44 45 |
# File 'lib/charming/internal/terminal/tty_backend.rb', line 34 def read_event(timeout: nil) return resize_event if resized? raw = @reader.read_keypress(echo: false, raw: true, nonblock: timeout) return nil unless raw return mouse_event(raw) if mouse_sequence?(raw) normalize_keypress(raw) rescue Errno::EAGAIN, IO::WaitReadable nil end |
#restore_focus_handler ⇒ Object
57 58 59 60 |
# File 'lib/charming/internal/terminal/tty_backend.rb', line 57 def restore_focus_handler Signal.trap("INFO", @previous_focus_handler) if @previous_focus_handler @previous_focus_handler = nil end |
#restore_resize_handler ⇒ Object
62 63 64 65 |
# File 'lib/charming/internal/terminal/tty_backend.rb', line 62 def restore_resize_handler Signal.trap("WINCH", @previous_winch_handler) if @previous_winch_handler @previous_winch_handler = nil end |
#show_cursor ⇒ Object
111 112 113 |
# File 'lib/charming/internal/terminal/tty_backend.rb', line 111 def show_cursor write_control(@cursor.show) end |
#size ⇒ Object
127 |
# File 'lib/charming/internal/terminal/tty_backend.rb', line 127 def size = [TTY::Screen.width, TTY::Screen.height] |
#write_frame(frame) ⇒ Object
94 95 96 97 |
# File 'lib/charming/internal/terminal/tty_backend.rb', line 94 def write_frame(frame) @output.write(frame) @output.flush end |
#write_lines(line_changes) ⇒ Object
99 100 101 |
# File 'lib/charming/internal/terminal/tty_backend.rb', line 99 def write_lines(line_changes, **) write_control(line_changes.map { |row, line| "\e[#{row};1H\e[2K#{line}" }.join) end |