Skip to content

Commit

Permalink
update noto
Browse files Browse the repository at this point in the history
  • Loading branch information
goFrendiAsgard committed Sep 10, 2024
1 parent 14d883c commit 8b77334
Show file tree
Hide file tree
Showing 31 changed files with 531 additions and 248 deletions.
4 changes: 2 additions & 2 deletions pyproject.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[tool.poetry]
name = "zrb-noto"
version = "0.0.6"
version = "0.0.10"
description = "Personal todo and logging management"
authors = ["Go Frendi Gunawan <gofrendiasgard@gmail.com>"]
license = "AGPL-3.0-or-later"
Expand All @@ -12,7 +12,7 @@ keywords = []

[tool.poetry.dependencies]
python = ">=3.10.0,<4.0.0"
zrb = ">=0.12.0"
zrb = ">=0.23.4"

[tool.poetry.dev-dependencies]
flake8 = "~7.0.0"
Expand Down
6 changes: 4 additions & 2 deletions src/zrb_noto/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,33 +2,35 @@
from .lint import lint_noto
from .list import list_noto
from .log import add_log, list_log, noto_log_group
from .note import note
from .sync import sync_noto
from .todo import (
add_todo,
archive_todo,
complete_todo,
delete_todo,
edit_todo,
find_todo,
list_todo,
noto_todo_group,
show_kanban,
start_todo,
stop_todo,
update_todo,
)
from .wiki import wiki_tasks

assert noto_group
assert noto_wiki_group
assert noto_log_group
assert note
assert add_log
assert list_log
assert noto_todo_group
assert add_todo
assert archive_todo
assert complete_todo
assert delete_todo
assert edit_todo
assert update_todo
assert find_todo
assert show_kanban
assert list_todo
Expand Down
2 changes: 2 additions & 0 deletions src/zrb_noto/_config.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,11 +11,13 @@
REMOTE_GIT_URL = os.getenv("NOTO_REMOTE_GIT", _DEFAULT_REMOTE_GIT_URL)
LOCAL_REPO_DIR = os.path.abspath(os.getenv("NOTO_LOCAL_REPO", f"{_HOME_DIR}/daily"))
TODO_FILE_PATH = os.getenv("NOTO_TODO_FILE", "todo.txt")
NOTE_FILE_PATH = os.getenv("NOTO_NOTE_FILE", "note.md")
DONE_FILE_PATH = os.getenv("NOTO_DONE_FILE", "done.txt")
WIKI_DIR_PATH = os.getenv("NOTO_WIKI_DIR", "wiki")
LOG_DIR_PATH = os.getenv("NOTO_LOG_DIR", "log")

TODO_ABS_FILE_PATH = os.path.abspath(os.path.join(LOCAL_REPO_DIR, TODO_FILE_PATH))
NOTE_ABS_FILE_PATH = os.path.abspath(os.path.join(LOCAL_REPO_DIR, NOTE_FILE_PATH))
DONE_ABS_FILE_PATH = os.path.abspath(os.path.join(LOCAL_REPO_DIR, DONE_FILE_PATH))
WIKI_ABS_DIR_PATH = os.path.abspath(os.path.join(LOCAL_REPO_DIR, WIKI_DIR_PATH))
LOG_ABS_DIR_PATH = os.path.abspath(os.path.join(LOCAL_REPO_DIR, LOG_DIR_PATH))
Expand Down
8 changes: 8 additions & 0 deletions src/zrb_noto/_helper.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,14 @@
import os
import shutil


def get_screen_width() -> int:
terminal_size = shutil.get_terminal_size()
return terminal_size.columns


def get_note_content(note_path: str) -> str:
if not os.path.isfile(note_path):
return ""
with open(note_path, "r") as f:
return f.read()
13 changes: 13 additions & 0 deletions src/zrb_noto/_input.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
from zrb import MultilineInput

from ._config import NOTE_ABS_FILE_PATH
from ._helper import get_note_content

content_input = MultilineInput(
name="content",
shortcut="c",
comment_prefix="<!--",
comment_suffix="-->",
extension="md",
default=lambda m: get_note_content(NOTE_ABS_FILE_PATH),
)
11 changes: 7 additions & 4 deletions src/zrb_noto/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -3,9 +3,9 @@
from zrb import Task, python_task, runner
from zrb.helper.task import show_lines

