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

feat(dashboard): introducing support for full resolution dashboard images in terminals that support Kitty graphics protocol #1586

Open
wants to merge 4 commits into
base: main
Choose a base branch
from

Conversation

saxon1964
Copy link
Contributor

@saxon1964 saxon1964 commented Mar 16, 2025

(as of 23 March, all suggestions and changes have been implemented and the following text has been updated accordingly)

Current dashboard implementation does not allow "real" images on snacks dashboard. All that you can get is chafa-like images that are just plain ugly (unless you are happy with character blocks colored with some almost random color). The real image is almost unrecognizable. In Kitty and several other terminals it is possible to render image natively. And all support for it is already there, built into snacks. I have just connected some simple dots.

1

Isn't it nice? :)

Here is my dashboard configuration:

dashboard = {
        enabled = true,
        sections = {
          {
            section = "header",
            padding = 1
          },
          {
            align = "center",
            text = {
              { nvimInfo, hl = "SnacksDashboardHeader" },
            },
          },
          { icon = " ", title = "Keymaps", section = "keys", indent = 2, padding = 0 },
          {
            action = ":Neorg index",
            key = "o",
            desc = "Neorg Index Page",
            icon = " ",
            indent = 2,
          },
          {
            action = ":Neorg journal today",
            key = "j",
            desc = "Neorg Journal Today",
            icon = " ",
            indent = 2,
            padding = 1,
          },
          { icon = " ", title = "Recent Files", section = "recent_files", indent = 2, padding = 1 },
          { icon = " ", title = "Projects", section = "projects", indent = 2, padding = 1 },
          {
            title = "Enjoy the day!",
            section = "image",
            pane = 2,
            height = 25,
            -- source can be a function that returns location (path or url) or a string representing the location 
            -- source = "~/dev/movies/server/src/main/resources/public/graphics/dalle-logo-large.jpeg",
            source = function() return "https://picsum.photos/600/600/?rnd" .. math.random(1, 1000000) end,
            padding = 1,
          },
          {
            section = "terminal",
            cmd = "fortune -s",
            height = 2,
            pane = 2,
            padding = 1,
          },
          {
            section = "startup",
            pane = 2,
          },
        },
      },

Notice that there is a new section ("image"). Image source is a web URL or a path to a local image, or a function that returns URL or path. In this particular case image is obtained from "picsum" online service. Snacks cashes images so by calling the same URL you'll get the same image from the cache. Because of that source key is a function that appends some random parameter to the URL. You always get one new image per session because URLs do not match strictly!

To function properly, a bug in placement.lua file has to be fixed as well (which I did). The code places extmark while image is being converted but it does not do proper cleanup when the conversion is over. That leaves an ugly image processing message on the screen above the image.

I believe this is a logical addition to snacks.dashboard - why stay limited to chafa in the era of kitty and other terminals that handle native images perfectly?

@saxon1964 saxon1964 changed the title feat(Dashboard): Introducing support for full resolution dashboard images in terminals that support Kitty graphics protocol feat(Dashboard): introducing support for full resolution dashboard images in terminals that support Kitty graphics protocol Mar 16, 2025
@saxon1964 saxon1964 changed the title feat(Dashboard): introducing support for full resolution dashboard images in terminals that support Kitty graphics protocol feat(dashboard): introducing support for full resolution dashboard images in terminals that support Kitty graphics protocol Mar 16, 2025
-- hash to prevent randomizing the same image url twice
local randomized_urls = {}

---@param opts {src:string, height?:number, width?:number, random_src_param?:boolean}|snacks.dashboard.Item
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Should the last param field be randomize_src instead of random_src_param?

I'm just asking genuinely as I'm not good at programming and might totally misunderstand.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks, good point! I'll fix that.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed.

@gwww
Copy link

gwww commented Mar 21, 2025

It's not up to me, but the randomized URL functionality seems better to not be in this. Maybe something like the following would be better:

@field source string|fun():string

That way the function can do whatever it wants, including randomizing.

Does this support files on the local file system?

(Note: I used the name source to be consistent with some other bits in Snacks)

@saxon1964
Copy link
Contributor Author

saxon1964 commented Mar 23, 2025

It's not up to me, but the randomized URL functionality seems better to not be in this. Maybe something like the following would be better:

@field source string|fun():string

That way the function can do whatever it wants, including randomizing.

Does this support files on the local file system?

(Note: I used the name source to be consistent with some other bits in Snacks)

This is a great idea and, in fact, allows for simpler implementation. I'll implement that today. And, yes, the code supports local images because Folke's Image class supports that out of the box.

Thanks for the tip!

@saxon1964 saxon1964 marked this pull request as draft March 23, 2025 08:01
…(path or URL) or a function that returns location
@saxon1964 saxon1964 requested a review from dpetka2001 March 23, 2025 10:38
@saxon1964 saxon1964 marked this pull request as ready for review March 23, 2025 10:39
@dpetka2001
Copy link
Contributor

@saxon1964 I'm just a simple user with very limited programming knowledge as a hobby of mine. I'm not a programmer. I haven't even looked at snacks.image code because it's too complicated for me. The previous comment I made was just because it was something quite obvious in the Lua annotations. You're much more experienced than me.

The maintainer will review this properly.

@nebunebu
Copy link

nebunebu commented Apr 2, 2025

I'd be very happy to see this or something like this added.

I am not a programmer, so this could just be a skill issue, but is it not feasible to add the align property the image section for the dashboard?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

5 participants