|
11 | 11 | import warnings
|
12 | 12 |
|
13 | 13 | from libtmux._internal.query_list import QueryList
|
14 |
| -from libtmux.common import tmux_cmd |
| 14 | +from libtmux.common import OptionMixin, tmux_cmd |
| 15 | +from libtmux.constants import OptionScope |
15 | 16 | from libtmux.formats import FORMAT_SEPARATOR
|
16 | 17 | from libtmux.neo import Obj, fetch_obj, fetch_objs
|
17 | 18 | from libtmux.pane import Pane
|
|
21 | 22 | from .common import (
|
22 | 23 | EnvironmentMixin,
|
23 | 24 | WindowDict,
|
24 |
| - handle_option_error, |
25 | 25 | has_gte_version,
|
26 | 26 | has_version,
|
27 | 27 | session_check_name,
|
|
35 | 35 |
|
36 | 36 |
|
37 | 37 | @dataclasses.dataclass()
|
38 |
| -class Session(Obj, EnvironmentMixin): |
| 38 | +class Session(Obj, EnvironmentMixin, OptionMixin): |
39 | 39 | """:term:`tmux(1)` :term:`Session` [session_manual]_.
|
40 | 40 |
|
41 | 41 | Holds :class:`Window` objects.
|
@@ -71,6 +71,7 @@ class Session(Obj, EnvironmentMixin):
|
71 | 71 | https://man.openbsd.org/tmux.1#DESCRIPTION. Accessed April 1st, 2018.
|
72 | 72 | """
|
73 | 73 |
|
| 74 | + default_scope: OptionScope = OptionScope.Session |
74 | 75 | server: "Server"
|
75 | 76 |
|
76 | 77 | def refresh(self) -> None:
|
@@ -170,155 +171,6 @@ def cmd(self, *args: t.Any, **kwargs: t.Any) -> tmux_cmd:
|
170 | 171 | Commands (tmux-like)
|
171 | 172 | """
|
172 | 173 |
|
173 |
| - def set_option( |
174 |
| - self, |
175 |
| - option: str, |
176 |
| - value: t.Union[str, int], |
177 |
| - _global: bool = False, |
178 |
| - ) -> "Session": |
179 |
| - """Set option ``$ tmux set-option <option> <value>``. |
180 |
| -
|
181 |
| - Parameters |
182 |
| - ---------- |
183 |
| - option : str |
184 |
| - the window option. such as 'default-shell'. |
185 |
| - value : str, int, or bool |
186 |
| - True/False will turn in 'on' and 'off'. You can also enter 'on' or |
187 |
| - 'off' directly. |
188 |
| - _global : bool, optional |
189 |
| - check for option globally across all servers (-g) |
190 |
| -
|
191 |
| - Raises |
192 |
| - ------ |
193 |
| - :exc:`exc.OptionError`, :exc:`exc.UnknownOption`, |
194 |
| - :exc:`exc.InvalidOption`, :exc:`exc.AmbiguousOption` |
195 |
| -
|
196 |
| - Notes |
197 |
| - ----- |
198 |
| - .. todo:: |
199 |
| -
|
200 |
| - Needs tests |
201 |
| - """ |
202 |
| - if isinstance(value, bool) and value: |
203 |
| - value = "on" |
204 |
| - elif isinstance(value, bool) and not value: |
205 |
| - value = "off" |
206 |
| - |
207 |
| - tmux_args: t.Tuple[t.Union[str, int], ...] = () |
208 |
| - |
209 |
| - if _global: |
210 |
| - tmux_args += ("-g",) |
211 |
| - |
212 |
| - assert isinstance(option, str) |
213 |
| - assert isinstance(value, (str, int)) |
214 |
| - |
215 |
| - tmux_args += ( |
216 |
| - option, |
217 |
| - value, |
218 |
| - ) |
219 |
| - |
220 |
| - proc = self.cmd("set-option", *tmux_args) |
221 |
| - |
222 |
| - if isinstance(proc.stderr, list) and len(proc.stderr): |
223 |
| - handle_option_error(proc.stderr[0]) |
224 |
| - |
225 |
| - return self |
226 |
| - |
227 |
| - def show_options( |
228 |
| - self, |
229 |
| - _global: t.Optional[bool] = False, |
230 |
| - ) -> t.Dict[str, t.Union[str, int]]: |
231 |
| - """Return dict of options for the session. |
232 |
| -
|
233 |
| - Parameters |
234 |
| - ---------- |
235 |
| - _global : bool, optional |
236 |
| - Pass ``-g`` flag for global variable (server-wide) |
237 |
| -
|
238 |
| - Returns |
239 |
| - ------- |
240 |
| - :py:obj:`dict` |
241 |
| -
|
242 |
| - Notes |
243 |
| - ----- |
244 |
| - Uses ``_global`` for keyword name instead of ``global`` to avoid |
245 |
| - colliding with reserved keyword. |
246 |
| - """ |
247 |
| - tmux_args: t.Tuple[str, ...] = () |
248 |
| - |
249 |
| - if _global: |
250 |
| - tmux_args += ("-g",) |
251 |
| - |
252 |
| - tmux_args += ("show-options",) |
253 |
| - session_output = self.cmd(*tmux_args).stdout |
254 |
| - |
255 |
| - session_options: t.Dict[str, t.Union[str, int]] = {} |
256 |
| - for item in session_output: |
257 |
| - key, val = item.split(" ", maxsplit=1) |
258 |
| - assert isinstance(key, str) |
259 |
| - assert isinstance(val, str) |
260 |
| - |
261 |
| - if isinstance(val, str) and val.isdigit(): |
262 |
| - session_options[key] = int(val) |
263 |
| - |
264 |
| - return session_options |
265 |
| - |
266 |
| - def show_option( |
267 |
| - self, |
268 |
| - option: str, |
269 |
| - _global: bool = False, |
270 |
| - ) -> t.Optional[t.Union[str, int, bool]]: |
271 |
| - """Return option value for the target session. |
272 |
| -
|
273 |
| - Parameters |
274 |
| - ---------- |
275 |
| - option : str |
276 |
| - option name |
277 |
| - _global : bool, optional |
278 |
| - use global option scope, same as ``-g`` |
279 |
| -
|
280 |
| - Returns |
281 |
| - ------- |
282 |
| - str, int, or bool |
283 |
| -
|
284 |
| - Raises |
285 |
| - ------ |
286 |
| - :exc:`exc.OptionError`, :exc:`exc.UnknownOption`, |
287 |
| - :exc:`exc.InvalidOption`, :exc:`exc.AmbiguousOption` |
288 |
| -
|
289 |
| - Notes |
290 |
| - ----- |
291 |
| - Uses ``_global`` for keyword name instead of ``global`` to avoid |
292 |
| - colliding with reserved keyword. |
293 |
| -
|
294 |
| - Test and return True/False for on/off string. |
295 |
| - """ |
296 |
| - tmux_args: t.Tuple[str, ...] = () |
297 |
| - |
298 |
| - if _global: |
299 |
| - tmux_args += ("-g",) |
300 |
| - |
301 |
| - tmux_args += (option,) |
302 |
| - |
303 |
| - cmd = self.cmd("show-options", *tmux_args) |
304 |
| - |
305 |
| - if isinstance(cmd.stderr, list) and len(cmd.stderr): |
306 |
| - handle_option_error(cmd.stderr[0]) |
307 |
| - |
308 |
| - if not len(cmd.stdout): |
309 |
| - return None |
310 |
| - |
311 |
| - value_raw: t.List[str] = next(item.split(" ") for item in cmd.stdout) |
312 |
| - |
313 |
| - assert isinstance(value_raw[0], str) |
314 |
| - assert isinstance(value_raw[1], str) |
315 |
| - |
316 |
| - value: t.Union[str, int] = ( |
317 |
| - int(value_raw[1]) if value_raw[1].isdigit() else value_raw[1] |
318 |
| - ) |
319 |
| - |
320 |
| - return value |
321 |
| - |
322 | 174 | def select_window(self, target_window: t.Union[str, int]) -> "Window":
|
323 | 175 | """Select window, return selected window.
|
324 | 176 |
|
|
0 commit comments