libtmux 0.1.0.alpha.1 → 0.1.0.alpha.3

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.
@@ -147,6 +147,10 @@ module LibTmux
147
147
  end
148
148
 
149
149
  class LogSocketReadiness
150
+ # tmux 3.8 parses more default key bindings under -v than 3.7, so the
151
+ # log reaches "server loop enter" past 1 MiB.
152
+ LIMIT = 1 << 22
153
+
150
154
  attr_reader :bytes_read
151
155
 
152
156
  def initialize(directory)
@@ -184,7 +188,7 @@ module LibTmux
184
188
  end
185
189
  while (bytes = @log.read(16_384))
186
190
  @bytes_read += bytes.bytesize
187
- if @bytes_read > 1 << 20
191
+ if @bytes_read > LIMIT
188
192
  raise CapacityError.new("owned startup log exceeded byte limit", phase: :startup, pid: child.pid, delivery: :possibly_sent)
189
193
  end
190
194
  text = @tail + bytes
@@ -0,0 +1,37 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "libtmux/errors"
4
+ require "libtmux/tmux_version"
5
+
6
+ module LibTmux
7
+ # Here "support" means which tmux releases libtmux supports, not helper
8
+ # code. Every server handle checks its daemon's release before any command
9
+ # runs; this module exposes that check.
10
+ module Support
11
+ # The version check: is this tmux daemon supported at all?
12
+ module Versions
13
+ MINIMUM = Internal::TmuxVersion::MINIMUM
14
+
15
+ module_function
16
+
17
+ # Returns the release text when +value+ is a supported release and
18
+ # raises UnsupportedFeatureError otherwise.
19
+ def require_supported(value)
20
+ Internal::TmuxVersion.require_supported(value).text
21
+ end
22
+
23
+ # The release text of the daemon behind +server+, such as "3.7d".
24
+ def server_version(server)
25
+ server.tmux_version
26
+ end
27
+ end
28
+
29
+ MINIMUM = Versions::MINIMUM
30
+
31
+ module_function
32
+
33
+ def require_supported(value) = Versions.require_supported(value)
34
+
35
+ def server_version(server) = Versions.server_version(server)
36
+ end
37
+ end
@@ -0,0 +1,219 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "libtmux/errors"
4
+ require "libtmux/capabilities"
5
+ require "securerandom"
6
+
7
+ module LibTmux
8
+ module Internal
9
+ # Version check for the tmux daemon a server handle is bound to.
10
+ #
11
+ # The connected daemon decides support, not the configured client. A
12
+ # release is read once through the retained route and every dispatch is
13
+ # then fenced against that exact text.
14
+ module TmuxVersion
15
+ MINIMUM = "3.3a"
16
+ CONTROL_MINIMUM = Internal::Capabilities.capability("control-mode").since
17
+ # The forms tmux has reported: X.Y, X.Y plus a patch letter, X.Y-rc and
18
+ # X.Y-rcN candidates, and next-X.Y development builds. Anything else,
19
+ # including master and openbsd-*, is not a release number. The candidate
20
+ # number is bounded so that it can never outrank the release it precedes.
21
+ PATTERN = /\A(?:next-(?<next_major>\d+)\.(?<next_minor>\d+)|(?<major>\d+)\.(?<minor>\d+)(?:(?<patch>[a-z])|(?<candidate>-rc(?<number>\d{1,3})?))?)\z/
22
+ # tmux resolves a command-alias entry by exact name before it resolves a
23
+ # command, so the full names run-shell and if-shell can be hijacked by
24
+ # one alias, as the existing link guards avoid with builtin_spellings.
25
+ # These unique prefixes are out of reach of such an entry, and
26
+ # verify_fence proves at the version check that the fence still rejects
27
+ # its marker, so an alias named for a prefix, or a future command
28
+ # sharing it, refuses the server instead of silently disabling the fence.
29
+ FENCE_COMMAND = "if-shel"
30
+ PROBE_COMMAND = "run-shel"
31
+ ABSENT = /no server running|error connecting to .*\((?:No such file or directory|Connection refused)\)/
32
+ # What a daemon older than 3.2 answers to the probe. run-shell gained -C
33
+ # in 3.2, so 3.1 reports its usage line; a 3.0 daemon speaks a protocol
34
+ # the client refuses before any command runs.
35
+ PRE_3_2_ANSWER = /\A(?:usage: run-shell \[-b\] \[-t target-pane\] shell-command|server version is too old for client)\n\z/
36
+ REMEDY = "upgrade tmux and restart the server, or pin the previous libtmux release"
37
+ # Marks the refusals that mean "this daemon is not the supported one",
38
+ # so a caller that cannot show the full message can still name the floor.
39
+ EXPECTED = "tmux #{MINIMUM} or newer"
40
+
41
+ # `key` orders releases: major, minor, patch letter, then the build kind.
42
+ # A development build or release candidate ranks below the release it
43
+ # names: next-X.Y < X.Y-rc < X.Y-rcN < X.Y < X.Ya.
44
+ STABLE = 1 << 20
45
+
46
+ Release = Data.define(:text, :key) do
47
+ include Comparable
48
+
49
+ # True only for the numbered release itself, not a patch letter,
50
+ # candidate or development build of it.
51
+ def exactly?(major, minor)
52
+ key == [major, minor, 0, STABLE]
53
+ end
54
+
55
+ def <=>(other)
56
+ key <=> other.key if other.is_a?(Release)
57
+ end
58
+ end
59
+
60
+ module_function
61
+
62
+ def parse(value, source: "the daemon")
63
+ match = value.is_a?(String) && PATTERN.match(value)
64
+ raise unrecognized(value, source) unless match
65
+
66
+ key = if match[:next_major]
67
+ [match[:next_major].to_i, match[:next_minor].to_i, 0, 0]
68
+ else
69
+ patch = match[:patch] ? match[:patch].ord - 96 : 0
70
+ kind = match[:candidate] ? 1 + match[:number].to_i : STABLE
71
+ [match[:major].to_i, match[:minor].to_i, patch, kind]
72
+ end
73
+ Release.new(text: value.dup.freeze, key: key.freeze)
74
+ end
75
+
76
+ def require_supported(value, minimum: MINIMUM, source: "the daemon")
77
+ release = parse(value, source: source)
78
+ if (release <=> parse(minimum)).negative?
79
+ reported = (source == "the daemon") ? "connected version" : "#{source} reports"
80
+ raise UnsupportedFeatureError.new("tmux #{minimum} or newer is required (#{reported} #{release.text}); #{REMEDY}",
81
+ phase: :admission, expected: EXPECTED)
82
+ end
83
+ release
84
+ end
85
+
86
+ # Names what the daemon or executable reported, shortened so that a
87
+ # long report cannot flood the message.
88
+ def unrecognized(value, source)
89
+ shown = value.is_a?(String) ? value.byteslice(0, 64).scrub("?") : value
90
+ UnsupportedFeatureError.new("tmux version is unrecognized (#{source} reports #{shown.inspect}); " \
91
+ "tmux #{MINIMUM} or newer is required; #{REMEDY}", phase: :admission, expected: EXPECTED)
92
+ end
93
+
94
+ # Control clients crash tmux servers older than 3.7 while they attach.
95
+ def require_control(release)
96
+ raise UnsupportedFeatureError.new("control connections need a version-checked server", phase: :admission) unless release
97
+
98
+ return release unless (release <=> parse(CONTROL_MINIMUM)).negative?
99
+
100
+ raise UnsupportedFeatureError.new("control connections require tmux #{CONTROL_MINIMUM} or newer " \
101
+ "(connected version #{release.text})", phase: :admission, delivery: :not_sent)
102
+ end
103
+
104
+ # Reads the daemon behind +prefix+. The block runs the argv and returns a
105
+ # CommandResult. Unlike display-message, run-shell -C fires no after-*
106
+ # hook and no command-error hook: tmux parses the expanded string after
107
+ # the command returned, so the private unknown command it reports has no
108
+ # effect and the version travels in the error text.
109
+ def connected(prefix)
110
+ marker = "libtmux-version-#{SecureRandom.hex(16)}-"
111
+ result = yield(prefix + [PROBE_COMMAND, "-C", marker + '#{version}'])
112
+ stderr = result.stderr.dup.force_encoding(Encoding::UTF_8)
113
+ if !result.success? && result.stdout.empty? && (match = /\Aunknown command: #{Regexp.escape(marker)}([^\n]+)\n\z/.match(stderr))
114
+ return require_supported(match[1])
115
+ end
116
+ if !result.success? && result.stdout.empty? && PRE_3_2_ANSWER.match?(stderr)
117
+ raise UnsupportedFeatureError.new("tmux #{MINIMUM} or newer is required (the connected daemon is older than 3.2); #{REMEDY}",
118
+ phase: :admission, expected: EXPECTED)
119
+ end
120
+ if ABSENT.match?(stderr)
121
+ raise TargetNotFoundError.new("no tmux server is running at the selected endpoint", phase: :admission)
122
+ end
123
+
124
+ raise CommandError.new("tmux did not report a daemon version", result: result, phase: :admission, delivery: :not_sent)
125
+ rescue Error => error
126
+ raise not_sent(error), cause: nil
127
+ end
128
+
129
+ # Reads the executable that would start a new daemon. tmux 3.3a prints
130
+ # its own file name where later releases print "tmux".
131
+ def executable(path)
132
+ result = yield([path, "-V"])
133
+ unless result.success?
134
+ raise CommandError.new("tmux did not report its version", result: result, phase: :admission, delivery: :not_sent)
135
+ end
136
+
137
+ names = ["tmux", File.basename(path)].uniq.map { |name| Regexp.escape(name.b) }.join("|")
138
+ match = /\A(?:#{names}) ([^\n]+)\n?\z/n.match(result.stdout)
139
+ raise unrecognized(result.stdout.strip, "the executable") unless match
140
+
141
+ require_supported(match[1], source: "the executable")
142
+ rescue Error => error
143
+ raise not_sent(error), cause: nil
144
+ end
145
+
146
+ # Reads daemon values through the probe's error text, which fires no
147
+ # hook, unlike display-message. tmux reports only the first unparseable
148
+ # word, so the fields expand to one comma-joined word.
149
+ def identity_argv(fields)
150
+ marker = "libtmux-identity-#{SecureRandom.hex(16)}-"
151
+ [[PROBE_COMMAND, "-C", marker + fields.map { |field| "\#{#{field}}" }.join(",")], marker]
152
+ end
153
+
154
+ def parse_identity(result, marker, count)
155
+ stderr = result.stderr.b
156
+ match = /\Aunknown command: #{Regexp.escape(marker.b)}([^\n ]*)\n\z/n.match(stderr)
157
+ values = match && match[1].split(",", -1)
158
+ unless !result.success? && result.stdout.empty? && values && values.length == count
159
+ raise ProtocolError.new("tmux did not report the requested identity", delivery: :observed, phase: :decode)
160
+ end
161
+
162
+ values.map(&:freeze)
163
+ end
164
+
165
+ # Leading commands for one dispatch. A daemon that no longer reports
166
+ # +release+ fails the if-shell condition and reaches the private marker,
167
+ # which tmux rejects before the commands that follow.
168
+ def fence(release)
169
+ marker = "libtmux-version-#{SecureRandom.hex(16)}"
170
+ [[FENCE_COMMAND, "-F", "\#{==:\#{version},#{release.text}}", "", marker, ";"], marker]
171
+ end
172
+
173
+ # A server runs this once when it is checked. A true if-shell condition
174
+ # runs a probe whose parse error names the marker, which shows that the
175
+ # fence command and the probe command are the real ones, with no failing
176
+ # command and so no hook.
177
+ def fence_check_argv
178
+ marker = "libtmux-fence-#{SecureRandom.hex(16)}"
179
+ [[FENCE_COMMAND, "-F", "1", "#{PROBE_COMMAND} -C #{marker}"], marker]
180
+ end
181
+
182
+ def verify_fence(result, marker)
183
+ return result if !result.success? && result.stdout.empty? && result.stderr.b == "unknown command: #{marker}\n".b
184
+
185
+ raise UnsupportedFeatureError.new("a tmux command-alias or command shadows #{FENCE_COMMAND} or " \
186
+ "#{PROBE_COMMAND}; remove it before opening a server", phase: :admission, delivery: :not_sent)
187
+ end
188
+
189
+ def check_fence(result, marker, release)
190
+ return result unless result.stderr.b.each_line.any? { |line| line.chomp == "unknown command: #{marker}".b }
191
+
192
+ raise UnsupportedFeatureError.new("the tmux daemon no longer reports #{release.text}; reopen the server",
193
+ phase: :admission, delivery: :not_sent, expected: EXPECTED)
194
+ end
195
+
196
+ # True for the refusals that name the supported floor: an unsupported,
197
+ # unreadable or changed daemon. Other unsupported features are not.
198
+ def refusal?(error)
199
+ error.is_a?(UnsupportedFeatureError) && error.expected == EXPECTED
200
+ end
201
+
202
+ # A fixed line for results that cannot carry the full message.
203
+ def hint
204
+ "tmux #{MINIMUM} or newer is required, and the daemon must stay the " \
205
+ "one the server opened with; upgrade tmux and restart the server"
206
+ end
207
+
208
+ # A failed probe sent nothing, whatever stage it failed in, so only the
209
+ # delivery is forced; the error keeps its own phase.
210
+ def not_sent(error)
211
+ details = {delivery: :not_sent, phase: error.phase, pid: error.pid, cleanup_errors: error.cleanup_errors,
212
+ expected: error.expected}
213
+ return error.class.new(error.message, result: error.result, **details) if error.is_a?(CommandError)
214
+
215
+ error.class.new(error.message, **details)
216
+ end
217
+ end
218
+ end
219
+ end
@@ -1,5 +1,5 @@
1
1
  # frozen_string_literal: true
2
2
 
3
3
  module LibTmux
4
- VERSION = "0.1.0.alpha.1"
4
+ VERSION = "0.1.0.alpha.3"
5
5
  end
data/lib/libtmux.rb CHANGED
@@ -8,6 +8,7 @@ require "libtmux/metadata"
8
8
  require "libtmux/selection"
9
9
  require "libtmux/entity"
10
10
  require "libtmux/server"
11
+ require "libtmux/support"
11
12
  require "libtmux/operations"
12
13
  require "libtmux/criteria"
13
14
  require "libtmux/capture"
data/schema/where-v1.json CHANGED
@@ -994,9 +994,16 @@
994
994
  "pane.pid": {
995
995
  "anyOf": [
996
996
  {
997
- "type": "integer",
998
- "minimum": 0,
999
- "maximum": 2147483647
997
+ "anyOf": [
998
+ {
999
+ "type": "integer",
1000
+ "minimum": 0,
1001
+ "maximum": 2147483647
1002
+ },
1003
+ {
1004
+ "type": "null"
1005
+ }
1006
+ ]
1000
1007
  },
1001
1008
  {
1002
1009
  "type": "object",
@@ -1004,9 +1011,16 @@
1004
1011
  "additionalProperties": false,
1005
1012
  "properties": {
1006
1013
  "equals": {
1007
- "type": "integer",
1008
- "minimum": 0,
1009
- "maximum": 2147483647
1014
+ "anyOf": [
1015
+ {
1016
+ "type": "integer",
1017
+ "minimum": 0,
1018
+ "maximum": 2147483647
1019
+ },
1020
+ {
1021
+ "type": "null"
1022
+ }
1023
+ ]
1010
1024
  },
1011
1025
  "not": {
1012
1026
  "$ref": "#/$defs/pane.pid"
@@ -1015,9 +1029,16 @@
1015
1029
  "type": "array",
1016
1030
  "maxItems": 1024,
1017
1031
  "items": {
1018
- "type": "integer",
1019
- "minimum": 0,
1020
- "maximum": 2147483647
1032
+ "anyOf": [
1033
+ {
1034
+ "type": "integer",
1035
+ "minimum": 0,
1036
+ "maximum": 2147483647
1037
+ },
1038
+ {
1039
+ "type": "null"
1040
+ }
1041
+ ]
1021
1042
  }
1022
1043
  },
1023
1044
  "lt": {
data/sig/fields.rbs CHANGED
@@ -28,7 +28,7 @@ module LibTmux
28
28
  def id: () -> String
29
29
  def window_id: () -> String
30
30
  def index: () -> Integer
31
- def pid: () -> Integer
31
+ def pid: () -> Integer?
32
32
  def current_command: () -> String
33
33
  def current_path: () -> String?
34
34
  def title: () -> String
data/sig/libtmux.rbs CHANGED
@@ -139,11 +139,25 @@ module LibTmux
139
139
  def send_keys: (*String, ?timeout: Numeric, ?cancel: untyped) -> CommandResult
140
140
  end
141
141
 
142
+ module Support
143
+ MINIMUM: String
144
+ def self.require_supported: (String value) -> String
145
+ def self.server_version: (Server server) -> String
146
+
147
+ module Versions
148
+ MINIMUM: String
149
+ def self.require_supported: (String value) -> String
150
+ def self.server_version: (Server server) -> String
151
+ end
152
+ end
153
+
142
154
  class Server
143
155
  attr_reader endpoint: Endpoint
144
- def self.open: [T] (?endpoint: Endpoint?, ?socket_path: String?, ?socket_name: String?, ?executable: String, ?max_requests: Integer, ?max_controls: Integer, ?close_timeout: Numeric) { (Server) -> T } -> T
145
- | (?endpoint: Endpoint?, ?socket_path: String?, ?socket_name: String?, ?executable: String, ?max_requests: Integer, ?max_controls: Integer, ?close_timeout: Numeric) -> Server
146
- def initialize: (?endpoint: Endpoint?, ?socket_path: String?, ?socket_name: String?, ?executable: String, ?max_requests: Integer, ?max_controls: Integer, ?close_timeout: Numeric) -> void
156
+ def self.open: [T] (?endpoint: Endpoint?, ?socket_path: String?, ?socket_name: String?, ?executable: String, ?max_requests: Integer, ?max_controls: Integer, ?close_timeout: Numeric, ?version_timeout: Numeric) { (Server) -> T } -> T
157
+ | (?endpoint: Endpoint?, ?socket_path: String?, ?socket_name: String?, ?executable: String, ?max_requests: Integer, ?max_controls: Integer, ?close_timeout: Numeric, ?version_timeout: Numeric) -> Server
158
+ def initialize: (?endpoint: Endpoint?, ?socket_path: String?, ?socket_name: String?, ?executable: String, ?max_requests: Integer, ?max_controls: Integer, ?close_timeout: Numeric, ?version_timeout: Numeric) -> void
159
+ def tmux_version: () -> String
160
+ def supports_control?: () -> bool
147
161
  def run: (Array[String], ?input: String, ?timeout: Numeric, ?cancel: untyped) -> CommandResult
148
162
  def close: () -> nil
149
163
  def diagnostics: () -> {admitted_requests: Integer, reserved_process_slots: Integer, control_connections: Integer, closed: bool, limits: {max_requests: Integer, max_controls: Integer, close_timeout: Numeric}}
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: libtmux
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.0.alpha.1
4
+ version: 0.1.0.alpha.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - libtmux contributors
@@ -87,7 +87,10 @@ extra_rdoc_files: []
87
87
  files:
88
88
  - LICENSE
89
89
  - README.md
90
+ - assets/logo.png
91
+ - assets/logo.svg
90
92
  - lib/libtmux.rb
93
+ - lib/libtmux/capabilities.rb
91
94
  - lib/libtmux/capture.rb
92
95
  - lib/libtmux/catalog.rb
93
96
  - lib/libtmux/child.rb
@@ -98,6 +101,7 @@ files:
98
101
  - lib/libtmux/errors.rb
99
102
  - lib/libtmux/group.rb
100
103
  - lib/libtmux/metadata.rb
104
+ - lib/libtmux/native_guard.rb
101
105
  - lib/libtmux/operations.rb
102
106
  - lib/libtmux/options.rb
103
107
  - lib/libtmux/owned.rb
@@ -108,7 +112,9 @@ files:
108
112
  - lib/libtmux/snapshot.rb
109
113
  - lib/libtmux/socket_readiness.rb
110
114
  - lib/libtmux/source_query.rb
115
+ - lib/libtmux/support.rb
111
116
  - lib/libtmux/terminal.rb
117
+ - lib/libtmux/tmux_version.rb
112
118
  - lib/libtmux/version.rb
113
119
  - schema/where-v1.json
114
120
  - sig/control.rbs
@@ -125,6 +131,7 @@ licenses:
125
131
  - MIT
126
132
  metadata:
127
133
  source_code_uri: https://github.com/libtmux/libtmux-ruby
134
+ documentation_uri: https://libtmux.org/en/ruby/v0.1.0.alpha.3/
128
135
  rdoc_options: []
129
136
  require_paths:
130
137
  - lib