lucc / nvimpager

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

`cat` mode stopped colorising syntax #101

Open kaliberr44 opened 1 month ago

kaliberr44 commented 1 month ago

I'm pretty sure it worked before, but right now cat mode is not colorising output (neither nvimpager "$1"). nvimpager -p "$1" works correctly with all the syntax colouring.

❯ uname -a
Darwin kaliberr44-mbp.private.net 23.5.0 Darwin Kernel Version 23.5.0: Wed May  1 20:12:58 PDT 2024; root:xnu-10063.121.3~5/RELEASE_ARM64_T6000 arm64 arm Darwin

❯ nvimpager -v                                               
nvimpager 0.13.0

❯ nvim -v                                               
NVIM v0.10.0
Build type: Release
LuaJIT 2.1.1716656478

nvimpager config:

-- This file is automatically loaded by plugins.core
vim.g.mapleader = " "
vim.g.maplocalleader = "\\"

vim.opt.clipboard = vim.env.SSH_TTY and "" or "unnamedplus" -- Sync with system clipboard
vim.opt.number = true -- Make line numbers default
-- Case-insensitive searching UNLESS \C or one or more capital letters in the search term
vim.opt.ignorecase = true
vim.opt.smartcase = true
vim.opt.termguicolors = true

vim.opt.shiftround = true -- Round indent
vim.opt.shiftwidth = 2 -- Size of an indent
vim.opt.tabstop = 2 -- Number of spaces tabs count for
vim.opt.scrolloff = 4 -- Lines of context
vim.opt.sidescrolloff = 8 -- Columns of context
vim.opt.linebreak = true -- Wrap lines at convenient points
vim.opt.laststatus = 3 -- global statusline
vim.opt.showmode = false -- Dont show mode since we have a statusline
vim.opt.showcmd = false -- Dont show command or size of selected area

vim.opt.runtimepath:append(",~/.local/share/nvim/lazy/github-nvim-theme")
vim.cmd("colorscheme github_dark_dimmed")

Sample file for syntax color check:

local M = {}

function M:peek()
    local child = Command("nvimpager")
        :args({
            "-c",
            tostring(self.file.url),
        })
        :stdout(Command.PIPED)
        :stderr(Command.PIPED)
        :spawn()

    if not child then
        return self:fallback_to_builtin()
    end

    local limit = self.area.h
    local i, lines = 0, ""
    repeat
        local next, event = child:read_line()
        if event == 1 then
            return self:fallback_to_builtin()
        elseif event ~= 0 then
            break
        end

        i = i + 1
        if i > self.skip then
            lines = lines .. next
        end
    until i >= self.skip + limit

    child:start_kill()
    if self.skip > 0 and i < self.skip + limit then
        ya.manager_emit("peek", { math.max(0, i - limit), only_if = self.file.url, upper_bound = true })
    else
        lines = lines:gsub("\t", string.rep(" ", PREVIEW.tab_size))
        ya.preview_widgets(self, { ui.Paragraph.parse(self.area, lines) })
    end
end

function M:seek(units)
    local h = cx.active.current.hovered
    if h and h.url == self.file.url then
        local step = math.floor(units * self.area.h / 10)
        ya.manager_emit("peek", {
            math.max(0, cx.active.preview.skip + step),
            only_if = self.file.url,
        })
    end
end

function M:fallback_to_builtin()
    local _, bound = ya.preview_code(self)
    if bound then
        ya.manager_emit("peek", { bound, only_if = self.file.url, upper_bound = true })
    end
end

return M
kaliberr44 commented 1 month ago

As a side note in cat mode these also seem not to work:

vim.opt.shiftwidth = 2 -- Size of an indent
vim.opt.tabstop = 2 -- Number of spaces tabs count for

nvimpager -c image nvimpager -p image

So maybe it is a problem of not loading config in cat mode?

lucc commented 1 month ago

Shiftwidth is a setting that only has an effect when editing, so it is not relevant for nvimpager (any mode). Tabstop is not yet supported in cat mode, this is tracked in #35.

You can check if the config file is loaded in cat mode by just adding some debug statement to the config file:

print("config loaded")
vim.cmd.quitall()

