|
7 | 7 | """
|
8 | 8 | import dataclasses
|
9 | 9 | import logging
|
| 10 | +import shlex |
10 | 11 | import typing as t
|
11 | 12 | import warnings
|
12 | 13 | from typing import overload
|
13 | 14 |
|
14 |
| -from libtmux.common import tmux_cmd |
| 15 | +from libtmux.common import handle_option_error, tmux_cmd |
| 16 | +from libtmux.constants import OPTION_SCOPE_FLAG_MAP, OptionScope |
15 | 17 | from libtmux.neo import Obj, fetch_obj
|
16 | 18 |
|
17 | 19 | from . import exc
|
18 | 20 |
|
19 | 21 | if t.TYPE_CHECKING:
|
| 22 | + from .common import PaneOptionDict |
20 | 23 | from .server import Server
|
21 | 24 | from .session import Session
|
22 | 25 | from .window import Window
|
@@ -326,6 +329,174 @@ def split_window(
|
326 | 329 | percent=percent,
|
327 | 330 | )
|
328 | 331 |
|
| 332 | + def set_option( |
| 333 | + self, |
| 334 | + option: str, |
| 335 | + value: t.Union[int, str], |
| 336 | + _format: t.Optional[bool] = None, |
| 337 | + unset: t.Optional[bool] = None, |
| 338 | + unset_panes: t.Optional[bool] = None, |
| 339 | + prevent_overwrite: t.Optional[bool] = None, |
| 340 | + suppress_warnings: t.Optional[bool] = None, |
| 341 | + append: t.Optional[bool] = None, |
| 342 | + g: t.Optional[bool] = None, |
| 343 | + scope: t.Optional[OptionScope] = None, |
| 344 | + ) -> "Pane": |
| 345 | + """Set option for tmux pane. |
| 346 | +
|
| 347 | + Wraps ``$ tmux set-option -p <option> <value>``. |
| 348 | +
|
| 349 | + Parameters |
| 350 | + ---------- |
| 351 | + option : str |
| 352 | + option to set, e.g. 'aggressive-resize' |
| 353 | + value : str |
| 354 | + window option value. True/False will turn in 'on' and 'off', |
| 355 | + also accepts string of 'on' or 'off' directly. |
| 356 | +
|
| 357 | + Raises |
| 358 | + ------ |
| 359 | + :exc:`exc.OptionError`, :exc:`exc.UnknownOption`, |
| 360 | + :exc:`exc.InvalidOption`, :exc:`exc.AmbiguousOption` |
| 361 | + """ |
| 362 | + flags: t.List[str] = [] |
| 363 | + if isinstance(value, bool) and value: |
| 364 | + value = "on" |
| 365 | + elif isinstance(value, bool) and not value: |
| 366 | + value = "off" |
| 367 | + |
| 368 | + if unset is not None and unset: |
| 369 | + assert isinstance(unset, bool) |
| 370 | + flags.append("-u") |
| 371 | + |
| 372 | + if unset_panes is not None and unset_panes: |
| 373 | + assert isinstance(unset_panes, bool) |
| 374 | + flags.append("-U") |
| 375 | + |
| 376 | + if _format is not None and _format: |
| 377 | + assert isinstance(_format, bool) |
| 378 | + flags.append("-F") |
| 379 | + |
| 380 | + if prevent_overwrite is not None and prevent_overwrite: |
| 381 | + assert isinstance(prevent_overwrite, bool) |
| 382 | + flags.append("-o") |
| 383 | + |
| 384 | + if suppress_warnings is not None and suppress_warnings: |
| 385 | + assert isinstance(suppress_warnings, bool) |
| 386 | + flags.append("-q") |
| 387 | + |
| 388 | + if append is not None and append: |
| 389 | + assert isinstance(append, bool) |
| 390 | + flags.append("-a") |
| 391 | + |
| 392 | + if g is not None and g: |
| 393 | + assert isinstance(g, bool) |
| 394 | + flags.append("-g") |
| 395 | + |
| 396 | + if scope is not None: |
| 397 | + assert scope in OPTION_SCOPE_FLAG_MAP |
| 398 | + flags.append( |
| 399 | + OPTION_SCOPE_FLAG_MAP[scope], |
| 400 | + ) |
| 401 | + |
| 402 | + cmd = self.cmd( |
| 403 | + "set-option", |
| 404 | + f"-t{self.pane_id}", |
| 405 | + option, |
| 406 | + value, |
| 407 | + *flags, |
| 408 | + ) |
| 409 | + |
| 410 | + if isinstance(cmd.stderr, list) and len(cmd.stderr): |
| 411 | + handle_option_error(cmd.stderr[0]) |
| 412 | + |
| 413 | + return self |
| 414 | + |
| 415 | + def show_options( |
| 416 | + self, |
| 417 | + g: t.Optional[bool] = False, |
| 418 | + scope: t.Optional[OptionScope] = OptionScope.Pane, |
| 419 | + ) -> "PaneOptionDict": |
| 420 | + """Return a dict of options for the pane. |
| 421 | +
|
| 422 | + For familiarity with tmux, the option ``option`` param forwards to |
| 423 | + pick a single option, forwarding to :meth:`Pane.show_option`. |
| 424 | +
|
| 425 | + Parameters |
| 426 | + ---------- |
| 427 | + g : str, optional |
| 428 | + Pass ``-g`` flag for global variable, default False. |
| 429 | + """ |
| 430 | + tmux_args: t.Tuple[str, ...] = () |
| 431 | + |
| 432 | + if g: |
| 433 | + tmux_args += ("-g",) |
| 434 | + |
| 435 | + if scope is not None: |
| 436 | + assert scope in OPTION_SCOPE_FLAG_MAP |
| 437 | + tmux_args += (OPTION_SCOPE_FLAG_MAP[scope],) |
| 438 | + |
| 439 | + cmd = self.cmd("show-pane-options", *tmux_args) |
| 440 | + |
| 441 | + output = cmd.stdout |
| 442 | + |
| 443 | + pane_options: "PaneOptionDict" = {} |
| 444 | + for item in output: |
| 445 | + key, val = shlex.split(item) |
| 446 | + assert isinstance(key, str) |
| 447 | + assert isinstance(val, str) |
| 448 | + |
| 449 | + if isinstance(val, str) and val.isdigit(): |
| 450 | + pane_options[key] = int(val) |
| 451 | + |
| 452 | + return pane_options |
| 453 | + |
| 454 | + def show_option( |
| 455 | + self, |
| 456 | + option: str, |
| 457 | + g: bool = False, |
| 458 | + scope: t.Optional[OptionScope] = OptionScope.Pane, |
| 459 | + ) -> t.Optional[t.Union[str, int]]: |
| 460 | + """Return a list of options for the pane. |
| 461 | +
|
| 462 | + todo: test and return True/False for on/off string |
| 463 | +
|
| 464 | + Parameters |
| 465 | + ---------- |
| 466 | + option : str |
| 467 | + g : bool, optional |
| 468 | + Pass ``-g`` flag, global. Default False. |
| 469 | +
|
| 470 | + Raises |
| 471 | + ------ |
| 472 | + :exc:`exc.OptionError`, :exc:`exc.UnknownOption`, |
| 473 | + :exc:`exc.InvalidOption`, :exc:`exc.AmbiguousOption` |
| 474 | + """ |
| 475 | + tmux_args: t.Tuple[t.Union[str, int], ...] = () |
| 476 | + |
| 477 | + if g: |
| 478 | + tmux_args += ("-g",) |
| 479 | + |
| 480 | + tmux_args += (option,) |
| 481 | + |
| 482 | + cmd = self.cmd("show-options", *tmux_args) |
| 483 | + |
| 484 | + if len(cmd.stderr): |
| 485 | + handle_option_error(cmd.stderr[0]) |
| 486 | + |
| 487 | + pane_options_output = cmd.stdout |
| 488 | + |
| 489 | + if not len(pane_options_output): |
| 490 | + return None |
| 491 | + |
| 492 | + value_raw = next(shlex.split(item) for item in pane_options_output) |
| 493 | + |
| 494 | + value: t.Union[str, int] = ( |
| 495 | + int(value_raw[1]) if value_raw[1].isdigit() else value_raw[1] |
| 496 | + ) |
| 497 | + |
| 498 | + return value |
| 499 | + |
329 | 500 | """
|
330 | 501 | Commands (helpers)
|
331 | 502 | """
|
|
0 commit comments