Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

update time regularly concept #5

Open
wants to merge 1 commit into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 26 additions & 8 deletions todoism/main.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
import time
import curses
import time
import threading

import todoism.utils as ut
import todoism.task as tsk
import todoism.print as pr
Expand All @@ -24,7 +26,9 @@ def main(stdscr):
curses.init_pair(5, curses.COLOR_WHITE, curses.COLOR_BLACK)
# Set up the screen
curses.curs_set(0)
stdscr.clear()
# stdscr.clear()
stdscr.refresh()

# Assuming color pair 0 represents the default colors
stdscr.bkgd(' ', curses.COLOR_BLACK | curses.A_NORMAL)

Expand All @@ -38,11 +42,25 @@ def main(stdscr):
done_cnt = tsk.done_count(task_list)
current_id = 1 if task_cnt > 0 else 0 # id of task selected
current_row = 1 if task_cnt > 0 else 0 # range: [0, height-1]
max_capacity = stdscr.getmaxyx()[0] - 1
max_capacity, max_x = stdscr.getmaxyx()
max_capacity = max(0, max_capacity - 1)
# print window of task id
start = 1 if task_cnt > 0 else 0
end = task_cnt if task_cnt < max_capacity else max_capacity
should_repaint = True


sb_win = curses.newwin(1, max_x, 0, 0)
tasks_win = curses.newwin(max_capacity, max_x, 1, 0)

def sb_loop(win):
while True:
pr.print_status_bar(win, done_cnt, task_cnt)
time.sleep(1)

sb_thread = threading.Thread(target=sb_loop, daemon=True, args=(sb_win,))
sb_thread.start()


while True:
task_cnt = len(task_list)
Expand All @@ -52,7 +70,7 @@ def main(stdscr):
color_selected = st.get_color_selected()
curses.init_pair(1, curses.COLOR_BLACK, color_selected)
pr.repaint(
stdscr,
tasks_win,
done_cnt,
task_cnt,
task_list,
Expand All @@ -66,7 +84,7 @@ def main(stdscr):
# siderbar_win.refresh()

if task_cnt == 0:
pr.print_status_bar(stdscr, done_cnt, task_cnt)
# pr.print_status_bar(stdscr, done_cnt, task_cnt)
pr.print_msg(stdscr, pr.empty_msg)

stdscr.refresh()
Expand Down Expand Up @@ -96,8 +114,8 @@ def main(stdscr):
start = task_cnt - (end - start - 1)
end = task_cnt
stdscr.erase()
pr.print_status_bar(stdscr, done_cnt, task_cnt)
pr.print_tasks(stdscr, task_list, current_id, start, end)
# pr.print_status_bar(stdscr, done_cnt, task_cnt)
pr.print_tasks(tasks_win, task_list, current_id, start, end)
stdscr.addstr(max_capacity if task_cnt >= max_capacity else task_cnt + 1, 4 if task_cnt < 9 else 3, f"{task_cnt + 1}.{' '}")
stdscr.refresh()

Expand All @@ -120,7 +138,7 @@ def main(stdscr):
else:
start = old_start
end = old_end
pr.print_tasks(stdscr, task_list, current_id, start, end)
pr.print_tasks(tasks_win, task_list, current_id, start, end)
stdscr.refresh()
curses.curs_set(0)
curses.noecho()
Expand Down
20 changes: 12 additions & 8 deletions todoism/print.py
Original file line number Diff line number Diff line change
Expand Up @@ -91,10 +91,12 @@ def print_tasks(stdscr, task_list, current_id, start, end):
if start > 0:
for i, task in enumerate(task_list[start - 1:end]):
if i + start == current_id: # handle task overflow: +start
print_task_selected(stdscr, task, i + 1) # +1 due to status bar
# print_task_selected(stdscr, task, i + 1) # +1 due to status bar
print_task_selected(stdscr, task, i)
stdscr.refresh()
else:
print_task(stdscr, task, i + 1)
# print_task(stdscr, task, i + 1)
print_task(stdscr, task, i)
stdscr.refresh()


Expand All @@ -106,7 +108,7 @@ def print_status_bar(stdscr, done_cnt, task_cnt):
percentage_num = int((done_cnt / task_cnt) * 100) if task_cnt > 0 else 0
status_bar = {
'tasks': f"Progress: {done_cnt}/{task_cnt} {percentage_num if task_cnt > 0 else 0}%",
'date': datetime.now().strftime("%Y-%m-%d %H:%M")
'date': datetime.now().strftime("%Y-%m-%d %H:%M:%S")
}
color_pair = 0
if percentage_num >= 67:
Expand All @@ -118,14 +120,16 @@ def print_status_bar(stdscr, done_cnt, task_cnt):
color_pair = 5
else:
color_pair = 4
stdscr.attron(curses.color_pair(color_pair))
stdscr.addstr(0, 0, f"{side_spaces}{status_bar['tasks']}")
stdscr.attroff(curses.color_pair(color_pair))
stdscr.addstr(f" | {status_bar['date']}{side_spaces}")

tasks = f"{side_spaces}{status_bar['tasks']}"
sdate = f" | {status_bar['date']}{side_spaces}"
n = min(max_x - 1, len(tasks) + len(sdate))
stdscr.addnstr(0, 0, tasks, n - len(tasks), curses.color_pair(color_pair))
stdscr.addnstr(sdate, n - len(sdate))
stdscr.refresh()

def print_main_view(stdscr, done_cnt, task_cnt, tasks, current_id, start, end):
print_status_bar(stdscr, done_cnt, task_cnt)
# print_status_bar(stdscr, done_cnt, task_cnt)
print_tasks(stdscr, tasks, current_id, start, end)

def repaint(stdscr, done_cnt, task_cnt, task_list, current_id, start, end):
Expand Down