about summary refs log tree commit diff stats
path: root/adapter/format/img2html.nim
diff options
context:
space:
mode:
authorbptato <nincsnevem662@gmail.com>2024-12-30 20:02:12 +0100
committerbptato <nincsnevem662@gmail.com>2024-12-30 20:02:12 +0100
commit92b99ad3480bce8ce8e76b423ebac7b5eecf92a1 (patch)
treeebe3cc447a08bc057365d041e0085f986d687298 /adapter/format/img2html.nim
parentb2da6ac64368761599f9e6f2db9d8c7722b9c506 (diff)
downloadchawan-92b99ad3480bce8ce8e76b423ebac7b5eecf92a1.tar.gz
Add built-in image viewer
It works by emitting a base64 URI inside an img tag.
Very inefficient, but useful if no external viewer is set up
(e.g. over SSH).
Diffstat (limited to 'adapter/format/img2html.nim')
-rw-r--r--adapter/format/img2html.nim22
1 files changed, 22 insertions, 0 deletions
diff --git a/adapter/format/img2html.nim b/adapter/format/img2html.nim
new file mode 100644
index 00000000..29604805
--- /dev/null
+++ b/adapter/format/img2html.nim
@@ -0,0 +1,22 @@
+import std/os
+
+import utils/twtstr
+
+proc main() =
+  if paramCount() != 2:
+    stderr.writeLine("Usage: img2html [content-type] [title]")
+    quit(1)
+  stdout.write("<!DOCTYPE html><title>" & paramStr(2).htmlEscape() &
+    "</title><img src='data:" & paramStr(1) & ";base64,")
+  var buffer {.noinit.}: array[6144, uint8]
+  var s = ""
+  while true:
+    let n = stdin.readBuffer(addr buffer[0], buffer.len)
+    if n == 0:
+      break
+    s.btoa(buffer.toOpenArray(0, n - 1))
+    stdout.write(s)
+    s.setLen(0)
+  stdout.write("'>")
+
+main()