Skip to content

Commit f7f7c34

Browse files
committed
!squash test
1 parent bab0241 commit f7f7c34

File tree

1 file changed

+174
-5
lines changed

1 file changed

+174
-5
lines changed

src/libtmux/neo.py

+174-5
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,13 @@
66
from typing import overload
77

88
from libtmux import exc
9-
from libtmux.common import PaneDict, handle_option_error, tmux_cmd
9+
from libtmux.common import (
10+
PaneDict,
11+
handle_option_error,
12+
has_version,
13+
session_check_name,
14+
tmux_cmd,
15+
)
1016
from libtmux.formats import FORMAT_SEPARATOR
1117

1218
if t.TYPE_CHECKING:
@@ -359,10 +365,6 @@ def select_window(self, target_window: t.Union[str, int]) -> "_Window":
359365
def attached_window(self) -> "_Window":
360366
"""
361367
Return active :class:`Window` object.
362-
363-
Returns
364-
-------
365-
:class:`Window`
366368
"""
367369
active_windows = []
368370
for window in self.windows:
@@ -380,6 +382,173 @@ def attached_window(self) -> "_Window":
380382
if len(self._windows) == 0:
381383
raise exc.LibTmuxException("No Windows")
382384

385+
def attach_session(self) -> "_Session":
386+
"""Return ``$ tmux attach-session`` aka alias: ``$ tmux attach``."""
387+
proc = self.cmd("attach-session", "-t%s" % self.session_id)
388+
389+
if proc.stderr:
390+
raise exc.LibTmuxException(proc.stderr)
391+
392+
return self
393+
394+
def kill_session(self) -> None:
395+
"""``$ tmux kill-session``."""
396+
397+
proc = self.cmd("kill-session", "-t%s" % self.session_id)
398+
399+
if proc.stderr:
400+
raise exc.LibTmuxException(proc.stderr)
401+
402+
def switch_client(self) -> "_Session":
403+
"""
404+
Switch client to this session.
405+
406+
Raises
407+
------
408+
:exc:`exc.LibTmuxException`
409+
"""
410+
proc = self.cmd("switch-client", "-t%s" % self.session_id)
411+
412+
if proc.stderr:
413+
raise exc.LibTmuxException(proc.stderr)
414+
415+
return self
416+
417+
def rename_session(self, new_name: str) -> "_Session":
418+
"""
419+
Rename session and return new :class:`Session` object.
420+
421+
Parameters
422+
----------
423+
new_name : str
424+
new session name
425+
426+
Raises
427+
------
428+
:exc:`exc.BadSessionName`
429+
"""
430+
session_check_name(new_name)
431+
432+
proc = self.cmd("rename-session", new_name)
433+
434+
if proc.stderr:
435+
if has_version("2.7") and "no current client" in proc.stderr:
436+
"""tmux 2.7 raises "no current client" warning on BSD systems.
437+
438+
Should be fixed next release:
439+
440+
- https://www.mail-archive.com/[email protected]/msg45186.html
441+
- https://marc.info/?l=openbsd-cvs&m=152183263526828&w=2
442+
"""
443+
else:
444+
raise exc.LibTmuxException(proc.stderr)
445+
446+
return self
447+
448+
def new_window(
449+
self,
450+
window_name: t.Optional[str] = None,
451+
start_directory: None = None,
452+
attach: bool = True,
453+
window_index: str = "",
454+
window_shell: t.Optional[str] = None,
455+
) -> "_Window":
456+
"""
457+
Return :class:`Window` from ``$ tmux new-window``.
458+
459+
By default, this will make the window active. For the new window
460+
to be created and not set to current, pass in ``attach=False``.
461+
462+
Parameters
463+
----------
464+
window_name : str, optional
465+
start_directory : str, optional
466+
working directory in which the new window is created.
467+
attach : bool, optional
468+
make new window the current window after creating it, default True.
469+
window_index : str
470+
create the new window at the given index position. Default is empty
471+
string which will create the window in the next available position.
472+
window_shell : str, optional
473+
execute a command on starting the window. The window will close
474+
when the command exits.
475+
476+
.. note::
477+
When this command exits the window will close. This feature is
478+
useful for long-running processes where the closing of the
479+
window upon completion is desired.
480+
481+
Returns
482+
-------
483+
:class:`Window`
484+
"""
485+
tmux_formats = ["#{window_id}" + FORMAT_SEPARATOR]
486+
487+
window_args: t.Tuple[str, ...] = tuple()
488+
489+
if not attach:
490+
window_args += ("-d",)
491+
492+
window_args += ("-P",)
493+
494+
if start_directory:
495+
# as of 2014-02-08 tmux 1.9-dev doesn't expand ~ in new-window -c.
496+
start_directory = os.path.expanduser(start_directory)
497+
window_args += ("-c%s" % start_directory,)
498+
499+
window_args += ('-F"%s"' % tmux_formats,) # output
500+
if window_name:
501+
window_args += ("-n%s" % window_name,)
502+
503+
window_args += (
504+
# empty string for window_index will use the first one available
505+
"-t%s:%s"
506+
% (self.session_id, window_index),
507+
)
508+
509+
if window_shell:
510+
window_args += (window_shell,)
511+
512+
cmd = self.cmd("new-window", *window_args)
513+
514+
if cmd.stderr:
515+
raise exc.LibTmuxException(cmd.stderr)
516+
517+
window_output = cmd.stdout[0]
518+
519+
window_formatters = dict(
520+
zip(["window_id"], window_output.split(FORMAT_SEPARATOR))
521+
)
522+
523+
return _Window.from_window_id(
524+
server=self.server, window_id=window_formatters["window_id"]
525+
)
526+
527+
def kill_window(self, target_window: t.Optional[str] = None) -> None:
528+
"""Close a tmux window, and all panes inside it, ``$ tmux kill-window``
529+
530+
Kill the current window or the window at ``target-window``. removing it
531+
from any sessions to which it is linked.
532+
533+
Parameters
534+
----------
535+
target_window : str, optional
536+
window to kill
537+
"""
538+
539+
if target_window:
540+
if isinstance(target_window, int):
541+
target = "-t%s:%d" % (self.name, target_window)
542+
else:
543+
target = "-t%s" % target_window
544+
545+
proc = self.cmd("kill-window", target)
546+
547+
if proc.stderr:
548+
raise exc.LibTmuxException(proc.stderr)
549+
550+
self.server._update_windows()
551+
383552
#
384553
# Redundant stuff we want to remove
385554
#

0 commit comments

Comments
 (0)