13
13
14
14
from libtmux ._internal .query_list import QueryList
15
15
from libtmux .common import has_gte_version , tmux_cmd
16
- from libtmux .constants import OPTION_SCOPE_FLAG_MAP , OptionScope
16
+ from libtmux .constants import OptionScope
17
17
from libtmux .neo import Obj , fetch_obj , fetch_objs
18
18
from libtmux .pane import Pane
19
19
20
20
from . import exc
21
- from .common import PaneDict , WindowOptionDict , handle_option_error
21
+ from .common import OptionMixin , PaneDict , WindowOptionDict
22
22
from .formats import FORMAT_SEPARATOR
23
23
24
24
if t .TYPE_CHECKING :
29
29
30
30
31
31
@dataclasses .dataclass ()
32
- class Window (Obj ):
32
+ class Window (Obj , OptionMixin ):
33
33
""":term:`tmux(1)` :term:`Window` [window_manual]_.
34
34
35
35
Holds :class:`Pane` objects.
@@ -77,6 +77,7 @@ class Window(Obj):
77
77
https://man.openbsd.org/tmux.1#DESCRIPTION. Accessed April 1st, 2018.
78
78
"""
79
79
80
+ default_scope : t .Optional [OptionScope ] = OptionScope .Window
80
81
server : "Server"
81
82
82
83
def refresh (self ) -> None :
@@ -343,89 +344,6 @@ def set_window_option(
343
344
344
345
return self .set_option (option = option , value = value )
345
346
346
- def set_option (
347
- self ,
348
- option : str ,
349
- value : t .Union [int , str ],
350
- _format : t .Optional [bool ] = None ,
351
- unset : t .Optional [bool ] = None ,
352
- unset_panes : t .Optional [bool ] = None ,
353
- prevent_overwrite : t .Optional [bool ] = None ,
354
- suppress_warnings : t .Optional [bool ] = None ,
355
- append : t .Optional [bool ] = None ,
356
- g : t .Optional [bool ] = None ,
357
- scope : t .Optional [OptionScope ] = None ,
358
- ) -> "Window" :
359
- """Set option for tmux window.
360
-
361
- Wraps ``$ tmux set-option <option> <value>``.
362
-
363
- Parameters
364
- ----------
365
- option : str
366
- option to set, e.g. 'aggressive-resize'
367
- value : str
368
- window option value. True/False will turn in 'on' and 'off',
369
- also accepts string of 'on' or 'off' directly.
370
-
371
- Raises
372
- ------
373
- :exc:`exc.OptionError`, :exc:`exc.UnknownOption`,
374
- :exc:`exc.InvalidOption`, :exc:`exc.AmbiguousOption`
375
- """
376
- flags : t .List [str ] = []
377
- if isinstance (value , bool ) and value :
378
- value = "on"
379
- elif isinstance (value , bool ) and not value :
380
- value = "off"
381
-
382
- if unset is not None and unset :
383
- assert isinstance (unset , bool )
384
- flags .append ("-u" )
385
-
386
- if unset_panes is not None and unset_panes :
387
- assert isinstance (unset_panes , bool )
388
- flags .append ("-U" )
389
-
390
- if _format is not None and _format :
391
- assert isinstance (_format , bool )
392
- flags .append ("-F" )
393
-
394
- if prevent_overwrite is not None and prevent_overwrite :
395
- assert isinstance (prevent_overwrite , bool )
396
- flags .append ("-o" )
397
-
398
- if suppress_warnings is not None and suppress_warnings :
399
- assert isinstance (suppress_warnings , bool )
400
- flags .append ("-q" )
401
-
402
- if append is not None and append :
403
- assert isinstance (append , bool )
404
- flags .append ("-a" )
405
-
406
- if g is not None and g :
407
- assert isinstance (g , bool )
408
- flags .append ("-g" )
409
-
410
- if scope is not None :
411
- assert scope in OPTION_SCOPE_FLAG_MAP
412
- flags .append (
413
- OPTION_SCOPE_FLAG_MAP [scope ],
414
- )
415
-
416
- cmd = self .cmd (
417
- "set-option" ,
418
- f"-t{ self .session_id } :{ self .window_index } " ,
419
- * flags ,
420
- option ,
421
- value ,
422
- )
423
-
424
- if isinstance (cmd .stderr , list ) and len (cmd .stderr ):
425
- handle_option_error (cmd .stderr [0 ])
426
-
427
- return self
428
-
429
347
def show_window_options (self , g : t .Optional [bool ] = False ) -> "WindowOptionDict" :
430
348
"""Show options for tmux window. Deprecated by :meth:`Window.show_options()`.
431
349
@@ -440,94 +358,6 @@ def show_window_options(self, g: t.Optional[bool] = False) -> "WindowOptionDict"
440
358
scope = OptionScope .Window ,
441
359
)
442
360
443
- @t .overload
444
- def show_options (
445
- self ,
446
- g : t .Optional [bool ],
447
- scope : t .Optional [OptionScope ],
448
- include_hooks : t .Optional [bool ],
449
- include_parents : t .Optional [bool ],
450
- values_only : t .Literal [True ],
451
- ) -> t .List [str ]:
452
- ...
453
-
454
- @t .overload
455
- def show_options (
456
- self ,
457
- g : t .Optional [bool ],
458
- scope : t .Optional [OptionScope ],
459
- include_hooks : t .Optional [bool ],
460
- include_parents : t .Optional [bool ],
461
- values_only : t .Literal [None ] = None ,
462
- ) -> "WindowOptionDict" :
463
- ...
464
-
465
- @t .overload
466
- def show_options (
467
- self ,
468
- g : t .Optional [bool ] = None ,
469
- scope : t .Optional [OptionScope ] = None ,
470
- include_hooks : t .Optional [bool ] = None ,
471
- include_parents : t .Optional [bool ] = None ,
472
- values_only : t .Literal [False ] = False ,
473
- ) -> "WindowOptionDict" :
474
- ...
475
-
476
- def show_options (
477
- self ,
478
- g : t .Optional [bool ] = False ,
479
- scope : t .Optional [OptionScope ] = OptionScope .Window ,
480
- include_hooks : t .Optional [bool ] = None ,
481
- include_parents : t .Optional [bool ] = None ,
482
- values_only : t .Optional [bool ] = False ,
483
- ) -> t .Union ["WindowOptionDict" , t .List [str ]]:
484
- """Return a dict of options for the window.
485
-
486
- Parameters
487
- ----------
488
- g : str, optional
489
- Pass ``-g`` flag for global variable, default False.
490
- """
491
- tmux_args : t .Tuple [str , ...] = ()
492
-
493
- if g :
494
- tmux_args += ("-g" ,)
495
-
496
- if scope is not None :
497
- assert scope in OPTION_SCOPE_FLAG_MAP
498
- tmux_args += (OPTION_SCOPE_FLAG_MAP [scope ],)
499
-
500
- if include_parents is not None and include_parents :
501
- tmux_args += ("-A" ,)
502
-
503
- if include_hooks is not None and include_hooks :
504
- tmux_args += ("-H" ,)
505
-
506
- if values_only is not None and values_only :
507
- tmux_args += ("-v" ,)
508
-
509
- cmd = self .cmd ("show-options" , * tmux_args )
510
-
511
- output = cmd .stdout
512
-
513
- # The shlex.split function splits the args at spaces, while also
514
- # retaining quoted sub-strings.
515
- # shlex.split('this is "a test"') => ['this', 'is', 'a test']
516
-
517
- window_options : "WindowOptionDict" = {}
518
- for item in output :
519
- try :
520
- key , val = shlex .split (item )
521
- except ValueError :
522
- logger .exception (f"Error extracting option: { item } " )
523
- assert isinstance (key , str )
524
- assert isinstance (val , str )
525
-
526
- if isinstance (val , str ) and val .isdigit ():
527
- window_options [key ] = int (val )
528
-
529
- return window_options
530
-
531
361
def show_window_option (
532
362
self ,
533
363
option : str ,
@@ -547,64 +377,6 @@ def show_window_option(
547
377
scope = OptionScope .Window ,
548
378
)
549
379
550
- def show_option (
551
- self ,
552
- option : str ,
553
- g : bool = False ,
554
- scope : t .Optional [OptionScope ] = OptionScope .Window ,
555
- include_hooks : t .Optional [bool ] = None ,
556
- include_parents : t .Optional [bool ] = None ,
557
- ) -> t .Optional [t .Union [str , int ]]:
558
- """Return option value for the target window.
559
-
560
- todo: test and return True/False for on/off string
561
-
562
- Parameters
563
- ----------
564
- option : str
565
- g : bool, optional
566
- Pass ``-g`` flag, global. Default False.
567
-
568
- Raises
569
- ------
570
- :exc:`exc.OptionError`, :exc:`exc.UnknownOption`,
571
- :exc:`exc.InvalidOption`, :exc:`exc.AmbiguousOption`
572
- """
573
- tmux_args : t .Tuple [t .Union [str , int ], ...] = ()
574
-
575
- if g :
576
- tmux_args += ("-g" ,)
577
-
578
- if scope is not None :
579
- assert scope in OPTION_SCOPE_FLAG_MAP
580
- tmux_args += (OPTION_SCOPE_FLAG_MAP [scope ],)
581
-
582
- if include_parents is not None and include_parents :
583
- tmux_args += ("-A" ,)
584
-
585
- if include_hooks is not None and include_hooks :
586
- tmux_args += ("-H" ,)
587
-
588
- tmux_args += (option ,)
589
-
590
- cmd = self .cmd ("show-options" , * tmux_args )
591
-
592
- if len (cmd .stderr ):
593
- handle_option_error (cmd .stderr [0 ])
594
-
595
- window_options_output = cmd .stdout
596
-
597
- if not len (window_options_output ):
598
- return None
599
-
600
- value_raw = next (shlex .split (item ) for item in window_options_output )
601
-
602
- value : t .Union [str , int ] = (
603
- int (value_raw [1 ]) if value_raw [1 ].isdigit () else value_raw [1 ]
604
- )
605
-
606
- return value
607
-
608
380
def rename_window (self , new_name : str ) -> "Window" :
609
381
"""Rename window.
610
382
@@ -623,8 +395,6 @@ def rename_window(self, new_name: str) -> "Window":
623
395
>>> window.rename_window('New name')
624
396
Window(@1 1:New name, Session($1 ...))
625
397
"""
626
- import shlex
627
-
628
398
lex = shlex .shlex (new_name )
629
399
lex .escape = " "
630
400
lex .whitespace_split = False
0 commit comments