hostty 0.1.0.alpha-x86_64-darwin

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 (57) 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/libghostty-vt.dylib +0 -0
  30. data/lib/hostty/ghostty/library.rb +148 -0
  31. data/lib/hostty/ghostty/mouse.rb +77 -0
  32. data/lib/hostty/ghostty/screen.rb +112 -0
  33. data/lib/hostty/manager/child.rb +36 -0
  34. data/lib/hostty/manager/events.rb +27 -0
  35. data/lib/hostty/manager/process_exited.rb +28 -0
  36. data/lib/hostty/manager/signal_received.rb +23 -0
  37. data/lib/hostty/manager.rb +102 -0
  38. data/lib/hostty/output.rb +37 -0
  39. data/lib/hostty/plain/app.rb +23 -0
  40. data/lib/hostty/plain/pipe.rb +55 -0
  41. data/lib/hostty/port_chooser.rb +62 -0
  42. data/lib/hostty/process_config.rb +80 -0
  43. data/lib/hostty/terminal/cell.rb +35 -0
  44. data/lib/hostty/terminal/style.rb +87 -0
  45. data/lib/hostty/tui/app.rb +277 -0
  46. data/lib/hostty/tui/desktop.rb +51 -0
  47. data/lib/hostty/tui/input.rb +69 -0
  48. data/lib/hostty/tui/layout.rb +68 -0
  49. data/lib/hostty/tui/mouse_event.rb +36 -0
  50. data/lib/hostty/tui/pane.rb +125 -0
  51. data/lib/hostty/tui/pty.rb +62 -0
  52. data/lib/hostty/tui/renderer.rb +85 -0
  53. data/lib/hostty/tui/selection.rb +47 -0
  54. data/lib/hostty/version.rb +5 -0
  55. data/lib/hostty.rb +35 -0
  56. data/sig/hostty.rbs +4 -0
  57. metadata +130 -0
