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