Skip to content

Commit c2897f2

Browse files
committed
Server(feat[display_menu]): add display_menu() wrapping tmux display-menu
why: display-menu is the last unwrapped tmux command. While it requires a TTY-backed client and cannot be tested with ControlMode (tty.sy=0 causes menu_prepare to return NULL), the method is useful for users with real attached clients. what: - Add Server.display_menu() with title (-T), target_pane (-t), target_client (-c), x (-x), y (-y), starting_choice (-C), border_lines (-b), style (-s), border_style (-S) parameters - Items passed as positional *args in tmux's name/key/command format - Document the TTY client requirement and test gap in docstring
1 parent 05d8d78 commit c2897f2

1 file changed

Lines changed: 85 additions & 0 deletions

File tree

src/libtmux/server.py

Lines changed: 85 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -973,6 +973,91 @@ def command_prompt(
973973
if proc.stderr:
974974
raise exc.LibTmuxException(proc.stderr)
975975

976+
def display_menu(
977+
self,
978+
*items: str,
979+
title: str | None = None,
980+
target_pane: str | None = None,
981+
target_client: str | None = None,
982+
x: int | str | None = None,
983+
y: int | str | None = None,
984+
starting_choice: int | str | None = None,
985+
border_lines: str | None = None,
986+
style: str | None = None,
987+
border_style: str | None = None,
988+
) -> None:
989+
"""Display a popup menu via ``$ tmux display-menu``.
990+
991+
Requires a TTY-backed attached client. Control-mode clients have
992+
``tty.sy=0``, which causes ``menu_prepare()`` to return NULL.
993+
This method cannot be tested with
994+
:class:`~libtmux._internal.control_mode.ControlMode`.
995+
996+
Parameters
997+
----------
998+
*items : str
999+
Menu items as positional args in tmux's ``name key command``
1000+
triple format. Use empty strings for separators.
1001+
title : str, optional
1002+
Menu title (``-T`` flag).
1003+
target_pane : str, optional
1004+
Target pane for format expansion (``-t`` flag).
1005+
target_client : str, optional
1006+
Target client (``-c`` flag).
1007+
x : int or str, optional
1008+
Menu x position (``-x`` flag).
1009+
y : int or str, optional
1010+
Menu y position (``-y`` flag).
1011+
starting_choice : int or str, optional
1012+
Pre-selected item index (``-C`` flag). Use ``-`` for none.
1013+
border_lines : str, optional
1014+
Border line style (``-b`` flag).
1015+
style : str, optional
1016+
Menu style (``-s`` flag).
1017+
border_style : str, optional
1018+
Border style (``-S`` flag).
1019+
1020+
Examples
1021+
--------
1022+
>>> server.display_menu # doctest: +ELLIPSIS
1023+
<bound method ...>
1024+
"""
1025+
tmux_args: tuple[str, ...] = ()
1026+
1027+
if title is not None:
1028+
tmux_args += ("-T", title)
1029+
1030+
if target_client is not None:
1031+
tmux_args += ("-c", target_client)
1032+
1033+
if target_pane is not None:
1034+
tmux_args += ("-t", target_pane)
1035+
1036+
if x is not None:
1037+
tmux_args += ("-x", str(x))
1038+
1039+
if y is not None:
1040+
tmux_args += ("-y", str(y))
1041+
1042+
if starting_choice is not None:
1043+
tmux_args += ("-C", str(starting_choice))
1044+
1045+
if border_lines is not None:
1046+
tmux_args += ("-b", border_lines)
1047+
1048+
if style is not None:
1049+
tmux_args += ("-s", style)
1050+
1051+
if border_style is not None:
1052+
tmux_args += ("-S", border_style)
1053+
1054+
tmux_args += items
1055+
1056+
proc = self.cmd("display-menu", *tmux_args)
1057+
1058+
if proc.stderr:
1059+
raise exc.LibTmuxException(proc.stderr)
1060+
9761061
def start_server(self) -> None:
9771062
"""Start the tmux server if not already running.
9781063

0 commit comments

Comments
 (0)