Skip to content

Commit

Permalink
refactor: *
Browse files Browse the repository at this point in the history
  • Loading branch information
niuiic committed Dec 9, 2024
1 parent 246909d commit 2f5240b
Show file tree
Hide file tree
Showing 4 changed files with 81 additions and 170 deletions.
95 changes: 61 additions & 34 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -9,11 +9,9 @@ Similar features to [silicon.nvim](https://github.com/krivahtoo/silicon.nvim), k
## Dependencies

- [silicon](https://github.com/Aloxaf/silicon)
- [niuiic/core.nvim](https://github.com/niuiic/core.nvim)
- [niuiic/omega.nvim](https://github.com/niuiic/omega.nvim)

## Usage

Just call `require("code-shot").shot()`, work in both `n`, `v` and `V` mode.
## Showcase

- Shot whole file

Expand All @@ -23,41 +21,70 @@ Just call `require("code-shot").shot()`, work in both `n`, `v` and `V` mode.

<img src="https://github.com/niuiic/assets/blob/main/code-shot.nvim/shot-range.gif" />

## Config
## Usage

Default config here.
- shot file to clipboard

```lua
require("code-shot").setup({
---@return string output file path
output = function()
local core = require("core")
local buf_name = vim.api.nvim_buf_get_name(0)
return core.file.name(buf_name) .. ".png"
end,
---@return string[]
-- select_area: {start_line: number, end_line: number} | nil
options = function(select_area)
if not select_area then
return {}
end
return {
"--line-offset",
select_area.start_line,
}
end,
})
local function shot_file_to_clipboard()
require("code-shot").shot(function(context)
vim.system(
{
"silicon",
"--to-clipboard",
"--pad-horiz",
"0",
"--pad-vert",
"0",
"--no-round-corner",
"--theme",
"Coldark-Dark",
"--no-line-number",
"--no-window-controls",
context.file_path,
},
nil,
function(result)
if result.code == 0 then
vim.notify("Shot code successfully", vim.log.levels.INFO)
else
vim.notify("Shot code failed", vim.log.levels.ERROR)
end
end
)
end)
end
```

Add any argument supported by silicon in `options`. For example, select a theme.
- shot selection to clipboard

```lua
require("code-shot").setup({
options = function()
return {
"--theme",
"DarkNeon",
}
end,
})
local function shot_selection_to_clipboard()
require("code-shot").shot(function(context)
vim.system(
{
"silicon",
"--to-clipboard",
"--pad-horiz",
"0",
"--pad-vert",
"0",
"--no-round-corner",
"--theme",
"Coldark-Dark",
"--no-line-number",
"--no-window-controls",
context.file_path,
},
nil,
function(result)
if result.code == 0 then
vim.notify("Shot code successfully", vim.log.levels.INFO)
else
vim.notify("Shot code failed", vim.log.levels.ERROR)
end
end
)
end)
end
```
92 changes: 20 additions & 72 deletions lua/code-shot/init.lua
Original file line number Diff line number Diff line change
@@ -1,74 +1,22 @@
local static = require("code-shot.static")
local core = require("core")
local utils = require("code-shot.utils")

local shot = function()
if vim.fn.executable("silicon") == 0 then
vim.notify("silicon is not found", vim.log.levels.ERROR, {
title = "Code Shot",
})
return
end

local source_file = vim.api.nvim_buf_get_name(0)
local use_temp_source = false
---@type {s_start: {row: number, col: number}, s_end: {row: number, col: number}} | nil
local select_area

if vim.fn.mode() == "v" or vim.fn.mode() == "V" then
use_temp_source = true
select_area = utils.selected_area()
source_file = utils.temp_file_path(source_file)
local lines = vim.api.nvim_buf_get_lines(0, select_area.start_line - 1, select_area.end_line, false)
vim.fn.writefile(lines, source_file)
elseif not core.file.file_or_dir_exists(source_file) then
use_temp_source = true
source_file = utils.temp_file_path(source_file)
local lines = vim.api.nvim_buf_get_lines(0, 0, -1, false)
vim.fn.writefile(lines, source_file)
end

core.text.cancel_selection()

local err = false
local args = {}

core.lua.list.each(static.config.options(select_area), function(option)
table.insert(args, option)
end)

local output_path = static.config.output()

core.lua.list.each({
"--output",
output_path,
source_file,
}, function(option)
table.insert(args, option)
end)

core.job.spawn("silicon", args, {}, function()
if not err then
vim.notify("Code shot succeed, output to " .. output_path, vim.log.levels.INFO, {
title = "Code Shot",
})
end
if use_temp_source then
vim.loop.fs_unlink(source_file)
end
end, function(_, data)
err = true
vim.notify("Code shot failed, error is " .. data, vim.log.levels.ERROR, {
title = "Code Shot",
})
end, function() end)
end

local setup = function(new_config)
static.config = vim.tbl_deep_extend("force", static.config, new_config or {})
local M = {}

---@class code-shot.Context
---@field file_path string
---@field file_type string
---@field selected_area omega.Area
---@field selection string

---@param run fun(context: code-shot.Context)
function M.shot(run)
---@type code-shot.Context
local context = {
file_path = vim.api.nvim_buf_get_name(0) or "",
file_type = vim.bo.filetype,
selected_area = require("omega").get_selected_area(),
selection = vim.fn.join(require("omega").get_selection() or {}, require("omega").get_line_ending(0)),
}

run(context)
end

return {
shot = shot,
setup = setup,
}
return M
20 changes: 0 additions & 20 deletions lua/code-shot/static.lua

This file was deleted.

44 changes: 0 additions & 44 deletions lua/code-shot/utils.lua

This file was deleted.

0 comments on commit 2f5240b

Please sign in to comment.