-
Notifications
You must be signed in to change notification settings - Fork 1
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix: move functions into menus and utils files
- Loading branch information
1 parent
25ea1f2
commit cc02fa7
Showing
4 changed files
with
144 additions
and
102 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
""" | ||
Various helpful menus and their helper functions. | ||
""" | ||
|
||
# pylint: disable=missing-function-docstring | ||
|
||
from typing import Any | ||
|
||
try: | ||
from pyfiglet import figlet_format as big | ||
|
||
FIGLET_FORMAT_EXISTS = True | ||
except ImportError: | ||
FIGLET_FORMAT_EXISTS = False | ||
|
||
from src.class_cursor import Cursor | ||
from src.class_todo import Todo | ||
from src.get_args import ( | ||
CONTROLS_BEGIN_INDEX, | ||
CONTROLS_END_INDEX, | ||
HELP_FILE, | ||
TKINTER_GUI, | ||
) | ||
from src.get_todo import hline | ||
from src.keys import Key | ||
from src.md_to_py import md_table_to_lines | ||
from src.print_todos import make_printable_sublist | ||
from src.utils import clamp, set_header | ||
|
||
if TKINTER_GUI: | ||
from tcurses import curses | ||
else: | ||
import curses | ||
|
||
|
||
def simple_scroll_keybinds( | ||
win: Any, cursor: int, len_lines: int, len_new_lines: int | ||
) -> int: | ||
try: | ||
key = win.getch() | ||
except Key.ctrl_c: | ||
return -1 | ||
if key in (Key.up, Key.k): | ||
cursor = clamp(cursor - 1, 0, len_lines - 2) | ||
elif key in (Key.down, Key.j, Key.enter): | ||
cursor = clamp(cursor + 1, 0, len_lines - len_new_lines - 1) | ||
else: | ||
return -1 | ||
return cursor | ||
|
||
|
||
def help_menu(parent_win: Any) -> None: | ||
parent_win.clear() | ||
set_header(parent_win, "Help (k/j to scroll):") | ||
lines = [] | ||
for line in md_table_to_lines( | ||
CONTROLS_BEGIN_INDEX, | ||
CONTROLS_END_INDEX, | ||
str(HELP_FILE), | ||
("<kbd>", "</kbd>", "(arranged alphabetically)"), | ||
): | ||
lines.append(line[:-2]) | ||
win = curses.newwin( | ||
min(parent_win.getmaxyx()[0] - 1, len(lines) + 2), | ||
len(lines[0]) + 2, | ||
1, | ||
(parent_win.getmaxyx()[1] - (len(lines[0]) + 1)) // 2, | ||
) | ||
win.box() | ||
parent_win.refresh() | ||
cursor = 0 | ||
win.addstr(1, 1, lines[0]) | ||
hline(win, 2, 0, curses.ACS_HLINE, win.getmaxyx()[1]) | ||
while True: | ||
new_lines, _, _ = make_printable_sublist( | ||
win.getmaxyx()[0] - 4, lines[2:], cursor, 0 | ||
) | ||
for i, line in enumerate(new_lines): | ||
win.addstr(i + 3, 1, line) | ||
win.refresh() | ||
cursor = simple_scroll_keybinds(win, cursor, len(lines), len(new_lines)) | ||
if cursor < 0: | ||
break | ||
parent_win.clear() | ||
|
||
|
||
def magnify(stdscr: Any, todos: list[Todo], selected: Cursor) -> None: | ||
if not FIGLET_FORMAT_EXISTS: | ||
set_header(stdscr, "Magnify dependency not available") | ||
return | ||
stdscr.clear() | ||
set_header(stdscr, "Magnifying...") | ||
lines = big( # pyright: ignore | ||
todos[int(selected)].display_text, width=stdscr.getmaxyx()[1] | ||
).split("\n") | ||
lines.append("") | ||
lines = [line.ljust(stdscr.getmaxyx()[1] - 2) for line in lines] | ||
cursor = 0 | ||
while True: | ||
new_lines, _, _ = make_printable_sublist( | ||
stdscr.getmaxyx()[0] - 2, lines, cursor, 0 | ||
) | ||
for i, line in enumerate(new_lines): | ||
stdscr.addstr(i + 1, 1, line) | ||
stdscr.refresh() | ||
cursor = simple_scroll_keybinds(stdscr, cursor, len(lines), len(new_lines)) | ||
if cursor < 0: | ||
break | ||
stdscr.refresh() | ||
stdscr.clear() |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,28 @@ | ||
""" | ||
General utilities, useful across multiple other files | ||
""" | ||
|
||
from typing import Any | ||
|
||
from src.get_args import TKINTER_GUI | ||
|
||
if TKINTER_GUI: | ||
from tcurses import curses | ||
else: | ||
import curses | ||
|
||
|
||
def clamp(number: int, minimum: int, maximum: int) -> int: | ||
""" | ||
Clamp a number in between a minimum and maximum. | ||
""" | ||
return min(max(number, minimum), maximum - 1) | ||
|
||
|
||
def set_header(stdscr: Any, message: str) -> None: | ||
""" | ||
Set the header to a specific message. | ||
""" | ||
stdscr.addstr( | ||
0, 0, message.ljust(stdscr.getmaxyx()[1]), curses.A_BOLD | curses.color_pair(2) | ||
) |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters