Skip to content

Commit 90684ef

Browse files
committed
Server,Pane(feat): add show_messages, prompt_history, send_prefix commands
why: These are the remaining simple commands that work headlessly and are useful for programmatic inspection and control. what: - Add Server.show_messages() wrapping show-messages - Add Server.show_prompt_history() wrapping show-prompt-history with prompt_type (-T) parameter - Add Server.clear_prompt_history() wrapping clear-prompt-history with prompt_type (-T) parameter - Add Pane.send_prefix() wrapping send-prefix with secondary (-2) flag - Add tests for all new methods
1 parent 118f61f commit 90684ef

4 files changed

Lines changed: 136 additions & 0 deletions

File tree

src/libtmux/pane.py

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1355,6 +1355,28 @@ def pipe(
13551355
if proc.stderr:
13561356
raise exc.LibTmuxException(proc.stderr)
13571357

1358+
def send_prefix(self, *, secondary: bool | None = None) -> None:
1359+
"""Send the prefix key to the pane via ``$ tmux send-prefix``.
1360+
1361+
Parameters
1362+
----------
1363+
secondary : bool, optional
1364+
Send the secondary prefix key (``-2`` flag).
1365+
1366+
Examples
1367+
--------
1368+
>>> pane.send_prefix()
1369+
"""
1370+
tmux_args: tuple[str, ...] = ()
1371+
1372+
if secondary:
1373+
tmux_args += ("-2",)
1374+
1375+
proc = self.cmd("send-prefix", *tmux_args)
1376+
1377+
if proc.stderr:
1378+
raise exc.LibTmuxException(proc.stderr)
1379+
13581380
def respawn(
13591381
self,
13601382
*,

src/libtmux/server.py

Lines changed: 84 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -528,6 +528,90 @@ def wait_for(
528528
if proc.stderr:
529529
raise exc.LibTmuxException(proc.stderr)
530530

531+
def show_messages(self) -> list[str]:
532+
"""Show server message log via ``$ tmux show-messages``.
533+
534+
Returns
535+
-------
536+
list[str]
537+
Server message log lines.
538+
539+
Examples
540+
--------
541+
>>> result = server.show_messages()
542+
>>> isinstance(result, list)
543+
True
544+
"""
545+
proc = self.cmd("show-messages")
546+
547+
if proc.stderr:
548+
raise exc.LibTmuxException(proc.stderr)
549+
550+
return proc.stdout
551+
552+
def show_prompt_history(
553+
self,
554+
*,
555+
prompt_type: str | None = None,
556+
) -> list[str]:
557+
"""Show prompt history via ``$ tmux show-prompt-history``.
558+
559+
Parameters
560+
----------
561+
prompt_type : str, optional
562+
Prompt type to show (``-T`` flag). One of: ``command``,
563+
``search``, ``target``, ``window-target``.
564+
565+
Returns
566+
-------
567+
list[str]
568+
Prompt history lines.
569+
570+
Examples
571+
--------
572+
>>> result = server.show_prompt_history()
573+
>>> isinstance(result, list)
574+
True
575+
"""
576+
tmux_args: tuple[str, ...] = ()
577+
578+
if prompt_type is not None:
579+
tmux_args += ("-T", prompt_type)
580+
581+
proc = self.cmd("show-prompt-history", *tmux_args)
582+
583+
if proc.stderr:
584+
raise exc.LibTmuxException(proc.stderr)
585+
586+
return proc.stdout
587+
588+
def clear_prompt_history(
589+
self,
590+
*,
591+
prompt_type: str | None = None,
592+
) -> None:
593+
"""Clear prompt history via ``$ tmux clear-prompt-history``.
594+
595+
Parameters
596+
----------
597+
prompt_type : str, optional
598+
Prompt type to clear (``-T`` flag). One of: ``command``,
599+
``search``, ``target``, ``window-target``.
600+
601+
Examples
602+
--------
603+
>>> server.clear_prompt_history()
604+
"""
605+
tmux_args: tuple[str, ...] = ()
606+
607+
if prompt_type is not None:
608+
tmux_args += ("-T", prompt_type)
609+
610+
proc = self.cmd("clear-prompt-history", *tmux_args)
611+
612+
if proc.stderr:
613+
raise exc.LibTmuxException(proc.stderr)
614+
531615
def set_buffer(
532616
self,
533617
data: str,

tests/test_pane.py

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -740,6 +740,13 @@ def test_split_percentage_size_mutual_exclusion(session: Session) -> None:
740740
pane.split(size=10, percentage=50)
741741

742742

743+
def test_send_prefix(session: Session) -> None:
744+
"""Test Pane.send_prefix() sends prefix key without error."""
745+
pane = session.active_window.active_pane
746+
assert pane is not None
747+
pane.send_prefix()
748+
749+
743750
def test_display_popup_runs_command(
744751
control_mode: t.Callable[..., t.Any],
745752
session: Session,

tests/test_server.py

Lines changed: 23 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -461,6 +461,29 @@ def test_tmux_bin_invalid_path_raise_if_dead() -> None:
461461
s.raise_if_dead()
462462

463463

464+
def test_show_messages(server: Server) -> None:
465+
"""Test Server.show_messages() returns message log."""
466+
server.new_session(session_name="showmsg_test")
467+
result = server.show_messages()
468+
assert isinstance(result, list)
469+
assert len(result) > 0 # at least the new-session command log
470+
471+
472+
def test_show_prompt_history(server: Server) -> None:
473+
"""Test Server.show_prompt_history() returns history."""
474+
server.new_session(session_name="showph_test")
475+
result = server.show_prompt_history()
476+
assert isinstance(result, list)
477+
478+
479+
def test_clear_prompt_history(server: Server) -> None:
480+
"""Test Server.clear_prompt_history() clears history."""
481+
server.new_session(session_name="clearph_test")
482+
server.clear_prompt_history()
483+
# Verify specific type can be cleared
484+
server.clear_prompt_history(prompt_type="command")
485+
486+
464487
def test_wait_for_set_flag(server: Server) -> None:
465488
"""Test Server.wait_for() with set_flag."""
466489
server.new_session(session_name="wait_test")

0 commit comments

Comments
 (0)