Skip to content

Commit

Permalink
refactor: add decorator for requiring node highlight
Browse files Browse the repository at this point in the history
  • Loading branch information
kraanzu committed Sep 18, 2024
1 parent 65b89ba commit 6bafb3e
Show file tree
Hide file tree
Showing 3 changed files with 30 additions and 27 deletions.
10 changes: 10 additions & 0 deletions dooit/ui/widgets/trees/_decorators.py
Original file line number Diff line number Diff line change
Expand Up @@ -33,3 +33,13 @@ def wrapper(self: "ModelTree", *args, **kwargs) -> Any:
self.force_refresh()

return wrapper


def require_highlighted_node(func: Callable) -> Callable:
def wrapper(self: "ModelTree", *args, **kwargs) -> Any:
if self.highlighted is None:
raise ValueError("No node is currently highlighted")

return func(self, *args, **kwargs)

return wrapper
30 changes: 16 additions & 14 deletions dooit/ui/widgets/trees/base_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
from dooit.api.todo import Todo
from dooit.api.workspace import Workspace
from collections import defaultdict
from dooit.ui.widgets.trees._decorators import require_highlighted_node

ModelType = Union[Todo, Workspace]

Expand All @@ -24,20 +25,21 @@ def tui(self) -> "Dooit":
raise ValueError("App is not a Dooit instance")

@property
@require_highlighted_node
def node(self) -> Option:
if self.highlighted is None:
raise ValueError("No node is currently highlighted")

assert self.highlighted is not None
return self.get_option_at_index(self.highlighted)

def action_cursor_down(self) -> None:
if self.highlighted == len(self._options) - 1:
return

return super().action_cursor_down()

def action_cursor_up(self) -> None:
if self.highlighted == 0:
return

return super().action_cursor_up()
# TODO: Uncomment this:
#
# def action_cursor_down(self) -> None:
# if self.highlighted == len(self._options) - 1:
# return
#
# return super().action_cursor_down()
#
# def action_cursor_up(self) -> None:
# if self.highlighted == 0:
# return
#
# return super().action_cursor_up()
17 changes: 4 additions & 13 deletions dooit/ui/widgets/trees/model_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
from dooit.ui.widgets.renderers.base_renderer import BaseRenderer
from .base_tree import BaseTree
from ._render_dict import RenderDict
from ._decorators import fix_highlight, refresh_tree
from ._decorators import fix_highlight, refresh_tree, require_highlighted_node

ModelType = TypeVar("ModelType", bound=Union[Todo, Workspace])
RenderDictType = TypeVar("RenderDictType", bound=RenderDict)
Expand Down Expand Up @@ -145,10 +145,8 @@ def _toggle_expand_node(self, _id: str) -> None:
else:
self._expand_node(_id)

@require_highlighted_node
def toggle_expand(self) -> None:
if self.highlighted is None or not self.node.id:
return

self._toggle_expand_node(self.node.id)

def _toggle_expand_parent(self, _id: str) -> None:
Expand All @@ -161,13 +159,8 @@ def _toggle_expand_parent(self, _id: str) -> None:
self.highlight_id(parent_id)
self._toggle_expand_node(parent_id)

@require_highlighted_node
def toggle_expand_parent(self) -> None:
if self.highlighted is None:
return

if not self.node.id:
return

self._toggle_expand_parent(self.node.id)

def _create_child_node(self) -> ModelType:
Expand Down Expand Up @@ -201,10 +194,8 @@ def add_sibling(self):
self.start_edit("description")

@refresh_tree
@require_highlighted_node
def remove_node(self):
if self.highlighted is None:
return

self.current_model.drop()

@refresh_tree
Expand Down

0 comments on commit 6bafb3e

Please sign in to comment.