Are you sure you want to append to rtp with the comma? I think it should be (the docs say it works like :set rtp+=

vim.opt.runtimepath:append("~/.local/share/nvim/lazy/github-nvim-theme")

Can you minimize your config to the smallest version that produces the error?

You can debug the bash part of nvimpager with bash -x nvimpager $args. Does the pager mode do something different than the cat mode at this point?

kaliberr44 commented 1 month ago

You can check if the config file is loaded in cat mode by just adding some debug statement to the config file:

print("config loaded")
vim.cmd.quitall()

This works for both cat and pager mode, thank you.

Are you sure you want to append to rtp with the comma? I think it should be (the docs say it works like :set rtp+=

vim.opt.runtimepath:append("~/.local/share/nvim/lazy/github-nvim-theme")

I did remove the comma, but that did not help.

Here is the minimal config with chosen syntax colouring:

vim.opt.runtimepath:append("~/.local/share/nvim/lazy/github-nvim-theme")
vim.cmd("colorscheme github_dark_dimmed")

Still the same issue:

❯ bash -x nvimpager -p ~/.config/yazi/plugins/nvimpager.yazi/init.lua
+ RUNTIME=/opt/homebrew/Cellar/nvimpager/0.13.0/share/nvimpager/runtime
+ PARENT=782
+ TMPFILE=
+ export RUNTIME
+ export PARENT
+ export TMPFILE
+ export NVIM_APPNAME=nvimpager
+ NVIM_APPNAME=nvimpager
+ mode=auto
+ nvim=nvim
+ getopts achpv flag
+ case $flag in
+ mode=pager
+ getopts achpv flag
+ shift 1
+ [[ 1 -eq 0 ]]
+ [[ ! -t 1 ]]
+ files=()
+ [[ 1 -gt 0 ]]
+ [[ -f /Users/kaliberr44/.config/yazi/plugins/nvimpager.yazi/init.lua ]]
+ files+=("$1")
+ shift
+ [[ 0 -gt 0 ]]
+ [[ -z '' ]]
+ [[ 1 -eq 0 ]]
+ [[ -n '' ]]
+ args=(-R --cmd 'set rtp+=$RUNTIME | lua nvimpager = require("nvimpager"); nvimpager.stage1()' -c 'lua nvimpager.stage2()')
+ [[ pager = cat ]]
+ [[ pager = auto ]]
+ exec -a nvimpager nvim -R --cmd 'set rtp+=$RUNTIME | lua nvimpager = require("nvimpager"); nvimpager.stage1()' -c 'lua nvimpager.stage2()' /Users/kaliberr44/.config/yazi/plugins/nvimpager.yazi/init.lua
❯ bash -x nvimpager -c ~/.config/yazi/plugins/nvimpager.yazi/init.lua
+ RUNTIME=/opt/homebrew/Cellar/nvimpager/0.13.0/share/nvimpager/runtime
+ PARENT=782
+ TMPFILE=
+ export RUNTIME
+ export PARENT
+ export TMPFILE
+ export NVIM_APPNAME=nvimpager
+ NVIM_APPNAME=nvimpager
+ mode=auto
+ nvim=nvim
+ getopts achpv flag
+ case $flag in
+ mode=cat
+ getopts achpv flag
+ shift 1
+ [[ 1 -eq 0 ]]
+ [[ ! -t 1 ]]
+ files=()
+ [[ 1 -gt 0 ]]
+ [[ -f /Users/kaliberr44/.config/yazi/plugins/nvimpager.yazi/init.lua ]]
+ files+=("$1")
+ shift
+ [[ 0 -gt 0 ]]
+ [[ -z '' ]]
+ [[ 1 -eq 0 ]]
+ [[ -n '' ]]
+ args=(-R --cmd 'set rtp+=$RUNTIME | lua nvimpager = require("nvimpager"); nvimpager.stage1()' -c 'lua nvimpager.stage2()')
+ [[ cat = cat ]]
+ args+=(--headless)
+ exec -a nvimpager nvim -R --cmd 'set rtp+=$RUNTIME | lua nvimpager = require("nvimpager"); nvimpager.stage1()' -c 'lua nvimpager.stage2()' --headless /Users/kaliberr44/.config/yazi/plugins/nvimpager.yazi/init.lua
lucc commented 1 month ago

This looks all good. You will have to debug what is actually going on inside nvimpager/neovim.

Some ideas:

kaliberr44 commented 1 month ago

:scriptnames shows that is loaded.

Even with minimal config (1 line):

vim.cmd("colorscheme retrobox")

something is off. In pager mode it looks OK: image

in cat mode there is colour change but it is completely wrong: image

kaliberr44 commented 1 month ago

With no config at all - it is the same. No colouring in cat mode and in pager it is fine.

lucc commented 1 month ago

I can see the same problem with the background color but for me the foreground color seems to work:

2024-07-17-065806_453x175_scrot

kaliberr44 commented 1 month ago

I'm 99% sure that it did work like a month ago, without any config change. So maybe an update of nvimpager or nvim or kitty did that.

psi4j commented 4 days ago

I'm 99% sure that it did work like a month ago, without any config change. So maybe an update of nvimpager or nvim or kitty did that.

I'm confused. Did you ever figure this out? I'm also trying to use nvimpager in cat mode for the previews with yazi, but I'm not having any luck. nvimpager doesn't show the correct syntax highlighting in any of the modes. Maybe I'm doing something wrong.