from ._config import IS_AUTO_SYNC
from ._config import CURRENT_TIME, IS_AUTO_SYNC, TODO_ABS_FILE_PATH
from ._group import noto_group
from .log._helper import get_pretty_log_lines
from .log._helper import get_log_file_name, get_pretty_log_lines
from .sync import create_sync_noto_task
from .todo._helper import get_pretty_todo_item_lines, get_todo_items

Expand All @@ -18,8 +18,11 @@
)
def list_noto(*args: Any, **kwargs: Any):
task: Task = kwargs.get("_task")
items = get_todo_items(completed=False)
show_lines(task, *get_pretty_log_lines(), "", *get_pretty_todo_item_lines(items))
items = get_todo_items(file_name=TODO_ABS_FILE_PATH, completed=False)
file_name = get_log_file_name(CURRENT_TIME)
show_lines(
task, *get_pretty_log_lines(file_name), "", *get_pretty_todo_item_lines(items)
)


if IS_AUTO_SYNC:
Expand Down
2 changes: 2 additions & 0 deletions src/zrb_noto/log/__init__.py
Original file line number Diff line number Diff line change
@@ -1,7 +1,9 @@
from ._group import noto_log_group
from .add import add_log
from .edit import edit_log
from .list import list_log

assert noto_log_group
assert add_log
assert list_log
assert edit_log
19 changes: 6 additions & 13 deletions src/zrb_noto/log/_helper.py
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,10 @@
import re
from datetime import datetime
from pathlib import Path
from typing import List, Optional

from zrb.helper.accessories.color import colored

from .._config import CURRENT_TIME, LOG_ABS_DIR_PATH
from .._config import LOG_ABS_DIR_PATH

_STATUS_COLOR_MAP = {
"START": "light_cyan",
Expand All @@ -18,7 +17,7 @@
}


def get_log_file_name(current_time: datetime = CURRENT_TIME) -> str:
def get_log_file_name(current_time: datetime) -> str:
year = current_time.year
month = current_time.strftime("%m")
date = current_time.strftime("%d")
Expand All @@ -27,7 +26,7 @@ def get_log_file_name(current_time: datetime = CURRENT_TIME) -> str:
)


def append_log_item(text: str, current_time: datetime = CURRENT_TIME) -> str:
def append_log_item(text: str, current_time: datetime) -> str:
file_name = get_log_file_name(current_time=current_time)
dir_path = Path(os.path.dirname(file_name))
dir_path.mkdir(parents=True, exist_ok=True)
Expand All @@ -39,16 +38,12 @@ def append_log_item(text: str, current_time: datetime = CURRENT_TIME) -> str:
file.write("\n")


def _get_log_lines(file_name: Optional[str] = None) -> List[str]:
if file_name is None:
file_name = get_log_file_name()
def _get_log_lines(file_name: str) -> list[str]:
log_str = get_log(file_name)
return log_str.split("\n")


