hostty 0.1.0.alpha-arm64-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,27 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hostty
4
+ class Manager
5
+ class Events
6
+ attr_reader :wakeup_io
7
+
8
+ def initialize
9
+ @queue = Thread::Queue.new
10
+ @wakeup_io, @wakeup_writer = IO.pipe
11
+ end
12
+
13
+ def push(event)
14
+ queue << event
15
+ wakeup_writer.write_nonblock(".", exception: false)
16
+ end
17
+
18
+ def pop(timeout: nil)
19
+ queue.pop(timeout:)&.tap { wakeup_io.read_nonblock(1, exception: false) }
20
+ end
21
+
22
+ private
23
+
24
+ attr_reader :queue, :wakeup_writer
25
+ end
26
+ end
27
+ end
@@ -0,0 +1,28 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hostty
4
+ class Manager
5
+ class ProcessExited
6
+ attr_reader :name
7
+
8
+ def initialize(name:, status:)
9
+ @name = name
10
+ @status = status
11
+ end
12
+
13
+ def interrupt? = false
14
+
15
+ def exit_code
16
+ status.exitstatus || (128 + status.termsig)
17
+ end
18
+
19
+ def announce(output)
20
+ output.notice("#{name} exited with code #{exit_code}.")
21
+ end
22
+
23
+ private
24
+
25
+ attr_reader :status
26
+ end
27
+ end
28
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hostty
4
+ class Manager
5
+ class SignalReceived
6
+ def initialize(signal:)
7
+ @signal = signal
8
+ end
9
+
10
+ def interrupt? = true
11
+
12
+ def exit_code = 0
13
+
14
+ def announce(output)
15
+ output.notice("Received SIG#{signal}.")
16
+ end
17
+
18
+ private
19
+
20
+ attr_reader :signal
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,102 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hostty
4
+ class Manager
5
+ def initialize(processes:, front_end:, output:, stop_timeout:)
6
+ @front_end = front_end
7
+ @output = output
8
+ @stop_timeout = stop_timeout
9
+ @events = Events.new
10
+ @children = processes.to_h { |process| [process.name, build_child(process)] }
11
+ @running = {}
12
+ end
13
+
14
+ def wakeup_io = events.wakeup_io
15
+
16
+ def run
17
+ with_signal_traps do
18
+ children.each_key { |name| start(name) }
19
+ front_end.wait(self)
20
+ ensure
21
+ stop
22
+ end
23
+ end
24
+
25
+ def next_event(timeout: nil)
26
+ events.pop(timeout:)&.tap do |event|
27
+ running.delete(event.name) unless event.interrupt?
28
+ end
29
+ end
30
+
31
+ def start(name)
32
+ return if running.key?(name)
33
+
34
+ children.fetch(name).start
35
+ running[name] = true
36
+ end
37
+
38
+ def terminate(name)
39
+ children.fetch(name).signal("TERM") if running.key?(name)
40
+ end
41
+
42
+ private
43
+
44
+ SIGNALS = %w[INT TERM HUP].freeze
45
+ KILL_TIMEOUT = 5
46
+ private_constant :SIGNALS, :KILL_TIMEOUT
47
+
48
+ attr_reader :front_end, :output, :stop_timeout, :events, :children, :running
49
+
50
+ def build_child(process)
51
+ Child.new(process:, launch: front_end.launch_for(process), events:)
52
+ end
53
+
54
+ def with_signal_traps
55
+ previous_handlers = SIGNALS.to_h { |signal| [signal, trap(signal) { events.push(SignalReceived.new(signal:)) }] }
56
+ yield
57
+ ensure
58
+ previous_handlers&.each { |signal, handler| trap(signal, handler || "DEFAULT") }
59
+ end
60
+
61
+ def receive(timeout: nil)
62
+ next_event(timeout:)&.tap { |event| event.announce(output) }
63
+ end
64
+
65
+ def stop
66
+ unless running.empty?
67
+ output.notice("Stopping all processes.")
68
+ signal_children("TERM")
69
+ kill_children unless wait_for_children(timeout: stop_timeout)
70
+ end
71
+
72
+ children.each_value(&:finish)
73
+ end
74
+
75
+ def wait_for_children(timeout:)
76
+ deadline = clock + timeout
77
+
78
+ until running.empty?
79
+ event = receive(timeout: (deadline - clock).clamp(0, timeout))
80
+ return false if event.nil? || event.interrupt?
81
+ end
82
+
83
+ true
84
+ end
85
+
86
+ def kill_children
87
+ output.notice("Killing the processes that are still running.")
88
+ signal_children("KILL")
89
+ return if wait_for_children(timeout: KILL_TIMEOUT)
90
+
91
+ output.notice("Stopped waiting for #{running.keys.join(", ")}.")
92
+ end
93
+
94
+ def signal_children(signal_name)
95
+ children.each_value { |child| child.signal(signal_name) }
96
+ end
97
+
98
+ def clock
99
+ Process.clock_gettime(Process::CLOCK_MONOTONIC)
100
+ end
101
+ end
102
+ end
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hostty
4
+ class Output
5
+ def initialize(names:, io:)
6
+ @io = io
7
+ @width = [*names, NOTICE_NAME].map(&:length).max
8
+ @colors = names.each_with_index.to_h { |name, index| [name, COLORS[index % COLORS.size]] }
9
+ @mutex = Mutex.new
10
+ end
11
+
12
+ def line(name, text)
13
+ prefix = "#{Time.now.strftime("%H:%M:%S")} #{name.ljust(width)} |"
14
+
15
+ mutex.synchronize { io.puts("#{colorize(prefix, colors.fetch(name, NOTICE_COLOR))} #{text}") }
16
+ end
17
+
18
+ def notice(text)
19
+ line(NOTICE_NAME, text)
20
+ end
21
+
22
+ private
23
+
24
+ COLORS = [36, 33, 32, 35, 34, 31].freeze
25
+ NOTICE_NAME = "hostty"
26
+ NOTICE_COLOR = 37
27
+ private_constant :COLORS, :NOTICE_NAME, :NOTICE_COLOR
28
+
29
+ attr_reader :io, :width, :colors, :mutex
30
+
31
+ def colorize(text, color)
32
+ return text unless io.tty?
33
+
34
+ "\e[#{color}m#{text}\e[0m"
35
+ end
36
+ end
37
+ end
@@ -0,0 +1,23 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hostty
4
+ module Plain
5
+ class App
6
+ def initialize(output:)
7
+ @output = output
8
+ end
9
+
10
+ def launch_for(_process)
11
+ Pipe.new(output:)
12
+ end
13
+
14
+ def wait(manager)
15
+ manager.next_event.tap { |event| event.announce(output) }.exit_code
16
+ end
17
+
18
+ private
19
+
20
+ attr_reader :output
21
+ end
22
+ end
23
+ end
@@ -0,0 +1,55 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hostty
4
+ module Plain
5
+ class Pipe
6
+ def initialize(output:)
7
+ @output = output
8
+ end
9
+
10
+ def start(process)
11
+ reader, writer = IO.pipe
12
+ pid = spawn_process(process, writer)
13
+ @reader_thread = Thread.new { forward(process.name, reader) }
14
+ pid
15
+ ensure
16
+ writer&.close
17
+ end
18
+
19
+ def finish
20
+ return unless reader_thread
21
+
22
+ reader_thread.kill unless reader_thread.join(OUTPUT_TIMEOUT)
23
+ end
24
+
25
+ private
26
+
27
+ OUTPUT_TIMEOUT = 2
28
+ private_constant :OUTPUT_TIMEOUT
29
+
30
+ attr_reader :output, :reader_thread
31
+
32
+ def spawn_process(process, writer)
33
+ Process.spawn(
34
+ process.environment,
35
+ "/bin/sh",
36
+ "-c",
37
+ process.command,
38
+ chdir: process.root_path,
39
+ pgroup: true,
40
+ in: File::NULL,
41
+ out: writer,
42
+ err: writer
43
+ )
44
+ end
45
+
46
+ def forward(name, reader)
47
+ reader.each_line { |text| output.line(name, text.chomp) }
48
+ rescue IOError
49
+ nil
50
+ ensure
51
+ reader.close
52
+ end
53
+ end
54
+ end
55
+ end
@@ -0,0 +1,62 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hostty
4
+ class PortChooser
5
+ attr_reader :notices
6
+
7
+ def initialize(skip_used_ports:, random: Random.new)
8
+ @skip_used_ports = skip_used_ports
9
+ @random = random
10
+ @owners = {}
11
+ @notices = []
12
+ end
13
+
14
+ def choose(name:, preferred:, movable:)
15
+ port = (preferred == "random") ? random.rand(RANDOM_PORTS) : preferred
16
+ return claim(port, name) if free?(port)
17
+ raise ConfigError, clash_message(name, port, movable:) unless movable && skip_used_ports
18
+
19
+ moved = next_free_port(name, port)
20
+ notices << "Port #{port} for #{name} is in use. #{name} uses #{moved}."
21
+ claim(moved, name)
22
+ end
23
+
24
+ private
25
+
26
+ RANDOM_PORTS = 10_000..59_999
27
+ MOVE_LIMIT = 100
28
+ private_constant :RANDOM_PORTS, :MOVE_LIMIT
29
+
30
+ attr_reader :skip_used_ports, :random, :owners
31
+
32
+ def free?(port)
33
+ !owners.key?(port) && !in_use?(port)
34
+ end
35
+
36
+ def in_use?(port)
37
+ TCPServer.new("0.0.0.0", port).close
38
+ false
39
+ rescue Errno::EADDRINUSE, Errno::EACCES
40
+ true
41
+ end
42
+
43
+ def claim(port, name)
44
+ owners[port] = name
45
+ port
46
+ end
47
+
48
+ def next_free_port(name, port)
49
+ candidates = (port + 1)..(port + MOVE_LIMIT)
50
+ candidates.find { |candidate| free?(candidate) } or
51
+ raise ConfigError, "No free port for #{name} from #{candidates.begin} to #{candidates.end}."
52
+ end
53
+
54
+ def clash_message(name, port, movable:)
55
+ owner = owners[port]
56
+ return "#{owner} and #{name} both use port #{port}." if owner
57
+ return "Port #{port} for #{name} is in use. Free the port, or change the port of #{name}." unless movable
58
+
59
+ "Port #{port} for #{name} is in use. Free the port, or set skip_used_ports to true."
60
+ end
61
+ end
62
+ end
@@ -0,0 +1,80 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hostty
4
+ class ProcessConfig
5
+ attr_reader :name, :root_path
6
+
7
+ def initialize(name:, settings:, default_port:, root_path:, ports:)
8
+ @name = name
9
+ @settings = settings.is_a?(String) ? {"command" => settings} : settings
10
+ @default_port = default_port
11
+ @root_path = root_path
12
+ @ports = ports
13
+ validate!
14
+ end
15
+
16
+ def fixed_port? = settings["port"].is_a?(Integer)
17
+
18
+ def command = settings["command"]
19
+
20
+ def domain = settings["domain"]
21
+
22
+ def port
23
+ @port ||= ports.choose(name:, preferred: settings.fetch("port", default_port), movable: !fixed_port?)
24
+ end
25
+
26
+ def url
27
+ return unless domain
28
+
29
+ "#{settings["ssl"] ? "https" : "http"}://#{domain}"
30
+ end
31
+
32
+ def environment
33
+ settings
34
+ .fetch("env", {})
35
+ .to_h { |key, value| [key.to_s, value.to_s] }
36
+ .merge("PORT" => port.to_s)
37
+ end
38
+
39
+ def nginx_options
40
+ {
41
+ domain:,
42
+ port:,
43
+ root_path:,
44
+ host: settings["host"],
45
+ ssl: settings["ssl"],
46
+ log: settings["log"]
47
+ }.compact
48
+ end
49
+
50
+ private
51
+
52
+ KEYS = %w[command domain port ssl log host env].freeze
53
+ PORTS = 1..65_535
54
+ private_constant :KEYS, :PORTS
55
+
56
+ attr_reader :settings, :default_port, :ports
57
+
58
+ def validate!
59
+ raise ConfigError, "#{name} must be a command or a list of settings." unless settings.is_a?(Hash)
60
+ raise ConfigError, "#{name} has unknown settings: #{unknown_keys.join(", ")}." if unknown_keys.any?
61
+ raise ConfigError, "#{name} needs a command." unless present_string?(command)
62
+ raise ConfigError, "#{name} needs a port from 1 to 65535, or \"random\"." unless valid_port_setting?
63
+ raise ConfigError, "#{name} needs a text value for its domain." unless domain.nil? || present_string?(domain)
64
+ raise ConfigError, "#{name} needs a list of names and values for its env." unless settings.fetch("env", {}).is_a?(Hash)
65
+ end
66
+
67
+ def unknown_keys
68
+ settings.keys.map(&:to_s) - KEYS
69
+ end
70
+
71
+ def valid_port_setting?
72
+ requested = settings.fetch("port", default_port)
73
+ requested == "random" || (requested.is_a?(Integer) && PORTS.cover?(requested))
74
+ end
75
+
76
+ def present_string?(value)
77
+ value.is_a?(String) && !value.strip.empty?
78
+ end
79
+ end
80
+ end
@@ -0,0 +1,35 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hostty
4
+ module Terminal
5
+ class Cell
6
+ attr_reader :char, :style, :width
7
+
8
+ def initialize(char:, style:, width: 1)
9
+ @char = char
10
+ @style = style
11
+ @width = width
12
+ freeze
13
+ end
14
+
15
+ def continuation? = width.zero?
16
+
17
+ def wide?
18
+ width == 2
19
+ end
20
+
21
+ def self.blank(style = Style.plain)
22
+ blanks[style.bg] ||= new(char: " ", style: style.background)
23
+ end
24
+
25
+ def self.blanks
26
+ @blanks ||= {}
27
+ end
28
+ private_class_method :blanks
29
+
30
+ def self.continuation(style)
31
+ new(char: "", style:, width: 0)
32
+ end
33
+ end
34
+ end
35
+ end
@@ -0,0 +1,87 @@
1
+ # frozen_string_literal: true
2
+
3
+ module Hostty
4
+ module Terminal
5
+ class Style
6
+ ATTRIBUTES = %i[fg bg bold dim italic underline blink inverse invisible strikethrough overline].freeze
7
+
8
+ attr_reader(*ATTRIBUTES)
9
+
10
+ def initialize(fg: nil, bg: nil, **flags)
11
+ @fg = fg
12
+ @bg = bg
13
+ FLAG_CODES.each_key { |flag| instance_variable_set(:"@#{flag}", flags.fetch(flag, false)) }
14
+ freeze
15
+ end
16
+
17
+ def ==(other)
18
+ other.is_a?(Style) && to_h == other.to_h
19
+ end
20
+
21
+ alias_method :eql?, :==
22
+
23
+ def hash = to_h.hash
24
+
25
+ def to_h
26
+ ATTRIBUTES.to_h { |attribute| [attribute, public_send(attribute)] }
27
+ end
28
+
29
+ def background
30
+ self.class.shared(Style.plain.to_h.merge(bg:))
31
+ end
32
+
33
+ def sgr
34
+ codes = [0]
35
+ FLAG_CODES.each { |flag, code| codes << code if public_send(flag) }
36
+ codes.concat(color_codes(fg, base: 30, extended: 38))
37
+ codes.concat(color_codes(bg, base: 40, extended: 48))
38
+ "\e[#{codes.join(";")}m"
39
+ end
40
+
41
+ def self.plain
42
+ @plain ||= new
43
+ end
44
+
45
+ def self.shared(attributes)
46
+ styles.clear if styles.size > SHARED_LIMIT
47
+ styles[attributes.values] ||= new(**attributes)
48
+ end
49
+
50
+ def self.styles
51
+ @styles ||= {plain.to_h.values => plain}
52
+ end
53
+ private_class_method :styles
54
+
55
+ private
56
+
57
+ FLAG_CODES = {
58
+ bold: 1,
59
+ dim: 2,
60
+ italic: 3,
61
+ underline: 4,
62
+ blink: 5,
63
+ inverse: 7,
64
+ invisible: 8,
65
+ strikethrough: 9,
66
+ overline: 53
67
+ }.freeze
68
+ SHARED_LIMIT = 1024
69
+ private_constant :FLAG_CODES, :SHARED_LIMIT
70
+
71
+ def color_codes(color, base:, extended:)
72
+ case color
73
+ when nil
74
+ []
75
+ when 0..7
76
+ [base + color]
77
+ when 8..15
78
+ [base + 52 + color]
79
+ when Integer
80
+ [extended, 5, color]
81
+ else
82
+ [extended, 2, *color]
83
+ end
84
+ end
85
+ end
86
+ end
87
+ end