gopher: properly render error lines

This commit is contained in:
dece 2021-06-13 01:55:48 +02:00
parent 54aeafd878
commit 36d0e6f7b1
3 changed files with 20 additions and 8 deletions

View file

@ -3,6 +3,9 @@
In Bebop we use a list of elements as produced by our parser. These elements are In Bebop we use a list of elements as produced by our parser. These elements are
converted into so-called "metalines", which are the text lines as they will be converted into so-called "metalines", which are the text lines as they will be
displayed, along with associated meta-data such as its type or a link's URL. displayed, along with associated meta-data such as its type or a link's URL.
Note that metalines can be generated by custom functions without relying on the
elements classes as they are quite coupled to Gemtext parsing/rendering.
""" """
import string import string
@ -33,6 +36,7 @@ class LineType(IntEnum):
PREFORMATTED = 6 PREFORMATTED = 6
BLOCKQUOTE = 7 BLOCKQUOTE = 7
LIST_ITEM = 8 LIST_ITEM = 8
ERROR = 9 # Not part of Gemtext but useful internally.
def generate_metalines(elements, width, dumb=False): def generate_metalines(elements, width, dumb=False):

View file

@ -70,5 +70,7 @@ def get_base_line_attributes(line_type) -> int:
return curses.color_pair(ColorPair.PREFORMATTED) return curses.color_pair(ColorPair.PREFORMATTED)
elif line_type == LineType.BLOCKQUOTE: elif line_type == LineType.BLOCKQUOTE:
return curses.color_pair(ColorPair.BLOCKQUOTE) | A_ITALIC return curses.color_pair(ColorPair.BLOCKQUOTE) | A_ITALIC
elif line_type == LineType.ERROR:
return curses.color_pair(ColorPair.ERROR) | curses.A_BOLD
else: # includes LineType.PARAGRAPH else: # includes LineType.PARAGRAPH
return curses.color_pair(ColorPair.NORMAL) return curses.color_pair(ColorPair.NORMAL)

View file

@ -53,7 +53,6 @@ USER_FRIENDLY_TYPES = {t.name.lower(): t for t in ItemType}
ICONS = { ICONS = {
ItemType.FILE: "📄", ItemType.FILE: "📄",
ItemType.DIR: "📂", ItemType.DIR: "📂",
ItemType.ERROR: "",
ItemType.SEARCH: "", ItemType.SEARCH: "",
ItemType.HTML: "🌐", ItemType.HTML: "🌐",
} }
@ -256,19 +255,26 @@ def parse_source(source: str, item_type: ItemType):
break break
parts = tline.split("\t") parts = tline.split("\t")
if len(parts) != 4: # If the map is poorly formatted and parts are missing, pad with
# TODO move me away # empty parts.
# Does not seem to be split by tabs, may be a file. while len(parts) < 4:
metalines.append(({"type": LineType.PARAGRAPH}, line)) parts.append("")
continue
item_type = ItemType(ltype) item_type = ItemType(ltype)
label, path, host, port = parts label, path, host, port = parts
# INFO: render as a simple text line.
if item_type == ItemType.INFO: if item_type == ItemType.INFO:
meta = {"type": LineType.PARAGRAPH} metalines.append(({"type": LineType.PARAGRAPH}, label))
metalines.append((meta, label))
continue continue
# ERROR: render as an error line.
if item_type == ItemType.ERROR:
metalines.append(({"type": LineType.ERROR}, label))
continue
# Other item types are rendered as links, with a special case for
# "URL:"-type selectors in HTML items.
if item_type == ItemType.HTML and path[:4].upper() == "URL:": if item_type == ItemType.HTML and path[:4].upper() == "URL:":
link_url = path[4:] link_url = path[4:]
else: else: