initial commit with color test

This commit is contained in:
Anthony Sottile
2019-07-12 19:51:48 -07:00
parent b9e9604a60
commit 13a6bbc51b
3 changed files with 124 additions and 0 deletions

1
.gitignore vendored Normal file
View File

@@ -0,0 +1 @@
/.mypy_cache

39
.pre-commit-config.yaml Normal file
View File

@@ -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

84
babi.py Normal file
View File

@@ -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())