lucc / nvimpager

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

Can support show line numbers using cat mode? #14

Closed kevinhwang91 closed 5 years ago

kevinhwang91 commented 5 years ago

cat with -n parameter can show line number, but nvimpager seems not to support this feature now.

lucc commented 5 years ago

At the moment that is not possible.

kevinhwang91 commented 5 years ago

get it, thanks.

lucc commented 5 years ago

I had a look at the code and I think :set number will never be visible in cat mode. That is just outside of the design of nvimpager.

nvimpager -c | cat -n is a crude workaround. You will have to live with the colors spilling over the line numbers. The code only resets the color at the end of the document. This was done in order to not emit additional color codes and reset codes for every line. Especially in git diff that makes a difference as many lines share the color with the one above.

kevinhwang91 commented 4 years ago

@lucc I use python to store the last ansi code of the current line for inserting the beginning of the next line and append the reset ansi code for the current line to prepare for the ready linenumber.

import sys
import re
prev = ""
for line in sys.stdin:
    print(prev + line.rstrip() + "\x1B[0m")
    for m in re.finditer(r"\x1B\[[0-9;]*m", line):
        pass
    if m:
        prev = m.group()

It works fine when combine cat -n or nl -ba. I think using lua (I have no idea about lua) can Implement like above code?