From 5b430afaa8c8d1d8bd76fa09f944d775d066e988 Mon Sep 17 00:00:00 2001 From: Arjun Mahishi Date: Fri, 1 Mar 2024 17:44:31 +0530 Subject: [PATCH] Add :FlowRunQuickCmd (#34) Running `:FlowRunQuickCmd` will open up a command editor. There you can type a command (just like custom commands). On pressing enter, the command will get executed and the output will be shown with the same configuration as custom commands. Closes #30 --- README.md | 1 + lua/flow/init.lua | 8 ++++++ lua/flow/vars.lua | 14 ++++++----- lua/flow/windows.lua | 59 ++++++++++++++++++++++++++++++++++++++++++++ plugin/flow.lua | 4 +++ 5 files changed, 80 insertions(+), 6 deletions(-) diff --git a/README.md b/README.md index f10b218..88f58f3 100644 --- a/README.md +++ b/README.md @@ -45,6 +45,7 @@ This is still a work in progress. So, PRs are welcome and appreciated. As of now | `:FlowLauncher` | Launches a [telescope](https://github.com/nvim-telescope/telescope.nvim) interface to manage custom commands. Read the docs [here](https://github.com/arjunmahishi/flow.nvim/wiki/Flow-launcher) | | `:FlowRunLastCmd` | Run the previously executed custom command | | `:FlowLastOutput` | Show the output of the last run command | +| `:FlowRunQuickCmd` | Run a shell command without saving it | ## Custom commands diff --git a/lua/flow/init.lua b/lua/flow/init.lua index 9711970..a19b096 100644 --- a/lua/flow/init.lua +++ b/lua/flow/init.lua @@ -3,6 +3,7 @@ local md = require('flow.markdown') local cmd = require("flow.cmd") local output = require("flow.output") local sql = require("flow.sql") +local windows = require("flow.windows") local default_setup_options = { output = { @@ -88,6 +89,12 @@ local function reload_plugin() print "reloaded flow" end +local function run_quick_cmd() + windows.open_quick_cmd_window(function(quick_cmd) + output.handle_output(quick_cmd, setup_options.output) + end) +end + local function setup(options) setup_options = options @@ -102,6 +109,7 @@ return { run_file = run_file, run_custom_cmd = run_custom_cmd, run_last_custom_cmd = run_last_custom_cmd, + run_quick_cmd = run_quick_cmd, show_last_output = show_last_output, reload_plugin = reload_plugin, setup = setup diff --git a/lua/flow/vars.lua b/lua/flow/vars.lua index 8e0bd3d..fea4541 100644 --- a/lua/flow/vars.lua +++ b/lua/flow/vars.lua @@ -1,9 +1,7 @@ -local function trim_spaces(str) - return string.gsub(str, "^%s*(.-)%s*$", "%1") -end +local util = require("flow.util") local function func_pwd() - return trim_spaces(vim.fn.system("pwd")) + return util.trim_space(vim.fn.system("pwd")) end local function func_curr_file() @@ -41,13 +39,17 @@ local function add_vars(vars) end end -local function vars_help_text() +local function vars_help_text(vars_only) local help_text = "# Variables available for use:\n" for key, _ in pairs(cmd_variables) do help_text = help_text .. string.format("# - $%s\n", key) end - help_text = help_text .. "#\n# save the changes and press to close the window\n" + if vars_only then + return util.trim_space(help_text) + end + + help_text = help_text .. "#\n# save the changes and press to close the window" return help_text end diff --git a/lua/flow/windows.lua b/lua/flow/windows.lua index 24b4d8d..e74835e 100644 --- a/lua/flow/windows.lua +++ b/lua/flow/windows.lua @@ -55,6 +55,65 @@ local function open_custom_cmd_window(file_name, file_type) }) end +local function open_quick_cmd_window(callback) + local win_cols = vim.api.nvim_get_option('columns') + local win_rows = vim.api.nvim_get_option('lines') + local help_text = util.str_split("\n\n"..vars.vars_help_text(true), "\n") + + local screen_coverage = 0.4 + local width = math.floor(win_cols * screen_coverage) + local height = #help_text + local col = math.floor((win_cols - width) / 2) + local row = math.floor((win_rows - height) / 2) + + local output_win_config = { + relative = 'editor', border = 'double', style = 'minimal', + title = 'flow: custom cmd', title_pos = 'center', + width = width, height = height, col = col, row = row, + } + + local win = vim.api.nvim_open_win(0, true, output_win_config) + local buffer = vim.api.nvim_create_buf(false, true) + vim.api.nvim_win_set_buf(win, buffer) + vim.bo.filetype = "bash" + + vim.api.nvim_buf_set_lines(buffer, 0, -1, false, help_text) + + -- start in insert mode + vim.api.nvim_command("startinsert") + + + local handle_enter = function() + local lines = vim.api.nvim_buf_get_lines(buffer, 0, -1, false) + local text = table.concat(lines, "\n") + if callback ~= nil then + callback(text) + end + + vim.api.nvim_buf_delete(buffer, {force = false}) + end + + + vim.api.nvim_buf_set_keymap(buffer, "n", "", ":q", {}) + + -- on pressing enter, call the callback with the text present in the buffer + vim.api.nvim_buf_set_keymap(buffer, "n", "", "", { + callback = handle_enter, + }) + + vim.api.nvim_buf_set_keymap(buffer, "i", "", "", { + callback = function () + -- switch to normal mode + vim.api.nvim_feedkeys( + vim.api.nvim_replace_termcodes("", true, false, true), 'n', false + ) + + handle_enter() + end + }) +end + return { open_custom_cmd_window = open_custom_cmd_window, + open_quick_cmd_window = open_quick_cmd_window, } diff --git a/plugin/flow.lua b/plugin/flow.lua index b9168f1..f1e056a 100644 --- a/plugin/flow.lua +++ b/plugin/flow.lua @@ -40,6 +40,10 @@ vim.api.nvim_create_user_command('FlowRunMDBlock', function() require('flow').run_block() end, {}) +vim.api.nvim_create_user_command('FlowRunQuickCmd', function() + require('flow').run_quick_cmd() +end, {}) + -- Autocommand vim.api.nvim_create_autocmd('FileType', { pattern = 'run-code-output',