Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Add support for previewing images in room scrollback #108

Merged
merged 2 commits into from Nov 16, 2023

Conversation

benjajaja
Copy link
Contributor

@benjajaja benjajaja commented May 20, 2023

An image says more than a thousand words:
image

There has been some work to get some small change into ratatui that allows for "skipping" of rendering some cells (characters). This in turn allowed to make a crate ratatui-image that provides a image widgets for ratatui, taking care of the terminal's graphics protocol automatically. Currently the supported "protocols" are sixels, kitty, and unicode-halfblocks. iTerm2's protocol should be easy to add, I just don't have access to a mac.

This PR uses ratatui-image widget's render() method to render images in the ScrollBack widget, in the chat lines. This seems hacky, but this kind of usage was actually intended and recommended for these kind of use cases in the discussion on the ratatui PR.

Currently, the download and image processing happens in one spawned tokio task per attachment. I also have a branch where only one "worker" is created, but I don't actually know if that is an actual improvement. I mainly made that branch/version to learn a bit more about tokio/tasks/threads in rust.

@benjajaja benjajaja force-pushed the image_preview branch 2 times, most recently from c9a0be7 to aab4f0e Compare May 20, 2023 09:06
src/worker.rs Outdated Show resolved Hide resolved
@benjajaja benjajaja force-pushed the image_preview branch 2 times, most recently from ffeda78 to 5e2ed67 Compare May 20, 2023 09:38
@benjajaja benjajaja marked this pull request as ready for review May 20, 2023 11:41
src/worker.rs Outdated Show resolved Hide resolved
@justchokingaround
Copy link

really looking forward to this!

@benjajaja
Copy link
Contributor Author

benjajaja commented Jun 7, 2023

It really only works with this patch on Alacritty, which seems to work because it simply never draws over image graphics. I'm working on fixing this in tui-rs-revival/ratatui in the linked PR, by setting some area to be "skipped".

@r3k2
Copy link

r3k2 commented Jun 21, 2023

This will be great. I am using foot on wayland/sway should work. please release! 👍

@r3k2
Copy link

r3k2 commented Oct 10, 2023

I was happy to see movement in this PR today, how is going? do you think if I patch this myself will it be somehow a bit stable?

@benjajaja benjajaja force-pushed the image_preview branch 4 times, most recently from 4cb4388 to ed1a01a Compare October 12, 2023 14:39
@benjajaja
Copy link
Contributor Author

benjajaja commented Oct 12, 2023

@r3k2 I don't know what you mean by "patch". I can give you a status update though:

  • Graphics support merged in ratatui and graphics related code has been extracted to new crate ratatui-image.
  • Iamb uses ratatui-image to render images with whatever protocol is detected (sixel, kitty, or unicode).
  • The attachments which are "autodownloaded" for rendering are not cached in any way (this should be configurable).
  • Windows builds won't work because sixel lib probably won't compile / link correctly and is missing terminal-font-size detection (rustix). We should be able to make use of the relevant feature in ratatui for font-size, but protocol-guessing is a whole 'nother can of worms. Maybe it should guess on linux, and just have a configuration setting for windows users to e.g. force sixels.

To maybe answer your question, if you want to tackle something, you could try with the cache so that images aren't downloaded over and over. There should probably be a config option to specificy a cache dir, similar to download_dir but maybe in the image_preview section.

I am trying to fix the windows builds.

@benjajaja benjajaja force-pushed the image_preview branch 2 times, most recently from d35d6e5 to e22decd Compare October 12, 2023 16:59
@r3k2
Copy link

r3k2 commented Oct 12, 2023

@benjajaja cool, who cares about windows? :D :D just release it and say only works on Linux/Nix systems for now and that windows will come.

@benjajaja benjajaja changed the title Image preview: sixel Image previews Oct 15, 2023
@benjajaja benjajaja force-pushed the image_preview branch 4 times, most recently from 736f3b0 to d60625e Compare October 15, 2023 19:12
@benjajaja benjajaja force-pushed the image_preview branch 5 times, most recently from 4e5170c to b3e97f8 Compare October 16, 2023 08:51
@r3k2
Copy link

r3k2 commented Oct 16, 2023

can't wait for this, thanks @benjajaja for all your work ❤️ this will prob be the first CLI/TUI matrix client with sixel and other image previews, gomuks only shows a ascii representation, it will def bump to the top "iamb" in our TUI/low/simple resources/vim-keys/ community when it comes to matrix clients

@benjajaja benjajaja force-pushed the image_preview branch 4 times, most recently from 2edc335 to ec22964 Compare October 19, 2023 08:56
@benjajaja benjajaja marked this pull request as ready for review October 19, 2023 10:15
@benjajaja benjajaja force-pushed the image_preview branch 2 times, most recently from 88a8ffe to 2861155 Compare October 23, 2023 12:35
@benjajaja benjajaja force-pushed the image_preview branch 3 times, most recently from 1874d0a to 91b23de Compare November 13, 2023 12:29
Additionally to enable the preview feature, one must add it to
config.json:
```json
    "image_preview": {}
```
See documentation for additional settings - they shouldn't be necessary
in most cases.

A feature flag for sixel support is added.

Windows is basically unsupported, but it is possible to force the
feature to use half-blocks, if the `protocol` option is set with `type`
and `font_size`.
@r3k2
Copy link

r3k2 commented Nov 13, 2023

I see all check passed and such, why we are not adding this already to the main IAMB? 🤔

@craftyguy
Copy link

craftyguy commented Nov 13, 2023 via email

@ulyssa ulyssa changed the title Image previews Add support for previewing images in room scrollback Nov 14, 2023
@ulyssa
Copy link
Owner

ulyssa commented Nov 14, 2023

As @craftyguy said, this is a big feature, and I like to make sure I understand and try out PRs locally before merging, so that I can document them correctly on the website and answer questions in #iamb-users when they come up.

I've been playing around with this locally and it's really cool! 🎉 This is amazing stuff @benjajaja ! Thank you for doing the halfblocks work, too, it'll make it possible for people without sixel-aware terminals or kitty to still get image previews.

I made a couple small changes in 164503b: I moved the picker setup code into src/base.rs so that it can run as part of ChatStore::new, and fixed a couple issues I ran into where the image_previews dir didn't exist yet, and images were missing the x offset for placing them in the right vertical split.

I see that you just pushed the updates to use ratatui-image@0.4.1 yesterday. If you have anything else you want to add to the PR, let me know. but otherwise I'm down to merge this, and take care of anything else in a follow-up PR.

@ulyssa
Copy link
Owner

ulyssa commented Nov 14, 2023

@benjajaja I've pushed some example docs to ulyssa/iamb.chat#18 . Let me know if you think that anything else should be mentioned there.

@benjajaja
Copy link
Contributor Author

@ulyssa There is nothing else I want to add for now. At some point later rataui-image will get support for iTerm2's graphics protocol (I can't test it because I don't have any mac around), but that should just be version bump.

Thank you for moving the setup code!

@ulyssa ulyssa merged commit 221faa8 into ulyssa:main Nov 16, 2023
3 checks passed
@ulyssa ulyssa added this to the v0.0.9 milestone Feb 29, 2024
@ulyssa ulyssa mentioned this pull request Mar 29, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

5 participants