@@ -0,0 +1,78 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hostty
4
+ class Config
5
+ FILE_NAME = ".hostty.yml"
6
+ BASE_PORT = 5000
7
+
8
+ def initialize(path:, base_port:)
9
+ @path = File.expand_path(path)
10
+ @base_port = base_port
11
+ validate!
12
+ end
13
+
14
+ def processes
15
+ @processes ||=
16
+ process_data.each_with_index.map do |(name, settings), index|
17
+ ProcessConfig.new(
18
+ name: name.to_s,
19
+ settings:,
20
+ default_port: base_port + (index * PORT_STEP),
21
+ root_path: File.dirname(path),
22
+ ports: port_chooser
23
+ )
24
+ end
25
+ end
26
+
27
+ def notices = port_chooser.notices
28
+
29
+ private
30
+
31
+ PORT_STEP = 100
32
+ KEYS = %w[processes skip_used_ports].freeze
33
+ private_constant :PORT_STEP, :KEYS
34
+
35
+ attr_reader :path, :base_port
36
+
37
+ def data
38
+ @data ||= YAML.safe_load_file(path)
39
+ rescue Errno::ENOENT
40
+ raise ConfigError, "No config file at #{path}."
41
+ rescue Psych::Exception => e
42
+ raise ConfigError, "The config file at #{path} is not valid YAML: #{e.message}"
43
+ end
44
+
45
+ def process_data = data["processes"]
46
+
47
+ def skip_used_ports = data.fetch("skip_used_ports", false)
48
+
49
+ def port_chooser
50
+ @port_chooser ||= PortChooser.new(skip_used_ports:)
51
+ end
52
+
53
+ def validate!
54
+ validate_shape!
55
+ raise ConfigError, "skip_used_ports must be true or false." unless [true, false].include?(skip_used_ports)
56
+
57
+ processes.partition(&:fixed_port?).flatten.each(&:port)
58
+ raise_on_duplicate_domains
59
+ end
60
+
61
+ def validate_shape!
62
+ raise ConfigError, "The config file at #{path} must be a list of settings." unless data.is_a?(Hash)
63
+ raise ConfigError, "The config file at #{path} must list at least one process under processes." unless process_data.is_a?(Hash) && process_data.any?
64
+ raise ConfigError, "The config file at #{path} has unknown settings: #{unknown_keys.join(", ")}." if unknown_keys.any?
65
+ end
66
+
67
+ def unknown_keys
68
+ data.keys.map(&:to_s) - KEYS
69
+ end
70
+
71
+ def raise_on_duplicate_domains
72
+ duplicates = processes.filter_map(&:domain).tally.select { |_domain, count| count > 1 }.keys
73
+ return if duplicates.empty?
74
+
75
+ raise ConfigError, "More than one process uses the domain #{duplicates.join(", ")}."
76
+ end
77
+ end
78
+ end
@@ -0,0 +1,22 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hostty
4
+ class Domains
5
+ def initialize(processes:, nginx:, output:)
6
+ @processes = processes.select(&:domain)
7
+ @nginx = nginx
8
+ @output = output
9
+ end
10
+
11
+ def add
12
+ processes.each do |process|
13
+ output.notice("Adding #{process.url} for #{process.name} on port #{process.port}.")
14
+ nginx.add!(process.nginx_options)
15
+ end
16
+ end
17
+
18
+ private
19
+
20
+ attr_reader :processes, :nginx, :output
21
+ end
22
+ end
@@ -0,0 +1,7 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hostty
4
+ class Error < StandardError; end
5
+
6
+ class ConfigError < Error; end
7
+ end
@@ -0,0 +1,203 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hostty
4
+ module Ghostty
5
+ class Frame
6
+ def initialize(terminal:)
7
+ @terminal = terminal
8
+ @state = Library.create(:ghostty_render_state_new, :ghostty_render_state_free)
9
+ @row_iterator = Library.create(:ghostty_render_state_row_iterator_new, :ghostty_render_state_row_iterator_free)
10
+ @cells = Library.create(:ghostty_render_state_row_cells_new, :ghostty_render_state_row_cells_free)
11
+ @row_iterator_reference = Library.reference(row_iterator)
12
+ @cells_reference = Library.reference(cells)
13
+ @styles = {}
14
+ end
15
+
16
+ def lines
17
+ Library.ghostty_render_state_update(state, terminal)
18
+ @lines = read_lines unless @lines && dirty == Library::RENDER_STATE_DIRTY_FALSE
19
+ Library.ghostty_render_state_clean(state)
20
+ @lines
21
+ end
22
+
23
+ private
24
+
25
+ KEYS = [Library::ROW_CELLS_DATA_RAW, Library::ROW_CELLS_DATA_STYLE, Library::ROW_CELLS_DATA_GRAPHEMES_UTF8].freeze
26
+ CELL_KEYS = [Library::CELL_DATA_WIDE, Library::CELL_DATA_CONTENT_TAG].freeze
27
+ TEXT_CAPACITY = 256
28
+ STYLE_LIMIT = 1024
29
+ private_constant :KEYS, :CELL_KEYS, :TEXT_CAPACITY, :STYLE_LIMIT
30
+
31
+ attr_reader :terminal, :state, :row_iterator, :cells, :row_iterator_reference, :cells_reference, :styles
32
+
33
+ def dirty
34
+ read_state(:int, Library::RENDER_STATE_DATA_DIRTY)
35
+ end
36
+
37
+ def read_state(type, data)
38
+ value = FFI::MemoryPointer.new(type)
39
+ Library.ghostty_render_state_get(state, data, value)
40
+ value.read(type)
41
+ end
42
+
43
+ def read_lines
44
+ previous = (dirty == Library::RENDER_STATE_DIRTY_FULL) ? {} : @rows_by_id || {}
45
+ @rows_by_id = {}
46
+ Library.ghostty_render_state_get(state, Library::RENDER_STATE_DATA_ROW_ITERATOR, row_iterator_reference)
47
+ @rows_by_id[row_id] = ((!row_dirty? && previous[row_id]) || read_row) while Library.ghostty_render_state_row_iterator_next(row_iterator)
48
+ @rows_by_id.values
49
+ end
50
+
51
+ def row_id
52
+ Library.ghostty_render_state_row_get(row_iterator, Library::ROW_DATA_ID, row_id_memory)
53
+ row_id_memory.get_bytes(0, 16)
54
+ end
55
+
56
+ def row_id_memory
57
+ @row_id_memory ||= FFI::MemoryPointer.new(:uint8, 16)
58
+ end
59
+
60
+ def row_dirty?
61
+ value = FFI::MemoryPointer.new(:uint8)
62
+ Library.ghostty_render_state_row_get(row_iterator, Library::ROW_DATA_DIRTY, value)
63
+ !value.read_uint8.zero?
64
+ end
65
+
66
+ def read_row
67
+ Library.ghostty_render_state_row_get(row_iterator, Library::ROW_DATA_CELLS, cells_reference)
68
+ row = []
69
+ row << read_cell while Library.ghostty_render_state_row_cells_next(cells)
70
+ row
71
+ end
72
+
73
+ def read_cell
74
+ result = Library.ghostty_render_state_row_cells_get_multi(cells, KEYS.size, keys, values, nil)
75
+ text = (result == Library::SUCCESS) ? text_memory.get_bytes(0, text_buffer[:len]).force_encoding(Encoding::UTF_8) : "�"
76
+ build_cell(text, raw_memory.read_uint64)
77
+ end
78
+
79
+ def build_cell(text, raw)
80
+ style_bytes = style_memory.get_bytes(0, 16)
81
+ return narrow_cell(text, style_bytes) if text.bytesize == 1
82
+
83
+ Library.ghostty_cell_get_multi(raw, CELL_KEYS.size, cell_keys, cell_values, nil)
84
+
85
+ case wide_memory.read_int
86
+ when Library::CELL_WIDE_SPACER_TAIL
87
+ Terminal::Cell.continuation(style_for(style_bytes))
88
+ when Library::CELL_WIDE_WIDE
89
+ Terminal::Cell.new(char: text, style: style_for(style_bytes), width: 2)
90
+ else
91
+ text.empty? ? blank_cell(raw, style_bytes) : Terminal::Cell.new(char: text, style: style_for(style_bytes))
92
+ end
93
+ end
94
+
95
+ def narrow_cell(text, style_bytes)
96
+ cells = narrow_cells[style_bytes] ||= {}
97
+ cells[text] ||= Terminal::Cell.new(char: text, style: style_for(style_bytes))
98
+ end
99
+
100
+ def narrow_cells
101
+ @narrow_cells = {} if @narrow_cells.nil? || @narrow_cells.size > STYLE_LIMIT
102
+ @narrow_cells
103
+ end
104
+
105
+ def blank_cell(raw, style_bytes)
106
+ case content_memory.read_int
107
+ when Library::CELL_CONTENT_BG_COLOR_PALETTE
108
+ background_cell(style_bytes, cell_bytes(raw, Library::CELL_DATA_COLOR_PALETTE, 1).first)
109
+ when Library::CELL_CONTENT_BG_COLOR_RGB
110
+ background_cell(style_bytes, cell_bytes(raw, Library::CELL_DATA_COLOR_RGB, 3))
111
+ else
112
+ narrow_cell(" ", style_bytes)
113
+ end
114
+ end
115
+
116
+ def background_cell(style_bytes, bg)
117
+ Terminal::Cell.blank(Terminal::Style.shared(style_for(style_bytes).to_h.merge(bg:)))
118
+ end
119
+
120
+ def cell_bytes(raw, data, count)
121
+ Library.ghostty_cell_get(raw, data, color_memory)
122
+ color_memory.get_bytes(0, count).bytes
123
+ end
124
+
125
+ def style_for(bytes)
126
+ styles.clear if styles.size > STYLE_LIMIT
127
+ styles[bytes] ||= decode_style(*bytes.unpack("C12S<"))
128
+ end
129
+
130
+ def decode_style(*colors, flags)
131
+ Terminal::Style.shared(
132
+ fg: color(*colors[0, 4]),
133
+ bg: color(*colors[4, 4]),
134
+ bold: flags.anybits?(1 << 0),
135
+ dim: flags.anybits?(1 << 2),
136
+ italic: flags.anybits?(1 << 1),
137
+ underline: flags.anybits?(0b111 << 8),
138
+ blink: flags.anybits?(1 << 3),
139
+ inverse: flags.anybits?(1 << 4),
140
+ invisible: flags.anybits?(1 << 5),
141
+ strikethrough: flags.anybits?(1 << 6),
142
+ overline: flags.anybits?(1 << 7)
143
+ )
144
+ end
145
+
146
+ def color(tag, *value)
147
+ case tag
148
+ when 1
149
+ value.first
150
+ when 2
151
+ value
152
+ end
153
+ end
154
+
155
+ def raw_memory
156
+ @raw_memory ||= FFI::MemoryPointer.new(:uint64)
157
+ end
158
+
159
+ def style_memory
160
+ @style_memory ||= FFI::MemoryPointer.new(:uint8, 16)
161
+ end
162
+
163
+ def text_memory
164
+ @text_memory ||= FFI::MemoryPointer.new(:uint8, TEXT_CAPACITY)
165
+ end
166
+
167
+ def text_buffer
168
+ @text_buffer ||= Library::Buffer.new.tap do |buffer|
169
+ buffer[:ptr] = text_memory
170
+ buffer[:cap] = TEXT_CAPACITY
171
+ end
172
+ end
173
+
174
+ def wide_memory
175
+ @wide_memory ||= FFI::MemoryPointer.new(:int)
176
+ end
177
+
178
+ def content_memory
179
+ @content_memory ||= FFI::MemoryPointer.new(:int)
180
+ end
181
+
182
+ def color_memory
183
+ @color_memory ||= FFI::MemoryPointer.new(:uint8, 4)
184
+ end
185
+
186
+ def cell_keys
187
+ @cell_keys ||= FFI::MemoryPointer.new(:int, CELL_KEYS.size).write_array_of_int(CELL_KEYS)
188
+ end
189
+
190
+ def cell_values
191
+ @cell_values ||= FFI::MemoryPointer.new(:pointer, CELL_KEYS.size).write_array_of_pointer([wide_memory, content_memory])
192
+ end
193
+
194
+ def keys
195
+ @keys ||= FFI::MemoryPointer.new(:int, KEYS.size).write_array_of_int(KEYS)
196
+ end
197
+
198
+ def values
199
+ @values ||= FFI::MemoryPointer.new(:pointer, KEYS.size).write_array_of_pointer([raw_memory, style_memory, text_buffer.pointer])
200
+ end
201
+ end
202
+ end
203
+ end
@@ -0,0 +1,148 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "ffi"
4
+
5
+ module Hostty
6
+ module Ghostty
7
+ module Library
8
+ extend FFI::Library
9
+
10
+ FILE_NAME = FFI.map_library_name("ghostty-vt")
11
+ PACKAGED = File.join(__dir__, FILE_NAME)
12
+ DEVELOPMENT = File.expand_path("../../../vendor/libghostty/lib/#{FILE_NAME}", __dir__)
13
+
14
+ ffi_lib [PACKAGED, DEVELOPMENT, "ghostty-vt"]
15
+
16
+ SUCCESS = 0
17
+ OUT_OF_SPACE = -3
18
+
19
+ TERMINAL_OPT_WRITE_PTY = 2
20
+ TERMINAL_OPT_SCROLLBACK_MAX_BYTES = 28
21
+ TERMINAL_OPT_SCROLLBACK_MAX_LINES = 29
22
+
23
+ TERMINAL_DATA_CURSOR_X = 3
24
+ TERMINAL_DATA_CURSOR_Y = 4
25
+ TERMINAL_DATA_ACTIVE_SCREEN = 6
26
+ TERMINAL_DATA_CURSOR_VISIBLE = 7
27
+ TERMINAL_DATA_MOUSE_TRACKING = 11
28
+ TERMINAL_DATA_SCROLLBACK_ROWS = 15
29
+ TERMINAL_DATA_MODE = 37
30
+
31
+ MODE_CURSOR_KEYS = 1
32
+ MODE_BRACKETED_PASTE = 2004
33
+
34
+ TERMINAL_SCREEN_ALTERNATE = 1
35
+
36
+ MOUSE_ACTIONS = {press: 0, release: 1, motion: 2}.freeze
37
+ MOUSE_BUTTONS = {left: 1, right: 2, middle: 3, wheel_up: 4, wheel_down: 5}.freeze
38
+ MOUSE_ENCODER_OPT_SIZE = 3
39
+ MOUSE_ENCODER_OPT_ANY_BUTTON_PRESSED = 4
40
+ MODS = {shift: 1 << 0, ctrl: 1 << 1, alt: 1 << 2}.freeze
41
+
42
+ SCROLL_VIEWPORT_BOTTOM = 1
43
+ SCROLL_VIEWPORT_DELTA = 2
44
+
45
+ RENDER_STATE_DATA_DIRTY = 3
46
+ RENDER_STATE_DATA_ROW_ITERATOR = 4
47
+ RENDER_STATE_DIRTY_FALSE = 0
48
+ RENDER_STATE_DIRTY_FULL = 2
49
+
50
+ ROW_DATA_DIRTY = 1
51
+ ROW_DATA_CELLS = 3
52
+ ROW_DATA_ID = 7
53
+
54
+ ROW_CELLS_DATA_RAW = 1
55
+ ROW_CELLS_DATA_STYLE = 2
56
+ ROW_CELLS_DATA_GRAPHEMES_UTF8 = 9
57
+
58
+ CELL_DATA_CONTENT_TAG = 2
59
+ CELL_DATA_WIDE = 3
60
+ CELL_DATA_COLOR_PALETTE = 10
61
+ CELL_DATA_COLOR_RGB = 11
62
+
63
+ CELL_CONTENT_BG_COLOR_PALETTE = 2
64
+ CELL_CONTENT_BG_COLOR_RGB = 3
65
+
66
+ CELL_WIDE_WIDE = 1
67
+ CELL_WIDE_SPACER_TAIL = 2
68
+
69
+ class ScrollViewport < FFI::Struct
70
+ layout :tag, :int, 0, :delta, :int64, 8, :padding, :uint64, 16
71
+ end
72
+
73
+ class Buffer < FFI::Struct
74
+ layout :ptr, :pointer, :cap, :size_t, :len, :size_t
75
+ end
76
+
77
+ class MousePosition < FFI::Struct
78
+ layout :x, :float, :y, :float
79
+ end
80
+
81
+ class MouseEncoderSize < FFI::Struct
82
+ layout :size, :size_t,
83
+ :screen_width, :uint32,
84
+ :screen_height, :uint32,
85
+ :cell_width, :uint32,
86
+ :cell_height, :uint32,
87
+ :padding_top, :uint32,
88
+ :padding_bottom, :uint32,
89
+ :padding_right, :uint32,
90
+ :padding_left, :uint32
91
+ end
92
+
93
+ callback :write_pty, %i[pointer pointer pointer size_t], :void
94
+
95
+ attach_function :ghostty_terminal_new, %i[pointer pointer uint16 uint16], :int
96
+ attach_function :ghostty_terminal_free, [:pointer], :void
97
+ attach_function :ghostty_terminal_resize, %i[pointer uint16 uint16 uint32 uint32], :int
98
+ attach_function :ghostty_terminal_set, %i[pointer int pointer], :int
99
+ attach_function :ghostty_terminal_vt_write, %i[pointer buffer_in size_t], :void
100
+ attach_function :ghostty_terminal_get, %i[pointer int pointer], :int
101
+ attach_function :ghostty_terminal_scroll_viewport, [:pointer, ScrollViewport.by_value], :int
102
+
103
+ attach_function :ghostty_render_state_new, %i[pointer pointer], :int
104
+ attach_function :ghostty_render_state_free, [:pointer], :void
105
+ attach_function :ghostty_render_state_update, %i[pointer pointer], :int
106
+ attach_function :ghostty_render_state_get, %i[pointer int pointer], :int
107
+ attach_function :ghostty_render_state_clean, [:pointer], :int
108
+
109
+ attach_function :ghostty_render_state_row_iterator_new, %i[pointer pointer], :int
110
+ attach_function :ghostty_render_state_row_iterator_free, [:pointer], :void
111
+ attach_function :ghostty_render_state_row_iterator_next, [:pointer], :bool
112
+ attach_function :ghostty_render_state_row_get, %i[pointer int pointer], :int
113
+
114
+ attach_function :ghostty_render_state_row_cells_new, %i[pointer pointer], :int
115
+ attach_function :ghostty_render_state_row_cells_free, [:pointer], :void
116
+ attach_function :ghostty_render_state_row_cells_next, [:pointer], :bool
117
+ attach_function :ghostty_render_state_row_cells_get_multi, %i[pointer size_t pointer pointer pointer], :int
118
+
119
+ attach_function :ghostty_cell_get, %i[uint64 int pointer], :int
120
+ attach_function :ghostty_cell_get_multi, %i[uint64 size_t pointer pointer pointer], :int
121
+
122
+ attach_function :ghostty_mouse_encoder_new, %i[pointer pointer], :int
123
+ attach_function :ghostty_mouse_encoder_free, [:pointer], :void
124
+ attach_function :ghostty_mouse_encoder_set, %i[pointer int pointer], :int
125
+ attach_function :ghostty_mouse_encoder_set_from_terminal, %i[pointer pointer], :int
126
+ attach_function :ghostty_mouse_encoder_encode, %i[pointer pointer pointer size_t pointer], :int
127
+
128
+ attach_function :ghostty_mouse_event_new, %i[pointer pointer], :int
129
+ attach_function :ghostty_mouse_event_free, [:pointer], :void
130
+ attach_function :ghostty_mouse_event_set_action, %i[pointer int], :int
131
+ attach_function :ghostty_mouse_event_set_button, %i[pointer int], :int
132
+ attach_function :ghostty_mouse_event_set_mods, %i[pointer uint16], :int
133
+ attach_function :ghostty_mouse_event_set_position, [:pointer, MousePosition.by_value], :int
134
+
135
+ def self.create(constructor, destructor, *arguments)
136
+ reference = FFI::MemoryPointer.new(:pointer)
137
+ result = public_send(constructor, nil, reference, *arguments)
138
+ raise Error, "#{constructor} failed with #{result}" unless result == SUCCESS
139
+
140
+ FFI::AutoPointer.new(reference.read_pointer, method(destructor))
141
+ end
142
+
143
+ def self.reference(handle)
144
+ FFI::MemoryPointer.new(:pointer).write_pointer(handle)
145
+ end
146
+ end
147
+ end
148
+ end
@@ -0,0 +1,77 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hostty
4
+ module Ghostty
5
+ class Mouse
6
+ def initialize(terminal:)
7
+ @terminal = terminal
8
+ @encoder = Library.create(:ghostty_mouse_encoder_new, :ghostty_mouse_encoder_free)
9
+ @event = Library.create(:ghostty_mouse_event_new, :ghostty_mouse_event_free)
10
+ end
11
+
12
+ def encode(mouse, rows:, cols:)
13
+ track_buttons(mouse)
14
+ prepare_encoder(rows:, cols:)
15
+ prepare_event(mouse)
16
+ Library.ghostty_mouse_encoder_encode(encoder, event, buffer, BUFFER_SIZE, written)
17
+ buffer.get_bytes(0, written.read(:size_t))
18
+ end
19
+
20
+ private
21
+
22
+ BUFFER_SIZE = 128
23
+ private_constant :BUFFER_SIZE
24
+
25
+ attr_reader :terminal, :encoder, :event
26
+
27
+ def track_buttons(mouse)
28
+ return if mouse.wheel?
29
+
30
+ @pressed = mouse.action != :release
31
+ end
32
+
33
+ def prepare_encoder(rows:, cols:)
34
+ Library.ghostty_mouse_encoder_set_from_terminal(encoder, terminal)
35
+ Library.ghostty_mouse_encoder_set(encoder, Library::MOUSE_ENCODER_OPT_SIZE, size(rows:, cols:))
36
+ pressed = FFI::MemoryPointer.new(:uint8).write_uint8(@pressed ? 1 : 0)
37
+ Library.ghostty_mouse_encoder_set(encoder, Library::MOUSE_ENCODER_OPT_ANY_BUTTON_PRESSED, pressed)
38
+ end
39
+
40
+ def size(rows:, cols:)
41
+ Library::MouseEncoderSize.new.tap do |size|
42
+ size[:size] = Library::MouseEncoderSize.size
43
+ size[:screen_width] = cols
44
+ size[:screen_height] = rows
45
+ size[:cell_width] = 1
46
+ size[:cell_height] = 1
47
+ end
48
+ end
49
+
50
+ def prepare_event(mouse)
51
+ Library.ghostty_mouse_event_set_action(event, Library::MOUSE_ACTIONS.fetch(mouse.action))
52
+ Library.ghostty_mouse_event_set_button(event, Library::MOUSE_BUTTONS.fetch(mouse.button))
53
+ Library.ghostty_mouse_event_set_mods(event, mods(mouse))
54
+ Library.ghostty_mouse_event_set_position(event, position(mouse))
55
+ end
56
+
57
+ def mods(mouse)
58
+ Library::MODS.sum { |name, bit| mouse.public_send(:"#{name}?") ? bit : 0 }
59
+ end
60
+
61
+ def position(mouse)
62
+ Library::MousePosition.new.tap do |position|
63
+ position[:x] = mouse.col + 0.5
64
+ position[:y] = mouse.row + 0.5
65
+ end
66
+ end
67
+
68
+ def buffer
69
+ @buffer ||= FFI::MemoryPointer.new(:uint8, BUFFER_SIZE)
70
+ end
71
+
72
+ def written
73
+ @written ||= FFI::MemoryPointer.new(:size_t)
74
+ end
75
+ end
76
+ end
77
+ end
@@ -0,0 +1,112 @@
1
+ # frozen_string_literal: true
2
+
3
+ require_relative "library"
4
+ require_relative "frame"
5
+ require_relative "mouse"
6
+
7
+ module Hostty
8
+ module Ghostty
9
+ class Screen
10
+ def initialize(rows:, cols:, reply: nil)
11
+ @reply = reply
12
+ @rows = rows
13
+ @cols = cols
14
+ @terminal = Library.create(:ghostty_terminal_new, :ghostty_terminal_free, cols, rows)
15
+ @frame = Frame.new(terminal:)
16
+ @mouse = Mouse.new(terminal:)
17
+ limit_scrollback
18
+ send_replies
19
+ end
20
+
21
+ def application_cursor_keys? = mode?(Library::MODE_CURSOR_KEYS)
22
+
23
+ def bracketed_paste? = mode?(Library::MODE_BRACKETED_PASTE)
24
+
25
+ def mouse_tracking? = !read(:uint8, Library::TERMINAL_DATA_MOUSE_TRACKING).zero?
26
+
27
+ def scrollback_size = read(:size_t, Library::TERMINAL_DATA_SCROLLBACK_ROWS)
28
+
29
+ def alternate_screen?
30
+ read(:int, Library::TERMINAL_DATA_ACTIVE_SCREEN) == Library::TERMINAL_SCREEN_ALTERNATE
31
+ end
32
+
33
+ def encode_mouse(event)
34
+ mouse.encode(event, rows:, cols:)
35
+ end
36
+
37
+ def feed(data)
38
+ Library.ghostty_terminal_vt_write(terminal, data, data.bytesize)
39
+ end
40
+
41
+ def lines(offset: 0)
42
+ scroll_to(offset) unless offset.zero? && @offset.to_i.zero?
43
+ frame.lines
44
+ end
45
+
46
+ def cursor
47
+ return if read(:uint8, Library::TERMINAL_DATA_CURSOR_VISIBLE).zero?
48
+
49
+ [read(:uint16, Library::TERMINAL_DATA_CURSOR_Y), read(:uint16, Library::TERMINAL_DATA_CURSOR_X)]
50
+ end
51
+
52
+ def resize(rows:, cols:)
53
+ @rows = rows
54
+ @cols = cols
55
+ Library.ghostty_terminal_resize(terminal, cols, rows, CELL_WIDTH, CELL_HEIGHT)
56
+ end
57
+
58
+ private
59
+
60
+ SCROLLBACK_LINES = 5000
61
+ SCROLLBACK_BYTES = 64 * 1024 * 1024
62
+ CELL_WIDTH = 8
63
+ CELL_HEIGHT = 16
64
+ private_constant :SCROLLBACK_LINES, :SCROLLBACK_BYTES, :CELL_WIDTH, :CELL_HEIGHT
65
+
66
+ attr_reader :reply, :rows, :cols, :terminal, :frame, :mouse
67
+
68
+ def limit_scrollback
69
+ set_size(Library::TERMINAL_OPT_SCROLLBACK_MAX_LINES, SCROLLBACK_LINES)
70
+ set_size(Library::TERMINAL_OPT_SCROLLBACK_MAX_BYTES, SCROLLBACK_BYTES)
71
+ end
72
+
73
+ def set_size(option, value)
74
+ Library.ghostty_terminal_set(terminal, option, FFI::MemoryPointer.new(:size_t).write(:size_t, value))
75
+ end
76
+
77
+ def send_replies
78
+ @write_pty = FFI::Function.new(:void, %i[pointer pointer pointer size_t]) do |_terminal, _userdata, data, length|
79
+ reply&.write(data.read_bytes(length))
80
+ end
81
+
82
+ Library.ghostty_terminal_set(terminal, Library::TERMINAL_OPT_WRITE_PTY, @write_pty)
83
+ end
84
+
85
+ def read(type, data)
86
+ value = FFI::MemoryPointer.new(type)
87
+ Library.ghostty_terminal_get(terminal, data, value)
88
+ value.read(type)
89
+ end
90
+
91
+ def mode?(number)
92
+ config = FFI::MemoryPointer.new(:uint8, 4)
93
+ config.put(:uint16, 0, number)
94
+ Library.ghostty_terminal_get(terminal, Library::TERMINAL_DATA_MODE, config)
95
+ !config.get(:uint8, 2).zero?
96
+ end
97
+
98
+ def scroll_to(offset)
99
+ @offset = offset
100
+ scroll(Library::SCROLL_VIEWPORT_BOTTOM)
101
+ scroll(Library::SCROLL_VIEWPORT_DELTA, -offset) if offset.positive?
102
+ end
103
+
104
+ def scroll(tag, delta = 0)
105
+ behavior = Library::ScrollViewport.new
106
+ behavior[:tag] = tag
107
+ behavior[:delta] = delta
108
+ Library.ghostty_terminal_scroll_viewport(terminal, behavior)
109
+ end
110
+ end
111
+ end
112
+ end
@@ -0,0 +1,36 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hostty
4
+ class Manager
5
+ class Child
6
+ def initialize(process:, launch:, events:)
7
+ @process = process
8
+ @launch = launch
9
+ @events = events
10
+ end
11
+
12
+ def name = process.name
13
+
14
+ def start
15
+ @pid = launch.start(process)
16
+ Thread.new(pid) { |child_pid| events.push(ProcessExited.new(name:, status: Process.wait2(child_pid).last)) }
17
+ end
18
+
19
+ def signal(signal_name)
20
+ return unless pid
21
+
22
+ Process.kill(signal_name, -pid)
23
+ rescue Errno::ESRCH, Errno::EPERM
24
+ nil
25
+ end
26
+
27
+ def finish
28
+ launch.finish
29
+ end
30
+
31
+ private
32
+
33
+ attr_reader :process, :launch, :events, :pid
34
+ end
35
+ end
36
+ end