Colorize text with ANSI escape sequences (8, 16, 256 or TrueColor)
baleia.nvim
[![Integration][integration-badge]][integration-runs]
Colorize text with ANSI escape sequences (8, 16, 256 or TrueColor).
Requirements: Neovim 0.9.0 or higher.
Install
Using lazy.nvim:
{
"m00qek/baleia.nvim",
version = "*",
config = function()
vim.g.baleia = require("baleia").setup({ })
-- Command to colorize the current buffer vim.api.nvimcreateuser_command("BaleiaColorize", function() vim.g.baleia.once(vim.api.nvimgetcurrent_buf()) end, { bang = true })
-- Command to show logs vim.api.nvimcreateuser_command("BaleiaLogs", vim.cmd.messages, { bang = true }) end, }
Usage & Configuration
Baleia exposes functions to colorize buffers. You can use them manually (commands) or automatically (autocmds).
1. Manual Colorization
The setup above creates a :BaleiaColorize command. Use this when you open a file with ANSI codes (like a log file) and want to colorize it once.
vim.g.baleia.once(vim.api.nvimgetcurrent_buf())
2. Automatic Colorization (Tailing Logs)
To automatically colorize lines as they are appended to a buffer (e.g., when tailing a log file), use baleia.automatically.
Example: Colorize all .log files:
vim.api.nvimcreateautocmd({ "BufWinEnter" }, {
pattern = "*.log",
callback = function()
vim.g.baleia.automatically(vim.api.nvimgetcurrent_buf())
end,
})
Example: Colorize Quickfix Window:
vim.api.nvimcreateautocmd({ "BufReadPost" }, {
pattern = "quickfix",
callback = function()
vim.g.baleia.automatically(vim.api.nvimgetcurrent_buf())
end,
})
Setup Options
Pass these options to require("baleia").setup({...}):
| option | default value | description | | -----------------| --------------- | --------------------------------------------------------- | | name | "BaleiaColors" | prefix used to name highlight groups | | stripansicodes | true | remove ANSI color codes from text | | linestartsat | 1 (one-indexed) | at which column start colorizing | | colors | NR8 | table mapping 256 color codes to vim colors | | async | true | highlight asynchronously | | chunk_size | 500 | number of lines to process per loop iteration (async) |
With Conjure
This can be used to colorize [Conjure][conjure] log buffer. To do it you must tell conjure to not strip ANSI escape codes:
{
"m00qek/baleia.nvim",
version = "*",
config = function()
vim.g.conjurebaleia = require("baleia").setup({ linestarts_at = 3 })
local augroup = vim.api.nvimcreateaugroup("ConjureBaleia", { clear = true })
vim.api.nvimcreateuser_command("BaleiaColorize", function() vim.g.conjurebaleia.once(vim.api.nvimgetcurrentbuf()) end, { bang = true })
vim.api.nvimcreateuser_command("BaleiaLogs", vim.cmd.messages, { bang = true }) end, }, { "Olical/conjure", ft = { "clojure", "fennel" }, config = function() require("conjure.main").main() require("conjure.mapping")["on-filetype"]() end, init = function() -- Print color codes if baleia.nvim is available local colorize = require("lazyvim.util").has("baleia.nvim") vim.g["conjure#log#stripansiescapesequencesline_limit"] = colorize and 1 or nil
-- Disable diagnostics in log buffer and colorize it vim.api.nvimcreateautocmd({ "BufWinEnter" }, { pattern = "conjure-log-*", callback = function() local buffer = vim.api.nvimgetcurrent_buf() vim.diagnostic.enable(false, { bufnr = buffer }) if colorize and vim.g.conjure_baleia then vim.g.conjure_baleia.automatically(buffer) end end, }) end, },
Developer API
baleia provides two functions, bufsetlines and bufsettext, that have the same interface as the default vim.api.nvimbufset_lines and vim.api.nvimbutset_text. Using those is very efficient because they do all color detection and ANSI code stripping before writing anything to the buffer.
local new_lines = { '\x1b[32mHello \x1b[33mworld!' }
-- appending using Neovim standard API local lastline = vim.api.nvimbufline_count(0) vim.api.nvimbufsetlines(0, lastline, lastline, true, newlines)
-- appending using Baleia API local lastline = vim.api.nvimbufline_count(0) local baleia = require('baleia').setup { } baleia.bufsetlines(0, lastline, lastline, true, new_lines)
[integration-badge]: https://github.com/m00qek/baleia.nvim/actions/workflows/integration.yml/badge.svg [integration-runs]: https://github.com/m00qek/baleia.nvim/actions/workflows/integration.yml [conjure]: https://github.com/Olical/conjure