libtmux-workspace 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.
checksums.yaml CHANGED
@@ -1,7 +1,7 @@
1
1
  ---
2
2
  SHA256:
3
- metadata.gz: 81a6e6bcb2e2b40052ea69a7ac812919bc40c061eff86d8f1ed24d74db1bdd89
4
- data.tar.gz: bed554f76a48119e57294fa4199479ce7d9ce1e9864d9a3189224daa11979be7
3
+ metadata.gz: 3625d967b7c3ec20bf317e01a55fac932785afb2beb696cd89df6f3e3dc82f01
4
+ data.tar.gz: 4221145fededd61e039bca57d4b09a15055c4f10acdf41ae265aa5da9b9ff345
5
5
  SHA512:
6
- metadata.gz: 42b471a92bea5e05bfea43e6005c0373df57aa5606e00c1e72af64960e33b0de755edfb48d6b4f6b07b665b8d59c9ef45b71a0e62cca087bbe82b50d12117b02
7
- data.tar.gz: 37069ac56ea23c718a2e1cf7029297151ca5672a96ca1d4420aeef4b3fbba99e6ead871aebe999e1dc0a828d3d75a9361549f52f20ca6b451c9d4f9300fbbe1e
6
+ metadata.gz: ad04686adb1594089b5dfd11abae5c3f97be897e6767a43bb85e3a8ed214752eb9a6a57d66ef7206a5263ff590f38b1a5d568f488fa8aca71e0e1bf835bf24e8
7
+ data.tar.gz: 7c5c575a4d5f54c660a1b8dde99cb44215e87be16e99b94b87c13ec5db456f26b3441d02450cb39a2662748842a11aba7548633171bc1c5d493a4c3abc4686df
data/CHANGELOG.md CHANGED
@@ -9,7 +9,18 @@ The gems are released together at one version, so every release has a section
9
9
  here. Changes that span gems, such as documentation, CI and release tooling, are
10
10
  in the [main changelog](../../CHANGELOG.md).
11
11
 
12
- ## 0.1.3 (unreleased)
12
+ ## 0.1.4 (unreleased)
13
+
14
+ ## 0.1.3 (2026-12-05)
15
+
16
+ - Workspace load errors identify the pane and failed directory check without
17
+ disclosing the resolved path. (#177)
18
+ - Workspace directories inherit from the containing session and window. A
19
+ leading tilde expands from the HOME supplied for the workspace. (#179)
20
+ - **Behavior change:** a pane written as `pane` or `blank`, or with a
21
+ `shell_command` that is only `pane` or `blank`, is now a pane with nothing to
22
+ run, as in tmuxp's blank-pane example. It was a pane that typed the word
23
+ `pane` or `blank` into its shell.
13
24
 
14
25
  ## 0.1.2 (2026-11-04)
15
26
 
data/README.md CHANGED
@@ -20,8 +20,10 @@
20
20
  [Guides](https://libtmux.org/en/ruby/latest/workspace/guides/) ·
21
21
  [RubyGems](https://rubygems.org/gems/libtmux-workspace)
22
22
 
23
- [![libtmux.org](https://img.shields.io/badge/docs-libtmux.org-0b7285)](https://libtmux.org/en/ruby/latest/workspace/)
23
+ [![compatibility](https://github.com/libtmux/libtmux-ruby/actions/workflows/compatibility.yml/badge.svg)](https://github.com/libtmux/libtmux-ruby/actions/workflows/compatibility.yml)
24
24
  [![libtmux-workspace](https://img.shields.io/gem/v/libtmux-workspace?label=libtmux-workspace)](https://rubygems.org/gems/libtmux-workspace)
25
+ [![libtmux.org](https://img.shields.io/badge/docs-libtmux.org-0b7285)](https://libtmux.org/en/ruby/latest/workspace/)
26
+ [![license](https://img.shields.io/badge/license-MIT-blue)](https://github.com/libtmux/libtmux-ruby/blob/master/LICENSE)
25
27
 
26
28
  </div>
27
29
 
@@ -30,15 +32,13 @@ Built on [libtmux](https://github.com/libtmux/libtmux-ruby/tree/master/gems/libt
30
32
 
31
33
  Check a workspace file, see the ordered steps a load would run, and create the
32
34
  session on a running tmux server, from the `libtmux-workspace` command or from
33
- Ruby.
34
-
35
- Validation and offline planning need no running daemon, and imports start no
36
- tmux process.
35
+ Ruby. Importing the Ruby library starts no tmux process.
37
36
 
38
37
  **Contents** — [Quick start](#quick-start) ·
39
38
  [Write the file](#write-the-file) ·
40
39
  [Create the session](#create-the-session) ·
41
40
  [Failed apply](#recover-from-a-failed-apply) ·
41
+ [Rejoin](#attach-to-an-existing-session) ·
42
42
  [Attach or switch](#attach-or-switch-to-the-new-session) ·
43
43
  [Scripts](#script-it) · [Leaves out](#what-it-leaves-out) ·
44
44
  [Support](#support-and-status) · [Documentation](#documentation)
@@ -52,8 +52,7 @@ newer, and tmux 3.3a or newer to load a plan:
52
52
  $ gem install libtmux-workspace
53
53
  ```
54
54
 
55
- Save this declared subset of tmuxp-style data as `workspace.yaml`. Relative
56
- directories resolve against the configuration file's directory.
55
+ Save this workspace configuration as `workspace.yaml`.
57
56
 
58
57
  <!-- example: workspace/config -->
59
58
  ```yaml
@@ -70,7 +69,7 @@ windows:
70
69
  ```
71
70
  <!-- /example -->
72
71
 
73
- Check the file. [`validate`][cli-validate] reads no tmux state:
72
+ Check the file:
74
73
 
75
74
  ```console
76
75
  $ libtmux-workspace validate workspace.yaml
@@ -80,8 +79,7 @@ $ libtmux-workspace validate workspace.yaml
80
79
  Workspace configuration is valid.
81
80
  ```
82
81
 
83
- Print the ordered operations a load would run. Offline [`plan`][cli-plan] does
84
- not contact tmux either:
82
+ Print the ordered operations a load would run:
85
83
 
86
84
  ```console
87
85
  $ libtmux-workspace plan workspace.yaml
@@ -102,6 +100,21 @@ offline_create: 11 ordered steps
102
100
  11. unset_session_option session (configuration)
103
101
  ```
104
102
 
103
+ | Command | What it checks |
104
+ | --- | --- |
105
+ | [`validate`][cli-validate] | Reads and normalizes the configuration, including path syntax. Needs no tmux server. |
106
+ | [`plan`][cli-plan] | Builds the ordered creation steps from the configuration. Needs no tmux server. |
107
+ | [`plan --live`][cli-plan] | Builds the steps against a snapshot of a running server and checks for a session name conflict. |
108
+ | [`load`][cli-load] | Checks the server and requires each planned pane directory to exist and be searchable before creating the session. |
109
+
110
+ At the workspace root, `start_directory: src` resolves next to the file. If
111
+ `src` is missing, validation and both plans can succeed. Load exits with
112
+ status 1 before creating the session.
113
+
114
+ Load errors omit filesystem paths. Use [`plan --json`](#script-it) to inspect
115
+ the normalized pane directories in `steps[].arguments.cwd`. Create the missing
116
+ directory or correct `start_directory`, then retry the load.
117
+
105
118
  ## Write the file
106
119
 
107
120
  ### [`Workspace.load`][workspace-load]: the declared subset
@@ -116,6 +129,9 @@ directory instead. Both return an immutable workspace:
116
129
  - Pane: a command string, or a mapping with `focus`, `split` and `size`.
117
130
  - Root, windows and panes accept `start_directory`, `environment`,
118
131
  `shell_command` and `shell_command_before`.
132
+ - A relative `start_directory` starts from its parent's directory. At the
133
+ root it starts from the file or parse base directory. For `~` or `~/path`,
134
+ pass an absolute `HOME` in `environment:` or supply `--env HOME=PATH`.
119
135
  - Layouts are `even-horizontal`, `even-vertical`, `main-horizontal`,
120
136
  `main-vertical` and `tiled`.
121
137
  - At most one window and one pane per window declare focus.
@@ -134,26 +150,31 @@ directory instead. Both return an immutable workspace:
134
150
  The full field, option and limit lists are in
135
151
  [Workspace parsing][behavior-workspace-parsing].
136
152
 
153
+ For a blank pane in a tmuxp migration, specify the session and window names
154
+ and set `panes: [{}]` in the window. The [tmuxp syntax record][tmuxp-syntax]
155
+ covers configuration shorthands and command compatibility.
156
+
157
+ The [`load` command][cli-load] takes one file.
158
+
137
159
  ## Create the session
138
160
 
139
161
  ### [`libtmux-workspace load`][cli-load]: build it on a running server
140
162
 
141
- `load` needs a running server. It never starts one, so start a private server
142
- with a keepalive session and no user configuration:
163
+ Set `TMUX_SOCKET` to the socket of a running tmux server that has no `work`
164
+ session. Run [a live plan][cli-plan] to check that server for a name conflict:
143
165
 
144
166
  ```console
145
- $ WORKSPACE_TMP=$(mktemp -d)
146
- ```
147
-
148
- ```console
149
- $ tmux -S "$WORKSPACE_TMP/tmux.sock" -f /dev/null new-session -d -s keepalive
167
+ $ libtmux-workspace plan \
168
+ --live \
169
+ --socket "$TMUX_SOCKET" \
170
+ workspace.yaml
150
171
  ```
151
172
 
152
- Create the workspace's session on that socket. It stays detached:
173
+ The plan begins with `captured_create`. Now create the detached session:
153
174
 
154
175
  ```console
155
176
  $ libtmux-workspace load \
156
- --socket "$WORKSPACE_TMP/tmux.sock" \
177
+ --socket "$TMUX_SOCKET" \
157
178
  workspace.yaml
158
179
  ```
159
180
 
@@ -161,24 +182,9 @@ $ libtmux-workspace load \
161
182
  Workspace created; 11 steps completed. Shell commands were dispatched.
162
183
  ```
163
184
 
164
- The new `work` session holds the `editor` window:
165
-
166
- ```console
167
- $ tmux -S "$WORKSPACE_TMP/tmux.sock" list-windows -t work
168
- ```
169
-
170
- Stop only the private server and remove its directory:
171
-
172
- ```console
173
- $ tmux -S "$WORKSPACE_TMP/tmux.sock" kill-server
174
- ```
175
-
176
- ```console
177
- $ rm -rf "$WORKSPACE_TMP"
178
- ```
179
-
180
185
  Loading the same file again fails with status 1: a plan creates a new session
181
- and never reconciles an existing one.
186
+ and never reconciles an existing one. To try the same without a server of your
187
+ own, run the Ruby program below, which starts and stops its own.
182
188
 
183
189
  ### [`Plan#apply`][workspace-plan-apply]: the same from Ruby
184
190
 
@@ -219,7 +225,8 @@ Dir.mktmpdir("workspace-") do |directory|
219
225
  puts "planned steps: #{plan.steps.length}"
220
226
  result = plan.apply(server: server)
221
227
  puts "applied: #{result.success?}"
222
- puts "sessions: #{server.list_sessions.map { |session| session.snapshot.name }.join(', ')}"
228
+ sessions = server.list_sessions.map { |session| session.snapshot.name }
229
+ puts "sessions: #{sessions.join(', ')}"
223
230
  end
224
231
  end
225
232
  ```
@@ -262,10 +269,10 @@ require "libtmux/workspace"
262
269
 
263
270
  LibTmux::Server.start do |server|
264
271
  server.new_session(name: "borrowed", command: ["/bin/cat"])
265
- crowded = LibTmux::Workspace.parse(
266
- JSON.generate({session_name: "crowded", windows: [{window_name: "small", panes: Array.new(40) { {} }}]}),
267
- format: :json, base_directory: __dir__
268
- )
272
+ document = {session_name: "crowded",
273
+ windows: [{window_name: "small", panes: Array.new(40) { {} }}]}
274
+ crowded = LibTmux::Workspace.parse(JSON.generate(document),
275
+ format: :json, base_directory: __dir__)
269
276
  begin
270
277
  crowded.plan.apply(server: server, compensate: true)
271
278
  rescue LibTmux::Workspace::ApplyError => error
@@ -273,7 +280,8 @@ LibTmux::Server.start do |server|
273
280
  puts "created a session: #{error.result.created_refs.key?('session')}"
274
281
  puts "compensation: #{error.result.compensation}"
275
282
  end
276
- puts "sessions: #{server.list_sessions.map { |session| session.snapshot.name }.join(', ')}"
283
+ sessions = server.list_sessions.map { |session| session.snapshot.name }
284
+ puts "sessions: #{sessions.join(', ')}"
277
285
  end
278
286
  ```
279
287
  <!-- /example -->
@@ -290,22 +298,37 @@ sessions: borrowed
290
298
 
291
299
  - Compensation removes the new session only after a tmux guard shows that
292
300
  every window and pane in it has a positively identified created reference.
293
- - An unknown initial entity, or a borrowed entity moved into the session,
301
+ An unknown initial entity, or a borrowed entity moved into the session,
294
302
  makes it refuse.
295
303
  - Compensation cannot undo what shell commands did. Without it, the partial
296
304
  state stays for you to inspect.
297
305
  - Diagnostics omit command payloads and paths. Names are never used to guess
298
306
  ownership.
299
307
 
308
+ If apply fails at `check_directories`, check the indicated pane's
309
+ `start_directory` and any inherited setting. The error's `path` field gives
310
+ the pane's zero-based position in both Ruby and JSON output.
311
+
300
312
  The command takes `--compensate`. The contracts are in
301
313
  [Workspace results][behavior-workspace-results].
302
314
 
315
+ ## Attach to an existing session
316
+
317
+ Run [`libtmux-workspace attach`][workspace-cli] from an interactive terminal with
318
+ `TERM` set. Use `--socket "$TMUX_SOCKET" '$1'`, substituting the session ID
319
+ from the load result. Keep the quotes to preserve the dollar sign.
320
+
321
+ If `load --attach` returns 3 with `result.success: true` and
322
+ `error.phase: "terminal"`, use `result.created_refs.session.id` with the
323
+ same socket to join the session already created.
324
+
303
325
  ## Attach or switch to the new session
304
326
 
305
327
  ### [`load --switch`][cli-load]: move a client to the session
306
328
 
307
- - `load --attach` opens the CLI's `/dev/tty` after creation and runs an owned
308
- terminal client until you detach. It requires a valid `TERM`.
329
+ - `load --attach` runs a terminal client after creation and waits for you to
330
+ detach. It requires `TERM` and the same terminal on stdin and stdout, with
331
+ at least one stream open for reading and writing.
309
332
  - `load --switch CLIENT` switches a current tmux client to the created session
310
333
  after apply, keeping the session's environment.
311
334
  - `CLIENT` is a client name, a full TTY path or a TTY path without `/dev/`.
@@ -338,13 +361,13 @@ $ libtmux-workspace plan \
338
361
  workspace.yaml
339
362
  ```
340
363
 
341
- [`load`][cli-load] and `plan --live` act on an existing server. Without
342
- `--socket`, they use the socket in `LIBTMUX_SOCKET_PATH`, else the name in
343
- `LIBTMUX_SOCKET`, else the server `TMUX` names when run inside tmux, else
344
- tmux's default socket. When both variables are set, the path wins, an empty
345
- variable counts as unset, and `--socket` ignores both. A command run from a tmux
346
- pane with none of these set acts on that pane's server. This detached creation
347
- example names its socket with a `TMUX_SOCKET` value:
364
+ Without `--socket`, load and live planning use the socket in
365
+ `LIBTMUX_SOCKET_PATH`, else the name in `LIBTMUX_SOCKET`, else the server
366
+ `TMUX` names when run inside tmux, else tmux's default socket.
367
+
368
+ When both variables are set, the path wins. An empty variable counts as unset,
369
+ and `--socket` ignores both. This example loads into the server
370
+ `TMUX_SOCKET` names:
348
371
 
349
372
  ```console
350
373
  $ libtmux-workspace load \
@@ -358,15 +381,15 @@ $ libtmux-workspace load \
358
381
 
359
382
  | Option | Effect |
360
383
  | --- | --- |
361
- | `--json` | Write one structured result or error object to stdout |
362
- | `--socket PATH` | Select the existing server's socket for `load` or `plan --live`; the default follows the socket variables, then `TMUX`, then tmux's default socket |
384
+ | `--json` | Write one structured result or error object to stdout for `validate`, `plan` or `load` |
385
+ | `--socket PATH` | Select the existing server's socket for `load`, `plan --live` or `attach`; required for `attach`, otherwise defaults to the socket variables, then `TMUX`, then tmux's default socket |
363
386
  | `--live` | Capture a snapshot before planning |
364
- | `--timeout SECONDS` | Bound each apply, live capture or switch operation; the default is 5 |
387
+ | `--timeout SECONDS` | Bound each apply, live capture, switch or attach session lookup; the default is 5 |
365
388
  | `--compensate` | Run guarded cleanup after an apply failure |
366
389
  | `--attach` | Attach this CLI's terminal after a successful `load` |
367
390
  | `--switch CLIENT` | Switch an explicit current client to the new session after `load` |
368
391
  | `--expand-environment` | Expand `${NAME}` in directories and environment values; requires `--env` |
369
- | `--env NAME=VALUE` | Supply a value for expansion; ambient environment variables are not copied |
392
+ | `--env NAME=VALUE` | Supply a value for substitution or an absolute `HOME` for `~` in `start_directory` |
370
393
 
371
394
  | Exit status | Meaning |
372
395
  | --- | --- |
@@ -398,25 +421,20 @@ values by design.
398
421
 
399
422
  ## Support and status
400
423
 
401
- Releases are numbered `0.1.N`, and each release increments `N`. The API is not
402
- settled: any release may change or remove exported identifiers, and the
403
- changelogs mark each break. Pin an exact version to avoid one.
404
- See the [changelog](CHANGELOG.md), and the [main
405
- changelog](https://github.com/libtmux/libtmux-ruby/blob/master/CHANGELOG.md)
406
- for changes that span gems.
407
-
408
- - **Ruby 3.3 or newer.** The gem depends on the same version of `libtmux`,
409
- JSON 3.0 and Psych 5.5.
410
- - **tmux 3.3a or newer** to load or apply a plan.
424
+ - **Unstable API.** Releases are numbered `0.1.N`, and any release may change
425
+ or remove exported identifiers. Pin an exact version. See the
426
+ [changelog](CHANGELOG.md), and the [main
427
+ changelog](https://github.com/libtmux/libtmux-ruby/blob/master/CHANGELOG.md)
428
+ for changes that span gems.
429
+ - **Ruby 3.3 or newer** and **tmux 3.3a or newer** to load or apply a plan. The
430
+ gem needs `libtmux` at the same version as itself.
411
431
  - **Older daemons.** `load` creates nothing and prints the refusal with the
412
432
  floor and the remedy. With `--json`, the refusal is in `error.message` and
413
433
  `error.kind` is `tmux_unsupported`.
414
- - **Linux, including WSL, and macOS.** The library and the command share the
415
- core [compatibility workflow][compat]: Ruby 3.3, 3.4 and 4.0 with tmux 3.3a
416
- through 3.8 on Ubuntu 24.04, with per-revision results. A push and a pull
417
- request run the oldest and newest of those releases, 3.3a and 3.8, and a
418
- release runs all of them. macOS is supported on a best-effort basis, and CI
419
- coverage for it is highly limited.
434
+ - **Linux, including WSL, and macOS.** CI runs on Linux; macOS is best effort.
435
+ The library and the command share the core
436
+ [compatibility workflow][compat]; see the
437
+ [CI matrix](../../.github/CONTRIBUTING.md#running-ci-against-any-tmux).
420
438
  - **History limit.** On tmux 3.3a to 3.6 the reused initial pane keeps the
421
439
  global `history-limit`. Set the server's global value before you apply to
422
440
  get one history size.
@@ -435,8 +453,10 @@ for changes that span gems.
435
453
  [behavior-client-switching]: ../../docs/reference/behavior.md#client-switching
436
454
  [behavior-workspace-apply]: ../../docs/reference/behavior.md#workspace-apply
437
455
  [behavior-workspace-parsing]: ../../docs/reference/behavior.md#workspace-parsing
456
+ [tmuxp-syntax]: ../../test/fixtures/tmuxp/syntax-support.json
438
457
  [behavior-workspace-plans]: ../../docs/reference/behavior.md#workspace-plans
439
458
  [behavior-workspace-results]: ../../docs/reference/behavior.md#workspace-results
459
+ [workspace-cli]: ../../docs/reference/behavior.md#workspace-cli
440
460
  [cli-load]: https://libtmux.org/en/ruby/latest/workspace/cli/load/
441
461
  [cli-plan]: https://libtmux.org/en/ruby/latest/workspace/cli/plan/
442
462
  [cli-validate]: https://libtmux.org/en/ruby/latest/workspace/cli/validate/
@@ -45,8 +45,13 @@ module LibTmux
45
45
  def initialize(result, failure)
46
46
  @result, @failure_class = result, failure.class.name.freeze
47
47
  delivery = result.uncertain? ? :possibly_sent : (result.effects.empty? ? :not_sent : :observed)
48
- super("workspace apply failed during #{result.failed_action} (#{failure_class})",
49
- phase: :apply, delivery: delivery, cleanup_errors: result.cleanup_errors)
48
+ if result.failed_action == :check_directories && failure.is_a?(ConfigError)
49
+ message, path, expected = failure.message, failure.path, failure.expected
50
+ else
51
+ message = "workspace apply failed during #{result.failed_action} (#{failure_class})"
52
+ end
53
+ super(message, path: path, expected: expected, phase: :apply,
54
+ delivery: delivery, cleanup_errors: result.cleanup_errors)
50
55
  end
51
56
  end
52
57
 
@@ -106,10 +111,18 @@ module LibTmux
106
111
  if snapshot.sessions.where(name: @plan.preconditions.fetch("session_name_absent")).exists?
107
112
  raise ConflictError.new("workspace creation requires an absent session name", phase: :apply)
108
113
  end
109
- @plan.steps.filter_map { |step| step.arguments["cwd"] }.uniq.each do |directory|
110
- unless File.directory?(directory) && File.executable?(directory)
111
- raise ConfigError.new("workspace directory is unavailable", expected: "existing accessible directory")
112
- end
114
+ action(:check_directories, mutation: false) { check_directories }
115
+ end
116
+
117
+ def check_directories
118
+ steps = @plan.steps.select { |step| step.arguments.key?("cwd") }
119
+ steps.uniq { |step| step.arguments.fetch("cwd") }.each do |step|
120
+ directory = step.arguments.fetch("cwd")
121
+ next if File.directory?(directory) && File.executable?(directory)
122
+
123
+ _, window, pane = step.produces.last.split(":")
124
+ raise ConfigError.new("workspace start_directory is unavailable",
125
+ path: "$.windows[#{window}].panes[#{pane}]", expected: "existing accessible directory")
113
126
  end
114
127
  end
115
128
 
@@ -2,6 +2,7 @@
2
2
 
3
3
  require "libtmux/workspace"
4
4
  require "optparse"
5
+ require "fcntl"
5
6
 
6
7
  module LibTmux
7
8
  class Workspace
@@ -22,7 +23,7 @@ module LibTmux
22
23
  return version if @arguments == ["--version"]
23
24
 
24
25
  command = @arguments.shift
25
- raise ArgumentError unless %w[validate plan load].include?(command)
26
+ raise ArgumentError unless %w[validate plan load attach].include?(command)
26
27
 
27
28
  @arguments.take_while { |argument| argument != "--" }.each_with_index do |argument, index|
28
29
  next unless argument == "--switch"
@@ -33,7 +34,10 @@ module LibTmux
33
34
  return help if @options[:help]
34
35
  return version if @options[:version]
35
36
 
37
+ return attach_existing if command == "attach"
38
+
36
39
  validate_arguments(command)
40
+ term = attachment_term if @options[:attach]
37
41
  file = configuration_file
38
42
  workspace = Workspace.load(file, expand_environment: @options.fetch(:expand_environment), environment: @options.fetch(:environment))
39
43
  case command
@@ -55,10 +59,11 @@ module LibTmux
55
59
  timeout: @options.fetch(:timeout))
56
60
  end
57
61
  if @options[:attach]
58
- File.open("/dev/tty", "r+") do |terminal|
59
- result = server.attach(session: @result.created_refs.fetch("session"), terminal: terminal, term: @environment.fetch("TERM"))
60
- raise TransportError.new("attached terminal client failed", phase: :terminal) unless result.success?
61
- end
62
+ terminal = attachment_terminal
63
+ raise TransportError.new("interactive terminal required", phase: :terminal) unless terminal
64
+
65
+ result = server.attach(session: @result.created_refs.fetch("session"), terminal: terminal, term: term)
66
+ raise TransportError.new("attached terminal client failed", phase: :terminal) unless result.success?
62
67
  end
63
68
  end
64
69
  emit(@result.to_h, "Workspace created; #{@result.completed_steps.length} steps completed. Shell commands were dispatched.")
@@ -83,18 +88,20 @@ module LibTmux
83
88
 
84
89
  def parser
85
90
  @parser ||= OptionParser.new do |options|
86
- options.banner = "Usage: libtmux-workspace validate|plan|load [options] [FILE]"
91
+ options.banner = "Usage: libtmux-workspace validate|plan|load [options] [FILE]\n" \
92
+ " libtmux-workspace attach --socket PATH [--timeout SECONDS] SESSION_ID"
87
93
  options.on("--json", "Write structured JSON to stdout") { @json = true }
88
- options.on("--socket PATH", "Socket of the existing tmux server for load or plan --live",
89
- "Default: LIBTMUX_SOCKET_PATH, then LIBTMUX_SOCKET, then the",
94
+ options.on("--socket PATH", "Existing tmux server socket for load, plan --live or attach",
95
+ "Required for attach. Otherwise defaults to LIBTMUX_SOCKET_PATH,",
96
+ "then LIBTMUX_SOCKET, then the",
90
97
  "server TMUX names, then tmux's default socket") { |value| @options[:socket] = value }
91
98
  options.on("--live", "Acquire a snapshot before planning") { @options[:live] = true }
92
- options.on("--timeout SECONDS", Float, "Per-operation apply/capture/switch deadline (default: 5)") { |value| @options[:timeout] = value }
99
+ options.on("--timeout SECONDS", Float, "Per-operation apply/capture/switch/session lookup deadline (default: 5)") { |value| @options[:timeout] = value }
93
100
  options.on("--compensate", "Attempt guarded cleanup of positively created resources on failure") { @options[:compensate] = true }
94
- options.on("--attach", "Attach this CLI terminal after successful load") { @options[:attach] = true }
101
+ options.on("--attach", "Attach after load using the terminal on stdin and stdout") { @options[:attach] = true }
95
102
  options.on("--switch CLIENT", "Switch the explicit current client after successful load") { |value| @options[:switch] = value }
96
103
  options.on("--expand-environment", "Expand ${NAME} in paths/environment using explicit --env values") { @options[:expand_environment] = true }
97
- options.on("--env NAME=VALUE", "Add an explicit expansion value; shell command text is unchanged") do |value|
104
+ options.on("--env NAME=VALUE", "Supply a substitution value or HOME for ~ in start_directory") do |value|
98
105
  name, contents = value.split("=", 2)
99
106
  raise ArgumentError unless contents && /\A[A-Za-z_][A-Za-z0-9_]*\z/.match?(name)
100
107
  raise ArgumentError if @options.fetch(:environment).key?(name)
@@ -126,13 +133,61 @@ module LibTmux
126
133
  end
127
134
  raise ArgumentError if command != "load" && (@options[:attach] || @options[:switch] || @options[:compensate])
128
135
  raise ArgumentError if @options[:live] && command != "plan"
129
- live = command == "load" || @options[:live]
136
+ live = %w[load attach].include?(command) || @options[:live]
130
137
  raise ArgumentError if @options.key?(:socket) && @options[:socket].empty?
131
138
  raise ArgumentError if !live && @options[:socket]
132
- if @options[:attach]
133
- term = @environment["TERM"]
134
- raise ArgumentError unless term.is_a?(String) && /\A[A-Za-z0-9][A-Za-z0-9_.+-]{0,127}\z/.match?(term)
139
+ end
140
+
141
+ def attach_existing
142
+ if @json || @options.values_at(:live, :attach, :switch, :compensate, :expand_environment).any? ||
143
+ !@options.fetch(:environment).empty?
144
+ @argument_message = "Attach accepts --socket and --timeout; use --help for usage."
145
+ raise ArgumentError
146
+ end
147
+ id = @arguments.first
148
+ unless @arguments.length == 1 && /\A\$(?:0|[1-9][0-9]*)\z/.match?(id)
149
+ @argument_message = "Provide one numeric session ID, for example '$1'."
150
+ raise ArgumentError
151
+ end
152
+
153
+ unless @options[:socket] && !@options[:socket].empty?
154
+ @argument_message = "Provide --socket PATH for the server that owns the session ID."
155
+ raise ArgumentError
135
156
  end
157
+ validate_arguments("attach")
158
+ terminal = attachment_terminal
159
+ return attachment_error("Attach requires an interactive terminal on stdin and stdout.") unless terminal
160
+ term = attachment_term
161
+
162
+ with_server do |server|
163
+ session = server.list_sessions(timeout: @options.fetch(:timeout)).find { |candidate| candidate.id == id }
164
+ return attachment_error("The session ID was not found on the selected tmux server.") unless session
165
+
166
+ result = server.attach(session: session.ref, terminal: terminal, term: term)
167
+ return attachment_error("The tmux terminal client failed to attach.") unless result.success?
168
+ end
169
+ 0
170
+ end
171
+
172
+ def attachment_term
173
+ term = @environment["TERM"]
174
+ return term if LibTmux::Server.valid_terminal_type?(term)
175
+
176
+ @argument_message = "Set TERM to the terminal type before attaching."
177
+ raise ArgumentError
178
+ end
179
+
180
+ def attachment_terminal
181
+ input = $stdin
182
+ return unless input.tty? && @out.is_a?(IO) && @out.tty? && input.stat.rdev == @out.stat.rdev
183
+
184
+ # tmux needs the actual terminal device on both streams.
185
+ [input, @out].find { |stream| (stream.fcntl(Fcntl::F_GETFL) & Fcntl::O_ACCMODE) == Fcntl::O_RDWR }
186
+ end
187
+
188
+ def attachment_error(message)
189
+ @err.puts(message)
190
+ 1
136
191
  end
137
192
 
138
193
  def configuration_file
@@ -199,7 +254,12 @@ module LibTmux
199
254
  value.fetch("error")["delivery"] = error.delivery.to_s
200
255
  value.fetch("error")["phase"] = error.phase&.to_s
201
256
  end
202
- value.fetch("error")["failure_class"] = error.failure_class if error.is_a?(Workspace::ApplyError)
257
+ if error.is_a?(Workspace::ApplyError)
258
+ details = value.fetch("error")
259
+ details["failure_class"] = error.failure_class
260
+ details["path"] = error.path if error.path
261
+ details["expected"] = error.expected if error.expected
262
+ end
203
263
  value["result"] = @result.to_h if @result
204
264
  if @json
205
265
  @out.puts(JSON.generate(value))
@@ -19,7 +19,8 @@ module LibTmux
19
19
  "main-pane-width" => :index, "main-pane-height" => :index,
20
20
  "window-status-format" => :text, "window-status-current-format" => :text,
21
21
  "pane-border-status" => %w[off top bottom]}.freeze
22
- private_constant :COMMON, :ROOT, :WINDOW, :PANE, :LAYOUTS, :SESSION_OPTIONS, :WINDOW_OPTIONS
22
+ BLANK_PANE = %w[blank pane].freeze
23
+ private_constant :COMMON, :ROOT, :WINDOW, :PANE, :LAYOUTS, :SESSION_OPTIONS, :WINDOW_OPTIONS, :BLANK_PANE
23
24
 
24
25
  def initialize(base, expand, environment, limits)
25
26
  @limits, @expand, @expanded_bytes = limits, expand, 0
@@ -130,11 +131,9 @@ module LibTmux
130
131
  end
131
132
 
132
133
  def common(input, parent, path)
133
- directory = if input.key?("start_directory")
134
- value = expand(text(input["start_directory"], "#{path}.start_directory", empty: false), "#{path}.start_directory")
135
- # Prefixing relative paths avoids File.expand_path's ambient ~ expansion.
136
- File.expand_path(value.start_with?(File::SEPARATOR) ? value : File.join(@base, value))
137
- else parent.fetch("start_directory")
134
+ directory = parent.fetch("start_directory")
135
+ if input.key?("start_directory")
136
+ directory = start_directory(input["start_directory"], directory, "#{path}.start_directory")
138
137
  end
139
138
  inherited_environment = parent.fetch("environment").merge(environment_map(input.fetch("environment", {}), "#{path}.environment"))
140
139
  before = parent.fetch("shell_command_before") + commands(input.fetch("shell_command_before", []), "#{path}.shell_command_before")
@@ -145,6 +144,19 @@ module LibTmux
145
144
  result
146
145
  end
147
146
 
147
+ def start_directory(input, parent, path)
148
+ value = expand(text(input, path, empty: false), path)
149
+ if value.start_with?("~")
150
+ fail_at(path, "~ or ~/path for home expansion") unless value == "~" || value.start_with?("~/")
151
+
152
+ home = @environment["HOME"]
153
+ fail_at(path, "absolute HOME in the supplied environment") unless home&.start_with?("/")
154
+
155
+ value = File.join(home, value.delete_prefix("~"))
156
+ end
157
+ File.absolute_path(value, parent)
158
+ end
159
+
148
160
  def object(value, allowed, path)
149
161
  fail_at(path, "configuration object") unless value.is_a?(Hash)
150
162
  unknown = value.keys - allowed
@@ -226,7 +238,9 @@ module LibTmux
226
238
  value = [value] if value.is_a?(String)
227
239
  fail_at(path, "shell command string or ordered string array") unless value.is_a?(Array)
228
240
  fail_at(path, "bounded command array") if value.length > @limits.fetch(:max_nodes)
229
- value.map { |command| text(command, path) }
241
+ commands = value.map { |command| text(command, path) }
242
+ # tmuxp reads a lone "pane" or "blank" as a pane with nothing to run.
243
+ (commands.one? && BLANK_PANE.include?(commands.first)) ? [] : commands
230
244
  end
231
245
 
232
246
  def boolean(value, path)
@@ -2,6 +2,6 @@
2
2
 
3
3
  module LibTmux
4
4
  class Workspace
5
- VERSION = "0.1.2"
5
+ VERSION = "0.1.3"
6
6
  end
7
7
  end
metadata CHANGED
@@ -1,7 +1,7 @@
1
1
  --- !ruby/object:Gem::Specification
2
2
  name: libtmux-workspace
3
3
  version: !ruby/object:Gem::Version
4
- version: 0.1.2
4
+ version: 0.1.3
5
5
  platform: ruby
6
6
  authors:
7
7
  - libtmux contributors
@@ -15,42 +15,54 @@ dependencies:
15
15
  requirements:
16
16
  - - '='
17
17
  - !ruby/object:Gem::Version
18
- version: 0.1.2
18
+ version: 0.1.3
19
19
  type: :runtime
20
20
  prerelease: false
21
21
  version_requirements: !ruby/object:Gem::Requirement
22
22
  requirements:
23
23
  - - '='
24
24
  - !ruby/object:Gem::Version
25
- version: 0.1.2
25
+ version: 0.1.3
26
26
  - !ruby/object:Gem::Dependency
27
27
  name: json
28
28
  requirement: !ruby/object:Gem::Requirement
29
29
  requirements:
30
- - - "~>"
30
+ - - ">="
31
31
  - !ruby/object:Gem::Version
32
32
  version: 3.0.2
33
+ - - "<"
34
+ - !ruby/object:Gem::Version
35
+ version: '4'
33
36
  type: :runtime
34
37
  prerelease: false
35
38
  version_requirements: !ruby/object:Gem::Requirement
36
39
  requirements:
37
- - - "~>"
40
+ - - ">="
38
41
  - !ruby/object:Gem::Version
39
42
  version: 3.0.2
43
+ - - "<"
44
+ - !ruby/object:Gem::Version
45
+ version: '4'
40
46
  - !ruby/object:Gem::Dependency
41
47
  name: psych
42
48
  requirement: !ruby/object:Gem::Requirement
43
49
  requirements:
44
- - - "~>"
50
+ - - ">="
45
51
  - !ruby/object:Gem::Version
46
52
  version: 5.5.0
53
+ - - "<"
54
+ - !ruby/object:Gem::Version
55
+ version: '6'
47
56
  type: :runtime
48
57
  prerelease: false
49
58
  version_requirements: !ruby/object:Gem::Requirement
50
59
  requirements:
51
- - - "~>"
60
+ - - ">="
52
61
  - !ruby/object:Gem::Version
53
62
  version: 5.5.0
63
+ - - "<"
64
+ - !ruby/object:Gem::Version
65
+ version: '6'
54
66
  description: Validate YAML or JSON workspace configurations, inspect creation plans
55
67
  and apply them to tmux.
56
68
  executables:
@@ -78,7 +90,7 @@ licenses:
78
90
  metadata:
79
91
  source_code_uri: https://github.com/libtmux/libtmux-ruby
80
92
  changelog_uri: https://github.com/libtmux/libtmux-ruby/blob/master/gems/libtmux-workspace/CHANGELOG.md
81
- documentation_uri: https://libtmux.org/en/ruby/v0.1.2/workspace/
93
+ documentation_uri: https://libtmux.org/en/ruby/v0.1.3/workspace/
82
94
  rdoc_options: []
83
95
  require_paths:
84
96
  - lib