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