From ceb857628192087bcab0c0436aa0274828d41769 Mon Sep 17 00:00:00 2001 From: TheOnePath Date: Sun, 8 Jan 2023 12:35:11 +0000 Subject: [PATCH] Added init.lua First initial commit of file as a backup to refactor. --- init.lua | 100 +++++++++++++++++++++++++++++++++++++++++++++++++++++++ 1 file changed, 100 insertions(+) create mode 100644 init.lua diff --git a/init.lua b/init.lua new file mode 100644 index 0000000..169110c --- /dev/null +++ b/init.lua @@ -0,0 +1,100 @@ +local plenary = require'plenary' +local popup = plenary.popup + +local presser = {} +windows = {} + +local api = vim.api + +local function _iter_table_dump( arr ) + for k, v in pairs(arr) do + print("Table:", k, v) + end +end + +local escape_chars = function ( text ) + return string.gsub(text, "[%(|%)|\\|%[|%]|%-|%{%}|%?|%+|%*|%^|%$|%.]", { + ["\\"] = "\\\\", + ["-"] = "\\-", + ["("] = "\\(", + [")"] = "\\)", + ["["] = "\\[", + ["]"] = "\\]", + ["{"] = "\\{", + ["}"] = "\\}", + ["?"] = "\\?", + ["+"] = "\\+", + ["*"] = "\\*", + ["^"] = "\\^", + ["$"] = "\\$", + ["."] = "\\.", + }) +end + +-- :@dev: clean the text once it has been fetched from buffer +local function _clean_buf ( text ) + if not type(text) == "string" or text == nil then + print("[DEBUG - _clean_buf] Given input is not of type string.") + return + end + + return escape_chars( text ) + :match( "^%s*(.-)%s*$" ) +end + +function presser.destroy_windows() +-- api.nvim_win_close(0, true) + + for k,v in pairs(windows) do +-- api.nvim_win_close(v, true) + print(k, v) + table.remove(windows, k) + end +end + +function presser.find_replace() + local padding = 128 + local buf_width = api.nvim_win_get_width(0) - padding + + local opts = { + minwidth = buf_width, + borderchars = { "─", "│", "─", "│", "╭", "╮", "╯", "╰" }, + } + + local win_title_opts = { + height = 3, + minwidth = buf_width + 2, + line = 9, + padding = { 1, 1, 1, 1 }, + } + + if not placeholder == nil then + vim.tbl_extend( "force", opts, { padding = { 0, buf_padding, 0, } } ) + end + + local win_title = popup.create( { "Find & Replace" }, win_title_opts ) + local win_b = popup.create({ "" }, vim.tbl_extend("force", opts, { title = "Replace", line = 16 })) + local win_t = popup.create({ "" }, vim.tbl_extend("force", opts, { title = "Find", line = 13 })) + print(win_t, win_b) + + table.insert(windows, win_t) + table.insert(windows, win_b) + table.insert(windows, win_title) + + --api.nvim_feedkeys(api.nvim_replace_termcodes("A", true, false, true), 'n', true) + + local win_t_buf = api.nvim_win_get_buf(win_t) + local win_b_buf = api.nvim_win_get_buf(win_b) + -- :lua require('presser').destroy_windows() + api.nvim_buf_set_keymap(win_t_buf, "n", "", ":lua require('presser').destroy_windows()", { noremap = true, silent = true }) + api.nvim_buf_set_keymap(win_b_buf, "n", "", ":close!", { noremap = true, silent = true }) + print(win_t_buf) + + local text = api.nvim_buf_get_lines(win_t_buf, 0, -1, false) + print(_clean_buf(text[1])) + +end + +presser.find_replace() + +return presser