From 85934eedcfe4b24e067f4a3303bc4e80caf9353b Mon Sep 17 00:00:00 2001 From: dece Date: Mon, 8 Mar 2021 22:30:39 +0100 Subject: [PATCH] rendering: show link preview after links --- bebop/colors.py | 5 ++++ bebop/rendering.py | 58 ++++++++++++++++++++++++++++++++-------------- 2 files changed, 45 insertions(+), 18 deletions(-) diff --git a/bebop/colors.py b/bebop/colors.py index 69554bd..9cd8aab 100644 --- a/bebop/colors.py +++ b/bebop/colors.py @@ -3,6 +3,7 @@ from enum import IntEnum class ColorPair(IntEnum): + # Colors for specific Gemtext line type. NORMAL = 0 ERROR = 1 LINK = 2 @@ -12,6 +13,9 @@ class ColorPair(IntEnum): TITLE_3 = 6 PREFORMATTED = 7 BLOCKQUOTE = 8 + + # Colors for other usage in the browser. + LINK_PREVIEW = 9 DEBUG = 99 @@ -26,4 +30,5 @@ def init_colors(): curses.init_pair(ColorPair.TITLE_3, curses.COLOR_MAGENTA, -1) curses.init_pair(ColorPair.PREFORMATTED, curses.COLOR_YELLOW, -1) curses.init_pair(ColorPair.BLOCKQUOTE, curses.COLOR_CYAN, -1) + curses.init_pair(ColorPair.LINK_PREVIEW, curses.COLOR_WHITE, -1) curses.init_pair(ColorPair.DEBUG, curses.COLOR_BLACK, curses.COLOR_GREEN) diff --git a/bebop/rendering.py b/bebop/rendering.py index 9c5163e..1c8337f 100644 --- a/bebop/rendering.py +++ b/bebop/rendering.py @@ -223,27 +223,49 @@ def render_lines(metalines, window, max_width): new_dimensions = num_lines, max_width window.resize(*new_dimensions) for line_index, metaline in enumerate(metalines): - meta, line = metaline - line = line[:max_width - 1] - line_type = meta["type"] - if line_type == LineType.TITLE_1: - attr = curses.color_pair(ColorPair.TITLE_1) | curses.A_BOLD - elif line_type == LineType.TITLE_2: - attr = curses.color_pair(ColorPair.TITLE_2) | curses.A_BOLD - elif line_type == LineType.TITLE_3: - attr = curses.color_pair(ColorPair.TITLE_3) - elif line_type == LineType.LINK: - attr = curses.color_pair(ColorPair.LINK) - elif line_type == LineType.PREFORMATTED: - attr = curses.color_pair(ColorPair.PREFORMATTED) - elif line_type == LineType.BLOCKQUOTE: - attr = curses.color_pair(ColorPair.BLOCKQUOTE) | curses.A_ITALIC - else: # includes LineType.PARAGRAPH - attr = curses.color_pair(ColorPair.NORMAL) try: - window.addstr(line, attr) + render_line(metaline, window, max_width) except ValueError: return new_dimensions if line_index < num_lines - 1: window.addstr("\n") return new_dimensions + + +def render_line(metaline, window, max_width): + """Write a single line to the window.""" + meta, line = metaline + line_type = meta["type"] + attributes = get_base_line_attributes(line_type) + line = line[:max_width - 1] + window.addstr(line, attributes) + if meta["type"] == LineType.LINK and "url" in meta: + url_text = f' - {meta["url"]}' + attributes = ( + curses.color_pair(ColorPair.LINK_PREVIEW) + | curses.A_DIM + | curses.A_ITALIC + ) + window.addstr(url_text, attributes) + + +def get_base_line_attributes(line_type): + """Return the base attributes for this line type. + + Other attributes may be freely used later for this line type but this is + what is used at the start of most lines of the given type. + """ + if line_type == LineType.TITLE_1: + return curses.color_pair(ColorPair.TITLE_1) | curses.A_BOLD + elif line_type == LineType.TITLE_2: + return curses.color_pair(ColorPair.TITLE_2) | curses.A_BOLD + elif line_type == LineType.TITLE_3: + return curses.color_pair(ColorPair.TITLE_3) + elif line_type == LineType.LINK: + return curses.color_pair(ColorPair.LINK) + elif line_type == LineType.PREFORMATTED: + return curses.color_pair(ColorPair.PREFORMATTED) + elif line_type == LineType.BLOCKQUOTE: + return curses.color_pair(ColorPair.BLOCKQUOTE) | curses.A_ITALIC + else: # includes LineType.PARAGRAPH + return curses.color_pair(ColorPair.NORMAL)