From 9c8693dc09ec7517a582077da42eaed045f20eb9 Mon Sep 17 00:00:00 2001 From: Ploum Date: Tue, 20 Feb 2024 10:42:52 +0100 Subject: [PATCH] display empty files instead of opening them with xdg-open --- CHANGELOG | 1 + ansicat.py | 12 ++++++++++++ 2 files changed, 13 insertions(+) diff --git a/CHANGELOG b/CHANGELOG index daa8fe9..be46e1b 100644 --- a/CHANGELOG +++ b/CHANGELOG @@ -2,6 +2,7 @@ ## 2.3 - Unreleased - offpunk/netcache: fix IPv6 as an URL (bug #40) +- ansicat: display empty files (instead of opening them with xdg-open) ## 2.2 - February 13th 2023 - cache folder is now configurable through $OFFPUNK_CACHE_PATH environment variable (by prx) diff --git a/ansicat.py b/ansicat.py index af33bb5..5c7477a 100755 --- a/ansicat.py +++ b/ansicat.py @@ -727,6 +727,13 @@ class GemtextRenderer(AbstractRenderer): links += hidden_links return r.get_final(), links +class EmptyRenderer(GemtextRenderer): + def get_mime(self): + return "text/empty" + def prepare(self,body,mode=None): + text= "(empty file)" + return [[text, "GemtextRenderer"]] + class GopherRenderer(AbstractRenderer): def get_mime(self): return "text/gopher" @@ -1312,11 +1319,16 @@ _FORMAT_RENDERERS = { "text/gopher": GopherRenderer, "image/*": ImageRenderer, "application/javascript": HtmlRenderer, + "application/json": HtmlRenderer, + "text/empty": EmptyRenderer, } def get_mime(path,url=None): #Beware, this one is really a shaddy ad-hoc function if not path: return None + #If the file is empty, simply returns it + elif os.path.exists(path) and os.stat(path).st_size == 0: + return "text/empty" elif url and url.startswith("gopher://"): #special case for gopher #code copy/pasted from netcache