arnaupv / nvim-devcontainer-cli

MIT License
62 stars 0 forks source link

Feature Request: Use toggleterminal to run commands #28

Open erichlf opened 2 months ago

erichlf commented 2 months ago

It would be pretty nice if instead of firing up a terminal that hides our vi session if toggleterminal could be leveraged to handle the terminal. This would actually allow the user to connect to the devcontainer and have the terminal as a floating window, a horizontal split, or a vertical split. I believe it would even be possible to stow the window and come back to it.

I have played around with TermExec cmd="devcontainer exec --workspace . zsh" dir=<project> and it looked promising. The main part missing from that command is that I wasn't sure how to auto-populate the dir portion.

erichlf commented 2 months ago

One issue I have run into trying to just make a keybinding for this is that when I run 103TermExec cmd="devcontainer exec --workspace . zsh" dir="." without ever having used <M-3> (the default keybinding defined when loading the plugin) it fails with an InvalidRange error, since I guess this terminal doesn't exist yet. But it works if I have used the original keybinding and I am able to them use <M-3> to hide the terminal and then bring it back up. Anyway, seems promising.

erichlf commented 2 months ago

I was able to get things to work with a function like the following:

exec_toggle = function(opts)
  local Terminal = require("toggleterm.terminal").Terminal
  local term = Terminal:new { cmd = opts.cmd, count = opts.count, direction = opts.direction }
  term:toggle(opts.size, opts.direction)
end

vim.keymap.set(
  { "n", "t" }, 
  opts.keymap, 
  function()
    exec_toggle { cmd = opts.cmd, count = opts.count, direction = opts.direction, size = opts.size() }
  end, 
  { desc = opts.label, noremap = true, silent = true }
)

opts.direction can be "float", ""horizontal", or "vertical". opts.size can be nil and opts.cmd = "devcontainer exec --workspace-folder . zsh". Obviously the shell should be an option and the workspace-folder should be inferred in some way.

I used

opts = {
  cmd = "devcontainer exec --workspace-folder . zsh", 
  keymap = "<leader>De", 
  label="Bring Up Terminal in Devcontainer", 
  direction="float", 
  size=nil,
  count=104
}
arnaupv commented 2 months ago

I'm definitely interested in this, I will explore it further once I have time. If you can provide a pull request if would be awesome! The way the terminal is created should depend on the tool nvim config, and there should be different ways allowed! The more the btter!

arnaupv commented 2 months ago

Just FYI currently if you work with tmux and open nvim inside the tmux, in case you use this tools another tmux window is created with access to the devcontainer. https://github.com/arnaupv/nvim-devcontainer-cli/blob/5bd6ab58095d60d6b961dff42bdd7b936f23389a/bin/open_shell_in_devcontainer.sh#L6

erichlf commented 2 months ago

I didn't recognize that it was doing that. Maybe that was why I had extra tmux sessions that I didn't recognize.

erichlf commented 2 months ago

There are some issues with the method I outlined. First, the simple one to fix, the keymap <leader>De is difficult to use to toggle the terminal as for me hitting escape doesn't always seem to enter normal mode.

Second, the command TermExec forwards commands to a terminal, in the case I posted above the ID is 104. Now this seems cool, cause I was able to then send compile commands and such, but if you change the directory things then run in that directory. So there are some caveats to this approach. But I think there is still something to this.

arnaupv commented 2 months ago

I currently do not have time to implement this, but PRs are more than welcome 🚀 !