|
14 | 14 | import warnings
|
15 | 15 |
|
16 | 16 | from libtmux._internal.query_list import QueryList
|
17 |
| -from libtmux.constants import WINDOW_DIRECTION_FLAG_MAP, WindowDirection |
| 17 | +from libtmux.common import tmux_cmd |
| 18 | +from libtmux.constants import WINDOW_DIRECTION_FLAG_MAP, OptionScope, WindowDirection |
18 | 19 | from libtmux.formats import FORMAT_SEPARATOR
|
19 | 20 | from libtmux.neo import Obj, fetch_obj, fetch_objs
|
| 21 | +from libtmux.options import OptionsMixin |
20 | 22 | from libtmux.pane import Pane
|
21 | 23 | from libtmux.window import Window
|
22 | 24 |
|
|
28 | 30 | has_version,
|
29 | 31 | session_check_name,
|
30 | 32 | )
|
31 |
| -from .options import handle_option_error |
32 | 33 |
|
33 | 34 | if t.TYPE_CHECKING:
|
34 | 35 | import sys
|
|
48 | 49 |
|
49 | 50 |
|
50 | 51 | @dataclasses.dataclass()
|
51 |
| -class Session(Obj, EnvironmentMixin): |
| 52 | +class Session(Obj, EnvironmentMixin, OptionsMixin): |
52 | 53 | """:term:`tmux(1)` :term:`Session` [session_manual]_.
|
53 | 54 |
|
54 | 55 | Holds :class:`Window` objects.
|
@@ -91,6 +92,7 @@ class Session(Obj, EnvironmentMixin):
|
91 | 92 | https://man.openbsd.org/tmux.1#DESCRIPTION. Accessed April 1st, 2018.
|
92 | 93 | """
|
93 | 94 |
|
| 95 | + default_option_scope: OptionScope | None = None |
94 | 96 | server: Server
|
95 | 97 |
|
96 | 98 | def __enter__(self) -> Self:
|
@@ -238,153 +240,6 @@ def cmd(
|
238 | 240 | Commands (tmux-like)
|
239 | 241 | """
|
240 | 242 |
|
241 |
| - def set_option( |
242 |
| - self, |
243 |
| - option: str, |
244 |
| - value: str | int, |
245 |
| - global_: bool = False, |
246 |
| - ) -> Session: |
247 |
| - """Set option ``$ tmux set-option <option> <value>``. |
248 |
| -
|
249 |
| - Parameters |
250 |
| - ---------- |
251 |
| - option : str |
252 |
| - the window option. such as 'default-shell'. |
253 |
| - value : str, int, or bool |
254 |
| - True/False will turn in 'on' and 'off'. You can also enter 'on' or |
255 |
| - 'off' directly. |
256 |
| - _global : bool, optional |
257 |
| - check for option globally across all servers (-g) |
258 |
| -
|
259 |
| - Raises |
260 |
| - ------ |
261 |
| - :exc:`exc.OptionError`, :exc:`exc.UnknownOption`, |
262 |
| - :exc:`exc.InvalidOption`, :exc:`exc.AmbiguousOption` |
263 |
| -
|
264 |
| - Notes |
265 |
| - ----- |
266 |
| - .. todo:: |
267 |
| -
|
268 |
| - Needs tests |
269 |
| - """ |
270 |
| - if isinstance(value, bool) and value: |
271 |
| - value = "on" |
272 |
| - elif isinstance(value, bool) and not value: |
273 |
| - value = "off" |
274 |
| - |
275 |
| - tmux_args: tuple[str | int, ...] = () |
276 |
| - |
277 |
| - if global_: |
278 |
| - tmux_args += ("-g",) |
279 |
| - |
280 |
| - assert isinstance(option, str) |
281 |
| - assert isinstance(value, (str, int)) |
282 |
| - |
283 |
| - tmux_args += ( |
284 |
| - option, |
285 |
| - value, |
286 |
| - ) |
287 |
| - |
288 |
| - proc = self.cmd("set-option", *tmux_args) |
289 |
| - |
290 |
| - if isinstance(proc.stderr, list) and len(proc.stderr): |
291 |
| - handle_option_error(proc.stderr[0]) |
292 |
| - |
293 |
| - return self |
294 |
| - |
295 |
| - def show_options( |
296 |
| - self, |
297 |
| - global_: bool | None = False, |
298 |
| - ) -> dict[str, str | int]: |
299 |
| - """Return dict of options for the session. |
300 |
| -
|
301 |
| - Parameters |
302 |
| - ---------- |
303 |
| - _global : bool, optional |
304 |
| - Pass ``-g`` flag for global variable (server-wide) |
305 |
| -
|
306 |
| - Returns |
307 |
| - ------- |
308 |
| - :py:obj:`dict` |
309 |
| -
|
310 |
| - Notes |
311 |
| - ----- |
312 |
| - Uses ``_global`` for keyword name instead of ``global`` to avoid |
313 |
| - colliding with reserved keyword. |
314 |
| - """ |
315 |
| - tmux_args: tuple[str, ...] = () |
316 |
| - |
317 |
| - if global_: |
318 |
| - tmux_args += ("-g",) |
319 |
| - |
320 |
| - tmux_args += ("show-options",) |
321 |
| - session_output = self.cmd(*tmux_args).stdout |
322 |
| - |
323 |
| - session_options: dict[str, str | int] = {} |
324 |
| - for item in session_output: |
325 |
| - key, val = item.split(" ", maxsplit=1) |
326 |
| - assert isinstance(key, str) |
327 |
| - assert isinstance(val, str) |
328 |
| - |
329 |
| - if isinstance(val, str) and val.isdigit(): |
330 |
| - session_options[key] = int(val) |
331 |
| - |
332 |
| - return session_options |
333 |
| - |
334 |
| - def show_option( |
335 |
| - self, |
336 |
| - option: str, |
337 |
| - global_: bool = False, |
338 |
| - ) -> str | int | bool | None: |
339 |
| - """Return option value for the target session. |
340 |
| -
|
341 |
| - Parameters |
342 |
| - ---------- |
343 |
| - option : str |
344 |
| - option name |
345 |
| - _global : bool, optional |
346 |
| - use global option scope, same as ``-g`` |
347 |
| -
|
348 |
| - Returns |
349 |
| - ------- |
350 |
| - str, int, or bool |
351 |
| -
|
352 |
| - Raises |
353 |
| - ------ |
354 |
| - :exc:`exc.OptionError`, :exc:`exc.UnknownOption`, |
355 |
| - :exc:`exc.InvalidOption`, :exc:`exc.AmbiguousOption` |
356 |
| -
|
357 |
| - Notes |
358 |
| - ----- |
359 |
| - Uses ``_global`` for keyword name instead of ``global`` to avoid |
360 |
| - colliding with reserved keyword. |
361 |
| -
|
362 |
| - Test and return True/False for on/off string. |
363 |
| - """ |
364 |
| - tmux_args: tuple[str, ...] = () |
365 |
| - |
366 |
| - if global_: |
367 |
| - tmux_args += ("-g",) |
368 |
| - |
369 |
| - tmux_args += (option,) |
370 |
| - |
371 |
| - cmd = self.cmd("show-options", *tmux_args) |
372 |
| - |
373 |
| - if isinstance(cmd.stderr, list) and len(cmd.stderr): |
374 |
| - handle_option_error(cmd.stderr[0]) |
375 |
| - |
376 |
| - if not len(cmd.stdout): |
377 |
| - return None |
378 |
| - |
379 |
| - value_raw: list[str] = next(item.split(" ") for item in cmd.stdout) |
380 |
| - |
381 |
| - assert isinstance(value_raw[0], str) |
382 |
| - assert isinstance(value_raw[1], str) |
383 |
| - |
384 |
| - value: str | int = int(value_raw[1]) if value_raw[1].isdigit() else value_raw[1] |
385 |
| - |
386 |
| - return value |
387 |
| - |
388 | 243 | def select_window(self, target_window: str | int) -> Window:
|
389 | 244 | """Select window and return the selected window.
|
390 | 245 |
|
|
0 commit comments