Skip to content

Commit

Permalink
feat: auto detect extra params for formatters
Browse files Browse the repository at this point in the history
  • Loading branch information
kraanzu committed Oct 18, 2024
1 parent c033d56 commit 2b20469
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
19 changes: 17 additions & 2 deletions dooit/ui/api/api_components/formatters/formatter_store.py
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
from typing import TYPE_CHECKING, Any, Callable, List, Optional
from typing import TYPE_CHECKING, Any, Callable, Dict, List, Optional
from uuid import uuid4
from dataclasses import dataclass

Expand Down Expand Up @@ -80,11 +80,26 @@ def current_formatter(self) -> FormatterFunc:

return enabled_formatters[-1]

def _get_function_params(self, func: Callable) -> List[str]:
return list(func.__code__.co_varnames)

def format_value(self, value: Any, model: ModelType) -> str:
params = dict(api=self.api)

def get_extra_args(func: Callable) -> Dict[str, Any]:
func_params = self._get_function_params(func)
extra_args = {}

for param in func_params:
if param in params:
extra_args[param] = params[param]

return extra_args

res = None

for func in reversed(self.formatter_functions):
res = func(value, model)
res = func(value, model, **get_extra_args(func))
if res is not None:
return res

Expand Down
5 changes: 1 addition & 4 deletions dooit/utils/default_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,6 @@
from dooit.ui.events.events import ModeChanged, Startup
from dooit.ui.widgets.bars import StatusBarWidget
from rich.text import Text
from functools import partial


@subscribe(ModeChanged)
Expand Down Expand Up @@ -173,9 +172,7 @@ def formatter_setup(api: DooitAPI, _):
api.formatter.todos.status.add(todo_status_formatter)
api.formatter.todos.description.add(todo_desc_formatter)
api.formatter.todos.due.add(todo_due_formatter)
api.formatter.todos.urgency.add(
partial(todo_urgency_formatter, api=api),
)
api.formatter.todos.urgency.add(todo_urgency_formatter)


@subscribe(Startup)
Expand Down

0 comments on commit 2b20469

Please sign in to comment.