2021-03-11 19:16:15 +01:00
|
|
|
"""Color definitions for curses."""
|
|
|
|
|
2021-02-12 19:01:42 +01:00
|
|
|
import curses
|
2021-05-30 02:21:47 +02:00
|
|
|
import logging
|
2021-02-12 19:01:42 +01:00
|
|
|
from enum import IntEnum
|
|
|
|
|
|
|
|
|
|
|
|
class ColorPair(IntEnum):
|
2021-03-08 22:30:39 +01:00
|
|
|
# Colors for specific Gemtext line type.
|
2021-02-12 19:01:42 +01:00
|
|
|
NORMAL = 0
|
|
|
|
ERROR = 1
|
|
|
|
LINK = 2
|
|
|
|
LINK_ID = 3
|
|
|
|
TITLE_1 = 4
|
|
|
|
TITLE_2 = 5
|
|
|
|
TITLE_3 = 6
|
|
|
|
PREFORMATTED = 7
|
|
|
|
BLOCKQUOTE = 8
|
2021-03-08 22:30:39 +01:00
|
|
|
|
|
|
|
# Colors for other usage in the browser.
|
2021-05-18 22:29:38 +02:00
|
|
|
DEBUG = 9
|
|
|
|
LINK_PREVIEW = 10
|
2021-02-12 19:01:42 +01:00
|
|
|
|
|
|
|
|
2021-05-29 23:59:50 +02:00
|
|
|
A_ITALIC = curses.A_ITALIC if hasattr(curses, "A_ITALIC") else curses.A_NORMAL
|
|
|
|
|
|
|
|
|
2021-05-30 02:21:47 +02:00
|
|
|
def init_colors(bg: int =-1):
|
2021-02-12 19:01:42 +01:00
|
|
|
curses.use_default_colors()
|
|
|
|
curses.init_pair(ColorPair.DEBUG, curses.COLOR_BLACK, curses.COLOR_GREEN)
|
2021-05-30 02:21:47 +02:00
|
|
|
try:
|
|
|
|
curses.init_pair(ColorPair.NORMAL, curses.COLOR_WHITE, bg)
|
|
|
|
curses.init_pair(ColorPair.ERROR, curses.COLOR_RED, bg)
|
|
|
|
curses.init_pair(ColorPair.LINK, curses.COLOR_CYAN, bg)
|
|
|
|
curses.init_pair(ColorPair.LINK_ID, curses.COLOR_WHITE, bg)
|
|
|
|
curses.init_pair(ColorPair.TITLE_1, curses.COLOR_GREEN, bg)
|
|
|
|
curses.init_pair(ColorPair.TITLE_2, curses.COLOR_MAGENTA, bg)
|
|
|
|
curses.init_pair(ColorPair.TITLE_3, curses.COLOR_MAGENTA, bg)
|
|
|
|
curses.init_pair(ColorPair.PREFORMATTED, curses.COLOR_YELLOW, bg)
|
2021-06-03 03:29:30 +02:00
|
|
|
curses.init_pair(ColorPair.BLOCKQUOTE, curses.COLOR_BLUE, bg)
|
2021-05-30 02:21:47 +02:00
|
|
|
curses.init_pair(ColorPair.LINK_PREVIEW, curses.COLOR_WHITE, bg)
|
|
|
|
except curses.error:
|
|
|
|
logging.error("Failed to init colors.")
|
|
|
|
if bg == -1:
|
|
|
|
logging.debug("Retrying with black background…")
|
|
|
|
init_colors(bg=curses.COLOR_BLACK)
|