Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
9 changes: 5 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -152,6 +152,8 @@ Pane commands take argument arrays.

[`send_text`][pane-send_text] types literal text, [`send_keys`][pane-send_keys]
sends key names such as `Enter`, and [`capture`][pane-capture] reads the screen.
Omit `name:` to let tmux name a session or window. Omit `command:` to run
its configured `default-command` or, when empty, `default-shell`.
[`Pane#resize`][pane-resize] and [`Window#resize`][window-resize] set sizes in
cells:

Expand All @@ -160,11 +162,9 @@ cells:
require "libtmux"

LibTmux::Server.start do |server|
created = server.new_session(
name: "work", command: ["/bin/sh"], receipt: true
)
created = server.new_session(command: ["/bin/sh"], receipt: true)
pane = created.pane
side = pane.split(direction: :horizontal, size: "40%", command: ["/bin/cat"])
side = pane.split(direction: :horizontal, size: "40%")

pane.send_text("echo café")
pane.send_keys("Enter")
Expand All @@ -176,6 +176,7 @@ LibTmux::Server.start do |server|
break if line
sleep 0.1
end
raise "shell did not print café" unless line
puts "captured: #{line}"

created.window.resize(width: 100, height: 30)
Expand Down
114 changes: 57 additions & 57 deletions docs/reference/api.md

Large diffs are not rendered by default.

13 changes: 8 additions & 5 deletions docs/reference/behavior.md
Original file line number Diff line number Diff line change
Expand Up @@ -356,11 +356,14 @@ aggregate client success only.
## Creation

`new_session`, `new_window` and `split` return exact tmux-assigned handles.
`command` requires a nonempty argv array. A single executable is protected
from tmux's single-string shell interpretation; shell execution requires an
explicit shell argv. Names and cwd are escaped against tmux format expansion;
environment values are literal strings. Environment defaults to empty, cwd
to unspecified, and new windows/splits use `focus: false`.
Omit `command:` to use tmux's `default-command`. When that option is empty,
tmux starts `default-shell`. To run a specific program, supply a nonempty
argv array. A single executable runs literally. To interpret shell syntax,
pass an explicit shell argv. Omit `name:` to let tmux name a session or window.

Names and cwd are escaped against tmux format expansion. Environment values
are literal strings. Environment defaults to empty, cwd to unspecified, and
new windows and splits use `focus: false`.

A supplied cwd is checked as a local directory before dispatch; concurrent
filesystem changes can still trigger tmux's native fallback. Window indexes
Expand Down
7 changes: 3 additions & 4 deletions examples/api/split_resize.rb
Original file line number Diff line number Diff line change
Expand Up @@ -5,11 +5,9 @@
require "libtmux"

LibTmux::Server.start do |server|
created = server.new_session(
name: "work", command: ["/bin/sh"], receipt: true
)
created = server.new_session(command: ["/bin/sh"], receipt: true)
pane = created.pane
side = pane.split(direction: :horizontal, size: "40%", command: ["/bin/cat"])
side = pane.split(direction: :horizontal, size: "40%")

pane.send_text("echo café")
pane.send_keys("Enter")
Expand All @@ -21,6 +19,7 @@
break if line
sleep 0.1
end
raise "shell did not print café" unless line
puts "captured: #{line}"

