feat(terminal): added Snacks.terminal.colorize() to replace ansi codes by colors

This commit is contained in:
Folke Lemaitre 2024-11-14 09:12:59 +01:00
parent 14f076e039
commit 519b6841c4
No known key found for this signature in database
GPG key ID: 41F8B1FBACAE2040
3 changed files with 65 additions and 0 deletions

View file

@ -167,4 +167,38 @@ function M.parse(cmd)
return args
end
--- Colorize the current buffer.
--- Replaces ansii color codes with the actual colors.
---
--- Example:
---
--- ```lua
--- ls -la | nvim - -c "lua Snacks.terminal.colorize()"
--- ```
function M.colorize()
vim.wo.number = false
vim.wo.relativenumber = false
vim.wo.statuscolumn = ""
vim.wo.signcolumn = "no"
vim.opt.listchars = { space = " " }
local buf = vim.api.nvim_get_current_buf()
local lines = vim.api.nvim_buf_get_lines(buf, 0, -1, false)
while #lines > 0 and vim.trim(lines[#lines]) == "" do
lines[#lines] = nil
end
vim.api.nvim_buf_set_lines(buf, 0, -1, false, {})
vim.api.nvim_chan_send(vim.api.nvim_open_term(buf, {}), table.concat(lines, "\r\n"))
vim.keymap.set("n", "q", "<cmd>q<cr>", { silent = true, buffer = buf })
vim.api.nvim_create_autocmd("TextChanged", {
buffer = buf,
callback = function()
pcall(vim.api.nvim_win_set_cursor, 0, { #lines, 0 })
end,
})
vim.api.nvim_create_autocmd("TermEnter", { buffer = buf, command = "stopinsert" })
end
return M