summaryrefslogtreecommitdiff
path: root/lua/functions.lua
diff options
context:
space:
mode:
authorHampusM <hampus@hampusmat.com>2024-01-17 01:02:10 +0100
committerHampusM <hampus@hampusmat.com>2024-01-17 01:02:10 +0100
commitcd6efe97c41be6ec5ccda57b53a835968acfdd79 (patch)
tree03896977c42b3d86c58134c4fe57195b3d133944 /lua/functions.lua
parent635b69ac3f88cacc30a862809d138d072d55ad3b (diff)
octoocto
Diffstat (limited to 'lua/functions.lua')
-rw-r--r--lua/functions.lua72
1 files changed, 72 insertions, 0 deletions
diff --git a/lua/functions.lua b/lua/functions.lua
index 12eca3a..8278ff0 100644
--- a/lua/functions.lua
+++ b/lua/functions.lua
@@ -12,6 +12,23 @@ function table.filter(list, predicate)
return filtered_list
end
+local function split(inputstr, sep)
+ if sep == nil then
+ sep = "%s"
+ end
+ local t={}
+ for str in string.gmatch(inputstr, "([^"..sep.."]+)") do
+ table.insert(t, str)
+ end
+ return t
+end
+
+local function file_exists(name)
+ local f=io.open(name,"r")
+
+ if f~=nil then io.close(f) return true else return false end
+end
+
function _G.check_back_space()
local col = vim.api.nvim_win_get_cursor(0)[2]
@@ -122,3 +139,58 @@ function _G.close_current_buffer()
end
end
+num = vim.fn.system("od -A n -t d -N 3 /dev/random")
+
+num = num:gsub("%s+", "")
+num = num:gsub("%s+", "")
+
+function _G.prepare_pr_review_curr_buf()
+ local buf_name = vim.fn.bufname()
+
+ local buf_name_parts = split(buf_name, "/")
+
+ local buf_file = buf_name_parts[#buf_name_parts]
+ local side = buf_name_parts[#buf_name_parts - 1]
+
+ local tmp_file = "/tmp/nvim-pr-review-" .. num .. "-"
+ .. side .. "-" .. buf_file
+
+ if file_exists(tmp_file) then
+ return
+ end
+
+ local lines = vim.api.nvim_buf_get_lines(0, 0, -1, true)
+
+ local file = io.open(tmp_file, "a")
+
+ if file == nil then
+ error("Temporary file not created")
+ return
+ end
+
+ for _, line in next, lines, nil do
+ file:write(line)
+ end
+
+ file:close()
+
+ vim.api.nvim_buf_set_name(0, tmp_file)
+end
+
+function _G.unprepare_pr_review_curr_buf()
+ local buf_name = vim.fn.bufname()
+
+ local buf_name_parts = split(buf_name, "/")
+
+ local buf_file = buf_name_parts[#buf_name_parts]
+ local side = buf_name_parts[#buf_name_parts - 1]
+
+ local tmp_file = "/tmp/nvim-pr-review-" .. num .. "-"
+ .. side .. "-" .. buf_file
+
+ if file_exists(tmp_file) == false then
+ return
+ end
+
+ os.remove(tmp_file)
+end