libtmux 0.1.2 → 0.1.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.
@@ -3,6 +3,86 @@
3
3
  require "json"
4
4
 
5
5
  module LibTmux
6
+ # Immutable feature decisions observed through one bound daemon.
7
+ # Obtain a report with {Server#capabilities}; it remains a historical
8
+ # observation after that server changes or closes, never mutation authority.
9
+ class Capabilities
10
+ # A native command and the flags a feature depends on.
11
+ Command = Data.define(:command, :flags) do
12
+ def to_h = super.transform_keys(&:to_s)
13
+ private_class_method :new, :[]
14
+ end
15
+
16
+ # A library feature, its requirement, and this daemon's support decision.
17
+ Feature = Data.define(:id, :name, :summary, :since, :handling, :error,
18
+ :apis, :commands, :except, :supported) do
19
+ def supported? = supported
20
+
21
+ def to_h
22
+ super.transform_keys(&:to_s).merge("handling" => handling.to_s,
23
+ "commands" => commands.map(&:to_h))
24
+ end
25
+
26
+ private_class_method :new, :[]
27
+ end
28
+
29
+ # A release-specific repair or refusal, including whether it applies here.
30
+ Workaround = Data.define(:id, :name, :summary, :releases, :handling,
31
+ :apis, :commands, :active) do
32
+ def active? = active
33
+
34
+ def to_h
35
+ super.transform_keys(&:to_s).merge("handling" => handling.to_s,
36
+ "commands" => commands.map(&:to_h))
37
+ end
38
+
39
+ private_class_method :new, :[]
40
+ end
41
+
42
+ attr_reader :binding_key, :pid, :start_time, :version, :features, :workarounds
43
+
44
+ def initialize(binding_key:, pid:, start_time:, release:)
45
+ @binding_key, @pid, @start_time = binding_key.dup.freeze, pid, start_time
46
+ @version = release.text.dup.freeze
47
+ @features = Internal::Capabilities::CAPABILITIES.map do |entry|
48
+ Feature.__send__(:new, **entry.to_h.transform_keys(&:to_sym),
49
+ error: entry.error, except: entry.except, handling: entry.handling,
50
+ commands: commands(entry.commands), supported: entry.available?(release))
51
+ end.freeze
52
+ @workarounds = Internal::Capabilities::WORKAROUNDS.map do |entry|
53
+ Workaround.__send__(:new, **entry.to_h.transform_keys(&:to_sym),
54
+ handling: entry.handling, commands: commands(entry.commands), active: entry.applies?(release))
55
+ end.freeze
56
+ freeze
57
+ end
58
+ private_class_method :new
59
+
60
+ # The feature named by the registry. An unknown id raises KeyError.
61
+ def feature(id)
62
+ features.find { |entry| entry.id == id } || raise(KeyError, "no capability #{id}")
63
+ end
64
+
65
+ def supports?(id) = feature(id).supported?
66
+
67
+ # The workaround named by the registry. An unknown id raises KeyError.
68
+ def workaround(id)
69
+ workarounds.find { |entry| entry.id == id } || raise(KeyError, "no workaround #{id}")
70
+ end
71
+
72
+ # A fresh JSON-compatible projection; its containers do not own the report.
73
+ def to_h
74
+ {"server_identity" => {"generation" => binding_key, "pid" => pid,
75
+ "start_time" => start_time, "tmux_version" => version},
76
+ "features" => features.map(&:to_h), "workarounds" => workarounds.map(&:to_h)}
77
+ end
78
+
79
+ private
80
+
81
+ def commands(entries)
82
+ entries.map { |entry| Command.__send__(:new, command: entry.command, flags: entry.flags) }.freeze
83
+ end
84
+ end
85
+
6
86
  module Internal
7
87
  # What the connected tmux daemon can do, in one table. Each gate in the
8
88
  # library reads its release floor or affected releases from here, and
