16
16
from libtmux ._internal .query_list import QueryList
17
17
from libtmux .common import has_gte_version , tmux_cmd
18
18
from libtmux .constants import (
19
- OPTION_SCOPE_FLAG_MAP ,
20
19
RESIZE_ADJUSTMENT_DIRECTION_FLAG_MAP ,
21
20
OptionScope ,
22
21
PaneDirection ,
27
26
from libtmux .pane import Pane
28
27
29
28
from . import exc
30
- from .options import handle_option_error
29
+ from .common import PaneDict , WindowOptionDict
30
+ from .options import OptionsMixin
31
31
32
32
if t .TYPE_CHECKING :
33
33
import sys
47
47
48
48
49
49
@dataclasses .dataclass ()
50
- class Window (Obj ):
50
+ class Window (Obj , OptionsMixin ):
51
51
""":term:`tmux(1)` :term:`Window` [window_manual]_.
52
52
53
53
Holds :class:`Pane` objects.
@@ -102,6 +102,7 @@ class Window(Obj):
102
102
https://man.openbsd.org/tmux.1#DESCRIPTION. Accessed April 1st, 2018.
103
103
"""
104
104
105
+ default_option_scope : OptionScope | None = OptionScope .Window
105
106
server : Server
106
107
107
108
def __enter__ (self ) -> Self :
@@ -439,230 +440,6 @@ def select_layout(self, layout: str | None = None) -> Window:
439
440
440
441
return self
441
442
442
- def set_option (
443
- self ,
444
- option : str ,
445
- value : int | str ,
446
- _format : bool | None = None ,
447
- unset : bool | None = None ,
448
- unset_panes : bool | None = None ,
449
- prevent_overwrite : bool | None = None ,
450
- suppress_warnings : bool | None = None ,
451
- append : bool | None = None ,
452
- g : bool | None = None ,
453
- scope : OptionScope | None = None ,
454
- ) -> Window :
455
- """Set option for tmux window.
456
-
457
- Wraps ``$ tmux set-option <option> <value>``.
458
-
459
- Parameters
460
- ----------
461
- option : str
462
- option to set, e.g. 'aggressive-resize'
463
- value : str
464
- window option value. True/False will turn in 'on' and 'off',
465
- also accepts string of 'on' or 'off' directly.
466
-
467
- Raises
468
- ------
469
- :exc:`exc.OptionError`, :exc:`exc.UnknownOption`,
470
- :exc:`exc.InvalidOption`, :exc:`exc.AmbiguousOption`
471
- """
472
- flags : list [str ] = []
473
- if isinstance (value , bool ) and value :
474
- value = "on"
475
- elif isinstance (value , bool ) and not value :
476
- value = "off"
477
-
478
- if unset is not None and unset :
479
- assert isinstance (unset , bool )
480
- flags .append ("-u" )
481
-
482
- if unset_panes is not None and unset_panes :
483
- assert isinstance (unset_panes , bool )
484
- flags .append ("-U" )
485
-
486
- if _format is not None and _format :
487
- assert isinstance (_format , bool )
488
- flags .append ("-F" )
489
-
490
- if prevent_overwrite is not None and prevent_overwrite :
491
- assert isinstance (prevent_overwrite , bool )
492
- flags .append ("-o" )
493
-
494
- if suppress_warnings is not None and suppress_warnings :
495
- assert isinstance (suppress_warnings , bool )
496
- flags .append ("-q" )
497
-
498
- if append is not None and append :
499
- assert isinstance (append , bool )
500
- flags .append ("-a" )
501
-
502
- if g is not None and g :
503
- assert isinstance (g , bool )
504
- flags .append ("-g" )
505
-
506
- if scope is not None :
507
- assert scope in OPTION_SCOPE_FLAG_MAP
508
- flags .append (
509
- OPTION_SCOPE_FLAG_MAP [scope ],
510
- )
511
-
512
- cmd = self .cmd (
513
- "set-option" ,
514
- "-w" ,
515
- * flags ,
516
- option ,
517
- value ,
518
- )
519
-
520
- if isinstance (cmd .stderr , list ) and len (cmd .stderr ):
521
- handle_option_error (cmd .stderr [0 ])
522
-
523
- return self
524
-
525
- @t .overload
526
- def show_options (
527
- self ,
528
- g : bool | None ,
529
- scope : OptionScope | None ,
530
- include_hooks : bool | None ,
531
- include_parents : bool | None ,
532
- values_only : t .Literal [True ],
533
- ) -> list [str ]: ...
534
-
535
- @t .overload
536
- def show_options (
537
- self ,
538
- g : bool | None ,
539
- scope : OptionScope | None ,
540
- include_hooks : bool | None ,
541
- include_parents : bool | None ,
542
- values_only : None = None ,
543
- ) -> WindowOptionDict : ...
544
-
545
- @t .overload
546
- def show_options (
547
- self ,
548
- g : bool | None = None ,
549
- scope : OptionScope | None = None ,
550
- include_hooks : bool | None = None ,
551
- include_parents : bool | None = None ,
552
- values_only : t .Literal [False ] = False ,
553
- ) -> WindowOptionDict : ...
554
-
555
- def show_options (
556
- self ,
557
- g : bool | None = False ,
558
- scope : OptionScope | None = OptionScope .Window ,
559
- include_hooks : bool | None = None ,
560
- include_parents : bool | None = None ,
561
- values_only : bool | None = False ,
562
- ) -> WindowOptionDict | list [str ]:
563
- """Return a dict of options for the window.
564
-
565
- Parameters
566
- ----------
567
- g : str, optional
568
- Pass ``-g`` flag for global variable, default False.
569
- """
570
- tmux_args : tuple [str , ...] = ()
571
-
572
- if g :
573
- tmux_args += ("-g" ,)
574
-
575
- if scope is not None :
576
- assert scope in OPTION_SCOPE_FLAG_MAP
577
- tmux_args += (OPTION_SCOPE_FLAG_MAP [scope ],)
578
-
579
- if include_parents is not None and include_parents :
580
- tmux_args += ("-A" ,)
581
-
582
- if include_hooks is not None and include_hooks :
583
- tmux_args += ("-H" ,)
584
-
585
- if values_only is not None and values_only :
586
- tmux_args += ("-v" ,)
587
-
588
- cmd = self .cmd ("show-options" , * tmux_args )
589
-
590
- output = cmd .stdout
591
-
592
- # The shlex.split function splits the args at spaces, while also
593
- # retaining quoted sub-strings.
594
- # shlex.split('this is "a test"') => ['this', 'is', 'a test']
595
-
596
- window_options : WindowOptionDict = {}
597
- for item in output :
598
- try :
599
- key , val = shlex .split (item )
600
- except ValueError :
601
- logger .exception (f"Error extracting option: { item } " )
602
- assert isinstance (key , str )
603
- assert isinstance (val , str )
604
-
605
- if isinstance (val , str ) and val .isdigit ():
606
- window_options [key ] = int (val )
607
-
608
- return window_options
609
-
610
- def show_option (
611
- self ,
612
- option : str ,
613
- g : bool = False ,
614
- scope : OptionScope | None = OptionScope .Window ,
615
- include_hooks : bool | None = None ,
616
- include_parents : bool | None = None ,
617
- ) -> str | int | None :
618
- """Return option value for the target window.
619
-
620
- todo: test and return True/False for on/off string
621
-
622
- Parameters
623
- ----------
624
- option : str
625
- g : bool, optional
626
- Pass ``-g`` flag, global. Default False.
627
-
628
- Raises
629
- ------
630
- :exc:`exc.OptionError`, :exc:`exc.UnknownOption`,
631
- :exc:`exc.InvalidOption`, :exc:`exc.AmbiguousOption`
632
- """
633
- tmux_args : tuple [str | int , ...] = ()
634
-
635
- if g :
636
- tmux_args += ("-g" ,)
637
-
638
- if scope is not None :
639
- assert scope in OPTION_SCOPE_FLAG_MAP
640
- tmux_args += (OPTION_SCOPE_FLAG_MAP [scope ],)
641
-
642
- if include_parents is not None and include_parents :
643
- tmux_args += ("-A" ,)
644
-
645
- if include_hooks is not None and include_hooks :
646
- tmux_args += ("-H" ,)
647
-
648
- tmux_args += (option ,)
649
-
650
- cmd = self .cmd ("show-options" , * tmux_args )
651
-
652
- if len (cmd .stderr ):
653
- handle_option_error (cmd .stderr [0 ])
654
-
655
- window_options_output = cmd .stdout
656
-
657
- if not len (window_options_output ):
658
- return None
659
-
660
- value_raw = next (shlex .split (item ) for item in window_options_output )
661
-
662
- value : str | int = int (value_raw [1 ]) if value_raw [1 ].isdigit () else value_raw [1 ]
663
-
664
- return value
665
-
666
443
def rename_window (self , new_name : str ) -> Window :
667
444
"""Rename window.
668
445
@@ -681,8 +458,6 @@ def rename_window(self, new_name: str) -> Window:
681
458
>>> window.rename_window('New name')
682
459
Window(@1 1:New name, Session($1 ...))
683
460
"""
684
- import shlex
685
-
686
461
lex = shlex .shlex (new_name )
687
462
lex .escape = " "
688
463
lex .whitespace_split = False
0 commit comments