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"- AUTO_WRAP_OFF =
"\e[?7l"- AUTO_WRAP_ON =
"\e[?7h"- 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.
26 27 28 29 30 31 32 33 34 |
# File 'lib/charming/internal/terminal/tty_backend.rb', line 26 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
124 125 126 |
# File 'lib/charming/internal/terminal/tty_backend.rb', line 124 def clear write_control(@cursor.clear_screen) end |
#disable_mouse_tracking ⇒ Object
78 79 80 81 82 83 84 85 86 |
# File 'lib/charming/internal/terminal/tty_backend.rb', line 78 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
69 70 71 72 73 74 75 76 |
# File 'lib/charming/internal/terminal/tty_backend.rb', line 69 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
108 109 110 |
# File 'lib/charming/internal/terminal/tty_backend.rb', line 108 def enter_alt_screen write_control(ALT_SCREEN_ON) end |
#hide_cursor ⇒ Object
120 121 122 |
# File 'lib/charming/internal/terminal/tty_backend.rb', line 120 def hide_cursor write_control(@cursor.hide) end |
#install_focus_handler ⇒ Object
53 54 55 56 57 |
# File 'lib/charming/internal/terminal/tty_backend.rb', line 53 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
49 50 51 |
# File 'lib/charming/internal/terminal/tty_backend.rb', line 49 def install_resize_handler @previous_winch_handler = Signal.trap("WINCH") { @resized = true } end |
#leave_alt_screen ⇒ Object
112 113 114 |
# File 'lib/charming/internal/terminal/tty_backend.rb', line 112 def leave_alt_screen write_control(ALT_SCREEN_OFF) end |
#mouse_enabled? ⇒ Boolean
88 89 90 |
# File 'lib/charming/internal/terminal/tty_backend.rb', line 88 def mouse_enabled? @mouse_enabled end |
#move_cursor(row, column) ⇒ Object
128 129 130 |
# File 'lib/charming/internal/terminal/tty_backend.rb', line 128 def move_cursor(row, column) write_control(@cursor.move_to(column - 1, row - 1)) end |
#notify_resize ⇒ Object
92 93 94 |
# File 'lib/charming/internal/terminal/tty_backend.rb', line 92 def notify_resize @resized = true end |
#read_event(timeout: nil) ⇒ Object
36 37 38 39 40 41 42 43 44 45 46 47 |
# File 'lib/charming/internal/terminal/tty_backend.rb', line 36 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
59 60 61 62 |
# File 'lib/charming/internal/terminal/tty_backend.rb', line 59 def restore_focus_handler Signal.trap("INFO", @previous_focus_handler) if @previous_focus_handler @previous_focus_handler = nil end |
#restore_resize_handler ⇒ Object
64 65 66 67 |
# File 'lib/charming/internal/terminal/tty_backend.rb', line 64 def restore_resize_handler Signal.trap("WINCH", @previous_winch_handler) if @previous_winch_handler @previous_winch_handler = nil end |
#show_cursor ⇒ Object
116 117 118 |
# File 'lib/charming/internal/terminal/tty_backend.rb', line 116 def show_cursor write_control(@cursor.show) end |
#size ⇒ Object
132 |
# File 'lib/charming/internal/terminal/tty_backend.rb', line 132 def size = [TTY::Screen.width, TTY::Screen.height] |
#write_frame(frame) ⇒ Object
96 97 98 99 100 |
# File 'lib/charming/internal/terminal/tty_backend.rb', line 96 def write_frame(frame) without_auto_wrap do write_positioned_lines(frame.to_s.lines(chomp: true)) end end |
#write_lines(line_changes) ⇒ Object
102 103 104 105 106 |
# File 'lib/charming/internal/terminal/tty_backend.rb', line 102 def write_lines(line_changes, **) without_auto_wrap do write_control(line_changes.map { |row, line| "\e[#{row};1H\e[2K#{line}" }.join) end end |