|
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 | from libtmux.common import tmux_cmd |
|
40 | 41 |
|
41 | 42 |
|
42 | 43 | @dataclasses.dataclass() |
43 | | -class Session(Obj, EnvironmentMixin): |
| 44 | +class Session(Obj, EnvironmentMixin, OptionsMixin): |
44 | 45 | """:term:`tmux(1)` :term:`Session` [session_manual]_. |
45 | 46 |
|
46 | 47 | Holds :class:`Window` objects. |
@@ -76,6 +77,7 @@ class Session(Obj, EnvironmentMixin): |
76 | 77 | https://man.openbsd.org/tmux.1#DESCRIPTION. Accessed April 1st, 2018. |
77 | 78 | """ |
78 | 79 |
|
| 80 | + default_option_scope: OptionScope | None = None |
79 | 81 | server: Server |
80 | 82 |
|
81 | 83 | def refresh(self) -> None: |
@@ -193,153 +195,6 @@ def cmd( |
193 | 195 | Commands (tmux-like) |
194 | 196 | """ |
195 | 197 |
|
196 | | - def set_option( |
197 | | - self, |
198 | | - option: str, |
199 | | - value: str | int, |
200 | | - global_: bool = False, |
201 | | - ) -> Session: |
202 | | - """Set option ``$ tmux set-option <option> <value>``. |
203 | | -
|
204 | | - Parameters |
205 | | - ---------- |
206 | | - option : str |
207 | | - the window option. such as 'default-shell'. |
208 | | - value : str, int, or bool |
209 | | - True/False will turn in 'on' and 'off'. You can also enter 'on' or |
210 | | - 'off' directly. |
211 | | - _global : bool, optional |
212 | | - check for option globally across all servers (-g) |
213 | | -
|
214 | | - Raises |
215 | | - ------ |
216 | | - :exc:`exc.OptionError`, :exc:`exc.UnknownOption`, |
217 | | - :exc:`exc.InvalidOption`, :exc:`exc.AmbiguousOption` |
218 | | -
|
219 | | - Notes |
220 | | - ----- |
221 | | - .. todo:: |
222 | | -
|
223 | | - Needs tests |
224 | | - """ |
225 | | - if isinstance(value, bool) and value: |
226 | | - value = "on" |
227 | | - elif isinstance(value, bool) and not value: |
228 | | - value = "off" |
229 | | - |
230 | | - tmux_args: tuple[str | int, ...] = () |
231 | | - |
232 | | - if global_: |
233 | | - tmux_args += ("-g",) |
234 | | - |
235 | | - assert isinstance(option, str) |
236 | | - assert isinstance(value, (str, int)) |
237 | | - |
238 | | - tmux_args += ( |
239 | | - option, |
240 | | - value, |
241 | | - ) |
242 | | - |
243 | | - proc = self.cmd("set-option", *tmux_args) |
244 | | - |
245 | | - if isinstance(proc.stderr, list) and len(proc.stderr): |
246 | | - handle_option_error(proc.stderr[0]) |
247 | | - |
248 | | - return self |
249 | | - |
250 | | - def show_options( |
251 | | - self, |
252 | | - global_: bool | None = False, |
253 | | - ) -> dict[str, str | int]: |
254 | | - """Return dict of options for the session. |
255 | | -
|
256 | | - Parameters |
257 | | - ---------- |
258 | | - _global : bool, optional |
259 | | - Pass ``-g`` flag for global variable (server-wide) |
260 | | -
|
261 | | - Returns |
262 | | - ------- |
263 | | - :py:obj:`dict` |
264 | | -
|
265 | | - Notes |
266 | | - ----- |
267 | | - Uses ``_global`` for keyword name instead of ``global`` to avoid |
268 | | - colliding with reserved keyword. |
269 | | - """ |
270 | | - tmux_args: tuple[str, ...] = () |
271 | | - |
272 | | - if global_: |
273 | | - tmux_args += ("-g",) |
274 | | - |
275 | | - tmux_args += ("show-options",) |
276 | | - session_output = self.cmd(*tmux_args).stdout |
277 | | - |
278 | | - session_options: dict[str, str | int] = {} |
279 | | - for item in session_output: |
280 | | - key, val = item.split(" ", maxsplit=1) |
281 | | - assert isinstance(key, str) |
282 | | - assert isinstance(val, str) |
283 | | - |
284 | | - if isinstance(val, str) and val.isdigit(): |
285 | | - session_options[key] = int(val) |
286 | | - |
287 | | - return session_options |
288 | | - |
289 | | - def show_option( |
290 | | - self, |
291 | | - option: str, |
292 | | - global_: bool = False, |
293 | | - ) -> str | int | bool | None: |
294 | | - """Return option value for the target session. |
295 | | -
|
296 | | - Parameters |
297 | | - ---------- |
298 | | - option : str |
299 | | - option name |
300 | | - _global : bool, optional |
301 | | - use global option scope, same as ``-g`` |
302 | | -
|
303 | | - Returns |
304 | | - ------- |
305 | | - str, int, or bool |
306 | | -
|
307 | | - Raises |
308 | | - ------ |
309 | | - :exc:`exc.OptionError`, :exc:`exc.UnknownOption`, |
310 | | - :exc:`exc.InvalidOption`, :exc:`exc.AmbiguousOption` |
311 | | -
|
312 | | - Notes |
313 | | - ----- |
314 | | - Uses ``_global`` for keyword name instead of ``global`` to avoid |
315 | | - colliding with reserved keyword. |
316 | | -
|
317 | | - Test and return True/False for on/off string. |
318 | | - """ |
319 | | - tmux_args: tuple[str, ...] = () |
320 | | - |
321 | | - if global_: |
322 | | - tmux_args += ("-g",) |
323 | | - |
324 | | - tmux_args += (option,) |
325 | | - |
326 | | - cmd = self.cmd("show-options", *tmux_args) |
327 | | - |
328 | | - if isinstance(cmd.stderr, list) and len(cmd.stderr): |
329 | | - handle_option_error(cmd.stderr[0]) |
330 | | - |
331 | | - if not len(cmd.stdout): |
332 | | - return None |
333 | | - |
334 | | - value_raw: list[str] = next(item.split(" ") for item in cmd.stdout) |
335 | | - |
336 | | - assert isinstance(value_raw[0], str) |
337 | | - assert isinstance(value_raw[1], str) |
338 | | - |
339 | | - value: str | int = int(value_raw[1]) if value_raw[1].isdigit() else value_raw[1] |
340 | | - |
341 | | - return value |
342 | | - |
343 | 198 | def select_window(self, target_window: str | int) -> Window: |
344 | 199 | """Select window and return the selected window. |
345 | 200 |
|
|
0 commit comments