def get_pretty_log_lines(file_name: Optional[str] = None) -> List[str]:
if file_name is None:
file_name = get_log_file_name()
def get_pretty_log_lines(file_name: str) -> list[str]:
log_str = get_log(file_name)
for keyword, color in _STATUS_COLOR_MAP.items():
log_str = re.sub(
Expand All @@ -62,9 +57,7 @@ def get_pretty_log_lines(file_name: Optional[str] = None) -> List[str]:
return log_str.split("\n")


def get_log(file_name: Optional[str] = None) -> str:
if file_name is None:
file_name = get_log_file_name()
def get_log(file_name: str) -> str:
dir_path = Path(os.path.dirname(file_name))
dir_path.mkdir(parents=True, exist_ok=True)
if not os.path.isfile(file_name):
Expand Down
43 changes: 43 additions & 0 deletions src/zrb_noto/log/_input.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,43 @@
import datetime
import os
from collections.abc import Mapping
from typing import Any

from zrb import MultilineInput, StrInput

from .._config import CURRENT_TIME
from ._helper import get_log_file_name


def _get_default_content(input_map: Mapping[str, Any]) -> str:
date_str = input_map.get("date")
current_time = datetime.strptime(date_str, "%Y-%m-%d")
file_name = get_log_file_name(current_time)
if not os.path.isfile(file_name):
return ""
with open(file_name, "r") as f:
return f.read()


text_input = StrInput(
name="text",
shortcut="t",
prompt="Text",
default="",
)

date_input = StrInput(
name="date",
shortcut="d",
prompt="Date (Y-m-d)",
default=CURRENT_TIME.strftime("%Y-%m-%d"),
)

content_input = MultilineInput(
name="content",
shortcut="c",
comment_prefix="<!--",
comment_suffix="-->",
extension="md",
default=_get_default_content,
)
30 changes: 9 additions & 21 deletions src/zrb_noto/log/add.py
Original file line number Diff line number Diff line change
@@ -1,48 +1,36 @@
from zrb import StrInput, Task, python_task, runner
from zrb import Task, python_task, runner
from zrb.helper.accessories.color import colored
from zrb.helper.task import show_lines

from .._config import IS_AUTO_SYNC
from .._config import CURRENT_TIME, IS_AUTO_SYNC
from ..sync import create_sync_noto_task
from ._group import noto_log_group
from ._helper import append_log_item, get_pretty_log_lines
from ._helper import append_log_item, get_log_file_name, get_pretty_log_lines
from ._input import text_input


@python_task(
name="add-item",
inputs=[
StrInput(
name="text",
shortcut="t",
prompt="Text",
default="",
),
],
inputs=[text_input],
retry=0,
)
def add_item(*args, **kwargs):
task: Task = kwargs.get("_task")
text = kwargs.get("text")
task.print_out(colored(f"Adding log: {text}", color="yellow"))
append_log_item(text)
append_log_item(text, current_time=CURRENT_TIME)


@python_task(
name="add",
group=noto_log_group,
inputs=[
StrInput(
name="text",
shortcut="t",
prompt="Text",
default="",
),
],
inputs=[text_input],
retry=0,
)
def add_log(*args, **kwargs):
task: Task = kwargs.get("_task")
show_lines(task, *get_pretty_log_lines())
file_name = get_log_file_name(CURRENT_TIME)
show_lines(task, *get_pretty_log_lines(file_name))


if IS_AUTO_SYNC:
Expand Down
57 changes: 57 additions & 0 deletions src/zrb_noto/log/edit.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,57 @@
from datetime import datetime

from zrb import Task, python_task, runner
from zrb.helper.task import show_lines

from .._config import IS_AUTO_SYNC
from ..sync import create_sync_noto_task
from ._group import noto_log_group
from ._helper import get_log_file_name, get_pretty_log_lines
from ._input import content_input, date_input


@python_task(
name="save-file",
inputs=[
date_input,
content_input,
],
retry=0,
)
def save_file(*args, **kwargs):
date_str = kwargs.get("date")
content = kwargs.get("content")
current_time = datetime.strptime(date_str, "%Y-%m-%d")
file_name = get_log_file_name(current_time)
with open(file_name, "w") as f:
f.write(content)


@python_task(
name="edit",
group=noto_log_group,
inputs=[
date_input,
content_input,
],
retry=0,
)
def edit_log(*args, **kwargs):
task: Task = kwargs.get("_task")
date_str = kwargs.get("date")
current_time = datetime.strptime(date_str, "%Y-%m-%d")
file_name = get_log_file_name(current_time)
show_lines(task, *get_pretty_log_lines(file_name))


if IS_AUTO_SYNC:
(
create_sync_noto_task(name="pre-sync")
>> save_file
>> create_sync_noto_task(name="post-sync")
>> edit_log
)
else:
save_file >> edit_log

runner.register(edit_log)
14 changes: 4 additions & 10 deletions src/zrb_noto/log/list.py
Original file line number Diff line number Diff line change
@@ -1,25 +1,19 @@
from datetime import datetime

from zrb import StrInput, Task, python_task, runner
from zrb import Task, python_task, runner
from zrb.helper.task import show_lines

from .._config import CURRENT_TIME, IS_AUTO_SYNC
from .._config import IS_AUTO_SYNC
from ..sync import create_sync_noto_task
from ._group import noto_log_group
from ._helper import get_log_file_name, get_pretty_log_lines
from ._input import date_input


@python_task(
name="list",
group=noto_log_group,
inputs=[
StrInput(
name="date",
shortcut="d",
prompt="Date (Y-m-d)",
default=CURRENT_TIME.strftime("%Y-%m-%d"),
),
],
inputs=[date_input],
retry=0,
)
def list_log(*args, **kwargs):
Expand Down
Loading

0 comments on commit 8b77334

Please sign in to comment.