From 13a6bbc51b0b8564cea4666bb76286a2187b3034 Mon Sep 17 00:00:00 2001 From: Anthony Sottile Date: Fri, 12 Jul 2019 19:51:48 -0700 Subject: [PATCH] initial commit with color test --- .gitignore | 1 + .pre-commit-config.yaml | 39 +++++++++++++++++++ babi.py | 84 +++++++++++++++++++++++++++++++++++++++++ 3 files changed, 124 insertions(+) create mode 100644 .gitignore create mode 100644 .pre-commit-config.yaml create mode 100644 babi.py diff --git a/.gitignore b/.gitignore new file mode 100644 index 0000000..020de35 --- /dev/null +++ b/.gitignore @@ -0,0 +1 @@ +/.mypy_cache diff --git a/.pre-commit-config.yaml b/.pre-commit-config.yaml new file mode 100644 index 0000000..5e014a4 --- /dev/null +++ b/.pre-commit-config.yaml @@ -0,0 +1,39 @@ +repos: +- repo: https://github.com/pre-commit/pre-commit-hooks + rev: v2.2.3 + hooks: + - id: trailing-whitespace + - id: end-of-file-fixer + - id: check-docstring-first + - id: check-yaml + - id: debug-statements + - id: double-quote-string-fixer + - id: name-tests-test + - id: requirements-txt-fixer +- repo: https://gitlab.com/pycqa/flake8 + rev: 3.7.8 + hooks: + - id: flake8 +- repo: https://github.com/pre-commit/mirrors-autopep8 + rev: v1.4.4 + hooks: + - id: autopep8 +- repo: https://github.com/asottile/reorder_python_imports + rev: v1.6.0 + hooks: + - id: reorder-python-imports + args: [--py3-plus] +- repo: https://github.com/asottile/add-trailing-comma + rev: v1.4.1 + hooks: + - id: add-trailing-comma + args: [--py36-plus] +- repo: https://github.com/asottile/pyupgrade + rev: v1.21.0 + hooks: + - id: pyupgrade + args: [--py36-plus] +- repo: https://github.com/pre-commit/mirrors-mypy + rev: v0.711 + hooks: + - id: mypy diff --git a/babi.py b/babi.py new file mode 100644 index 0000000..368a3ae --- /dev/null +++ b/babi.py @@ -0,0 +1,84 @@ +import _curses +import argparse +import curses +from typing import Dict +from typing import Tuple + + +def _get_color_pair_mapping() -> Dict[Tuple[int, int], int]: + ret = {} + i = 0 + for bg in range(-1, 16): + for fg in range(bg, 16): + ret[(fg, bg)] = i + i += 1 + return ret + + +COLORS = _get_color_pair_mapping() +del _get_color_pair_mapping + + +def _has_colors() -> bool: + # https://github.com/python/typeshed/pull/3115 + return curses.has_colors and curses.COLORS >= 16 # type: ignore + + +def _color(fg: int, bg: int) -> int: + if _has_colors(): + if bg > fg: + return curses.A_REVERSE | curses.color_pair(COLORS[(bg, fg)]) + else: + return curses.color_pair(COLORS[(fg, bg)]) + else: + if bg > fg: + return curses.A_REVERSE | curses.color_pair(0) + else: + return curses.color_pair(0) + + +def _init_colors(stdscr: '_curses._CursesWindow') -> None: + curses.use_default_colors() + if not _has_colors(): + return + for (fg, bg), pair in COLORS.items(): + if pair == 0: # cannot reset pair 0 + continue + curses.init_pair(pair, fg, bg) + + +def _color_test(stdscr: '_curses._CursesWindow') -> None: + maxy, maxx = stdscr.getmaxyx() + if maxy < 16 or maxx < 64: + raise SystemExit('--color-test needs a window of at least 64 x 16') + + x = y = 0 + for fg in range(-1, 16): + for bg in range(-1, 16): + if bg > fg: + s = f'*{COLORS[bg, fg]:3}' + else: + s = f' {COLORS[fg, bg]:3}' + stdscr.addstr(y, x, s, _color(fg, bg)) + x += 4 + y += 1 + x = 0 + + +def c_main(stdscr: '_curses._CursesWindow', args: argparse.Namespace) -> None: + _init_colors(stdscr) + if args.color_test: + _color_test(stdscr) + stdscr.getch() + + +def main() -> int: + parser = argparse.ArgumentParser() + parser.add_argument('--color-test', action='store_true') + args = parser.parse_args() + curses.wrapper(c_main, args) + return 0 + + +if __name__ == '__main__': + exit(main())