From 2f5240b97fa3fb06cb2767ad3542ace6d72dd4f9 Mon Sep 17 00:00:00 2001 From: niuiic Date: Mon, 9 Dec 2024 12:27:39 +0800 Subject: [PATCH] refactor: * --- README.md | 95 ++++++++++++++++++++++++++-------------- lua/code-shot/init.lua | 92 +++++++++----------------------------- lua/code-shot/static.lua | 20 --------- lua/code-shot/utils.lua | 44 ------------------- 4 files changed, 81 insertions(+), 170 deletions(-) delete mode 100644 lua/code-shot/static.lua delete mode 100644 lua/code-shot/utils.lua diff --git a/README.md b/README.md index d0e495b..28250a0 100644 --- a/README.md +++ b/README.md @@ -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 @@ -23,41 +21,70 @@ Just call `require("code-shot").shot()`, work in both `n`, `v` and `V` mode. -## 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 ``` diff --git a/lua/code-shot/init.lua b/lua/code-shot/init.lua index 856ff06..8d4ce1b 100644 --- a/lua/code-shot/init.lua +++ b/lua/code-shot/init.lua @@ -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 diff --git a/lua/code-shot/static.lua b/lua/code-shot/static.lua deleted file mode 100644 index e417a9e..0000000 --- a/lua/code-shot/static.lua +++ /dev/null @@ -1,20 +0,0 @@ -local config = { - output = function() - local core = require("core") - local buf_name = vim.api.nvim_buf_get_name(0) - return core.file.name(buf_name) .. ".png" - end, - options = function(select_area) - if not select_area then - return {} - end - return { - "--line-offset", - select_area.start_line, - } - end, -} - -return { - config = config, -} diff --git a/lua/code-shot/utils.lua b/lua/code-shot/utils.lua deleted file mode 100644 index eb08a0c..0000000 --- a/lua/code-shot/utils.lua +++ /dev/null @@ -1,44 +0,0 @@ -local core = require("core") - -local temp_file_path = function(source_file) - local dir = core.file.dir(source_file) - local name = core.file.name(source_file) - local extension = core.file.extension(source_file) - - local prefix = "_" - local temp_file = string.format("%s/%s%s.%s", dir, prefix, name, extension) - - while core.file.file_or_dir_exists(temp_file) do - prefix = prefix .. "_" - temp_file = string.format("%s/%s%s.%s", dir, prefix, name, extension) - end - - return temp_file -end - -local selected_area = function() - if vim.fn.mode() == "n" then - local pos = vim.api.nvim_win_get_cursor(0) - return { - pos[1], - pos[1], - } - end - - local start_line = vim.fn.getpos("v")[2] - local end_line = vim.fn.getpos(".")[2] - - if start_line > end_line then - start_line, end_line = end_line, start_line - end - - return { - start_line = start_line, - end_line = end_line, - } -end - -return { - temp_file_path = temp_file_path, - selected_area = selected_area, -}