Home

Awesome

silicon.lua

silicon is a lua plugin for neovim to generate beautiful images of code snippet using silicon

<video src = "https://user-images.githubusercontent.com/79555780/198016165-7a47ac6c-e329-4025-8d66-f9b34bd52658.mp4"></video>

✨ Features

⚡️ Requirements

📦 Installation

Install the plugin with your preferred package manager:

packer

-- Lua
use {
  "narutoxy/silicon.lua",
  requires = { "nvim-lua/plenary.nvim" },
  config = function()
    require('silicon').setup({})
  end
}

vim-plug

" Vim Script
Plug 'nvim-lua/plenary.nvim'
Plug 'narutoxy/silicon.lua'

lua require('silicon').setup({})

⚙️ Configuratioon

silicon comes with the following defaults:

{
	theme = "auto",
	output = "SILICON_${year}-${month}-${date}_${time}.png", -- auto generate file name based on time (absolute or relative to cwd)
	bgColor = vim.g.terminal_color_5,
	bgImage = "", -- path to image, must be png
	roundCorner = true,
	windowControls = true,
	lineNumber = true,
	font = "monospace",
	lineOffset = 1, -- from where to start line number
	linePad = 2, -- padding between lines
	padHoriz = 80, -- Horizontal padding
	padVert = 100, -- vertical padding
	shadowBlurRadius = 10,
	shadowColor = "#555555",
	shadowOffsetX = 8,
	shadowOffsetY = 8,
	gobble = false, -- enable lsautogobble like feature
	debug = false, -- enable debug output
}

🚀 Usage

Keymaps

-- Generate image of lines in a visual selection
vim.keymap.set('v', '<Leader>s',  function() silicon.visualise_api() end )
-- Generate image of a whole buffer, with lines in a visual selection highlighted
vim.keymap.set('v', '<Leader>bs', function() silicon.visualise_api({to_clip = true, show_buf = true}) end )
-- Generate visible portion of a buffer
vim.keymap.set('n', '<Leader>s',  function() silicon.visualise_api({to_clip = true, visible = true}) end )
-- Generate current buffer line in normal mode
vim.keymap.set('n', '<Leader>s',  function() silicon.visualise_api({to_clip = true}) end )

Command line

Calling silicon.visualise_api with lua in command line doesn't work due to lua not supporting ranges.
This means that the moment you hit enter, you leave visual mode before lua function is called. While this populates two registers, using them doesn't work with "v" mode maps.
A workaround has been implemented, and a shorthand that forces it is available as .visualise_cmdline:

Notes

Colorscheme reloading

Autogenerated silicon themes are unaware of dark/light variants. Utility functions had been exposed that allow regenerating theme on demand:

This lets one regenerate theme when switching background setting or otherwise reloading ColorScheme:

vim.api.nvim_create_augroup('SiliconRefresh', { clear = true })
vim.api.nvim_create_autocmd({ 'ColorScheme' },
	{
	group = 'SiliconRefresh',
	callback = function()
		silicon_utils.build_tmTheme()
		silicon_utils.reload_silicon_cache({async = true})
	end,
	desc = 'Reload silicon themes cache on colorscheme switch',
	}
)