nvim ASCII color

Baleia Auto-Colorize for Neovim

Problem

Auto-run baleia.nvim to colorize ANSI escape codes when opening, reloading, or creating .log files.

Key Gotchas

  1. lazy.nvim race condition — Don’t use a separate autocmd that depends on vim.g.baleia before the plugin loads. Instead, use lazy.nvim’s built-in event pattern matching to trigger loading, then handle the buffer inside config.

  2. automatically() vs once() — They do different things:

    • once(buf) — colorizes existing buffer content immediately.
    • automatically(buf) — attaches an nvim_buf_attach handler for future lines only; does NOT touch current content.
  3. Reloads (:e, :e!) — config only runs once. You need a BufReadPost autocmd to re-colorize on reload.

  4. automatically() is idempotent — it checks a baleia_on_new_lines buffer var and skips if already attached. Safe to call multiple times.

Solution

All logic lives in the plugin spec — no separate autocmd files needed.

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
-- lua/plugins/baleia.lua
return {
    "m00qek/baleia.nvim",
    version = "*",
    -- lazy.nvim loads the plugin only when a .log file is opened
    event = { "BufReadPost *.log", "BufNewFile *.log" },
    config = function()
        vim.g.baleia = require("baleia").setup({ })

        local function colorize(buf)
            vim.g.baleia.once(buf)           -- colorize existing content
            vim.g.baleia.automatically(buf)  -- hook new lines (idempotent)
        end

        -- Handle reloads (:e, :e!) and future log file opens
        vim.api.nvim_create_autocmd({ "BufReadPost", "BufNewFile" }, {
            pattern = "*.log",
            callback = function()
                colorize(vim.api.nvim_get_current_buf())
            end,
        })

        -- Handle the buffer that triggered lazy loading
        -- (the autocmd above won't fire for it — event already passed)
        colorize(vim.api.nvim_get_current_buf())

        -- Manual colorize command for non-log files
        vim.api.nvim_create_user_command("BaleiaColorize", function()
            vim.g.baleia.once(vim.api.nvim_get_current_buf())
        end, { bang = true })

        -- Show baleia logs
        vim.api.nvim_create_user_command("BaleiaLogs", vim.cmd.messages, { bang = true })
    end,
}

How Each Scenario Works

Scenario What fires once automatically
First open .log Explicit call in config Colorizes existing ANSI Attaches buf handler
Reload (:e!) BufReadPost autocmd Re-colorizes reloaded content No-op (already attached)
Open another .log BufReadPost autocmd Colorizes existing ANSI Attaches buf handler
Create new .log BufNewFile autocmd No-op (empty buffer) Attaches buf handler

Notes

  • For tail -f / live-log scenarios, automatically ensures newly appended lines are colorized as they arrive.
  • Remove the BufReadPost quickfix entry from the event list if you don’t need quickfix colorization.
  • Tested on baleia.nvim v1.x (m00qek/baleia.nvim).

相关内容

william 支付宝支付宝
william 微信微信
0%