display empty files instead of opening them with xdg-open

This commit is contained in:
Ploum 2024-02-20 10:42:52 +01:00
parent 4e3d3ce62d
commit 9c8693dc09
2 changed files with 13 additions and 0 deletions

View File

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

View File

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