Skip to content

Commit 7dd11fe

Browse files
committed
hooks(refactor): remove redundant bulk operations methods
why: These methods are redundant with existing API what: - Remove get_hook_indices: use show_hook().keys() instead - Remove get_hook_values: use show_hook() directly (returns SparseArray) - Remove clear_hook: use unset_hook() without index (clears all) - Remove append_hook: use set_hook(..., append=True) instead - Update set_hooks to use unset_hook instead of clear_hook Squash target: d4de173 (hooks feat: add bulk operations)
1 parent a785f74 commit 7dd11fe

File tree

1 file changed

+1
-222
lines changed

1 file changed

+1
-222
lines changed

src/libtmux/hooks.py

Lines changed: 1 addition & 222 deletions
Original file line numberDiff line numberDiff line change
@@ -403,117 +403,6 @@ def show_hook(
403403
hooks = Hooks.from_stdout(hooks_output)
404404
return getattr(hooks, hook.replace("-", "_"), None)
405405

406-
# --- Bulk operations API ---
407-
408-
def get_hook_indices(
409-
self,
410-
hook: str,
411-
global_: bool = False,
412-
scope: OptionScope | _DefaultOptionScope | None = DEFAULT_OPTION_SCOPE,
413-
ignore_errors: bool | None = None,
414-
) -> list[int]:
415-
"""Get list of indices that exist for a hook.
416-
417-
Parameters
418-
----------
419-
hook : str
420-
Hook name, e.g. 'session-renamed'
421-
global_ : bool
422-
Use global hooks
423-
scope : OptionScope | None
424-
Scope for the hook
425-
ignore_errors : bool | None
426-
Suppress errors
427-
428-
Returns
429-
-------
430-
list[int]
431-
Sorted list of indices that exist for this hook.
432-
433-
Examples
434-
--------
435-
>>> session.set_hook('session-renamed[0]', 'display-message "test"')
436-
Session($...)
437-
438-
>>> session.set_hook('session-renamed[5]', 'display-message "test"')
439-
Session($...)
440-
441-
>>> session.get_hook_indices('session-renamed')
442-
[0, 5]
443-
444-
>>> session.clear_hook('session-renamed')
445-
Session($...)
446-
"""
447-
hooks_output = self._show_hook(
448-
hook=hook,
449-
global_=global_,
450-
scope=scope,
451-
ignore_errors=ignore_errors,
452-
)
453-
if hooks_output is None:
454-
return []
455-
hooks = Hooks.from_stdout(hooks_output)
456-
hook_array = getattr(hooks, hook.replace("-", "_"), None)
457-
if hook_array is None or not isinstance(hook_array, SparseArray):
458-
return []
459-
return sorted(hook_array.keys())
460-
461-
def get_hook_values(
462-
self,
463-
hook: str,
464-
global_: bool = False,
465-
scope: OptionScope | _DefaultOptionScope | None = DEFAULT_OPTION_SCOPE,
466-
ignore_errors: bool | None = None,
467-
) -> SparseArray[str]:
468-
"""Get all indexed values for a hook as SparseArray.
469-
470-
Parameters
471-
----------
472-
hook : str
473-
Hook name, e.g. 'session-renamed'
474-
global_ : bool
475-
Use global hooks
476-
scope : OptionScope | None
477-
Scope for the hook
478-
ignore_errors : bool | None
479-
Suppress errors
480-
481-
Returns
482-
-------
483-
SparseArray[str]
484-
SparseArray containing hook values at their original indices.
485-
486-
Examples
487-
--------
488-
>>> session.set_hook('session-renamed[0]', 'display-message "first"')
489-
Session($...)
490-
491-
>>> values = session.get_hook_values('session-renamed')
492-
>>> 0 in values
493-
True
494-
495-
>>> 'display-message' in values[0]
496-
True
497-
498-
>>> session.clear_hook('session-renamed')
499-
Session($...)
500-
"""
501-
hooks_output = self._show_hook(
502-
hook=hook,
503-
global_=global_,
504-
scope=scope,
505-
ignore_errors=ignore_errors,
506-
)
507-
if hooks_output is None:
508-
return SparseArray()
509-
hooks = Hooks.from_stdout(hooks_output)
510-
hook_array = getattr(hooks, hook.replace("-", "_"), None)
511-
if hook_array is None or not isinstance(hook_array, SparseArray):
512-
return SparseArray()
513-
# Type narrowing doesn't work with isinstance for generics, so cast
514-
result: SparseArray[str] = hook_array
515-
return result
516-
517406
def set_hooks(
518407
self,
519408
hook: str,
@@ -589,7 +478,7 @@ def set_hooks(
589478
Session($...)
590479
"""
591480
if clear_existing:
592-
self.clear_hook(hook, global_=global_, scope=scope)
481+
self.unset_hook(hook, global_=global_, scope=scope)
593482

594483
# Convert list to dict with sequential indices
595484
if isinstance(values, list):
@@ -604,113 +493,3 @@ def set_hooks(
604493
)
605494

606495
return self
607-
608-
def clear_hook(
609-
self,
610-
hook: str,
611-
global_: bool | None = None,
612-
scope: OptionScope | _DefaultOptionScope | None = DEFAULT_OPTION_SCOPE,
613-
ignore_errors: bool | None = None,
614-
) -> Self:
615-
"""Unset all indexed values for a hook.
616-
617-
Parameters
618-
----------
619-
hook : str
620-
Hook name, e.g. 'session-renamed'
621-
global_ : bool | None
622-
Use global hooks
623-
scope : OptionScope | None
624-
Scope for the hook
625-
ignore_errors : bool | None
626-
Suppress errors when unsetting
627-
628-
Returns
629-
-------
630-
Self
631-
Returns self for method chaining.
632-
633-
Examples
634-
--------
635-
>>> session.set_hook('session-renamed[0]', 'display-message "test"')
636-
Session($...)
637-
638-
>>> session.set_hook('session-renamed[1]', 'display-message "test2"')
639-
Session($...)
640-
641-
>>> session.get_hook_indices('session-renamed')
642-
[0, 1]
643-
644-
>>> session.clear_hook('session-renamed')
645-
Session($...)
646-
647-
>>> session.get_hook_indices('session-renamed')
648-
[]
649-
"""
650-
indices = self.get_hook_indices(
651-
hook,
652-
global_=global_ if global_ is not None else False,
653-
scope=scope,
654-
ignore_errors=ignore_errors,
655-
)
656-
for index in indices:
657-
self.unset_hook(
658-
f"{hook}[{index}]",
659-
global_=global_,
660-
scope=scope,
661-
ignore_errors=ignore_errors,
662-
)
663-
return self
664-
665-
def append_hook(
666-
self,
667-
hook: str,
668-
value: str,
669-
global_: bool | None = None,
670-
scope: OptionScope | _DefaultOptionScope | None = DEFAULT_OPTION_SCOPE,
671-
) -> Self:
672-
"""Append a hook value at the next available index.
673-
674-
Parameters
675-
----------
676-
hook : str
677-
Hook name, e.g. 'session-renamed'
678-
value : str
679-
Hook command to append
680-
global_ : bool | None
681-
Use global hooks
682-
scope : OptionScope | None
683-
Scope for the hook
684-
685-
Returns
686-
-------
687-
Self
688-
Returns self for method chaining.
689-
690-
Examples
691-
--------
692-
>>> session.set_hook('session-renamed[0]', 'display-message "first"')
693-
Session($...)
694-
695-
>>> session.append_hook('session-renamed', 'display-message "appended"')
696-
Session($...)
697-
698-
>>> session.get_hook_indices('session-renamed')
699-
[0, 1]
700-
701-
>>> session.clear_hook('session-renamed')
702-
Session($...)
703-
"""
704-
indices = self.get_hook_indices(
705-
hook,
706-
global_=global_ if global_ is not None else False,
707-
scope=scope,
708-
)
709-
next_index = max(indices) + 1 if indices else 0
710-
self.set_hook(
711-
f"{hook}[{next_index}]",
712-
value,
713-
global_=global_,
714-
scope=scope,
715-
)
716-
return self

0 commit comments

Comments
 (0)