mirror of
https://github.com/folke/snacks.nvim
synced 2025-08-04 10:49:08 +00:00
feat(terminal): added Snacks.terminal.colorize()
to replace ansi codes by colors
This commit is contained in:
parent
14f076e039
commit
519b6841c4
3 changed files with 65 additions and 0 deletions
|
@ -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
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue