Skip to content

Commit 4c6cf5e

Browse files
authored
feat(pane): add capability to use [-SE] flags to capture_pane (#465)
In re: #458
2 parents 4c55f59 + f39ce04 commit 4c6cf5e

File tree

2 files changed

+80
-2
lines changed

2 files changed

+80
-2
lines changed

src/libtmux/pane.py

+31-2
Original file line numberDiff line numberDiff line change
@@ -155,13 +155,42 @@ def resize_pane(self, *args: t.Any, **kwargs: t.Any) -> "Pane":
155155
self.refresh()
156156
return self
157157

158-
def capture_pane(self) -> t.Union[str, t.List[str]]:
158+
def capture_pane(
159+
self,
160+
start: t.Union["Literal['-']", t.Optional[int]] = None,
161+
end: t.Union["Literal['-']", t.Optional[int]] = None,
162+
) -> t.Union[str, t.List[str]]:
159163
"""
160164
Capture text from pane.
161165
162166
``$ tmux capture-pane`` to pane.
167+
``$ tmux capture-pane -S -10`` to pane.
168+
``$ tmux capture-pane`-E 3` to pane.
169+
``$ tmux capture-pane`-S - -E -` to pane.
170+
171+
Parameters
172+
----------
173+
start: [str,int]
174+
Specify the starting line number.
175+
Zero is the first line of the visible pane.
176+
Positive numbers are lines in the visible pane.
177+
Negative numbers are lines in the history.
178+
‘-’ is the start of the history.
179+
Default: None
180+
end: [str,int]
181+
Specify the ending line number.
182+
Zero is the first line of the visible pane.
183+
Positive numbers are lines in the visible pane.
184+
Negative numbers are lines in the history.
185+
‘-’ is the end of the visible pane
186+
Default: None
163187
"""
164-
return self.cmd("capture-pane", "-p").stdout
188+
cmd = ["capture-pane", "-p"]
189+
if start is not None:
190+
cmd.extend(["-S", str(start)])
191+
if end is not None:
192+
cmd.extend(["-E", str(end)])
193+
return self.cmd(*cmd).stdout
165194

166195
def send_keys(
167196
self,

tests/test_pane.py

+49
Original file line numberDiff line numberDiff line change
@@ -90,3 +90,52 @@ def test_capture_pane(session: Session) -> None:
9090
assert pane_contents == r'$ printf "\n%s\n" "Hello World !"{}'.format(
9191
"\n\nHello World !\n$"
9292
)
93+
94+
95+
def test_capture_pane_start(session: Session) -> None:
96+
env = shutil.which("env")
97+
assert env is not None, "Cannot find usable `env` in PATH."
98+
99+
session.new_window(
100+
attach=True,
101+
window_name="capture_pane_start",
102+
window_shell=f"{env} PS1='$ ' sh",
103+
)
104+
pane = session.attached_window.attached_pane
105+
assert pane is not None
106+
pane_contents = "\n".join(pane.capture_pane())
107+
assert pane_contents == "$"
108+
pane.send_keys(r'printf "%s"', literal=True, suppress_history=False)
109+
pane_contents = "\n".join(pane.capture_pane())
110+
assert pane_contents == '$ printf "%s"\n$'
111+
pane.send_keys("clear -x", literal=True, suppress_history=False)
112+
pane_contents = "\n".join(pane.capture_pane())
113+
assert pane_contents == "$"
114+
pane_contents_start = pane.capture_pane(start=-2)
115+
assert pane_contents_start[0] == '$ printf "%s"'
116+
assert pane_contents_start[1] == "$ clear -x"
117+
assert pane_contents_start[-1] == "$"
118+
pane_contents_start = pane.capture_pane(start="-")
119+
assert pane_contents == "$"
120+
121+
122+
def test_capture_pane_end(session: Session) -> None:
123+
env = shutil.which("env")
124+
assert env is not None, "Cannot find usable `env` in PATH."
125+
126+
session.new_window(
127+
attach=True,
128+
window_name="capture_pane_end",
129+
window_shell=f"{env} PS1='$ ' sh",
130+
)
131+
pane = session.attached_window.attached_pane
132+
assert pane is not None
133+
pane_contents = "\n".join(pane.capture_pane())
134+
assert pane_contents == "$"
135+
pane.send_keys(r'printf "%s"', literal=True, suppress_history=False)
136+
pane_contents = "\n".join(pane.capture_pane())
137+
assert pane_contents == '$ printf "%s"\n$'
138+
pane_contents = "\n".join(pane.capture_pane(end=0))
139+
assert pane_contents == '$ printf "%s"'
140+
pane_contents = "\n".join(pane.capture_pane(end="-"))
141+
assert pane_contents == '$ printf "%s"\n$'

0 commit comments

Comments
 (0)