lucc / nvimpager

Use nvim as a pager to view manpages, diffs, etc with nvim's syntax highlighting
Other
386 stars 20 forks source link

bad ansi escape sequences parsing? #79

Open name-snrl opened 1 year ago

name-snrl commented 1 year ago

In some cases escape sequences are not converted to colors.


to reproduce:

nvimpager -p part.log

part.log

nvimpager full.log

full.log

I use fish, many of its man pages have the same problem, e.g. man fish-doc


I have tried several setups (with alacritty 0.11.0):

Also, I tried changing TERM to xterm-256color. Everywhere the same result.

lucc commented 1 year ago

What is the \e[K sequence supposed to do? If I cat part.log they don't seem to do anything.

PS: vimpager is unrelated.

lucc commented 1 year ago

The full log is not highlighted because nvimpager just checks the first 100 lines to decide if we should highlight terminal escape codes (function check_escape_sequences() in pager_mode())

name-snrl commented 1 year ago

What is the \e[K sequence supposed to do?

I don't really understand what it does. But often "less" is used with -R, this option ignores all sequences except color. I think we need implement this option.

name-snrl commented 1 year ago

The full log is not highlighted because nvimpager just checks the first 100 lines to decide if we should highlight terminal escape codes (function check_escape_sequences() in pager_mode())

Can we use io.read() instead of vim.api.nvim_buf_get_lines()?

lucc commented 1 year ago

about: io.read: why? I think it reads stdin or we need to open the file again with io.open. But nvim has opened it already so why not use nvim_buf_get_lines?

about ignoring unknown sequences: there are quite some of them: https://en.wikipedia.org/wiki/ANSI_escape_code

When stripping them from the buffer we match much more than when concealing them:

https://github.com/lucc/nvimpager/blob/2251f29e0d4a243cd83ca50bd4edcce06ea43ae9/lua/nvimpager.lua#L372-L379

https://github.com/lucc/nvimpager/blob/2251f29e0d4a243cd83ca50bd4edcce06ea43ae9/lua/nvimpager.lua#L635-L637

I have to read through the wiki article a bit and improve these regexes I think (help obviously apreciated :)

name-snrl commented 1 year ago

io.read: why? I think it reads stdin or we need to open the file again with io.open. But nvim has opened it already so why not use nvim_buf_get_lines?

I don't like this limitation of a hundred lines. If we check the whole file, io.read will be faster.

simple test:

local function with_io_read()
  local start = os.clock()
  local file = io.open(vim.fn.expand('%'), "r"):read("*all")

  file:find('\27%[[;?]*[0-9.;]*[A-Za-z]')
  return os.clock() - start
end

local function with_for_loop()
  local start = os.clock()

  for _, line in ipairs(vim.api.nvim_buf_get_lines(0, 0, -1, false)) do
    if line:find('\27%[[;?]*[0-9.;]*[A-Za-z]') ~= nil then
      return os.clock() - start
    end
  end

  return os.clock() - start
end

local function test()
  local msg = string.format('io.read  = %s\nfor-loop = %s\n-----',
    with_io_read(), with_for_loop())
  vim.api.nvim_echo({ { msg } }, true, {})
end

vim.keymap.set('n', 't', test)
name-snrl commented 1 year ago

I don't like this limitation of a hundred lines

OR we can simply create an option for this

lucc commented 1 year ago

I introduced the limit because somebody might use nvimpager with a large log file. At work I have log files that are easily 1-10 GB in size. No idea how long it would take with these.

I tested your example with the full.log file you provided and io.read is indead faster. I will have to see with a bigger log file.

bqv commented 1 year ago

What is the \e[K sequence supposed to do? If I cat part.log they don't seem to do anything.

I don't know what it does, but like I mentioned, it needs to be at least hidden.

often "less" is used with -R, this option ignores all sequences except color. I think we need implement this option.

good idea

illfygli commented 1 year ago

Seems like RGB colors are not parsed either. Try e.g.

printf 'normal\e[48:2:0:162:31mgreen\n'

and then open that.

lucc commented 1 year ago

@illfygli currently only color sequences constructed with semicolon are recognized.

Reading Wikipedia it is not clear to me if the colon syntax is officially defined by the ansi standard and if so if it is the same as the semicolon syntax.

I tested xfce-termial, terminator, konsole, allacritty, kitty and xterm and all of them support your example. st does not.

illfygli commented 1 year ago

I tested xfce-termial, terminator, konsole, allacritty, kitty and xterm and all of them support your example. st does not.

Interesting, I didn't notice that it was not using ; when I copied an example. :sweat_smile:

With ;, the colors are filtered out, but it would be nice if they were supported, like the basic colors.

lucc commented 1 year ago

@illfygli thanks for the heads up. I was under the impression that the escape sequences should be concealed but the colors displayed. This is a bug, I think that this did work at some point.

lucc commented 1 year ago

@illfygli I checked again and it depends on the value of 'termguicolors'. So please try set tgc.

illfygli commented 1 year ago

@illfygli I checked again and it depends on the value of 'termguicolors'. So please try set tgc.

Ah yes, that works. Thanks!