diff options
| author | srdusr <trevorgray@srdusr.com> | 2023-11-02 23:18:22 +0200 |
|---|---|---|
| committer | srdusr <trevorgray@srdusr.com> | 2023-11-04 17:38:29 +0200 |
| commit | bac5f39ece4d2eb8630e6091438a020b80ede934 (patch) | |
| tree | 70e5444c5e81f194ff44dc0678b9ad45f2ca0d55 | |
| parent | 564d516413ffe4a56979bc6afbcd9da7f0edb1c8 (diff) | |
| download | dotfiles-bac5f39ece4d2eb8630e6091438a020b80ede934.tar.gz dotfiles-bac5f39ece4d2eb8630e6091438a020b80ede934.zip | |
Run executable file
| -rw-r--r-- | .config/nvim/lua/user/mods.lua | 48 |
1 files changed, 48 insertions, 0 deletions
diff --git a/.config/nvim/lua/user/mods.lua b/.config/nvim/lua/user/mods.lua index 7c2db16..883c1c7 100644 --- a/.config/nvim/lua/user/mods.lua +++ b/.config/nvim/lua/user/mods.lua @@ -970,5 +970,53 @@ end -------------------------------------------------- +-- Run executable file +local interpreters = { + python = 'python', + lua = 'lua', + bash = 'bash', + zsh = 'zsh', + perl = 'perl', + ruby = 'ruby', + node = 'node', + rust = 'rust', + php = 'php', +} + +function M.RunCurrentFile() + local file_path = vim.fn.expand('%:p') + local file = io.open(file_path, 'r') + + if not file then + print('Error: Unable to open the file') + return + end + + local shebang = file:read() + file:close() + + local interpreter = shebang:match('#!%s*(.-)$') + if not interpreter then + print('Error: No shebang line found in the file') + return + end + + -- Remove leading spaces and any arguments, extracting the interpreter name + interpreter = interpreter:gsub('^%s*([^%s]+).*', '%1') + + local cmd = interpreters[interpreter] + + if not cmd then + cmd = interpreter -- Set the command to the interpreter directly + end + + -- Run the file using the determined interpreter + vim.fn.jobstart(cmd .. ' ' .. file_path, { + cwd = vim.fn.expand('%:p:h'), + }) +end + +-------------------------------------------------- + -- ... return M |
