d0c-s4vage / lookatme

An interactive, terminal-based markdown presenter
https://lookatme.readthedocs.io/en/latest/
MIT License
2.1k stars 63 forks source link

Support ANSI sequences #119

Open pwntester opened 3 years ago

pwntester commented 3 years ago

Since ueberzug is not working on MacOS, I though about using iterm support for rendering images via ANSI sequences as explained here

I tried it on lookatme but does not work. Simple ANSI sequences such as \x1b[1;31mFOO don't work neither. I think it would be great to support ANSI sequences to introduce color and images

Thanks!

pwntester commented 3 years ago

I tried using the terminal as in:

```terminal8
echo "\x1b[1;31mFOO"

But got this error:

Error rendering slide 1: Focus Widget <LineBox selectable flow widget <BoxAdapter selectable flow widget height=8>> at position 7 within listbox calculated cursor coords (1, 2) but rendered cursor coords None! Rerun with --debug to view the full traceback in logs Aborted!

bew commented 3 years ago

Agreed, also I'd love to not put ansi stuff like this in the file, but have lookatme (a plugin?) do it for me, we may need to add some size constraints in some way, to ensure the image fits correctly.. NOTE: that this ansi sequence not only works with iterm2 but also with Wezterm, which is cross platform (windows, mac, linux), and would allow image display anywhere, even over ssh.

d0c-s4vage commented 3 years ago

Hmm, the last time I looked into this was to try to get imgcat (and similar tools) to display images correctly. Back then the recommended way was to translate the ANSI sequences into urwid text markup. All other suggestions had to do with printing the ANSI sequences in an embedded terminal, which urwid then would directly translate for you (not my favorite idea). This wouldn't work with iTerm though, because it needs to directly receive the ANSI sequences (https://iterm2.com/documentation-images.html)

HOWEVER it seems like there's a better way (https://github.com/urwid/urwid/issues/177#issuecomment-715355589) that uses a custom widget (https://github.com/kpj/pdftty/blob/master/pdftty/ansi_widget.py) to directly send the ANSI sequences to the terminal, which is what we would need to make use of iTerm's image rendering feature. Thank you @kpj for sharing this approach!

I think building on/adapting @kpj's ANSIWidget class inside an iterm-specific lookatme extension could be a good approach.