Skip to content

Commit

Permalink
feat: connect formatter funcs with config
Browse files Browse the repository at this point in the history
TODO: optimize formatter calls
  • Loading branch information
kraanzu committed Sep 27, 2024
1 parent 80f76b7 commit 3bf798e
Show file tree
Hide file tree
Showing 4 changed files with 29 additions and 7 deletions.
14 changes: 13 additions & 1 deletion dooit/ui/api/api_components/formatters/formatter_store.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from typing import Callable, Optional
from typing import Any, Callable, List, Optional
from uuid import uuid4
from dataclasses import dataclass

from dooit.api.workspace import ModelType


@dataclass
class FormatterFunc:
Expand Down Expand Up @@ -32,3 +34,13 @@ def add(self, func: Callable, id: Optional[str] = None) -> str:
@trigger_refresh
def remove(self, id: str) -> None:
self.formatters.pop(id, None)

@property
def formatter_functions(self) -> List[Callable]:
return [formatter.func for formatter in self.formatters.values()]

def format_value(self, value: Any, model: ModelType) -> str:
for func in self.formatter_functions:
value = func(value, model)

return value
18 changes: 14 additions & 4 deletions dooit/ui/widgets/renderers/base_renderer.py
Original file line number Diff line number Diff line change
@@ -1,16 +1,19 @@
from typing import Dict, List, Union
from typing import TYPE_CHECKING, Dict, List, Union
from rich.console import RenderableType
from rich.table import Table
from dooit.api import Todo, Workspace
from ..inputs.simple_input import SimpleInput

ModelType = Union[Todo, Workspace]

if TYPE_CHECKING:
from dooit.ui.widgets.trees.model_tree import ModelTree


class BaseRenderer:
editing: str = ""

def __init__(self, model: ModelType, tree):
def __init__(self, model: ModelType, tree: "ModelTree"):
self._model = model
self.tree = tree
self.post_init()
Expand Down Expand Up @@ -64,14 +67,21 @@ def make_renderable(self) -> RenderableType:
row.append("")

for item in layout:

attr = item.value
if attr == "description":
table.add_column(attr, ratio=1)
else:
table.add_column(attr, width=self._get_max_width(attr))

row.append(self._get_component(attr).render())
component = self._get_component(attr)

if component.is_editing:
item = component.render()
else:
formatter = self.tree.formatter
item = getattr(formatter, attr).format_value(component.model_value, component.model)

row.append(item)

table.add_row(*row)
return table
Expand Down
2 changes: 1 addition & 1 deletion dooit/ui/widgets/trees/todos_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -34,7 +34,7 @@ def _switch_to_workspace(self) -> None:

@property
def formatter(self) -> "TodoFormatter":
return self.app.api.formatters.todos
return self.app.api.formatter.todos

@property
def layout(self):
Expand Down
2 changes: 1 addition & 1 deletion dooit/ui/widgets/trees/workspaces_tree.py
Original file line number Diff line number Diff line change
Expand Up @@ -30,7 +30,7 @@ def _get_children(self, id: str) -> List[Workspace]:

@property
def formatter(self) -> "WorkspaceFormatter":
return self.app.api.formatters.workspaces
return self.app.api.formatter.workspaces

@property
def layout(self):
Expand Down

0 comments on commit 3bf798e

Please sign in to comment.