edluffy / hologram.nvim

👻 A cross platform terminal image viewer for Neovim. Extensible and fast, written in Lua and C. Works on macOS and Linux.
MIT License
1.28k stars 31 forks source link

Error: Expected lua number #11

Closed SystematicError closed 2 years ago

SystematicError commented 2 years ago

Hey, I recently decided to try out this plugin and noticed that when running the following command:lua require("hologram").add_image(0, "/tmp/test.png", 5, 5) I get this error:

E5108: Error executing lua ...e/pack/packer/start/hologram.nvim/lua/hologram/image.lua:20: Expected lua number                                                                              
stack traceback:                                                                                                                                                                            
        [C]: in function 'nvim_buf_set_extmark'                                                                                                                                             
        ...e/pack/packer/start/hologram.nvim/lua/hologram/image.lua:20: in function 'new'                                                                                                   
        ...te/pack/packer/start/hologram.nvim/lua/hologram/init.lua:120: in function 'add_image'                                                                                            
        [string ":lua"]:1: in main chunk     

After scouring the code, I noticed something in this snippet:

function Image:new(opts)
    opts = opts or {}

    local cur_row, cur_col = unpack(vim.api.nvim_win_get_cursor(0))
    opts.row = opts.row or cur_row
    opts.col = opts.col or cur_col

    local buf = vim.api.nvim_get_current_buf()
    local ext = vim.api.nvim_buf_set_extmark(buf, vim.g.hologram_extmark_ns, opts.row, opts.col, {})

    local obj = setmetatable({
        id = buf*100 + ext,
        source = opts.source
    }, self)

    obj:identify()

    return obj
end

Here, no fallback value is set for vim.g.hologram_extmark_ns, so if the user hasn't defined it, it returns nil, which the nvim api does not like. I'm not really sure what extmark_ns really does so I'm not submitting a pull request, but hopefully this issue should be easy to fix :)

Nvim version info:

NVIM v0.6.1
Build type: Release
LuaJIT 2.0.5
Features: +acl +iconv +tui
Cassin01 commented 2 years ago

I think your configuration didn't call require'hologram'.setup() before hand, so if you are packer user , can you try to use the code below (and :PackerComple).

  use {'edluffy/hologram.nvim',
      config = function()
          require'hologram'.setup {}
          local function map(input, output)
              vim.api.nvim_set_keymap('n', input, output, { noremap = true, silent = false})
          end
          function Fft()
              local my_image = require('hologram.image'):new({ source="/Users/.../something.png", row=2,col=5})
              my_image:transmit()    
          end
          map('<space>lf', ":lua Fft()<cr>")
      end,
  }

If you get line value outside range error, fill the file with space.

SystematicError commented 2 years ago

Thanks for the help! Adding the setup function fixes it. Initially didn't have it due to it not being mentioned in the README

winkee01 commented 2 years ago

@Cassin01 I got the error line value outside range, what does it mean? what do you mean by fill the file with space???

edluffy commented 2 years ago

@winkee01 The row for an image has to be a valid line inside the buffer in which it is placed (with 0 based indices). I probably didn't make that clear enough in the docs. I have a lot of changes planned for this plugin so this issue and a few others should be fixed soon.

winkee01 commented 2 years ago

Sorry, still didn't quite understand. my settings is your minimal

local my_image = require('hologram.image'):new({
        source = '/Users/.../Documents/Chun-Li.jpeg',
        row = 11,
        col = 0,
    })

And I opened a file with 200+ lines, which is obviously more than 11 rows. But I still get this error.

Cassin01 commented 2 years ago

The buffer must be filled with any words till the row you defined (11 at that time). Can you fill the file that you want to show the picture on with any 11 words?

winkee01 commented 2 years ago

I filled my file with 100 x 100 spaces, still not able to display the image. Do you mean I have to fill it with printable characters?

Ok, this time, I fill the file with characters, but still can't display the image.

edluffy commented 2 years ago

Only PNG file format is supported right now so that is probably why

Cassin01 commented 2 years ago

you mean I have to fill it with printable characters?

No

I filled my file with 100 x 100 spaces, still not able to display the image.

My solution may not work or there is another problem on your environment.

winkee01 commented 2 years ago

I tested PNG, not working at also.

CleanShot 2022-07-11 at 23 01 11@2x
edluffy commented 2 years ago

Can you try the latest commit? I have made a few changes which includes encoding and sending only the image path to the terminal instead of the entire image contents - which is a considerable performance increase and should mean that masses of base64 data isn't dumped into the terminal like that.

winkee01 commented 2 years ago

I tried the latest commit, but still nothing was shown.

CleanShot 2022-07-11 at 23 09 13@2x

this is my test image laptop.png image

edluffy commented 2 years ago

It works fine for me. What OS are you using and what version of Kitty do you have installed? lp

winkee01 commented 2 years ago

I am using macOS Monterey, and I just installed kitty on my machine (never used it before), so I guess it's the latest version 0.25.2.

So my question is: except for your minimal settings, did I miss any other configs?