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