11local M = {}
22
3- local config = require (" freeze-code.config" )
3+ local os_utils = require (" freeze-code.utils" ).os
4+ local is_win = os_utils .is_win
5+ local is_macos = os_utils .is_macos
6+ -- local is_unix = os_utils.is_unix
7+
8+ local tmp_freeze_path = " /tmp/freeze-code.nvim"
9+
10+ local setup_bin_path = function ()
11+ if not vim .loop .fs_stat (tmp_freeze_path ) then
12+ vim .fn .system ({
13+ " git" ,
14+ " clone" ,
15+ " --filter=blob:none" ,
16+ " https://github.com/AlejandroSuero/freeze-code.nvim.git" ,
17+ " --branch=main" , -- latest stable release
18+ tmp_freeze_path ,
19+ })
20+ end
21+ end
422
5- local job = {}
23+ M . job = {}
624
7- local stdio = { stdout = " " , stderr = " " }
25+ M . stdio = { stdout = " " , stderr = " " }
826
927--- Closing job in a safely way
1028--- @param h uv_pipe_t | uv_process_t : ` stdout | stderr | handle `
@@ -15,52 +33,56 @@ local function safe_close(h)
1533end
1634
1735local function stop_job ()
18- if job == nil then
36+ if M . job == nil then
1937 return
2038 end
21- if not job .stdout == nil then
22- job .stdout :read_stop ()
23- safe_close (job .stdout )
39+ if not M . job .stdout == nil then
40+ M . job .stdout :read_stop ()
41+ safe_close (M . job .stdout )
2442 end
25- if not job .stderr == nil then
26- job .stderr :read_stop ()
27- safe_close (job .stderr )
43+ if not M . job .stderr == nil then
44+ M . job .stderr :read_stop ()
45+ safe_close (M . job .stderr )
2846 end
29- if not job .handle == nil then
30- safe_close (job .handle )
47+ if not M . job .handle == nil then
48+ safe_close (M . job .handle )
3149 end
32- job = nil
50+ M . job = nil
3351end
3452
3553--- The function called on exit of from the event loop
3654--- @param msg string : Message to display if success
3755--- @return function cb : Schedule wrap callback function
38- local function on_exit (msg )
56+ function M .on_exit (msg )
57+ local freeze_code = require (" freeze-code" )
3958 return vim .schedule_wrap (function (code , _ )
4059 if code == 0 then
4160 vim .notify (" [freeze-code] " .. msg , vim .log .levels .INFO , { title = " FreezeCode" })
4261 else
43- vim .notify (stdio .stdout , vim .log .levels .ERROR , { title = " Freeze" })
62+ vim .notify (M .stdio .stdout , vim .log .levels .ERROR , { title = " Freeze" })
63+ end
64+ if freeze_code .config .copy == true then
65+ freeze_code .copy (freeze_code .config )
4466 end
4567 stop_job ()
4668 end )
4769end
4870
49- local function on_output (err , data )
71+ function M . on_output (err , data )
5072 if err then
5173 -- what should we really do here?
5274 vim .api .nvim_err_writeln (vim .inspect (err ))
5375 end
5476 if data then
55- stdio .stdout = stdio .stdout .. data
77+ M . stdio .stdout = M . stdio .stdout .. data
5678 end
5779end
5880
5981--- Checks if the given cmd executes.
6082--- @param cmd string
6183--- @param path_to_check string
6284--- @return boolean success : true if executes , false otherwise
63- local function check_executable (cmd , path_to_check )
85+ function M . check_executable (cmd , path_to_check )
6486 if vim .fn .executable (cmd ) == 0 then
6587 vim .api .nvim_err_write (
6688 string.format (
@@ -73,64 +95,36 @@ local function check_executable(cmd, path_to_check)
7395 return true
7496end
7597
76- --- Freeze file with a range
77- --- @param s_line ? number : line to start range
78- --- @param e_line ? number : line to start range
79- --- @param opts ? FreezeConfig
80- M .freeze = function (s_line , e_line , opts )
81- config = vim .tbl_extend (" force" , {}, config , opts or {})
82- s_line = s_line or 1
83- e_line = e_line or vim .api .nvim_buf_line_count (0 )
84-
85- local cmd = config .freeze_path
98+ local copy_by_os = function (opts )
99+ setup_bin_path ()
100+ local bin_path = tmp_freeze_path .. " /bin"
101+ local binaries = {
102+ macos = bin_path .. " /pngcopy-macos" ,
103+ linux = bin_path .. " /pngcopy-linux" ,
104+ windows = bin_path .. " /pngcopy-windows.ps1" ,
105+ }
86106
87- if not check_executable (" freeze" , cmd ) then
88- return
107+ local cmd = " "
108+ if is_win then
109+ cmd = " pwsh " .. binaries .windows .. " " .. opts .output
110+ return os.execute (cmd )
111+ elseif is_macos then
112+ cmd = " sh " .. binaries .macos .. " " .. opts .output
113+ return os.execute (cmd )
89114 end
115+ cmd = " sh " .. binaries .linux .. " " .. opts .output
116+ os.execute (cmd )
117+ end
90118
91- local lang = vim .api .nvim_buf_get_option (0 , " filetype" )
92- local file = vim .api .nvim_buf_get_name (0 )
93- local conf = config .freeze_config .config
94- local dir = config .dir
95- local theme = config .freeze_config .theme
96- local output = config .freeze_config .output
97-
98- if output ~= " freeze" then
99- local t_stamp = os.date (" %Y%m%d%H%M%S" )
100- local filename = file :match (" ^.+/(.*)$" ) or file
101-
102- output = string.format (" %s_%s_%s" , tostring (t_stamp ), filename , output )
119+ M .copy = function (opts )
120+ copy_by_os (opts )
121+ local cmd = " "
122+ if is_win then
123+ cmd = " rm -r -Force " .. tmp_freeze_path
124+ else
125+ cmd = " rm -rf " .. tmp_freeze_path
103126 end
104-
105- config .output = dir .. " /" .. output .. " .png"
106-
107- local cmd_args = {
108- " --output" ,
109- config .output ,
110- " --language" ,
111- lang ,
112- " --lines" ,
113- s_line .. " ," .. e_line ,
114- " --config" ,
115- conf ,
116- " --theme" ,
117- theme ,
118- file ,
119- }
120-
121- job = {}
122- job .stdout = vim .loop .new_pipe (false )
123- job .stderr = vim .loop .new_pipe (false )
124-
125- local job_opts = {
126- args = cmd_args ,
127- stdio = { nil , job .stdout , job .stderr },
128- }
129-
130- local msg = " frozen frame in path=" .. config .output
131- job .handle = vim .loop .spawn (cmd , job_opts , on_exit (msg ))
132- vim .loop .read_start (job .stdout , vim .schedule_wrap (on_output ))
133- vim .loop .read_start (job .stderr , vim .schedule_wrap (on_output ))
127+ os.execute (cmd )
134128end
135129
136130return M
0 commit comments