Add a test suite and some basic navigation

This commit is contained in:
Anthony Sottile
2019-07-27 19:22:43 -07:00
parent 13a6bbc51b
commit c52702d0a6
11 changed files with 249 additions and 2 deletions

66
babi.py
View File

@@ -4,6 +4,8 @@ import curses
from typing import Dict
from typing import Tuple
VERSION_STR = 'babi v0'
def _get_color_pair_mapping() -> Dict[Tuple[int, int], int]:
ret = {}
@@ -63,18 +65,78 @@ def _color_test(stdscr: '_curses._CursesWindow') -> None:
x += 4
y += 1
x = 0
stdscr.get_wch()
def _write_header(
stdscr: '_curses._CursesWindow',
filename: str,
*,
modified: bool,
) -> None:
filename = filename or '<<new file>>'
if modified:
filename += ' *'
centered_filename = filename.center(curses.COLS)[len(VERSION_STR) + 2:]
s = f' {VERSION_STR} {centered_filename}'
stdscr.addstr(0, 0, s, curses.A_REVERSE)
def _write_status(stdscr: '_curses._CursesWindow', status: str) -> None:
stdscr.addstr(curses.LINES - 1, 0, ' ' * (curses.COLS - 1))
if status:
status = f' {status} '
offset = (curses.COLS - len(status)) // 2
stdscr.addstr(curses.LINES - 1, offset, status, curses.A_REVERSE)
def c_main(stdscr: '_curses._CursesWindow', args: argparse.Namespace) -> None:
_init_colors(stdscr)
if args.color_test:
_color_test(stdscr)
stdscr.getch()
return _color_test(stdscr)
filename = args.filename
status = ''
status_action_counter = -1
position_y, position_x = 0, 0
def _set_status(s: str) -> None:
nonlocal status, status_action_counter
status = s
status_action_counter = 25
while True:
if status_action_counter == 0:
status = ''
status_action_counter -= 1
_write_header(stdscr, filename, modified=False)
_write_status(stdscr, status)
stdscr.move(position_y + 1, position_x)
wch = stdscr.get_wch()
key = wch if isinstance(wch, int) else ord(wch)
keyname = curses.keyname(key)
if key == curses.KEY_DOWN:
position_y = min(position_y + 1, curses.LINES - 2)
elif key == curses.KEY_UP:
position_y = max(position_y - 1, 0)
elif key == curses.KEY_RIGHT:
position_x = min(position_x + 1, curses.COLS - 1)
elif key == curses.KEY_LEFT:
position_x = max(position_x - 1, 0)
elif keyname == b'^X':
return
else:
_set_status(f'unknown key: {keyname} ({key})')
def main() -> int:
parser = argparse.ArgumentParser()
parser.add_argument('--color-test', action='store_true')
parser.add_argument('filename', nargs='?')
args = parser.parse_args()
curses.wrapper(c_main, args)
return 0