created.window.resize(width: 100, height: 30)
Expand Down
2 changes: 1 addition & 1 deletion examples/manifest.json
Original file line number Diff line number Diff line change
Expand Up @@ -202,7 +202,7 @@
"LibTmux::Pane#resize",
"LibTmux::Window#resize"
],
"description": "Split a pane, type into the first pane and poll until its output line appears, then resize the window and the second pane. The shell echoes the typed command, so the poll matches a whole line.",
"description": "Start an automatically named session running /bin/sh. Read a whole output line from the shell and resize a second pane running the default command.",
"output": "captured: café\nside pane: 20 columns\n"
}
},
Expand Down
2 changes: 2 additions & 0 deletions gems/libtmux/CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,8 @@ in the [main changelog](../../CHANGELOG.md).
- `Server#write_buffer` raises `UnsupportedFeatureError` for an empty payload
before sending it to tmux. Nonempty writes keep their existing behavior.
(#172)
- Session, window and pane creation can run the daemon's configured command or
shell. Omit the session or window name to let tmux choose it. (#186)
- `Server#list_windows` and `Server#list_panes` return empty collections when
their bound daemon is running without sessions. (#174)

Expand Down
11 changes: 7 additions & 4 deletions gems/libtmux/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -250,9 +250,11 @@ matching panes: 1

### [`Server#new_session`][server-new_session]: a session and its first window

`new_session` starts a detached session that runs `command:`, an argv array.
Keep the session reference to target it by ID. Read the name tmux stored
from a snapshot.
Omit `name:` to let tmux name the detached session. Omit `command:` to
use tmux's `default-command` or, when empty, `default-shell`. Pass an argv
array to run a specific program. The receipt includes the session, window
and pane IDs. Keep the session reference to target it by ID and read the
effective name from a snapshot.

<!-- example: api_new_session/main -->
```ruby
Expand Down Expand Up @@ -288,7 +290,8 @@ More: [Session name behavior by tmux release][behavior-creation].
### [`Session#new_window`][session-new_window]: add a window

[`Pane#split`][pane-split] and [`Window#split`][window-split] add panes the
same way. A new window takes a `name:` and the same `command:` as a session:
same way. A new window accepts the same optional `name:` and `command:`
as a session. Splits also accept `command:`.

<!-- example: api_new_window/main -->
```ruby
Expand Down
13 changes: 7 additions & 6 deletions gems/libtmux/lib/libtmux/entity.rb
Original file line number Diff line number Diff line change
Expand Up @@ -126,7 +126,8 @@ def target
# - Window links: {#list_window_links}
#
# **Creating a window.** A new window starts detached unless `focus: true`, and
# runs `command:`, an argv array rather than a shell string. It answers the
# uses tmux’s name and default command or shell unless you supply `name:`
# and `command:` (an argv array). It answers the
# {Window}, or with `receipt: true` a {CreationReceipt}. `index:` places it at
# that index.
#
Expand Down Expand Up @@ -156,7 +157,7 @@ def target
#
# - Expand a format: {Entity#display}
class Session < Entity
def new_window(name:, command:, index: nil, cwd: nil, environment: {}, focus: false, receipt: false, timeout: 5.0, cancel: nil)
def new_window(name: nil, command: nil, index: nil, cwd: nil, environment: {}, focus: false, receipt: false, timeout: 5.0, cancel: nil)
server.__send__(:create_window, ref, name: name, command: command, index: index,
cwd: cwd, environment: environment, focus: focus, receipt: receipt, timeout: timeout, cancel: cancel)
end
Expand Down Expand Up @@ -194,7 +195,7 @@ def list_panes(timeout: 5.0, cancel: nil)
# - Panes: {#list_panes}
#
# **Splitting and arranging.** Splitting needs `direction:`, `:horizontal` or
# `:vertical`, and `command:`, an argv array. `size:` is a count of cells or a
# `:vertical`, with optional `command:` to run an argv array. `size:` is a count of cells or a
# percentage string such as `"30%"`, and the new pane is detached unless
# `focus: true`. It answers the new {Pane}. A layout is a native name, an
# unambiguous prefix of one, or a saved layout. JSON layouts raise
Expand Down Expand Up @@ -230,7 +231,7 @@ def list_panes(timeout: 5.0, cancel: nil)
server.__send__(:list_entities, :pane, ["-t", target], timeout: timeout, cancel: cancel)
end

def split(direction:, command:, size: nil, cwd: nil, environment: {}, focus: false, timeout: 5.0, cancel: nil)
def split(direction:, command: nil, size: nil, cwd: nil, environment: {}, focus: false, timeout: 5.0, cancel: nil)
server.__send__(:split_window, ref, direction: direction, command: command, size: size,
cwd: cwd, environment: environment, focus: focus, timeout: timeout, cancel: cancel)
end
Expand Down Expand Up @@ -309,7 +310,7 @@ def select_layout(layout, timeout: 5.0, cancel: nil)
# - Capture: {#capture}
#
# **Splitting and arranging.** Splitting needs `direction:`, `:horizontal` or
# `:vertical`, and `command:`, an argv array. It answers the new {Pane}, which
# `:vertical`, with optional `command:` to run an argv array. It answers the new {Pane}, which
# stays detached unless `focus: true`. Breaking out moves the pane into a new
# window in the session you name and answers that {Window}.
#
Expand Down Expand Up @@ -342,7 +343,7 @@ def select_layout(layout, timeout: 5.0, cancel: nil)
# - Hooks: {Entity#hooks}
# - Expand a format: {Entity#display}
class Pane < Entity
def split(direction:, command:, size: nil, cwd: nil, environment: {}, focus: false, timeout: 5.0, cancel: nil)
def split(direction:, command: nil, size: nil, cwd: nil, environment: {}, focus: false, timeout: 5.0, cancel: nil)
server.__send__(:split_window, ref, direction: direction, command: command, size: size,
cwd: cwd, environment: environment, focus: focus, timeout: timeout, cancel: cancel)
end
Expand Down
23 changes: 15 additions & 8 deletions gems/libtmux/lib/libtmux/server.rb
Original file line number Diff line number Diff line change
Expand Up @@ -99,8 +99,9 @@ module LibTmux
# - Down: {Session#list_windows}, {Session#list_panes}, {Window#list_panes}
# - Up: {Entity#server}
#
# **Changing things.** Creating a session starts it detached, running
# `command:`, an argv array rather than a shell string, and answers the
# **Changing things.** A new session starts detached. Omit `name:` for a tmux
# name and `command:` for its default command or shell. Supply an argv
# array to run a particular program. The call returns the
# {Session} or, with `receipt: true`, a {CreationReceipt}. Kill stops the tmux
# server, which closing does only for an owned server. Switching a client
# points the client named by `client:` at a session.
Expand Down Expand Up @@ -307,9 +308,10 @@ def kill(timeout: 5.0, cancel: nil)
# Read `session.snapshot.name` for the effective name and retain
# `session.ref` for subsequent targeting by ID.
# @raise [UnsupportedFeatureError] if exact tmux 3.7 refuses punctuation
def new_session(name:, command:, width: nil, height: nil, window_name: nil, cwd: nil, environment: {}, receipt: false, timeout: 5.0, cancel: nil)
def new_session(name: nil, command: nil, width: nil, height: nil, window_name: nil, cwd: nil, environment: {}, receipt: false, timeout: 5.0, cancel: nil)
budget = operation_budget(timeout, cancel)
arguments = ["new-session", "-d", "-P", "-F", creation_format(:session, receipt), "-s", literal_name(name)]
arguments = ["new-session", "-d", "-P", "-F", creation_format(:session, receipt)]
arguments.concat(["-s", literal_name(name)]) unless name.nil?
arguments.concat(["-n", literal_name(window_name)]) if window_name
{"-x" => width, "-y" => height}.each do |flag, value|
next if value.nil?
Expand All @@ -318,7 +320,7 @@ def new_session(name:, command:, width: nil, height: nil, window_name: nil, cwd:
arguments.concat([flag, value.to_s])
end
arguments.concat(creation_options(cwd: cwd, environment: environment))
create_entity(:session, arguments + ["--"] + pane_command(command), receipt: receipt, **budget.options)
create_entity(:session, arguments + creation_command(command), receipt: receipt, **budget.options)
end

def list_sessions(timeout: 5.0, cancel: nil)
Expand Down Expand Up @@ -624,6 +626,10 @@ def literal_name(name)
name.gsub("#", "##")
end

def creation_command(command)
command.nil? ? [] : ["--", *pane_command(command)]
end

def pane_command(command)
validate_argv(command)
raise ArgumentError, "command executable must not be empty" if command.first.empty?
Expand Down Expand Up @@ -727,9 +733,10 @@ def create_window(ref, name:, command:, index: nil, cwd: nil, environment: {}, f

destination = target(ref, :session)
destination += ":#{index}" unless index.nil?
arguments = ["new-window", *(focus ? [] : ["-d"]), "-P", "-F", creation_format(:window, receipt), "-t", destination, "-n", literal_name(name)]
arguments = ["new-window", *(focus ? [] : ["-d"]), "-P", "-F", creation_format(:window, receipt), "-t", destination]
arguments.concat(["-n", literal_name(name)]) unless name.nil?
arguments.concat(creation_options(cwd: cwd, environment: environment))
create_entity(:window, arguments + ["--"] + pane_command(command), receipt: receipt, **budget.options)
create_entity(:window, arguments + creation_command(command), receipt: receipt, **budget.options)
end

def split_window(ref, direction:, command:, size: nil, cwd: nil, environment: {}, focus: false, timeout: 5.0, cancel: nil)
Expand All @@ -749,7 +756,7 @@ def split_window(ref, direction:, command:, size: nil, cwd: nil, environment: {}
arguments = ["split-window", *(focus ? [] : ["-d"]), flag, "-P", "-F", id_format(:pane), "-t", target(ref, ref.kind)]
arguments.concat(["-l", size.to_s]) if size
arguments.concat(creation_options(cwd: cwd, environment: environment))
create_entity(:pane, arguments + ["--"] + pane_command(command), **budget.options)
create_entity(:pane, arguments + creation_command(command), **budget.options)
end
end
end
12 changes: 6 additions & 6 deletions gems/libtmux/sig/libtmux.rbs
Original file line number Diff line number Diff line change
Expand Up @@ -124,22 +124,22 @@ module LibTmux
class Session < Entity
def snapshot: (**untyped) -> SessionSnapshot
def kill: (?expected_windows: Array[EntityRef]?, ?expected_panes: Array[EntityRef]?, ?timeout: Numeric, ?cancel: untyped) -> CommandResult
def new_window: (name: String, command: Array[String], receipt: true, ?index: Integer?, ?cwd: String?, ?environment: Hash[String, String], ?focus: bool, ?timeout: Numeric, ?cancel: untyped) -> CreationReceipt
| (name: String, command: Array[String], ?receipt: false, ?index: Integer?, ?cwd: String?, ?environment: Hash[String, String], ?focus: bool, ?timeout: Numeric, ?cancel: untyped) -> Window
def new_window: (?name: String?, ?command: Array[String]?, receipt: true, ?index: Integer?, ?cwd: String?, ?environment: Hash[String, String], ?focus: bool, ?timeout: Numeric, ?cancel: untyped) -> CreationReceipt
| (?name: String?, ?command: Array[String]?, ?receipt: false, ?index: Integer?, ?cwd: String?, ?environment: Hash[String, String], ?focus: bool, ?timeout: Numeric, ?cancel: untyped) -> Window
def list_windows: (?timeout: Numeric, ?cancel: untyped) -> Array[Window]
def list_panes: (?timeout: Numeric, ?cancel: untyped) -> Array[Pane]
end

class Window < Entity
def snapshot: (**untyped) -> WindowSnapshot
def list_panes: (?timeout: Numeric, ?cancel: untyped) -> Array[Pane]
def split: (direction: :horizontal | :vertical, command: Array[String], ?size: Integer | String | nil, ?cwd: String?, ?environment: Hash[String, String], ?focus: bool, ?timeout: Numeric, ?cancel: untyped) -> Pane
def split: (direction: :horizontal | :vertical, ?command: Array[String]?, ?size: Integer | String | nil, ?cwd: String?, ?environment: Hash[String, String], ?focus: bool, ?timeout: Numeric, ?cancel: untyped) -> Pane
def select_layout: (String | Symbol, ?timeout: Numeric, ?cancel: untyped) -> CommandResult
end

class Pane < Entity
def snapshot: (**untyped) -> PaneSnapshot
def split: (direction: :horizontal | :vertical, command: Array[String], ?size: Integer | String | nil, ?cwd: String?, ?environment: Hash[String, String], ?focus: bool, ?timeout: Numeric, ?cancel: untyped) -> Pane
def split: (direction: :horizontal | :vertical, ?command: Array[String]?, ?size: Integer | String | nil, ?cwd: String?, ?environment: Hash[String, String], ?focus: bool, ?timeout: Numeric, ?cancel: untyped) -> Pane
def capture: (?start: Integer | String | nil, ?finish: Integer | String | nil, ?join: bool, ?escapes: bool, ?escape_bytes: bool, ?preserve_trailing: bool, ?trim_trailing: bool, ?alternate: bool, ?mode_screen: bool, ?pending: bool, ?timeout: Numeric, ?cancel: untyped) -> CommandResult
def send_text: (String, ?timeout: Numeric, ?cancel: untyped) -> CommandResult
def send_keys: (*String, ?timeout: Numeric, ?cancel: untyped) -> CommandResult
Expand Down Expand Up @@ -237,8 +237,8 @@ module LibTmux
def close: () -> nil
def diagnostics: () -> {admitted_requests: Integer, reserved_process_slots: Integer, control_connections: Integer, closed: bool, limits: {max_requests: Integer, max_controls: Integer, close_timeout: Numeric}}
def kill: (?timeout: Numeric, ?cancel: untyped) -> CommandResult
def new_session: (name: String, command: Array[String], receipt: true, ?width: Integer?, ?height: Integer?, ?window_name: String?, ?cwd: String?, ?environment: Hash[String, String], ?timeout: Numeric, ?cancel: untyped) -> CreationReceipt
| (name: String, command: Array[String], ?receipt: false, ?width: Integer?, ?height: Integer?, ?window_name: String?, ?cwd: String?, ?environment: Hash[String, String], ?timeout: Numeric, ?cancel: untyped) -> Session
def new_session: (?name: String?, ?command: Array[String]?, receipt: true, ?width: Integer?, ?height: Integer?, ?window_name: String?, ?cwd: String?, ?environment: Hash[String, String], ?timeout: Numeric, ?cancel: untyped) -> CreationReceipt
| (?name: String?, ?command: Array[String]?, ?receipt: false, ?width: Integer?, ?height: Integer?, ?window_name: String?, ?cwd: String?, ?environment: Hash[String, String], ?timeout: Numeric, ?cancel: untyped) -> Session
def list_sessions: (?timeout: Numeric, ?cancel: untyped) -> Array[Session]
def list_windows: (?timeout: Numeric, ?cancel: untyped) -> Array[Window]
def list_panes: (?timeout: Numeric, ?cancel: untyped) -> Array[Pane]
Expand Down
10 changes: 10 additions & 0 deletions test/integration/readme_commands_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -251,6 +251,16 @@ def test_the_runner_rejects_what_the_readmes_must_not_do
assert_match(/does not show a command/, refusal("Prose only.\n"))
end

def test_split_recipe_does_not_require_a_posix_login_shell
sandbox = Sandbox.new
recipe = File.join(Blocks::ROOT, "examples/api/split_resize.rb")
result = sandbox.run("env", "SHELL=/bin/cat", "LC_ALL=C", Gem.ruby, recipe)
assert result.status.success?, result.stderr
assert_equal "captured: café\nside pane: 20 columns\n".b, result.stdout.b
ensure
sandbox&.close
end

def test_each_workspace_load_runs_against_a_server_of_its_own
workspace = "```yaml\nsession_name: a\nwindows:\n - window_name: a\n panes: [{}]\n```\n"
load = "```console\n$ libtmux-workspace load \\\n --socket \"$TMUX_SOCKET\" \\\n workspace.yaml\n```\n"
Expand Down
22 changes: 22 additions & 0 deletions test/integration/server_test.rb
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,28 @@
require "socket"

class ServerTest < Minitest::Test
def test_omitted_creation_names_and_commands_use_tmux_defaults
LibTmuxTest::TmuxFixture.open do |fixture|
open_server(fixture) do |server|
server.options(scope: :session).set("default-command", "exec /bin/cat")
native, _, status = fixture.tmux("new-window", "-d", "-P", "-F", '#{pane_start_command}', "-t", "fixture")
assert status.success?
assert_includes native, "/bin/cat"
created = server.new_session(receipt: true)
added = created.entity.new_window(name: nil, command: nil, receipt: true)
panes = [created.pane, added.pane, added.window.split(direction: :horizontal),
added.pane.split(direction: :vertical)]
panes.each do |pane|
assert_equal native, pane.display('#{pane_start_command}').text
end
refute_empty created.entity.snapshot.name
refute_empty added.window.snapshot.name
assert_raises(ArgumentError) { created.entity.new_window(command: []) }
assert_raises(ArgumentError) { server.new_session(name: "") }
end
end
end

def test_literal_creation_topology_and_terminal_input
LibTmuxTest::TmuxFixture.open do |fixture|
executable = File.join(File.dirname(fixture.socket_path), "literal ; executable")
Expand Down
Loading