~lioploum/offpunk

9c8693dc09ec7517a582077da42eaed045f20eb9 — Ploum 2 months ago 4e3d3ce
display empty files instead of opening them with xdg-open
2 files changed, 13 insertions(+), 0 deletions(-)

M CHANGELOG
M ansicat.py
M CHANGELOG => CHANGELOG +1 -0
@@ 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)

M ansicat.py => ansicat.py +12 -0
@@ 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