hostty 0.1.0.alpha

This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
Files changed (56) hide show
  1. checksums.yaml +7 -0
  2. data/.hostty.yml +18 -0
  3. data/.ruby-version +1 -0
  4. data/LICENSE.txt +21 -0
  5. data/README.md +65 -0
  6. data/Rakefile +70 -0
  7. data/THIRD_PARTY_NOTICES.txt +247 -0
  8. data/docs/assets/demo.svg +168 -0
  9. data/docs/assets/hostty-dark-static.svg +27 -0
  10. data/docs/assets/hostty-dark.svg +92 -0
  11. data/docs/assets/hostty-light-static.svg +27 -0
  12. data/docs/assets/hostty-light.svg +92 -0
  13. data/docs/development/README.md +120 -0
  14. data/docs/development/architecture.md +169 -0
  15. data/docs/features/README.md +45 -0
  16. data/docs/features/command-line.md +46 -0
  17. data/docs/features/configuration.md +131 -0
  18. data/docs/features/domains.md +81 -0
  19. data/docs/features/plain-output.md +56 -0
  20. data/docs/features/stopping.md +69 -0
  21. data/docs/features/tabs.md +120 -0
  22. data/docs/features/terminal-emulator.md +66 -0
  23. data/exe/hostty +6 -0
  24. data/lib/hostty/cli.rb +86 -0
  25. data/lib/hostty/config.rb +78 -0
  26. data/lib/hostty/domains.rb +22 -0
  27. data/lib/hostty/exceptions.rb +7 -0
  28. data/lib/hostty/ghostty/frame.rb +203 -0
  29. data/lib/hostty/ghostty/library.rb +148 -0
  30. data/lib/hostty/ghostty/mouse.rb +77 -0
  31. data/lib/hostty/ghostty/screen.rb +112 -0
  32. data/lib/hostty/manager/child.rb +36 -0
  33. data/lib/hostty/manager/events.rb +27 -0
  34. data/lib/hostty/manager/process_exited.rb +28 -0
  35. data/lib/hostty/manager/signal_received.rb +23 -0
  36. data/lib/hostty/manager.rb +102 -0
  37. data/lib/hostty/output.rb +37 -0
  38. data/lib/hostty/plain/app.rb +23 -0
  39. data/lib/hostty/plain/pipe.rb +55 -0
  40. data/lib/hostty/port_chooser.rb +62 -0
  41. data/lib/hostty/process_config.rb +80 -0
  42. data/lib/hostty/terminal/cell.rb +35 -0
  43. data/lib/hostty/terminal/style.rb +87 -0
  44. data/lib/hostty/tui/app.rb +277 -0
  45. data/lib/hostty/tui/desktop.rb +51 -0
  46. data/lib/hostty/tui/input.rb +69 -0
  47. data/lib/hostty/tui/layout.rb +68 -0
  48. data/lib/hostty/tui/mouse_event.rb +36 -0
  49. data/lib/hostty/tui/pane.rb +125 -0
  50. data/lib/hostty/tui/pty.rb +62 -0
  51. data/lib/hostty/tui/renderer.rb +85 -0
  52. data/lib/hostty/tui/selection.rb +47 -0
  53. data/lib/hostty/version.rb +5 -0
  54. data/lib/hostty.rb +35 -0
  55. data/sig/hostty.rbs +4 -0
  56. metadata +129 -0
