From 644e8ba08e32eb2329b613b0d527d506703f3be9 Mon Sep 17 00:00:00 2001 From: Lionel Dricot Date: Tue, 4 Jul 2023 13:59:16 +0200 Subject: [PATCH] to the point where netcache become an evidence --- ansirenderer.py | 43 +++++++++++++++++++++++++++++++++++++++++++ offpunk.py | 42 ------------------------------------------ 2 files changed, 43 insertions(+), 42 deletions(-) diff --git a/ansirenderer.py b/ansirenderer.py index 5e42de7..6c59ab4 100644 --- a/ansirenderer.py +++ b/ansirenderer.py @@ -150,6 +150,49 @@ def looks_like_base64(src,baseurl): imgurl = urllib.parse.urljoin(baseurl, imgname) return imgurl,imgdata +#return ANSI text that can be show by less +def inline_image(img_file,width): + #Chafa is faster than timg inline. Let use that one by default + inline = None + ansi_img = "" + #We avoid errors by not trying to render non-image files + if shutil.which("file"): + mime = run("file -b --mime-type %s", parameter=img_file).strip() + if not "image" in mime: + return ansi_img + if _HAS_CHAFA: + if _HAS_PIL and not _NEW_CHAFA: + # this code is a hack to remove frames from animated gif + img_obj = Image.open(img_file) + if hasattr(img_obj,"n_frames") and img_obj.n_frames > 1: + # we remove all frames but the first one + img_obj.save(img_file,format="gif",save_all=False) + inline = "chafa --bg white -s %s -f symbols" + elif _NEW_CHAFA: + inline = "chafa --bg white -t 1 -s %s -f symbols --animate=off" + if not inline and _NEW_TIMG: + inline = "timg --frames=1 -p q -g %sx1000" + if inline: + cmd = inline%width + " %s" + try: + ansi_img = run(cmd, parameter=img_file) + except Exception as err: + ansi_img = "***image failed : %s***\n" %err + return ansi_img + +def terminal_image(img_file): + #Render by timg is better than old chafa. + # it is also centered + cmd = None + if _NEW_TIMG: + cmd = "timg --loops=1 -C" + elif _HAS_CHAFA: + cmd = "chafa -d 0 --bg white -t 1 -w 1" + if cmd: + cmd = cmd + " %s" + run(cmd, parameter=img_file, direct_output=True) + + # First, we define the different content->text renderers, outside of the rest # (They could later be factorized in other files or replaced) class AbstractRenderer(): diff --git a/offpunk.py b/offpunk.py index 4a8b6ea..5c5f738 100755 --- a/offpunk.py +++ b/offpunk.py @@ -55,48 +55,6 @@ try: except ModuleNotFoundError: _HAS_SETPROCTITLE = False -#return ANSI text that can be show by less -def inline_image(img_file,width): - #Chafa is faster than timg inline. Let use that one by default - inline = None - ansi_img = "" - #We avoid errors by not trying to render non-image files - if shutil.which("file"): - mime = run("file -b --mime-type %s", parameter=img_file).strip() - if not "image" in mime: - return ansi_img - if _HAS_CHAFA: - if _HAS_PIL and not _NEW_CHAFA: - # this code is a hack to remove frames from animated gif - img_obj = Image.open(img_file) - if hasattr(img_obj,"n_frames") and img_obj.n_frames > 1: - # we remove all frames but the first one - img_obj.save(img_file,format="gif",save_all=False) - inline = "chafa --bg white -s %s -f symbols" - elif _NEW_CHAFA: - inline = "chafa --bg white -t 1 -s %s -f symbols --animate=off" - if not inline and _NEW_TIMG: - inline = "timg --frames=1 -p q -g %sx1000" - if inline: - cmd = inline%width + " %s" - try: - ansi_img = run(cmd, parameter=img_file) - except Exception as err: - ansi_img = "***image failed : %s***\n" %err - return ansi_img - -def terminal_image(img_file): - #Render by timg is better than old chafa. - # it is also centered - cmd = None - if _NEW_TIMG: - cmd = "timg --loops=1 -C" - elif _HAS_CHAFA: - cmd = "chafa -d 0 --bg white -t 1 -w 1" - if cmd: - cmd = cmd + " %s" - run(cmd, parameter=img_file, direct_output=True) - def parse_mime(mime): options = {} if mime: