Skip to content

Commit

Permalink
Add :FlowRunQuickCmd (#34)
Browse files Browse the repository at this point in the history
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
  • Loading branch information
arjunmahishi authored Mar 1, 2024
1 parent 395e53f commit 5b430af
Show file tree
Hide file tree
Showing 5 changed files with 80 additions and 6 deletions.
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
8 changes: 8 additions & 0 deletions lua/flow/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {
Expand Down Expand Up @@ -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

Expand All @@ -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
Expand Down
14 changes: 8 additions & 6 deletions lua/flow/vars.lua
Original file line number Diff line number Diff line change
@@ -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()
Expand Down Expand Up @@ -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 <esc> 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 <esc> to close the window"
return help_text
end

Expand Down
59 changes: 59 additions & 0 deletions lua/flow/windows.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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", "<esc>", ":q<cr>", {})

-- on pressing enter, call the callback with the text present in the buffer
vim.api.nvim_buf_set_keymap(buffer, "n", "<cr>", "", {
callback = handle_enter,
})

vim.api.nvim_buf_set_keymap(buffer, "i", "<cr>", "<esc>", {
callback = function ()
-- switch to normal mode
vim.api.nvim_feedkeys(
vim.api.nvim_replace_termcodes("<Esc>", 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,
}
4 changes: 4 additions & 0 deletions plugin/flow.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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',
Expand Down

0 comments on commit 5b430af

Please sign in to comment.