@@ -0,0 +1,277 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hostty
4
+ module Tui
5
+ class App
6
+ def initialize(stdin:, stdout:, desktop:)
7
+ @stdin = stdin
8
+ @stdout = stdout
9
+ @desktop = desktop
10
+ @rows, @cols = stdout.winsize
11
+ @panes = []
12
+ @selected = 0
13
+ @input = Input.new
14
+ @renderer = Renderer.new(io: stdout)
15
+ @outer_modes = {}
16
+ end
17
+
18
+ def launch_for(process)
19
+ Pane.new(name: process.name, url: process.url, rows: rows - 2, cols:).tap { |pane| panes << pane }
20
+ end
21
+
22
+ def wait(manager)
23
+ with_terminal do
24
+ until @exit_code
25
+ wait_for_io(manager)
26
+ autoscroll_when_due
27
+ check_size
28
+ render_when_due
29
+ end
30
+ end
31
+
32
+ @exit_code
33
+ end
34
+
35
+ private
36
+
37
+ FRAME_SECONDS = 0.016
38
+ IDLE_SECONDS = 0.25
39
+ DOUBLE_PRESS_SECONDS = 0.5
40
+ SCROLL_LINES = 3
41
+ AUTOSCROLL_SECONDS = 0.025
42
+ AUTOSCROLL_SPEEDUP_SECONDS = 0.5
43
+ AUTOSCROLL_MAX_LINES = 10
44
+ ENTER = "\e[?1049h\e[?1002h\e[?1006h\e[?25l\e[2J"
45
+ LEAVE = "\e[?1006l\e[?1002l\e[?2004l\e[?1l\e[0m\e[?25h\e[?1049l"
46
+ OUTER_MODES = {application_cursor_keys?: 1, bracketed_paste?: 2004}.freeze
47
+ private_constant :FRAME_SECONDS, :IDLE_SECONDS, :DOUBLE_PRESS_SECONDS, :SCROLL_LINES, :AUTOSCROLL_SECONDS, :AUTOSCROLL_SPEEDUP_SECONDS, :AUTOSCROLL_MAX_LINES, :ENTER, :LEAVE, :OUTER_MODES
48
+
49
+ attr_reader :stdin, :stdout, :desktop, :rows, :cols, :panes, :input, :renderer
50
+
51
+ def selected = panes[@selected]
52
+
53
+ def layout
54
+ @layout ||= Layout.new(names: panes.map(&:name), rows:, cols:)
55
+ end
56
+
57
+ def with_terminal
58
+ stdin.raw do
59
+ stdout.write(ENTER)
60
+ @dirty = true
61
+ yield
62
+ ensure
63
+ stdout.write(LEAVE)
64
+ end
65
+ end
66
+
67
+ def wait_for_io(manager)
68
+ readable, = IO.select([stdin, manager.wakeup_io, *panes.filter_map(&:io)], nil, nil, select_timeout)
69
+ Array(readable).each { |io| handle(io, manager) }
70
+ end
71
+
72
+ def handle(io, manager)
73
+ if io == stdin
74
+ read_input(manager)
75
+ elsif io == manager.wakeup_io
76
+ read_events(manager)
77
+ else
78
+ read_output(panes.find { |pane| pane.io == io })
79
+ end
80
+ end
81
+
82
+ def read_input(manager)
83
+ data = stdin.read_nonblock(4096, exception: false)
84
+ return if data == :wait_readable
85
+ return quit unless data
86
+
87
+ input.parse(data).each { |event| apply(event, manager) }
88
+ @dirty = true
89
+ end
90
+
91
+ def apply(event, manager)
92
+ case event
93
+ in [:interrupt, bytes]
94
+ interrupt(bytes)
95
+ in [:next_tab]
96
+ @selected = (@selected + 1) % panes.size
97
+ in [:keys, bytes]
98
+ selected.write(bytes)
99
+ in [:mouse, mouse]
100
+ handle_mouse(mouse, manager)
101
+ end
102
+ end
103
+
104
+ def handle_mouse(mouse, manager)
105
+ return selected.send_mouse(mouse.at(row: mouse.row - 1)) if program_mouse?(mouse)
106
+ return wheel(mouse) if mouse.wheel?
107
+ return unless mouse.button == :left
108
+
109
+ case mouse.action
110
+ when :press
111
+ click(mouse.row, mouse.col, manager)
112
+ when :motion
113
+ drag(mouse) if @selecting
114
+ when :release
115
+ finish_selection(mouse.row, mouse.col) if @selecting
116
+ end
117
+ end
118
+
119
+ def program_mouse?(mouse)
120
+ !@selecting && !mouse.shift? && pane_row?(mouse.row) && selected.screen.mouse_tracking?
121
+ end
122
+
123
+ def pane_row?(row) = (1..layout.pane_rows).cover?(row)
124
+
125
+ def drag(mouse)
126
+ @pointer = pane_point(mouse.row, mouse.col)
127
+ direction = autoscroll_direction(mouse.row)
128
+ @autoscroll_started_at = clock unless direction == @autoscroll
129
+ @autoscroll = direction
130
+ selected.select_to(**@pointer)
131
+ end
132
+
133
+ def autoscroll_direction(row)
134
+ if row < 1
135
+ 1
136
+ elsif row > layout.pane_rows
137
+ -1
138
+ end
139
+ end
140
+
141
+ def autoscroll_when_due
142
+ return unless @selecting && @autoscroll && clock - @autoscrolled_at.to_f >= AUTOSCROLL_SECONDS
143
+
144
+ @autoscrolled_at = clock
145
+ selected.scroll(@autoscroll * autoscroll_lines)
146
+ selected.select_to(**@pointer)
147
+ @dirty = true
148
+ end
149
+
150
+ def autoscroll_lines
151
+ (1 + ((clock - @autoscroll_started_at) / AUTOSCROLL_SPEEDUP_SECONDS).floor).clamp(1, AUTOSCROLL_MAX_LINES)
152
+ end
153
+
154
+ def wheel(mouse)
155
+ up = mouse.button == :wheel_up
156
+ return selected.write(arrow_key(up) * SCROLL_LINES) if selected.screen.alternate_screen?
157
+
158
+ selected.scroll(up ? SCROLL_LINES : -SCROLL_LINES)
159
+ selected.select_to(**@pointer) if @selecting && @pointer
160
+ end
161
+
162
+ def arrow_key(up)
163
+ prefix = selected.screen.application_cursor_keys? ? "\eO" : "\e["
164
+ "#{prefix}#{up ? "A" : "B"}"
165
+ end
166
+
167
+ def finish_selection(row, col)
168
+ @selecting = false
169
+ @autoscroll = nil
170
+ selected.select_to(**pane_point(row, col))
171
+ copy_selection
172
+ end
173
+
174
+ def pane_point(row, col)
175
+ {row: (row - 1).clamp(0, layout.pane_rows - 1), col: col.clamp(0, cols - 1)}
176
+ end
177
+
178
+ def copy_selection
179
+ text = selected.selected_text
180
+ desktop.copy(text) if text
181
+ end
182
+
183
+ def interrupt(bytes)
184
+ return quit if @interrupted_at && clock - @interrupted_at <= DOUBLE_PRESS_SECONDS
185
+
186
+ @interrupted_at = clock
187
+ selected.write(bytes)
188
+ end
189
+
190
+ def click(row, col, manager)
191
+ index = layout.tab_at(row:, col:)
192
+ return @selected = index if index
193
+ return desktop.open(selected.url) if layout.url_at?(row:, col:, url: selected.url)
194
+ return restart(selected, manager) if layout.restart_at?(row:, col:, offset: selected.offset)
195
+
196
+ return unless pane_row?(row)
197
+
198
+ @selecting = true
199
+ @pointer = pane_point(row, col)
200
+ selected.select_from(**@pointer)
201
+ end
202
+
203
+ def restart(pane, manager)
204
+ return manager.start(pane.name) unless pane.running?
205
+
206
+ pane.request_restart
207
+ manager.terminate(pane.name)
208
+ end
209
+
210
+ def read_events(manager)
211
+ while (event = manager.next_event(timeout: 0))
212
+ return @exit_code = event.exit_code if event.interrupt?
213
+
214
+ pane = panes.find { |candidate| candidate.name == event.name }
215
+ pane.exited(event.exit_code)
216
+ manager.start(pane.name) if pane.take_restart_request
217
+ @dirty = true
218
+ end
219
+ end
220
+
221
+ def read_output(pane)
222
+ return unless pane
223
+
224
+ @dirty = true if pane.read && pane.equal?(selected)
225
+ end
226
+
227
+ def quit
228
+ @exit_code = 0
229
+ end
230
+
231
+ def check_size
232
+ size = stdout.winsize
233
+ return if size == [rows, cols]
234
+
235
+ @rows, @cols = size
236
+ @layout = nil
237
+ panes.each { |pane| pane.resize(rows: rows - 2, cols:) }
238
+ renderer.reset
239
+ @dirty = true
240
+ end
241
+
242
+ def render_when_due
243
+ return unless @dirty && frame_due?
244
+
245
+ sync_outer_modes
246
+ renderer.render(layout:, panes:, selected:)
247
+ @dirty = false
248
+ @rendered_at = clock
249
+ end
250
+
251
+ def frame_due?
252
+ @rendered_at.nil? || clock - @rendered_at >= FRAME_SECONDS
253
+ end
254
+
255
+ def select_timeout
256
+ return AUTOSCROLL_SECONDS if @selecting && @autoscroll && !@dirty
257
+ return IDLE_SECONDS unless @dirty
258
+
259
+ [FRAME_SECONDS - (clock - (@rendered_at || 0)), 0].max
260
+ end
261
+
262
+ def sync_outer_modes
263
+ OUTER_MODES.each do |query, mode|
264
+ enabled = selected.screen.public_send(query)
265
+ next if @outer_modes[mode] == enabled
266
+
267
+ stdout.write("\e[?#{mode}#{enabled ? "h" : "l"}")
268
+ @outer_modes[mode] = enabled
269
+ end
270
+ end
271
+
272
+ def clock
273
+ Process.clock_gettime(Process::CLOCK_MONOTONIC)
274
+ end
275
+ end
276
+ end
277
+ end
@@ -0,0 +1,51 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hostty
4
+ module Tui
5
+ class Desktop
6
+ def initialize(io:)
7
+ @io = io
8
+ end
9
+
10
+ def copy(text)
11
+ io.write("\e]52;c;#{[text].pack("m0")}\a")
12
+ copy_with_command(text)
13
+ end
14
+
15
+ def open(url)
16
+ Process.detach(Process.spawn(OPENER, url, out: File::NULL, err: File::NULL))
17
+ rescue SystemCallError
18
+ nil
19
+ end
20
+
21
+ private
22
+
23
+ OPENER = RUBY_PLATFORM.include?("darwin") ? "open" : "xdg-open"
24
+ CLIPBOARD_COMMANDS = [
25
+ %w[pbcopy],
26
+ %w[wl-copy],
27
+ %w[xclip -selection clipboard],
28
+ %w[xsel --clipboard --input]
29
+ ].freeze
30
+ private_constant :OPENER, :CLIPBOARD_COMMANDS
31
+
32
+ attr_reader :io
33
+
34
+ def copy_with_command(text)
35
+ return unless clipboard_command
36
+
37
+ IO.popen(clipboard_command, "w", err: File::NULL) { |pipe| pipe.write(text) }
38
+ rescue SystemCallError, IOError
39
+ nil
40
+ end
41
+
42
+ def clipboard_command
43
+ @clipboard_command ||= CLIPBOARD_COMMANDS.find { |command| installed?(command.first) }
44
+ end
45
+
46
+ def installed?(name)
47
+ ENV.fetch("PATH", "").split(File::PATH_SEPARATOR).any? { |dir| File.executable?(File.join(dir, name)) }
48
+ end
49
+ end
50
+ end
51
+ end
@@ -0,0 +1,69 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hostty
4
+ module Tui
5
+ class Input
6
+ def initialize
7
+ @pending = +"".b
8
+ end
9
+
10
+ def parse(data)
11
+ @pending << data.b
12
+ events = []
13
+ events << next_event until @pending.empty? || incomplete_mouse?
14
+ events.compact
15
+ end
16
+
17
+ private
18
+
19
+ CTRL_C = "\x03".b
20
+ SHIFT_TAB = "\e[Z".b
21
+ SPECIAL = /\x03|\e\[<|\e\[Z/n
22
+ MOUSE = /\A\e\[<(\d+);(\d+);(\d+)([Mm])/n
23
+ INCOMPLETE_MOUSE = /\A\e\[<[\d;]*\z/n
24
+ BUTTONS = [:left, :middle, :right, nil].freeze
25
+ WHEEL_BUTTONS = [:wheel_up, :wheel_down, nil, nil].freeze
26
+ MODIFIER_BITS = {shift: 4, alt: 8, ctrl: 16}.freeze
27
+ private_constant :CTRL_C, :SHIFT_TAB, :SPECIAL, :MOUSE, :INCOMPLETE_MOUSE, :BUTTONS, :WHEEL_BUTTONS, :MODIFIER_BITS
28
+
29
+ def incomplete_mouse?
30
+ INCOMPLETE_MOUSE.match?(@pending)
31
+ end
32
+
33
+ def next_event
34
+ index = @pending.index(SPECIAL)
35
+ return [:keys, take(index || @pending.size)] unless index&.zero?
36
+ return [:interrupt, take(1)] if @pending.start_with?(CTRL_C)
37
+ return take(SHIFT_TAB.size) && [:next_tab] if @pending.start_with?(SHIFT_TAB)
38
+
39
+ match = MOUSE.match(@pending)
40
+ return [:keys, take(@pending.size)] unless match
41
+
42
+ take(match[0].size)
43
+ mouse_event(match)
44
+ end
45
+
46
+ def take(size)
47
+ @pending.slice!(0, size)
48
+ end
49
+
50
+ def mouse_event(match)
51
+ code = match[1].to_i
52
+ event = MouseEvent.new(
53
+ action: mouse_action(code, match[4]),
54
+ button: (code.anybits?(64) ? WHEEL_BUTTONS : BUTTONS)[code & 0b11],
55
+ row: match[3].to_i - 1,
56
+ col: match[2].to_i - 1,
57
+ modifiers: MODIFIER_BITS.filter_map { |name, bit| name if code.anybits?(bit) }
58
+ )
59
+ [:mouse, event] if event.button
60
+ end
61
+
62
+ def mouse_action(code, final)
63
+ return :release if final == "m"
64
+
65
+ code.anybits?(32) ? :motion : :press
66
+ end
67
+ end
68
+ end
69
+ end
@@ -0,0 +1,68 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hostty
4
+ module Tui
5
+ class Layout
6
+ RESTART_BUTTON = "[restart]"
7
+
8
+ attr_reader :rows, :cols
9
+
10
+ def initialize(names:, rows:, cols:)
11
+ @names = names
12
+ @rows = rows
13
+ @cols = cols
14
+ end
15
+
16
+ def pane_rows = rows - 2
17
+
18
+ def help_row = rows - 1
19
+
20
+ def tabs_width = tabs.sum { |tab| tab[:width] }
21
+
22
+ def tabs
23
+ @tabs ||= begin
24
+ start = 0
25
+
26
+ names.each_with_index.filter_map do |name, index|
27
+ width = name.length + 4
28
+ next if start + width > cols
29
+
30
+ {index:, start:, width:}.tap { start += width }
31
+ end
32
+ end
33
+ end
34
+
35
+ def url_start(url)
36
+ return unless url
37
+
38
+ start = cols - url.length - 1
39
+ start if start > tabs_width
40
+ end
41
+
42
+ def url_at?(row:, col:, url:)
43
+ start = url_start(url)
44
+ row.zero? && !start.nil? && (start...(start + url.length)).cover?(col)
45
+ end
46
+
47
+ def tab_at(row:, col:)
48
+ return unless row.zero?
49
+
50
+ tabs.find { |tab| (tab[:start]...tab[:start] + tab[:width]).cover?(col) }&.fetch(:index)
51
+ end
52
+
53
+ def help_text(offset:)
54
+ scroll = offset.positive? ? "Scrolled up #{offset}" : "Scroll: history"
55
+ " Switch: click or Shift+Tab | #{scroll} | #{RESTART_BUTTON} | Quit: Ctrl-C twice "
56
+ end
57
+
58
+ def restart_at?(row:, col:, offset:)
59
+ start = help_text(offset:).index(RESTART_BUTTON)
60
+ row == help_row && (start...(start + RESTART_BUTTON.length)).cover?(col)
61
+ end
62
+
63
+ private
64
+
65
+ attr_reader :names
66
+ end
67
+ end
68
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hostty
4
+ module Tui
5
+ class MouseEvent
6
+ attr_reader :action, :button, :row, :col
7
+
8
+ def initialize(action:, button:, row:, col:, modifiers: [])
9
+ @action = action
10
+ @button = button
11
+ @row = row
12
+ @col = col
13
+ @modifiers = modifiers
14
+ freeze
15
+ end
16
+
17
+ def shift? = modifiers.include?(:shift)
18
+
19
+ def alt? = modifiers.include?(:alt)
20
+
21
+ def ctrl? = modifiers.include?(:ctrl)
22
+
23
+ def wheel?
24
+ %i[wheel_up wheel_down].include?(button)
25
+ end
26
+
27
+ def at(row:)
28
+ self.class.new(action:, button:, row:, col:, modifiers:)
29
+ end
30
+
31
+ private
32
+
33
+ attr_reader :modifiers
34
+ end
35
+ end
36
+ end
@@ -0,0 +1,125 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hostty
4
+ module Tui
5
+ class Pane
6
+ attr_reader :name, :url, :screen, :offset
7
+
8
+ def initialize(name:, url:, rows:, cols:)
9
+ @name = name
10
+ @url = url
11
+ @rows = rows
12
+ @cols = cols
13
+ @pty = Pty.new
14
+ @screen = Ghostty::Screen.new(rows:, cols:, reply: pty)
15
+ @offset = 0
16
+ @running = false
17
+ end
18
+
19
+ def running? = @running
20
+
21
+ def io = pty.reader
22
+
23
+ def start(process)
24
+ notify("\e[2m[hostty] Starting #{name}.") if @exit_code
25
+ @exit_code = nil
26
+ @running = true
27
+ pty.start(process, rows:, cols:)
28
+ end
29
+
30
+ def finish
31
+ pty.close
32
+ end
33
+
34
+ def read
35
+ data = pty.read
36
+ screen.feed(data) if data
37
+ !data&.empty?
38
+ end
39
+
40
+ def write(data)
41
+ @offset = 0
42
+ @selection = nil
43
+ pty.write(data)
44
+ end
45
+
46
+ def send_mouse(event)
47
+ bytes = screen.encode_mouse(event)
48
+ write(bytes) unless bytes.empty?
49
+ end
50
+
51
+ def select_from(row:, col:)
52
+ @selection = Selection.new(anchor: [history_row(row), col])
53
+ end
54
+
55
+ def select_to(row:, col:)
56
+ @selection = selection&.to([history_row(row), col])
57
+ end
58
+
59
+ def selected_columns(row, width)
60
+ selection.columns(history_row(row), width) if selecting?
61
+ end
62
+
63
+ def selected_text
64
+ selection.text(history_lines(selection.rows)) if selecting?
65
+ end
66
+
67
+ def exited(exit_code)
68
+ nil while read && io
69
+ @exit_code = exit_code
70
+ @running = false
71
+ notify("\e[31m[hostty] #{name} exited with code #{exit_code}.")
72
+ end
73
+
74
+ def request_restart
75
+ @restart_requested = true
76
+ end
77
+
78
+ def take_restart_request
79
+ @restart_requested.tap { @restart_requested = false }
80
+ end
81
+
82
+ def scroll(lines)
83
+ @offset = (offset + lines).clamp(0, screen.scrollback_size)
84
+ end
85
+
86
+ def lines
87
+ screen.lines(offset:)
88
+ end
89
+
90
+ def resize(rows:, cols:)
91
+ @rows = rows
92
+ @cols = cols
93
+ screen.resize(rows:, cols:)
94
+ pty.resize(rows:, cols:)
95
+ @selection = nil
96
+ scroll(0)
97
+ end
98
+
99
+ private
100
+
101
+ attr_reader :rows, :cols, :pty, :selection
102
+
103
+ def selecting?
104
+ !selection.nil? && !selection.empty?
105
+ end
106
+
107
+ def history_row(row)
108
+ screen.scrollback_size - offset + row
109
+ end
110
+
111
+ def history_lines(range)
112
+ size = screen.scrollback_size
113
+
114
+ range.step(rows).each_with_object({}) do |first, lines|
115
+ page_offset = (size - first).clamp(0, size)
116
+ screen.lines(offset: page_offset).each_with_index { |cells, index| lines[size - page_offset + index] = cells }
117
+ end
118
+ end
119
+
120
+ def notify(text)
121
+ screen.feed("\e[0m\r\n#{text}\e[0m\r\n")
122
+ end
123
+ end
124
+ end
125
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hostty
4
+ module Tui
5
+ class Pty
6
+ attr_reader :reader
7
+
8
+ def start(process, rows:, cols:)
9
+ close
10
+ @reader, @writer, pid = PTY.spawn(
11
+ process.environment,
12
+ "/bin/sh",
13
+ "-c",
14
+ 'cd "$1" && exec /bin/sh -c "$2"',
15
+ "hostty",
16
+ process.root_path,
17
+ process.command
18
+ )
19
+ resize(rows:, cols:)
20
+ pid
21
+ end
22
+
23
+ def read
24
+ return unless reader
25
+
26
+ data = reader.read_nonblock(READ_SIZE, exception: false)
27
+ return "" if data == :wait_readable
28
+
29
+ close unless data
30
+ data
31
+ rescue Errno::EIO, IOError
32
+ close
33
+ nil
34
+ end
35
+
36
+ def write(data)
37
+ writer&.write_nonblock(data, exception: false)
38
+ rescue Errno::EIO, Errno::EPIPE, IOError
39
+ nil
40
+ end
41
+
42
+ def resize(rows:, cols:)
43
+ reader&.winsize = [rows, cols]
44
+ rescue Errno::EIO, Errno::ENOTTY, IOError
45
+ nil
46
+ end
47
+
48
+ def close
49
+ [reader, writer].compact.reject(&:closed?).each(&:close)
50
+ @reader = nil
51
+ @writer = nil
52
+ end
53
+
54
+ private
55
+
56
+ READ_SIZE = 65_536
57
+ private_constant :READ_SIZE
58
+
59
+ attr_reader :writer
60
+ end
61
+ end
62
+ end