From 653eb48cae27d63b44a876cf2a9dad328e43629d Mon Sep 17 00:00:00 2001 From: Jason Lee Date: Thu, 23 Mar 2023 21:11:19 +0800 Subject: [Feat] Deployed PyPI pakcage v0.1.2 issue #1 --- README.md | 1 - dist/girok-0.1.0-py3-none-any.whl | Bin 0 -> 35642 bytes dist/girok-0.1.0.tar.gz | Bin 0 -> 28634 bytes dist/girok-0.1.1-py3-none-any.whl | Bin 0 -> 35713 bytes dist/girok-0.1.1.tar.gz | Bin 0 -> 28690 bytes dist/girok-0.1.2-py3-none-any.whl | Bin 0 -> 35877 bytes dist/girok-0.1.2.tar.gz | Bin 0 -> 28749 bytes girok/__init__.py | 1 + girok/__main__.py | 0 girok/api/auth.py | 34 + girok/api/category.py | 131 ++ girok/api/task.py | 146 +++ girok/calendar_cli/calendar_app.py | 37 + girok/calendar_cli/calendar_container.py | 324 +++++ girok/calendar_cli/calendar_main.css | 252 ++++ girok/calendar_cli/calendar_main.py | 181 +++ girok/calendar_cli/sidebar.py | 201 +++ .../textual_demo_2023-03-11T22_49_21_681307.svg | 246 ++++ girok/commands/auth.py | 89 ++ girok/commands/calendar.py | 10 + girok/commands/category.py | 124 ++ girok/commands/task.py | 569 +++++++++ girok/config.json | 4 + girok/config.py | 22 + girok/constants.py | 53 + girok/girok.py | 48 + girok/styles/stopwatch03.css | 53 + girok/utils/__init__.py | 0 girok/utils/auth.py | 54 + girok/utils/calendar.py | 132 ++ girok/utils/display.py | 253 ++++ girok/utils/general.py | 52 + girok/utils/task.py | 161 +++ new_requirements.txt | 43 + poetry.lock | 1277 ++++++++++++++++++++ processreq.py | 22 + pyproject.toml | 59 + tests/__init__.py | 0 38 files changed, 4578 insertions(+), 1 deletion(-) create mode 100644 dist/girok-0.1.0-py3-none-any.whl create mode 100644 dist/girok-0.1.0.tar.gz create mode 100644 dist/girok-0.1.1-py3-none-any.whl create mode 100644 dist/girok-0.1.1.tar.gz create mode 100644 dist/girok-0.1.2-py3-none-any.whl create mode 100644 dist/girok-0.1.2.tar.gz create mode 100644 girok/__init__.py create mode 100644 girok/__main__.py create mode 100644 girok/api/auth.py create mode 100644 girok/api/category.py create mode 100644 girok/api/task.py create mode 100644 girok/calendar_cli/calendar_app.py create mode 100644 girok/calendar_cli/calendar_container.py create mode 100644 girok/calendar_cli/calendar_main.css create mode 100644 girok/calendar_cli/calendar_main.py create mode 100644 girok/calendar_cli/sidebar.py create mode 100644 girok/calendar_cli/textual_demo_2023-03-11T22_49_21_681307.svg create mode 100644 girok/commands/auth.py create mode 100644 girok/commands/calendar.py create mode 100644 girok/commands/category.py create mode 100644 girok/commands/task.py create mode 100644 girok/config.json create mode 100644 girok/config.py create mode 100644 girok/constants.py create mode 100644 girok/girok.py create mode 100644 girok/styles/stopwatch03.css create mode 100644 girok/utils/__init__.py create mode 100644 girok/utils/auth.py create mode 100644 girok/utils/calendar.py create mode 100644 girok/utils/display.py create mode 100644 girok/utils/general.py create mode 100644 girok/utils/task.py create mode 100644 new_requirements.txt create mode 100644 poetry.lock create mode 100644 processreq.py create mode 100644 pyproject.toml create mode 100644 tests/__init__.py diff --git a/README.md b/README.md index b087646..e69de29 100644 --- a/README.md +++ b/README.md @@ -1 +0,0 @@ -# girok-cli \ No newline at end of file diff --git a/dist/girok-0.1.0-py3-none-any.whl b/dist/girok-0.1.0-py3-none-any.whl new file mode 100644 index 0000000..fb2c24f Binary files /dev/null and b/dist/girok-0.1.0-py3-none-any.whl differ diff --git a/dist/girok-0.1.0.tar.gz b/dist/girok-0.1.0.tar.gz new file mode 100644 index 0000000..0851a9d Binary files /dev/null and b/dist/girok-0.1.0.tar.gz differ diff --git a/dist/girok-0.1.1-py3-none-any.whl b/dist/girok-0.1.1-py3-none-any.whl new file mode 100644 index 0000000..52d4501 Binary files /dev/null and b/dist/girok-0.1.1-py3-none-any.whl differ diff --git a/dist/girok-0.1.1.tar.gz b/dist/girok-0.1.1.tar.gz new file mode 100644 index 0000000..2ea2e29 Binary files /dev/null and b/dist/girok-0.1.1.tar.gz differ diff --git a/dist/girok-0.1.2-py3-none-any.whl b/dist/girok-0.1.2-py3-none-any.whl new file mode 100644 index 0000000..43623d5 Binary files /dev/null and b/dist/girok-0.1.2-py3-none-any.whl differ diff --git a/dist/girok-0.1.2.tar.gz b/dist/girok-0.1.2.tar.gz new file mode 100644 index 0000000..fdebf13 Binary files /dev/null and b/dist/girok-0.1.2.tar.gz differ diff --git a/girok/__init__.py b/girok/__init__.py new file mode 100644 index 0000000..48fef32 --- /dev/null +++ b/girok/__init__.py @@ -0,0 +1 @@ +__version__ = "0.1.2" \ No newline at end of file diff --git a/girok/__main__.py b/girok/__main__.py new file mode 100644 index 0000000..e69de29 diff --git a/girok/api/auth.py b/girok/api/auth.py new file mode 100644 index 0000000..6d71a38 --- /dev/null +++ b/girok/api/auth.py @@ -0,0 +1,34 @@ +import requests +from girok.config import get_config +cfg = get_config() + +base_url = cfg.base_url + +def register(email, password): + resp = requests.post(base_url + "/register", json={ + "email": email, + "password": password + }) + return resp + + +def login(email, password): + files = { + "username": (None, email), + "password": (None, password) + } + + resp = requests.post(base_url + "/login", files=files) + return resp + + +def validate_access_token(access_token): + options = { + "headers": { + "Authorization": "Bearer " + access_token, + } + } + + + resp = requests.get(base_url + "/validate-access-token", headers=options['headers']) + return resp \ No newline at end of file diff --git a/girok/api/category.py b/girok/api/category.py new file mode 100644 index 0000000..f01d274 --- /dev/null +++ b/girok/api/category.py @@ -0,0 +1,131 @@ +import requests +from rich.console import Console + +from girok.config import get_config +import girok.utils.auth as auth_utils +import girok.utils.general as general_utils +import girok.utils.display as display_utils +import girok.constants as constants + +console = Console() +cfg = get_config() + +def get_categories(): + resp = requests.get( + cfg.base_url + "/categories", + headers=auth_utils.build_jwt_header(cfg.config_path) + ) + if resp.status_code == 200: + return general_utils.bytes2dict(resp.content) + + + +def add_category(cat_str: str, color=None): + cats = cat_str.split('/') + resp = requests.post( + cfg.base_url + "/categories", + json={ + "names": cats, + "color": color + }, + headers=auth_utils.build_jwt_header(cfg.config_path) + ) + return resp + + +def remove_category(cat_str: str): + cats = cat_str.split('/') + resp = requests.delete( + cfg.base_url + "/categories", + json={ + "cats": cats + }, + headers=auth_utils.build_jwt_header(cfg.config_path) + ) + + return resp + + +def rename_category(cat_str: str, new_name: str): + cats = cat_str.split('/') + resp = requests.patch( + cfg.base_url + "/categories/name", + json={ + "cats": cats, + "new_name": new_name + }, + headers=auth_utils.build_jwt_header(cfg.config_path) + ) + + return resp + + +def move_category(cat_str: str, new_parent_cat_str: str): + if cat_str.endswith('/'): + cat_str = cat_str[:-1] + if new_parent_cat_str.endswith('/'): + new_parent_cat_str = new_parent_cat_str[:-1] + + cats = cat_str.split('/') if cat_str else [] + new_parent_cats = new_parent_cat_str.split('/') if new_parent_cat_str else [] + resp = requests.patch( + cfg.base_url + "/categories/parent", + json={ + "cats": cats, + "new_parent_cats": new_parent_cats + }, + headers=auth_utils.build_jwt_header(cfg.config_path) + ) + return resp + + +def get_last_cat_id(cats: list): + resp = requests.get( + cfg.base_url + "/categories/last-cat-id", + json={ + "cats": cats + }, + headers=auth_utils.build_jwt_header(cfg.config_path) + ) + if resp.status_code == 200: + return general_utils.bytes2dict(resp.content)['cat_id'] + elif resp.status_code == 400: + err_msg = general_utils.bytes2dict(resp.content)['detail'] + display_utils.center_print(err_msg, constants.DISPLAY_TERMINAL_COLOR_ERROR) + exit(0) + else: + display_utils.center_print(resp.content, constants.DISPLAY_TERMINAL_COLOR_ERROR) + exit(0) + + +def get_category_color(cat_id: int): + resp = requests.get( + cfg.base_url + f"/categories/{cat_id}/color", + headers=auth_utils.build_jwt_header(cfg.config_path) + ) + + if resp.status_code == 200: + return general_utils.bytes2dict(resp.content)['color'] + elif resp.status_code == 400: + err_msg = general_utils.bytes2dict(resp.content)['detail'] + display_utils.center_print(err_msg, constants.DISPLAY_TERMINAL_COLOR_ERROR) + exit(0) + else: + display_utils.center_print(resp.content, constants.DISPLAY_TERMINAL_COLOR_ERROR) + exit(0) + + +def get_color_dict(): + resp = requests.get( + cfg.base_url + "/categories/color", + headers=auth_utils.build_jwt_header(cfg.config_path) + ) + if resp.status_code == 200: + color_dict = general_utils.bytes2dict(resp.content) + return color_dict + elif resp.status_code == 400: + err_msg = general_utils.bytes2dict(resp.content)['detail'] + display_utils.center_print(err_msg, constants.DISPLAY_TERMINAL_COLOR_ERROR) + else: + display_utils.center_print("Error occurred.", constants.DISPLAY_TERMINAL_COLOR_ERROR) + \ No newline at end of file diff --git a/girok/api/task.py b/girok/api/task.py new file mode 100644 index 0000000..04e972b --- /dev/null +++ b/girok/api/task.py @@ -0,0 +1,146 @@ +from typing import Union +from textual import log +from datetime import datetime +import requests + +from girok.config import get_config +import girok.utils.auth as auth_utils +import girok.utils.display as display_utils +import girok.utils.general as general_utils +import girok.utils.task as task_utils +import girok.constants as constants + +cfg = get_config() + +def create_task(task_data: dict): + print(task_data) + resp = requests.post( + cfg.base_url + "/tasks", + json=task_data, + headers=auth_utils.build_jwt_header(cfg.config_path) + ) + if resp.status_code == 201: + task = general_utils.bytes2dict(resp.content) + task_id = task['task_id'] + return task_id + elif resp.status_code == 400: + err_msg = general_utils.bytes2dict(resp.content)['detail'] + display_utils.center_print(err_msg, constants.DISPLAY_TERMINAL_COLOR_ERROR) + else: + display_utils.center_print("Error occurred.", constants.DISPLAY_TERMINAL_COLOR_ERROR) + + +def get_single_task(task_id: int): + resp = requests.get( + cfg.base_url + f"/tasks/{task_id}", + headers=auth_utils.build_jwt_header(cfg.config_path), + ) + if resp.status_code == 200: + task = general_utils.bytes2dict(resp.content) + return task + elif resp.status_code == 400: + err_msg = general_utils.bytes2dict(resp.content)['detail'] + display_utils.center_print(err_msg, constants.DISPLAY_TERMINAL_COLOR_ERROR) + else: + display_utils.center_print(resp.content, constants.DISPLAY_TERMINAL_COLOR_ERROR) + + + +def get_tasks( + cats: Union[list, None] = None, + start_date: Union[str, None] = None, + end_date: Union[str, None] = None, + tag: Union[str, None] = None, + priority: Union[int, None] = None, + no_priority: bool = False, + view: str = "category" +): + query_str_obj = { + "category": cats, + "start_date": start_date, + "end_date": end_date, + "tag": tag, + "priority": priority, + "no_priority": no_priority, + "view": view + } + resp = requests.get( + cfg.base_url + "/tasks", + headers=auth_utils.build_jwt_header(cfg.config_path), + params=query_str_obj + ) + return resp + + +def remove_task(task_id: int): + resp = requests.delete( + cfg.base_url + f"/tasks/{task_id}", + headers=auth_utils.build_jwt_header(cfg.config_path), + ) + return resp + + +def get_tags(): + resp = requests.get( + cfg.base_url + "/tasks/tags", + headers=auth_utils.build_jwt_header(cfg.config_path) + ) + + return resp + + +def change_task_tag(task_id: int, new_tag_name: str): + resp = requests.patch( + cfg.base_url + f"/tasks/{task_id}/tag", + headers=auth_utils.build_jwt_header(cfg.config_path), + json={ + "new_tag_name": new_tag_name + } + ) + + if resp.status_code == 204: + display_utils.center_print(f"Successfully renamed [ID: {task_id}] tag to {new_tag_name}.", "black on green") + elif resp.status_code == 400: + err_msg = general_utils.bytes2dict(resp.content)['detail'] + display_utils.center_print(err_msg, constants.DISPLAY_TERMINAL_COLOR_ERROR) + else: + display_utils.center_print(resp.content, constants.DISPLAY_TERMINAL_COLOR_ERROR) + + +def change_task_priority(task_id: int, new_priority: int): + resp = requests.patch( + cfg.base_url + f"/tasks/{task_id}/priority", + headers=auth_utils.build_jwt_header(cfg.config_path), + json={ + "new_priority": new_priority + } + ) + + if resp.status_code == 204: + display_utils.center_print(f"Successfully change [ID: {task_id}] priority to {new_priority}.", "black on green") + elif resp.status_code == 400: + err_msg = general_utils.bytes2dict(resp.content)['detail'] + display_utils.center_print(err_msg, constants.DISPLAY_TERMINAL_COLOR_ERROR) + else: + display_utils.center_print(resp.content, constants.DISPLAY_TERMINAL_COLOR_ERROR) + + +def change_task_date(task_id: int, new_date: str): + resp = requests.patch( + cfg.base_url + f"/tasks/{task_id}/date", + headers=auth_utils.build_jwt_header(cfg.config_path), + json={ + "new_date": new_date + } + ) + + if resp.status_code == 204: + display_utils.center_print(f"Successfully change [ID: {task_id}] date to {new_date}.", "black on green") + elif resp.status_code == 400: + err_msg = general_utils.bytes2dict(resp.content)['detail'] + display_utils.center_print(err_msg, constants.DISPLAY_TERMINAL_COLOR_ERROR) + else: + display_utils.center_print(resp.content, constants.DISPLAY_TERMINAL_COLOR_ERROR) + + + diff --git a/girok/calendar_cli/calendar_app.py b/girok/calendar_cli/calendar_app.py new file mode 100644 index 0000000..443aba6 --- /dev/null +++ b/girok/calendar_cli/calendar_app.py @@ -0,0 +1,37 @@ +from textual.app import App, ComposeResult +from textual.containers import Container, Horizontal, Vertical +from textual.widgets import Button, Footer, Header, Static, Label, Placeholder, Tree +from textual.messages import Message +from textual.widget import Widget + +import girok.api.category as category_api +import girok.utils.calendar as calendar_utils +from rich.table import Table +from rich.style import Style +from textual import log + +from girok.calendar_cli.sidebar import SidebarContainer, CategoryTree +from girok.calendar_cli.calendar_container import CalendarContainer +import girok.constants as constants + +class CalendarApp(Horizontal): + CSS_PATH = "./demo_dock.css" + + def compose(self): + yield SidebarContainer(id="sidebar-container") + yield CalendarContainer(id="calendar-container") + + def on_category_tree_category_changed(self, event): + self.query_one(CalendarContainer).update_cat_path(event.cat_path) + cat_tree = self.query_one(CategoryTree) + + def on_tag_tree_tag_changed(self, event): + tag = event.tag + if tag.endswith(" " + constants.LEFT_ARROW_EMOJI): + tag = tag[:-2] + if tag == "All Tags": + tag = "" + self.query_one(CalendarContainer).update_tag(tag) + + def on_category_tree_custom_test_message(self, event): + self.refresh() \ No newline at end of file diff --git a/girok/calendar_cli/calendar_container.py b/girok/calendar_cli/calendar_container.py new file mode 100644 index 0000000..38d54c9 --- /dev/null +++ b/girok/calendar_cli/calendar_container.py @@ -0,0 +1,324 @@ +import asyncio +from datetime import datetime, timedelta +import calendar + +from textual.app import App, ComposeResult +from textual.containers import Container, Horizontal, Vertical +from textual.widgets import Button, Footer, Header, Static, Label, Placeholder, Tree, DataTable +from textual.messages import Message +from textual.widget import Widget +from textual.reactive import var +from rich.text import Text +from rich.style import Style +from rich.segment import Segment +from rich.panel import Panel +from rich.markdown import Markdown +from textual import log + +import girok.api.task as task_api +import girok.api.category as category_api +import girok.utils.calendar as calendar_utils +import girok.utils.general as general_utils +import girok.utils.task as task_utils +import girok.constants as constants +from girok.calendar_cli.sidebar import CategoryTree, TagTree + + +class WeekdayBarContainer(Horizontal): + pass + +class CalendarHeader(Vertical): + year = datetime.now().year + month = datetime.now().month + cat_path = "" + tag = "" + + def on_mount(self): + self.display_date() + + def compose(self): + month_name = task_utils.get_month_name_by_number(self.month) + + with Horizontal(): + with Container(id="calendar-header-category-container"): + yield Static(self.cat_path, id="calendar-header-category") + with Container(id="calendar-header-date-container"): + yield Static(Text(f"{month_name} {self.year}", style=Style(color=constants.CALENDAR_HEADER_DATE_COLOR, bold=True)), id="calendar-header-date") + with Container(id="calendar-header-tag-container"): + yield Static(f"{self.tag}", id="calendar-header-tag") + yield Horizontal() + with WeekdayBarContainer(id="weekday-bar"): + yield Static(Text("Monday", style=Style(color=constants.CALENDAR_WEEKDAY_NAME_COLOR, bold=True)), classes="calendar-weekday-name") + yield Static(Text("Tuesday", style=Style(color=constants.CALENDAR_WEEKDAY_NAME_COLOR, bold=True)), classes="calendar-weekday-name") + yield Static(Text("Wednesday", style=Style(color=constants.CALENDAR_WEEKDAY_NAME_COLOR, bold=True)), classes="calendar-weekday-name") + yield Static(Text("Thursday", style=Style(color=constants.CALENDAR_WEEKDAY_NAME_COLOR, bold=True)), classes="calendar-weekday-name") + yield Static(Text("Friday", style=Style(color=constants.CALENDAR_WEEKDAY_NAME_COLOR, bold=True)), classes="calendar-weekday-name") + yield Static(Text("Saturday", style=Style(color="#87C5FA", bold=True)), classes="calendar-weekday-name") + yield Static(Text("Sunday", style=Style(color="#DB4455", bold=True)), classes="calendar-weekday-name") + + def update_year_and_month(self, year, month): + self.year, self.month = year, month + self.display_date() + + def update_cat_path(self, new_cat_path): + self.cat_path = new_cat_path + self.display_date() + + def update_tag(self, new_tag): + self.tag = new_tag + self.display_date() + + def display_date(self): + month_name = task_utils.get_month_name_by_number(self.month, abbr=False) + calendar_header_category = self.query_one("#calendar-header-category") + calendar_header_date = self.query_one("#calendar-header-date") + calendar_header_tag = self.query_one("#calendar-header-tag") + calendar_weekday_bar = self.query_one("#weekday-bar") + + calendar_header_category.update(Text(f"Category: /{self.cat_path}", style=Style(color=constants.CALENDAR_HEADER_DATE_COLOR))) + calendar_header_date.update(Text(f"{month_name} {self.year}", style=Style(color=constants.CALENDAR_HEADER_DATE_COLOR, bold=True))) + calendar_header_tag.update(Text(f"Tag: {self.tag}", style=Style(color=constants.CALENDAR_HEADER_DATE_COLOR))) + + +class CalendarCell(Vertical): + pass + + +class Calendar(Container): + year = datetime.now().year + month = datetime.now().month + cat_path = "" # If "", show all categories + tag = "" + tasks = [] + can_focus = True + cur_month_first_day_cell_num = None + cur_focused_cell_cord = (None, None) + cur_focused_cell = None + m = 5 + n = 7 + grid = [[False for _ in range(7)] for _ in range(5)] + is_pop_up = False + + class TaskCellSelected(Message): + def __init__(self, cell_tasks, year, month, day): + super().__init__() + self.cell_tasks = cell_tasks + self.year = year + self.month = month + self.day = day + + def on_mount(self): + self.update_calendar() + + def compose(self): + for i in range(35): + yield CalendarCell(classes="calendar-cell", id=f"cell{i}") + + def on_key(self, event): + if self.is_pop_up: + return + x, y = self.cur_focused_cell_cord + if event.key == 'h': # left + next_cell_coord = (x, y - 1) + elif event.key == 'j': # down + next_cell_coord = (x + 1, y) + elif event.key == 'k': # up + next_cell_coord = (x - 1, y) + elif event.key == 'l': # right + next_cell_coord = (x, y + 1) + elif event.key == 'o': + pass + else: + return + + if event.key in ['h', 'j', 'k', 'l']: # moving on cells + nx, ny = next_cell_coord + if nx < 0 or ny < 0 or nx >= self.m or ny >= self.n: # Out of matrix + return + + if not self.grid[nx][ny]: # Out of boundary of the current month + return + + prev_cell_num = calendar_utils.convert_coord_to_cell_num(*self.cur_focused_cell_cord) + prev_cell = self.query_one(f"#cell{prev_cell_num}") + calendar_utils.remove_left_arrow(prev_cell) + + cur_cell_num = calendar_utils.convert_coord_to_cell_num(nx, ny) + next_cell = self.query_one(f"#cell{cur_cell_num}") + calendar_utils.add_left_arrow(next_cell) + + self.cur_focused_cell_cord = (nx, ny) + self.cur_focused_cell = next_cell + elif event.key == 'o': # select a cell + cur_cell_num = calendar_utils.convert_coord_to_cell_num(x, y) + cur_cell = self.query_one(f"#cell{cur_cell_num}") + cell_tasks = cur_cell.children[1:] # task data + + # Retrieve tasks for the selected day + selected_day = calendar_utils.convert_cell_num_to_day(self.year, self.month, cur_cell_num) + cell_tasks = list(filter( + lambda x: calendar_utils.get_date_obj_from_str_separated_by_T(x['deadline']).day == selected_day, + self.tasks + )) + self.post_message(self.TaskCellSelected(cell_tasks, self.year, self.month, selected_day)) + self.is_pop_up = True + + def on_focus(self): + x, y = calendar_utils.convert_cell_num_to_coord(self.cur_month_first_day_cell_num) + target_cell = self.query_one(f"#cell{self.cur_month_first_day_cell_num}") + # target_cell.add_class("focused-cell") + calendar_utils.add_left_arrow(target_cell) + self.cur_focused_cell_cord = (x, y) + self.cur_focused_cell = target_cell + + def update_year_and_month(self, year, month): + self.year, self.month = year, month + self.update_calendar() + + def update_cat_path(self, new_cat_path: str): + self.cat_path = new_cat_path + self.update_calendar(show_arrow=False) + + def update_tag(self, new_tag: str): + self.tag = new_tag + self.update_calendar(show_arrow=False) + + def refresh_cell_days(self): + self.grid = [[False for _ in range(7)] for _ in range(5)] + first_weekday, total_days = calendar.monthrange(self.year, self.month) + self.cur_month_first_day_cell_num = first_weekday + now = datetime.now() + for i in range(35): + cell = self.query_one(f"#cell{i}") + for child in cell.walk_children(): + child.remove() + if i >= first_weekday and i <= first_weekday + total_days - 1: + x, y = calendar_utils.convert_cell_num_to_coord(i) + self.grid[x][y] = True + day = calendar_utils.convert_cell_num_to_day(self.year, self.month, i) + day_text = Text() + day_text.append(f"{day}") + if self.year == now.year and self.month == now.month and day == now.day: + day_text = Text(str(day_text), style=Style(bgcolor=constants.CALENDAR_TODAY_COLOR, color="black")) + # day_text.stylize(style=Style(bgcolor=constants.CALENDAR_TODAY_COLOR, color="black")) + cell.mount(Label(day_text, id=f"cell-header-{i}")) + + def update_calendar(self, show_arrow=True): + """ + If val == "", then "root category" is selected + """ + if self.cat_path == "": # all categories + cat_list = None + elif self.cat_path == "No Category": + cat_list = [''] + else: + cat_list = self.cat_path[:-1].split('/') + + if self.tag == "": + tag = None + else: + tag = self.tag + + log("CATS", cat_list), + log("tag", tag) + + start_date, end_date = task_utils.build_time_window_by_year_and_month(self.year, self.month) + resp = task_api.get_tasks( + cats=cat_list, + start_date=start_date, + end_date=end_date, + tag=tag, + view="list" + ) + + if resp.status_code == 200: + self.refresh_cell_days() + tasks = general_utils.bytes2dict(resp.content)['tasks'] + self.tasks = tasks + first_weekday, last_day = calendar.monthrange(self.year, self.month) + + if self.cur_focused_cell: + if show_arrow: + calendar_utils.remove_left_arrow(self.cur_focused_cell) + self.cur_focused_cell_cord = calendar_utils.convert_cell_num_to_coord(first_weekday) + self.cur_focused_cell = self.query_one(f"#cell{first_weekday}") # update + if show_arrow: + calendar_utils.add_left_arrow(self.cur_focused_cell) + + for idx, task in enumerate(tasks): + full_date = task['deadline'] + day = datetime.strptime(full_date, "%Y-%m-%dT%H:%M:%S").day + cell_num = calendar_utils.convert_day_to_cell_num(self.year, self.month, day) + cell = self.query_one(f"#cell{cell_num}") + color = task['color'] + name = task['name'] + # if len(name) > 13: + # name = name[:13] + ".." + task_item_name = Text() + task_item_name.append("●", style=constants.CIRCLE_COLOR[color]) + task_item_name.append(" " + name) + + task_item = Static(task_item_name, id=f"task-cell{cell_num}-{idx}", classes="task-item") + cell.mount(task_item) + + task_item = self.query_one(f"#task-cell{cell_num}-{idx}") + task_item.styles.overflow_x = "hidden" + task_item.styles.overflow_y = "hidden" + + elif resp.status_code == 400: + err_msg = general_utils.bytes2dict(resp.content)['detail'] + exit(0) + else: + exit(0) + + +class CalendarContainer(Vertical): + year = datetime.now().year + month = datetime.now().month + cat_path = None # If none, show all categories + tag = None + + def update_month_by_offset(self, offset: int): + new_year, new_month = task_utils.get_year_and_month_by_month_offset( + month_offset=offset, + year=self.year, + month=self.month + ) + self.year, self.month = new_year, new_month + calendar_header = self.query_one(CalendarHeader) + cal = self.query_one(Calendar) + + calendar_header.update_year_and_month(self.year, self.month) + cal.update_year_and_month(self.year, self.month) + + def update_year_and_month(self, year: int, month: int): + self.year, self.month = year, month + calendar_header = self.query_one(CalendarHeader) + cal = self.query_one(Calendar) + + calendar_header.update_year_and_month(self.year, self.month) + cal.update_year_and_month(self.year, self.month) + + def update_cat_path(self, cat_path: str): + """ + cat_path: ex) HKU/COMP3230 or "" + """ + self.cat_path = cat_path + cal = self.query_one(Calendar) + cal.update_cat_path(new_cat_path=cat_path) + + cal_header = self.query_one(CalendarHeader) + cal_header.update_cat_path(new_cat_path=cat_path) + + + def update_tag(self, tag: str): + self.tag = tag + cal = self.query_one(Calendar) + cal.update_tag(new_tag=tag) + cal_header = self.query_one(CalendarHeader) + cal_header.update_tag(new_tag=tag) + + + def compose(self): + yield CalendarHeader(id="calendar-header") + yield Calendar(id="calendar") diff --git a/girok/calendar_cli/calendar_main.css b/girok/calendar_cli/calendar_main.css new file mode 100644 index 0000000..ee7300a --- /dev/null +++ b/girok/calendar_cli/calendar_main.css @@ -0,0 +1,252 @@ +/* :root { + --task-color: #a8c1d1; + --default-font-color: #8f929b; + --calendar-date-color: #c4c7d0; + --category-color: #9bdfbb; + --border-color: grey; + --default-background-color: #171921; +} */ + +* { + padding: 0; + margin: 0; + color: #8f929b; + background: #171921; + overflow: hidden hidden; +} + +Screen { + /* background: white; */ + layers: below above; + layer: below; +} + +#app-calendar { + layout: horizontal; +} + +#sidebar-container { + /* display: none; */ + height: 100%; + width: 2fr; + border: solid grey; + padding-left: 3; + padding-top: 1; +} + +#sidebar-header { + /* border: solid grey; */ + content-align: center middle; + /* background: white; */ +} + +#sidebar-main-container { + layout: vertical; + margin-bottom: -10; +} +#sidebar { + box-sizing: content-box; + /* height: 5fr; */ + height: auto; +} + +#tag-tree { + box-sizing: content-box; + /* height: 5fr; */ + height: auto; + margin-top: 2; +} + +#calendar-container { + height: 100%; + width: 10fr; + border: solid grey; + align: center middle; + /* background: white; */ +} + +#calendar-container { + /* background: red; */ +} + +#calendar-header { + /* content-align: center middle; */ + height: 3; + /* background: blue; */ +} + +#calendar { + /* background: yellow; */ +} + +#calendar-header-category-container { + /* content-align-horizontal: left; */ + margin-left: 2; + width: 1fr; +} + +#calendar-header-category { + content-align-horizontal: left; +} + +#calendar-header-date-container { + width: 1fr; +} + +#calendar-header-date { + content-align-horizontal: center; +} + +#calendar-header-tag-container { + /* content-align-horizontal: left; */ + margin-right: 2; + width: 1fr; +} + +#calendar-header-tag { + margin-right: 3; + content-align-horizontal: right; + color: #9bdfbb; +} + +#calendar-header-place-holder { + width: 1fr; +} + +#weekday-bar { + width: 100%; + background: red; + margin-left: 2; +} + +.calendar-weekday-name { + width: 1fr; + content-align-horizontal: center; +} + +#calendar { + layout: grid; + grid-size: 7 5; + grid-columns: 1fr; + grid-rows: 1fr; + height: 95%; + margin-left: 2; + padding-top: 0; + /* outline: solid grey; */ + /* background: blue; */ +} + +.vertical { + width: 1fr; + border: solid grey; +} + +.calendar-cell { + height: 100%; + border-top: grey; + /* background: grey; */ + /* border: grey; */ +} + +.weekday-name { + content-align: center middle; + color: antiquewhite; +} + +.task-item-container { + box-sizing: content-box; + height: 1; + overflow: hidden hidden; + width: 95%; +} + +.task-item { + /* color: rgb(168, 193, 209); */ + /* color: #d1cec0; */ + color: #b9b7ad; + /* width: 100%; */ + height: 1; +} + +#calendar-header-category { + color: #9bdfbb; +} + +.focused-cell { + background: #222530; + /* layer: above; */ +} + +/* .pop-up { + layer: above; + background: red; + content-align: center middle; +} + +.task-pop-up-container { + layer: above; + layout: grid; + grid-size: 3 3; + grid-columns: 1fr 10fr 1fr; + grid-rows: 1fr 10fr 1fr; + width: 50%; + height: 50%; + background: white; +} + +.task-pop-up-table { + width: auto; + height: auto; + background: blue; +} */ + +Screen { + align: center middle; +} + +.task-pop-up-container { + layer: above; + width: 90%; + height: auto; +} + +.task-pop-up-header-container { + layer: above; + align: center middle; + height: 1; +} + +.task-pop-up-header { + layer: above; + background: #3e4348; + content-align: center bottom; + content-align-vertical: bottom; + /* color: #f5edd1; */ +} + +.task-pop-up-table-container { + layer: above; + align: center middle; + background: #222429; + height: auto; +} + +.task-pop-up-table { + layer: above; + background: #222429; + content-align-horizontal: center; + width: 90%; +} + +/* :root { + --task-color: #a8c1d1; + --default-font-color: #8f929b; + --calendar-date-color: #c4c7d0; + --category-color: #9bdfbb; + --border-color: grey; + --default-background-color: #171921; +} */ + +.calendar-weekday-name { + width: 1fr; +} diff --git a/girok/calendar_cli/calendar_main.py b/girok/calendar_cli/calendar_main.py new file mode 100644 index 0000000..6e2acce --- /dev/null +++ b/girok/calendar_cli/calendar_main.py @@ -0,0 +1,181 @@ +from datetime import datetime +import calendar + +from textual.app import App, ComposeResult +from textual.containers import Container, Horizontal, Vertical +from textual.widgets import Button, Footer, Header, Static, Label, Placeholder, Tree +from textual.widget import Widget +from textual.messages import Message +from textual import log +from textual.reactive import var, reactive +from textual.pilot import Pilot +from rich.text import Text +from rich.table import Table +from rich.style import Style +from rich import box +from rich.table import Column + +import girok.api.category as category_api +import girok.utils.calendar as calendar_utils +import girok.utils.display as display_utils + +from girok.calendar_cli.calendar_app import CalendarApp +from girok.calendar_cli.calendar_container import CalendarContainer, Calendar +from girok.calendar_cli.sidebar import CategoryTree, SidebarContainer, TagTree +import girok.constants as constants + + +class Entry(App): + CSS_PATH = "./calendar_main.css" + current_focused = "CategoryTree" + is_pop_up = False + BINDINGS = [ + ("q", "quit", "Quit Nuro"), + ("u", "show_previous_month", "Show prev month"), + ("i", "show_next_month", "Show next month"), + ("y", "show_current_month", "Show current month"), + ("ctrl+l", "focus_on_calendar", "Move to calendar"), + ("ctrl+g", "focus_on_sidebar", "Move to sidebar"), + ("ctrl+j", "move_down_to_tag_tree", "Move down to tag tree"), + ("ctrl+k", "move_up_to_category_tree", "Move up to category tree"), + ("o", 'close_pop_up', "Close pop up box"), + ("f", "toggle_files", "Toggle Files") + ] + show_sidebar = reactive(True) + pilot = None + + def on_mount(self): + self.set_focus(self.query_one(CategoryTree)) + + def compose(self): + yield CalendarApp() + # yield Footer() + + # Display pop-up box when selecting a cell + def on_calendar_task_cell_selected(self, event: Calendar.TaskCellSelected): + cell_tasks = event.cell_tasks + year, month, day = event.year, event.month, event.day + table = display_utils.build_task_table(cell_tasks) + + self.query_one(CalendarContainer).mount( + Vertical( + Static(Text(f"{day} {calendar.month_name[month]} {year}", style=Style(bold=True, color=constants.TABLE_HEADER_DATE_COLOR)), classes="task-pop-up-header"), + Container(Static(table, classes="task-pop-up-table"), classes="task-pop-up-table-container"), + classes='task-pop-up-container', + ) + ) + self.is_pop_up = True + + def action_quit(self): + self.exit() + + def action_show_next_month(self): + if self.is_pop_up: + return + calendar_container = self.query_one(CalendarContainer) + calendar_container.update_month_by_offset(1) + + def action_show_previous_month(self): + if self.is_pop_up: + return + calendar_container = self.query_one(CalendarContainer) + calendar_container.update_month_by_offset(-1) + + def action_show_current_month(self): + if self.is_pop_up: + return + calendar_container = self.query_one(CalendarContainer) + now = datetime.now() + cur_year, cur_month = now.year, now.month + calendar_container.update_year_and_month(cur_year, cur_month) + + def action_focus_on_calendar(self): + if self.is_pop_up: + return + self.set_focus(self.query_one(Calendar)) + self.current_focused = "Calendar" + + cat_tree = self.query_one(CategoryTree) + tag_tree = self.query_one(TagTree) + calendar_utils.remove_left_arrow_tree(cat_tree.highlighted_node) + calendar_utils.remove_left_arrow_tree(tag_tree.highlighted_node) + calendar_utils.remove_highlight(cat_tree.highlighted_node) + calendar_utils.remove_highlight(tag_tree.highlighted_node) + + def action_focus_on_sidebar(self): + if self.is_pop_up: + return + self.set_focus(self.query_one(CategoryTree)) + self.current_focused = "CategoryTree" + + cal = self.query_one(Calendar) + + # self.pilot.hover(Calendar) + + if self.is_pop_up: + return + + cat_tree = self.query_one(CategoryTree) + calendar_utils.remove_left_arrow(cal.cur_focused_cell) + calendar_utils.add_highlight(cat_tree.highlighted_node) + + ############# UNKNOWN ERROR - Temporary Fix ############ + ngbrs = [self.query_one("#cell0"), self.query_one("#cell1"), self.query_one("#cell2"), self.query_one("#cell3"), self.query_one("#cell4"), self.query_one("#cell5"), self.query_one("#cell6")] + for ngbr_cell in ngbrs: + temp = [] + while ngbr_cell.children: + child = ngbr_cell.children[0] + temp.append(child.render()) + child.remove() + + for child in temp: + ngbr_cell.mount(Static(child)) + ###################################################### + + def action_move_down_to_tag_tree(self): + if self.is_pop_up: + return + if self.current_focused != "CategoryTree": + return + tag_tree = self.query_one(TagTree) + self.set_focus(tag_tree) + self.current_focused = "TagTree" + category_tree = self.query_one(CategoryTree) + calendar_utils.remove_highlight(category_tree.highlighted_node) + calendar_utils.remove_left_arrow_tree(category_tree.highlighted_node) + calendar_utils.add_left_arrow_tree(tag_tree.highlighted_node) + calendar_utils.add_highlight(tag_tree.highlighted_node) + + def action_move_up_to_category_tree(self): + if self.is_pop_up: + return + if self.current_focused != "TagTree": + return + category_tree = self.query_one(CategoryTree) + self.set_focus(category_tree) + self.current_focused = "CategoryTree" + tag_tree = self.query_one(TagTree) + calendar_utils.remove_highlight(tag_tree.highlighted_node) + calendar_utils.remove_left_arrow_tree(tag_tree.highlighted_node) + calendar_utils.add_left_arrow_tree(category_tree.highlighted_node) + calendar_utils.add_highlight(category_tree.highlighted_node) + + def action_close_pop_up(self): + if not self.is_pop_up: + return + self.query_one(".task-pop-up-container").remove() + self.query_one(Calendar).is_pop_up = False + self.is_pop_up = False + + def action_toggle_files(self): + self.show_sidebar = not self.show_sidebar + sidebar_container = self.query_one(SidebarContainer) + if self.show_sidebar: + sidebar_container.styles.display = "block" + else: + sidebar_container.styles.display = "none" + + +if __name__ == '__main__': + app = Entry() + app.run() \ No newline at end of file diff --git a/girok/calendar_cli/sidebar.py b/girok/calendar_cli/sidebar.py new file mode 100644 index 0000000..f790273 --- /dev/null +++ b/girok/calendar_cli/sidebar.py @@ -0,0 +1,201 @@ +from textual.app import App, ComposeResult +from textual.containers import Container, Horizontal, Vertical +from textual.widgets import Button, Footer, Header, Static, Label, Placeholder, Tree +from textual.messages import Message +from textual.widget import Widget +from textual.reactive import var, reactive +from textual import events + +import girok.api.category as category_api +import girok.api.task as task_api +import girok.utils.calendar as calendar_utils +import girok.utils.general as general_utils +import girok.constants as constants +from rich.style import Style +from rich.text import Text +from textual import log +from textual.widgets._tree import TreeNode + +class CategoryTree(Tree): + CSS_PATH = "./demo_dock.css" + cats = dict() + can_focus = True + can_focus_children = True + auto_expand=False + highlighted_node = None + selected_node = None + init_select = False + init_highlight = False + class CategoryChanged(Message): + def __init__(self, cat_path: str): + super().__init__() + self.cat_path = cat_path + + class CustomTestMessage(Message): + def __init__(self): + super().__init__() + + def on_mount(self): + # Select the topmost node + # self.select_node(self.root) + self.highlighted_node = self.root + self.selected_node = self.root + # calendar_utils.add_highlight(self.selected_node) + calendar_utils.add_left_arrow_tree(self.highlighted_node) + # self.temp_node_selected() + # self.select_node(self.selected_node) + # self.action_select_cursor() + + self.line = 0 + self.cats = category_api.get_categories() + self.root.expand() + + for cat in self.cats: + top_cat = self.root.add(cat, expand=True, data={"color": self.cats[cat]['color']}) + top_cat.allow_expand = True + calendar_utils.build_category_tree(top_cat, self.cats[cat]['subcategories']) + + def on_key(self, evt): + if evt.key == "j": + self.action_cursor_down() + elif evt.key == "k": + self.action_cursor_up() + elif evt.key == "o": + self.action_select_cursor() + + def render_label(self, node, base_style: Style, style: Style): + node_label = node._label.copy() + icon = "" + if node.parent is None: + icon = "📖 " + elif node.parent.parent is None: + icon = Text("● ", style=constants.CIRCLE_COLOR[node.data['color']]) + text = Text() + text.append(icon) + text.append(node_label) + return text + + def on_tree_node_selected(self, event: Tree.NodeSelected): + event.stop() + # calendar_utils.remove_highlight(self.selected_node) + # calendar_utils.add_highlight(event.node) + full_cat_path = calendar_utils.get_full_path_from_node(event.node) + self.selected_node = event.node + self.post_message(self.CategoryChanged(full_cat_path)) + + + def on_tree_node_highlighted(self, event: Tree.NodeHighlighted): + event.stop() + prev_highlighted_node = self.highlighted_node + calendar_utils.remove_left_arrow_tree(prev_highlighted_node) + calendar_utils.remove_highlight(prev_highlighted_node) + calendar_utils.add_left_arrow_tree(event.node) + calendar_utils.add_highlight(event.node) + self.highlighted_node = event.node + + def on_focus(self, evt): + calendar_utils.add_left_arrow_tree(self.highlighted_node) + + +class TagTree(Tree): + CSS_PATH = "./demo_dock.css" + tags = [] + can_focus = True + can_focus_children = True + auto_expand=False + highlighted_node = None + selected_node = None + + class TagChanged(Message): + def __init__(self, tag: str): + super().__init__() + self.tag = tag + + class CustomTestMessage(Message): + def __init__(self): + super().__init__() + + def on_mount(self): + self.select_node(self.root) + self.action_select_cursor() + self.highlighted_node = self.root + self.selected_node = self.root + + resp = task_api.get_tags() + if resp.status_code == 200: + self.tags = general_utils.bytes2dict(resp.content)['tags'] + elif resp.status_code == 400: + err_msg = general_utils.bytes2dict(resp.content)['detail'] + exit(0) + else: + exit(0) + + self.root.expand() + + for tag in self.tags: + self.root.add(tag, expand=True) + + def on_key(self, evt): + if evt.key == "j": + self.action_cursor_down() + elif evt.key == "k": + self.action_cursor_up() + elif evt.key == "o": + self.action_select_cursor() + + def on_focus(self, evt): + calendar_utils.add_left_arrow_tree(self.highlighted_node) + + def render_label(self, node, base_style: Style, style: Style): + node_label = node._label.copy() + + icon = "" + if node.parent is None: + icon = "📖 " + elif node.parent.parent is None: + icon = Text("● ", style="white") + + text = Text() + text.append(icon) + text.append(node_label) + return text + + def on_tree_node_selected(self, event: Tree.NodeSelected): + event.stop() + # calendar_utils.remove_highlight(self.selected_node) + tag = str(event.node._label) + if tag.endswith(" " + constants.LEFT_ARROW_EMOJI): + tag = tag[:-2] + + self.post_message(self.TagChanged(tag)) + event.node.set_label(tag) + # calendar_utils.add_highlight(event.node) + self.selected_node = event.node + # self.post_message(self.CustomTestMessage()) + + def on_tree_node_highlighted(self, event: Tree.NodeHighlighted): + event.stop() + prev_highlighted_node = self.highlighted_node + calendar_utils.remove_highlight(prev_highlighted_node) + calendar_utils.remove_left_arrow_tree(prev_highlighted_node) + calendar_utils.add_left_arrow_tree(event.node) + calendar_utils.add_highlight(event.node) + self.highlighted_node = event.node + + +class SidebarMainContainer(Vertical): + CSS_PATH = "./demo_dock.css" + def compose(self): + yield CategoryTree("All Categories", id="sidebar") + yield TagTree("All Tags", id="tag-tree") + + +class SidebarContainer(Vertical): + CSS_PATH = "./demo_dock.css" + + def compose(self): + yield SidebarMainContainer(id="sidebar-main-container") + + + + diff --git a/girok/calendar_cli/textual_demo_2023-03-11T22_49_21_681307.svg b/girok/calendar_cli/textual_demo_2023-03-11T22_49_21_681307.svg new file mode 100644 index 0000000..816c090 --- /dev/null +++ b/girok/calendar_cli/textual_demo_2023-03-11T22_49_21_681307.svg @@ -0,0 +1,246 @@ + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + + Textual Demo + + + + + + + +