@@ -79,10 +159,14 @@ module LibTmux
79
159
  CONTROL_CHANNELS = %w[LibTmux::ControlConnection#exchange LibTmux::Async::ControlConnection#exchange].freeze
80
160
 
81
161
  CAPABILITIES = [
162
+ Capability.new(id: "owned-terminal-program", name: "Owned terminal program", since: "3.7",
163
+ handling: :refuse, error: ERROR,
164
+ summary: "Run literal argv in an owned terminal and retain its final screen on the bound daemon.",
165
+ apis: ["LibTmux.start_program", "LibTmux.run_program", "LibTmux::Server#start_program", "LibTmux::Async.start_program", "LibTmux::Async.run_program"],
166
+ commands: []),
82
167
  Capability.new(id: "control-mode", name: "Control mode", since: "3.7", handling: :refuse, error: ERROR,
83
168
  summary: "Attach a library-managed control client and stream events.",
84
- apis: ["LibTmux::Server#open_control", "LibTmux::Async::Server#open_control",
85
- "LibTmux::ControlConnection.open", "LibTmux::ControlConnection.new"],
169
+ apis: ["LibTmux::Server#open_control", "LibTmux::Async::Server#open_control"],
86
170
  commands: [Command.new(command: "attach-session")]),
87
171
  Capability.new(id: "buffer-delete-named", name: "Named buffer deletion", since: "3.4", handling: :refuse, error: ERROR,
88
172
  summary: "Delete one paste buffer by name without risking another buffer.",
@@ -48,32 +48,27 @@ module LibTmux
48
48
  private
49
49
 
50
50
  def acquire_server_info(budget)
51
- version, pid, start_time = read_identity(budget)
52
- version = version.dup.force_encoding(Encoding::UTF_8)
51
+ identity = read_identity(budget)
53
52
  begin
54
- TmuxVersion.require_supported(version)
53
+ TmuxVersion.require_supported(identity.fetch(:version))
55
54
  rescue UnsupportedFeatureError => error
56
55
  raise UnsupportedFeatureError.new(error.message, delivery: :observed, phase: :capture)
57
56
  end
58
- unless pid.match?(/\A[1-9][0-9]{0,9}\z/n) && start_time.match?(/\A-?[0-9]{1,19}\z/n)
59
- raise ProtocolError.new("invalid capture server identity metadata", delivery: :observed, phase: :capture)
60
- end
61
- {version: version, pid: Integer(pid, 10), start_time: Integer(start_time, 10),
62
- capabilities: {metadata: :byte_counted, field_baseline: TmuxVersion::MINIMUM}}
57
+ identity.merge(capabilities: {metadata: :byte_counted, field_baseline: TmuxVersion::MINIMUM})
63
58
  end
64
59
 
65
60
  # The identity travels in the probe's error text so that no user hook fires.
66
61
  def read_identity(budget)
67
62
  started_at = monotonic
68
63
  remaining = check_budget(budget)
69
- argv, marker = TmuxVersion.identity_argv(%w[version pid start_time])
64
+ argv, marker = TmuxVersion.daemon_identity_argv
70
65
  result = @server.run(argv, timeout: remaining, cancel: budget.fetch(:cancel))
71
- values = TmuxVersion.parse_identity(result, marker, 3)
66
+ identity = TmuxVersion.parse_daemon_identity(result, marker, phase: :capture)
72
67
  budget[:bytes] += result.stderr.bytesize
73
68
  budget[:rows] += 1
74
69
  budget.fetch(:reads) << {source: :server, attempt: 1, started_at: started_at, finished_at: monotonic,
75
70
  bytes: result.stderr.bytesize, rows: 1}
76
- values
71
+ identity
77
72
  end
78
73
 
79
74
  def acquire_rows(budget, attempt, clients)
@@ -375,6 +375,9 @@ module LibTmux
375
375
  nil
376
376
  end
377
377
  private_class_method :attach_cleanup_details
378
+ # Server#open_control supplies the binding and session ID and accounts for
379
+ # the connection; callers never hold a binding to pass here.
380
+ private_class_method :new, :open
378
381
 
379
382
  def initialize(binding:, session_id:, reconnect: nil, max_requests: 32, max_command_bytes: 1 << 18,
380
383
  max_queue_bytes: 1 << 20, max_line_bytes: 1 << 18, max_reply_bytes: 1 << 20,
@@ -99,8 +99,65 @@ module LibTmux
99
99
  end
100
100
  end
101
101
 
102
+ # A session on one server, reached through a handle that wraps an {EntityRef}.
103
+ # A handle never caches state: each method runs one tmux command, and
104
+ # {Entity#snapshot} reads the session back as a record. Methods take `timeout:`
105
+ # and `cancel:`, and raise {CommandError} when tmux rejects the command. A
106
+ # listing that fails raises, so an empty array means tmux answered with nothing
107
+ # to list.
108
+ #
109
+ # # What is on here
110
+ #
111
+ # The methods fall into the groups below.
112
+ #
113
+ # **Finding the session.** The server wraps a ref in a handle without running
114
+ # a command, and a ref from another server raises {TargetNotFoundError}. A
115
+ # handle reads back as a snapshot record through its ref.
116
+ #
117
+ # - Handles: {Server#session}, {Server#list_sessions}
118
+ # - Read back as a record: {Entity#snapshot}
119
+ #
120
+ # **Looking inside a session.** Listing answers a frozen array of handles. The
121
+ # pane listing covers every window of the session, and the link listing
122
+ # answers each {WindowLink}, the session's own index for a window.
123
+ #
124
+ # - Windows: {#list_windows}
125
+ # - Panes: {#list_panes}
126
+ # - Window links: {#list_window_links}
127
+ #
128
+ # **Creating a window.** A new window starts detached unless `focus: true`, and
129
+ # uses tmux’s name and default command or shell unless you supply `name:`
130
+ # and `command:` (an argv array). It answers the
131
+ # {Window}, or with `receipt: true` a {CreationReceipt}. `index:` places it at
132
+ # that index.
133
+ #
134
+ # - Create a window: {#new_window}
135
+ #
136
+ # **Changing things.** Rename takes a nonempty name without NUL, and tmux 3.7
137
+ # refuses `:` and `.` in it. Linking puts an existing window into this session
138
+ # at an index. Kill stops the session; with both `expected_windows:` and
139
+ # `expected_panes:` it refuses unless the session holds only those windows and
140
+ # panes, and raises {TargetNotFoundError} otherwise.
141
+ #
142
+ # - Rename: {#rename}
143
+ # - Link a window: {#link_window}
144
+ # - Kill: {#kill}
145
+ #
146
+ # **Options, hooks and environment.** Ask for {Options} or {Hooks} scoped to the
147
+ # session. Read, set, and unset one variable in the session's environment.
148
+ #
149
+ # - Options: {Entity#options}
150
+ # - Hooks: {Entity#hooks}
151
+ # - Read a variable: {#environment}
152
+ # - Set a variable: {#set_environment}
153
+ # - Unset a variable: {#unset_environment}
154
+ #
155
+ # **Expanding a format.** Display expands a tmux format against the session; the
156
+ # argument is a format, not literal text.
157
+ #
158
+ # - Expand a format: {Entity#display}
102
159
  class Session < Entity
103
- def new_window(name:, command:, index: nil, cwd: nil, environment: {}, focus: false, receipt: false, timeout: 5.0, cancel: nil)
160
+ def new_window(name: nil, command: nil, index: nil, cwd: nil, environment: {}, focus: false, receipt: false, timeout: 5.0, cancel: nil)
104
161
  server.__send__(:create_window, ref, name: name, command: command, index: index,
105
162
  cwd: cwd, environment: environment, focus: focus, receipt: receipt, timeout: timeout, cancel: cancel)
106
163
  end
@@ -114,6 +171,57 @@ module LibTmux
114
171
  end
115
172
  end
116
173
 
174
+ # A physical window on one server, reached through a handle that wraps an
175
+ # {EntityRef}. Its index and whether it is current belong to a {WindowLink},
176
+ # so one window can sit in several sessions. Methods take `timeout:` and
177
+ # `cancel:`, and raise {CommandError} when tmux rejects the command.
178
+ #
179
+ # # What is on here
180
+ #
181
+ # The methods fall into the groups below.
182
+ #
183
+ # **Finding the window.** The server wraps a ref in a handle without running a
184
+ # command, and a ref from another server raises {TargetNotFoundError}. A
185
+ # session lists its windows, and a link answers the window it points at.
186
+ #
187
+ # - Handles: {Server#window}, {Server#list_windows}
188
+ # - From a session: {Session#list_windows}
189
+ # - From a link: {WindowLink#window}
190
+ # - Read back as a record: {Entity#snapshot}
191
+ #
192
+ # **Looking inside a window.** Listing answers a frozen array of {Pane}
193
+ # handles.
194
+ #
195
+ # - Panes: {#list_panes}
196
+ #
197
+ # **Splitting and arranging.** Splitting needs `direction:`, `:horizontal` or
198
+ # `:vertical`, with optional `command:` to run an argv array. `size:` is a count of cells or a
199
+ # percentage string such as `"30%"`, and the new pane is detached unless
200
+ # `focus: true`. It answers the new {Pane}. A layout is a native name, an
201
+ # unambiguous prefix of one, or a saved layout. JSON layouts raise
202
+ # {UnsupportedFeatureError} on a tmux without them. Resizing takes `width:`
203
+ # or `height:`, and raises `ArgumentError` when given neither.
204
+ #
205
+ # - Split: {#split}
206
+ # - Choose a layout: {#select_layout}
207
+ # - Resize: {#resize}
208
+ #
209
+ # **Changing things.** Rename takes a nonempty name without NUL, and tmux 3.7
210
+ # refuses `:` and `.` in it. Respawn runs `command:` again in the window, and
211
+ # with `kill: true` replaces a running command. Kill removes the window from
212
+ # the server.
213
+ #
214
+ # - Rename: {#rename}
215
+ # - Respawn: {#respawn}
216
+ # - Kill: {Entity#kill}
217
+ #
218
+ # **Options, hooks and formats.** Ask for {Options} or {Hooks} scoped to the
219
+ # window. Display expands a tmux format against the window; the argument is a
220
+ # format, not literal text.
221
+ #
222
+ # - Options: {Entity#options}
223
+ # - Hooks: {Entity#hooks}
224
+ # - Expand a format: {Entity#display}
117
225
  class Window < Entity
118
226
  LAYOUTS = %w[even-horizontal even-vertical main-horizontal main-vertical tiled].freeze
119
227
  MIRRORED_LAYOUTS = %w[main-horizontal-mirrored main-vertical-mirrored].freeze
@@ -123,7 +231,7 @@ module LibTmux
123
231
  server.__send__(:list_entities, :pane, ["-t", target], timeout: timeout, cancel: cancel)
124
232
  end
125
233
 
126
- def split(direction:, command:, size: nil, cwd: nil, environment: {}, focus: false, timeout: 5.0, cancel: nil)
234
+ def split(direction:, command: nil, size: nil, cwd: nil, environment: {}, focus: false, timeout: 5.0, cancel: nil)
127
235
  server.__send__(:split_window, ref, direction: direction, command: command, size: size,
128
236
  cwd: cwd, environment: environment, focus: focus, timeout: timeout, cancel: cancel)
129
237
  end
@@ -169,8 +277,73 @@ module LibTmux
169
277
  end
170
278
  end
171
279
 
280
+ # A physical pane on one server, reached through a handle that wraps an
281
+ # {EntityRef}. Methods take `timeout:` and `cancel:`, and raise
282
+ # {CommandError} when tmux rejects the command.
283
+ #
284
+ # # What is on here
285
+ #
286
+ # The methods fall into the groups below.
287
+ #
288
+ # **Finding the pane.** The server wraps a ref in a handle without running a
289
+ # command, and a ref from another server raises {TargetNotFoundError}. A
290
+ # session or a window lists its panes.
291
+ #
292
+ # - Handles: {Server#pane}, {Server#list_panes}
293
+ # - From a session or window: {Session#list_panes}, {Window#list_panes}
294
+ # - Read back as a record: {Entity#snapshot}
295
+ #
296
+ # **Sending input.** Text goes to the pane literally, so key names in it are
297
+ # typed as characters. Keys are tmux key names such as `Enter` and `C-c`, and at
298
+ # least one is required. Whether a program consumed the input is not known
299
+ # when the command returns.
300
+ #
301
+ # - Literal text: {#send_text}
302
+ # - Named keys: {#send_keys}
303
+ #
304
+ # **Reading output.** Capture answers the {CommandResult}, whose stdout holds
305
+ # the screen. `start:` and `finish:` are integers or `"-"` for the edge of the
306
+ # history. The alternate screen, the mode screen and the pending input are
307
+ # mutually exclusive. Trimming trailing spaces and capturing the mode screen
308
+ # raise {UnsupportedFeatureError} on a tmux that does not advertise the flag.
309
+ #
310
+ # - Capture: {#capture}
311
+ #
312
+ # **Splitting and arranging.** Splitting needs `direction:`, `:horizontal` or
313
+ # `:vertical`, with optional `command:` to run an argv array. It answers the new {Pane}, which
314
+ # stays detached unless `focus: true`. Breaking out moves the pane into a new
315
+ # window in the session you name and answers that {Window}.
316
+ #
317
+ # - Split: {#split}
318
+ # - Resize: {#resize}
319
+ # - Focus: {#select}
320
+ # - Swap places: {#swap}
321
+ # - Join or move: {#join}, {#move}
322
+ # - Break out: {#break_out}
323
+ #
324
+ # **Copy mode, buffers and pipes.** Enter copy mode and send it a command, paste
325
+ # a named buffer, or pipe the pane's output to a shell command. The shell
326
+ # command and its format expansion are explicitly requested.
327
+ #
328
+ # - Copy mode: {#copy_mode}, {#copy_command}
329
+ # - Paste a buffer: {#paste}
330
+ # - Pipe output: {#pipe}
331
+ #
332
+ # **Changing things.** Respawn runs `command:` again in the pane, and with
333
+ # `kill: true` replaces a running command. Kill removes the pane.
334
+ #
335
+ # - Respawn: {#respawn}
336
+ # - Kill: {Entity#kill}
337
+ #
338
+ # **Options, hooks and formats.** Ask for {Options} or {Hooks} scoped to the
339
+ # pane. Display expands a tmux format against the pane; the argument is a
340
+ # format, not literal text.
341
+ #
342
+ # - Options: {Entity#options}
343
+ # - Hooks: {Entity#hooks}
344
+ # - Expand a format: {Entity#display}
172
345
  class Pane < Entity
173
- def split(direction:, command:, size: nil, cwd: nil, environment: {}, focus: false, timeout: 5.0, cancel: nil)
346
+ def split(direction:, command: nil, size: nil, cwd: nil, environment: {}, focus: false, timeout: 5.0, cancel: nil)
174
347
  server.__send__(:split_window, ref, direction: direction, command: command, size: size,
175
348
  cwd: cwd, environment: environment, focus: focus, timeout: timeout, cancel: cancel)
176
349
  end
@@ -2,7 +2,7 @@
2
2
 
3
3
  module LibTmux
4
4
  class Error < StandardError
5
- attr_reader :delivery, :phase, :pid, :cleanup_errors, :entity, :path, :expected
5
+ attr_reader :delivery, :phase, :pid, :cleanup_errors, :entity, :path, :expected, :text
6
6
 
7
7
  def initialize(message, delivery: :not_sent, phase: nil, pid: nil, cleanup_errors: [], entity: nil, path: nil, expected: nil)
8
8
  super(message)
@@ -13,6 +13,7 @@ module LibTmux
13
13
  @entity = entity
14
14
  @path = path&.dup&.freeze
15
15
  @expected = expected&.dup&.freeze
16
+ @text = nil
16
17
  end
17
18
 
18
19
  private
@@ -20,6 +21,10 @@ module LibTmux
20
21
  def attach_cleanup_errors(errors)
21
22
  @cleanup_errors = (@cleanup_errors + errors).freeze
22
23
  end
24
+
25
+ def attach_wait_text(text)
26
+ @text = text
27
+ end
23
28
  end
24
29
 
25
30
  class InvalidFilterError < Error
@@ -43,6 +48,20 @@ module LibTmux
43
48
  class OutcomeUnknown < Error; end
44
49
  class ClosedError < Error; end
45
50
 
51
+ # The pane or server ended before a text wait matched.
52
+ class PaneDied < Error
53
+ attr_reader :reason
54
+
55
+ def initialize(reason, text)
56
+ message = {pane_exited: "pane process exited", pane_gone: "pane was removed",
57
+ server_gone: "tmux server stopped"}.fetch(reason)
58
+ super(message, phase: :read, delivery: :observed)
59
+ @reason = reason
60
+ attach_wait_text(text)
61
+ end
62
+ private_class_method :new
63
+ end
64
+
46
65
  class CommandError < Error
47
66
  attr_reader :result
48
67
 
@@ -51,7 +51,14 @@ module LibTmux
51
51
  execute_typed(["wait-for", *[flag].compact, "--", channel], timeout: timeout, cancel: cancel)
52
52
  end
53
53
 
54
+ # Load binary data into a named paste buffer, replacing any existing bytes.
55
+ # Use {#delete_buffer} for removal on tmux 3.4 and newer.
56
+ # @raise [UnsupportedFeatureError] if data has zero bytes
54
57
  def write_buffer(name:, data:, timeout: 5.0, cancel: nil)
58
+ if data.is_a?(String) && data.empty?
59
+ raise UnsupportedFeatureError.new("tmux cannot write a zero-byte buffer", phase: :admission)
60
+ end
61
+
55
62
  execute_typed(["load-buffer", "-b", name, "--", "-"], input: data, timeout: timeout, cancel: cancel)
56
63
  end
57
64
 
@@ -0,0 +1,54 @@
1
+ # frozen_string_literal: true
2
+
3
+ require "json"
4
+
5
+ module LibTmux
6
+ # Built-in option and hook definitions from the pinned tmux source release.
7
+ module OptionCatalog
8
+ # Metadata for a built-in tmux option.
9
+ class Option
10
+ attr_reader :name, :type, :scopes, :range, :choices
11
+
12
+ def initialize(name, definition)
13
+ @name = name
14
+ @type = definition.fetch("type").to_sym
15
+ @scopes = definition.fetch("scopes").map(&:to_sym).freeze
16
+ @range = Range.new(*definition.fetch("range")) if definition.key?("range")
17
+ @choices = definition.fetch("choices", [].freeze)
18
+ @array = definition.fetch("array", false)
19
+ freeze
20
+ end
21
+ private_class_method :new
22
+
23
+ # Whether tmux defines the option as an array.
24
+ def array? = @array
25
+ end
26
+
27
+ # A built-in hook and the scopes declared by tmux.
28
+ class Hook
29
+ attr_reader :name, :scopes
30
+
31
+ def initialize(name, scopes)
32
+ @name = name
33
+ @scopes = scopes.map(&:to_sym).freeze
34
+ freeze
35
+ end
36
+ private_class_method :new
37
+ end
38
+
39
+ data = JSON.parse(File.binread(File.expand_path("../../schema/option-catalog.json", __dir__)), freeze: true)
40
+ TMUX_VERSION = data.fetch("tmux_version")
41
+ OPTIONS = data.fetch("options").to_h { |name, definition| [name, Option.__send__(:new, name, definition)] }.freeze
42
+ HOOKS = data.fetch("hooks").to_h { |name, scopes| [name, Hook.__send__(:new, name, scopes)] }.freeze
43
+ private_constant :TMUX_VERSION, :OPTIONS, :HOOKS
44
+
45
+ # The source release for both catalogs, independent of any connected daemon.
46
+ def self.tmux_version = TMUX_VERSION
47
+
48
+ # A frozen hash keyed by canonical option name, sorted by name.
49
+ def self.options = OPTIONS
50
+
51
+ # A frozen hash keyed by canonical hook name, sorted by name.
52
+ def self.hooks = HOOKS
53
+ end
54
+ end
@@ -46,8 +46,17 @@ module LibTmux
46
46
  end
47
47
  end
48
48
 
49
- # Explicit acquisition and mutation at one option scope.
49
+ # Explicit acquisition and mutation at one option scope. For built-in
50
+ # option types, scopes and allowed values, see {OptionCatalog.options}.
50
51
  class Options
52
+ # tmux prints the user option name as-is and its value as one args_escape token.
53
+ USER_OPTION_RECORD = /
54
+ \A(@[^\n]*)[ ]
55
+ ("(?:[^"\\]|\\.)*"|'(?:[^'\\]|\\.)*'|(?:[^\s"'\\]|\\[^\s])+)
56
+ \n\z
57
+ /nx
58
+ private_constant :USER_OPTION_RECORD
59
+
51
60
  def initialize(server, ref: nil, scope: nil)
52
61
  @server, @ref, @scope = server, ref, scope
53
62
  freeze
@@ -55,22 +64,39 @@ module LibTmux
55
64
  private_class_method :new
56
65
 
57
66
  def list(name: nil, inherited: false, timeout: 5.0, cancel: nil)
58
- if inherited && is_a?(Hooks)
67
+ hooks = is_a?(Hooks)
68
+ if inherited && hooks
59
69
  raise UnsupportedFeatureError.new("inherited hook acquisition is not implemented", phase: :admission)
60
70
  end
61
- command = is_a?(Hooks) ? "show-hooks" : "show-options"
71
+ command = hooks ? "show-hooks" : "show-options"
62
72
  args = [command, *scope_flags]
63
- args << "-A" if inherited && !is_a?(Hooks)
73
+ args << "-A" if inherited
64
74
  args.concat(["--", option_name(name)]) if name
65
75
  result = @server.__send__(:execute_typed, args, timeout: timeout, cancel: cancel)
76
+ user_keys = {}
66
77
  result.stdout.lines.map do |line|
67
- match = /\A([^\s\[\]*]+)(?:\[(\d+)\])?(\*)?(?: (.*))?\n\z/n.match(line)
78
+ user_option = !hooks && line.start_with?("@")
79
+ pattern = user_option ? USER_OPTION_RECORD : /\A(@[^\s\[\]]*|[^\s\[\]*]+)(?:\[(\d+)\])?(\*)?(?: (.*))?\n\z/n
80
+ match = pattern.match(line)
68
81
  raise ProtocolError.new("malformed escaped option record", phase: :decode, delivery: :observed) unless match
69
82
 
70
- value = match[4]
71
- raw = is_a?(Hooks) ? (value || "".b) : decode_escaped(value || "".b)
72
- OptionValue.__send__(:new, name: match[1], index: match[2] && Integer(match[2], 10), raw: raw,
73
- inherited: !match[3].nil?, array: !match[2].nil? || value.nil?, present: !value.nil?)
83
+ if user_option
84
+ key, value = match.captures
85
+ index = nil
86
+ # Only named reads print inherited user options and append '*'.
87
+ parent = inherited && name ? key == "#{name.b}*" : false
88
+ key = name.b if parent
89
+ if user_keys[key]
90
+ raise ProtocolError.new("duplicate user option record", phase: :decode, delivery: :observed)
91
+ end
92
+ user_keys[key] = true
93
+ else
94
+ key, index, parent_marker, value = match.captures
95
+ parent = !parent_marker.nil?
96
+ end
97
+ raw = hooks ? (value || "".b) : decode_escaped(value || "".b)
98
+ OptionValue.__send__(:new, name: key, index: index && Integer(index, 10), raw: raw,
99
+ inherited: parent, array: !index.nil? || value.nil?, present: !value.nil?)
74
100
  end.freeze
75
101
  end
76
102
 
@@ -125,7 +151,8 @@ module LibTmux
125
151
  unless name.is_a?(String) && name.match?(/\A[^\s\0]+\z/)
126
152
  raise ArgumentError, "option names cannot contain whitespace or NUL"
127
153
  end
128
- @server.__send__(:literal_name, name)
154
+ # tmux expands option names as formats.
155
+ name.gsub("#", "##")
129
156
  end
130
157
 
131
158
  def indexed_name(name, index)
@@ -161,7 +188,8 @@ module LibTmux
161
188
  end
162
189
  end
163
190
 
164
- # Hook values are executable tmux command strings, never literal data.
191
+ # Hook values are executable tmux command strings. See {OptionCatalog.hooks}
192
+ # for the built-in names and their scopes.
165
193
  class Hooks < Options
166
194
  def get(name, index: nil, inherited: false, timeout: 5.0, cancel: nil)
167
195
  super
data/lib/libtmux/owned.rb CHANGED
@@ -206,9 +206,11 @@ module LibTmux
206
206
  def initialize(executable: "tmux", config: nil, timeout: 5.0, cancel: nil, **options)
207
207
  begin
208
208
  Thread.handle_interrupt(Exception => :never) do
209
+ @startup_budget = operation_budget(timeout, cancel)
209
210
  @daemon = OwnedDaemon.new(executable: executable, config: config, timeout: timeout, cancel: cancel)
210
211
  super(endpoint: @daemon.endpoint, **options)
211
212
  @binding_ready = true
213
+ @startup_budget.options
212
214
  Thread.handle_interrupt(Exception => :immediate) { nil }
213
215
  end
214
216
  rescue Exception => failure
@@ -218,6 +220,8 @@ module LibTmux
218
220
  failure.__send__(:attach_cleanup_errors, ["owned daemon close failed (#{cleanup.class})"]) if failure.is_a?(Error)
219
221
  end
220
222
  raise failure
223
+ ensure
224
+ @startup_budget = nil
221
225
  end
222
226
  end
223
227
 
@@ -249,6 +253,13 @@ module LibTmux
249
253
 
250
254
  nil
251
255
  end
256
+
257
+ private
258
+
259
+ def version_probe_options(timeout)
260
+ remaining = @startup_budget.options
261
+ remaining.merge(timeout: [timeout, remaining.fetch(:timeout)].min)
262
+ end
252
263
  end
253
264
  private_constant :SocketReadiness, :OwnedDaemon, :OwnedServer
254
265
  end