Skip to content

Commit

Permalink
Switch to using toggleterm
Browse files Browse the repository at this point in the history
  • Loading branch information
erichlf committed Apr 29, 2024
1 parent f61b37f commit 97da018
Show file tree
Hide file tree
Showing 9 changed files with 242 additions and 167 deletions.
2 changes: 1 addition & 1 deletion Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -8,7 +8,7 @@ test:
--headless \
--noplugin \
-u ${TESTS_INIT} \
-c "PlenaryBustedFile tests/devcontainer_cli/unit_tests.lua"
-c "PlenaryBustedFile tests/devcontainer-cli/unit_tests.lua"

test_all:
@nvim \
Expand Down
25 changes: 20 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -76,6 +76,7 @@ make assumptions about how you work.

- [docker](https://docs.docker.com/get-docker/)
- [devcontainer-cli](https://github.com/devcontainers/cli#npm-install)
- [toggleterm](https://github.com/akinsho/toggleterm.nvim)

## 🔧 Installation

Expand All @@ -84,6 +85,7 @@ make assumptions about how you work.
```lua
{
"erichlf/devcontainer-cli.nvim",
dependencies = { 'akinsho/toggleterm.nvim' },
opts = {
-- whather to verify that the final devcontainer should be run
interactive = false,
Expand All @@ -107,29 +109,34 @@ make assumptions about how you work.
-- stylua: ignore
{
"<leader>Du",
":DevcontainerUp<cr>",
":DevcontainerUp<CR>",
desc = "Bring up the DevContainer",
},
{
"<leader>Dc",
":DevcontainerConnect<cr>",
":DevcontainerConnect<CR>",
desc = "Connect to DevContainer",
},
{
"<leader>De",
":DevcontainerExec<cr>",
":DevcontainerExec<CR>",
desc = "Execute a command in DevContainer",
},
{
"<leader>Db",
":DevcontainerExec cd build && make<cr>",
":DevcontainerExec cd build && make<CR>",
desc = "Execute build command in DevContainer",
},
{
"<leader>Dt",
":DevcontainerExec cd build && make test<cr>",
":DevcontainerExec cd build && make test<CR>",
desc = "Execute test command in DevContainer",
},
{
"<leader>DT",
"<CMD>DevContainerToggle<CR>",
desc = "Toggle the current DevContainer Terminal"
},
}
},
```
Expand Down Expand Up @@ -160,6 +167,14 @@ There are 3 main commands: `:DevcontainerUp`, `:DevcontainerExec`, and `:Devcont
continue working in your current session and run commands in the
devcontainer via `DevcontainerExec`.

During execution using `DevcontainerUp` or `DevcontainerExec` it is possible
to toggle the terminal via `t` while in normal mode and then to bring it back
you can run `:DevContainerToggle`. Additionally you could bring it back through
`:TermSelect`.

During the execution of a Devcontainer process you can also type `q` or `<esc>`
to kill the process and exit the terminal window.

## Tests

Tests are executed automatically on each PR using Github Actions.
Expand Down
5 changes: 5 additions & 0 deletions doc/devcontainer-cli.nvim.txt
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,11 @@ DevcontainerUp *DevcontainerUp*
DevcontainerExec *DevcontainerExec*
Runs a given command in the projects devcontainer.

DevcontainerToggle *DevcontainerToggle*
Toggles the current devcontainer window. It is expected that only one
devcontainer window is open at a time and so this will only toggle the last
devcontainer window.

DevcontainerConnect *DevcontainerConnect*
Closes the nvim sessions (all sessions fromt the terminal) and opens a new
terminal which is connected in the docker container, ready to execute the
Expand Down
44 changes: 33 additions & 11 deletions lua/devcontainer-cli/devcontainer_cli.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,5 @@
local config = require("devcontainer-cli.config")
local devcontainer_utils = require("devcontainer-cli.devcontainer_utils")
local utils = require("devcontainer-cli.devcontainer_utils")

local M = {}

Expand All @@ -11,28 +11,50 @@ local function define_autocommands()
-- It connects with the Devcontainer just after quiting neovim.
-- TODO: checks that the devcontainer is not already connected
-- TODO: checks that there is a devcontainer running
vim.schedule(function()
local command = config.nvim_plugin_folder .. "/bin/connect_to_devcontainer.sh"
vim.fn.jobstart(command, { detach = true })
end)
vim.schedule(
function()
local command = config.nvim_plugin_folder .. "/bin/connect_to_devcontainer.sh"
vim.fn.jobstart(command, { detach = true })
end
)
end,
})
end

-- executes a given command in the devcontainer of the current project directory
---@param opts (table) options for executing the command
function M.exec(opts)
vim.validate({ args = { opts.args, "string" } })
if opts.args == nil or opts.args == "" then
devcontainer_utils.exec()
else
devcontainer_utils.exec_cmd(opts.args)
local args = opts.args
vim.validate({ args = { args, "string", true } })

local parsed = {
cmd = nil,
direction = nil,
}

if args ~= nil then
parsed = utils.parse(args)

vim.validate({
cmd = { parsed.cmd, "string", true },
direction = { parsed.direction, "string", true },
})
if parsed.cmd == nil and parsed.direction == nil then
parsed.cmd = args
end
end

utils.exec(parsed.cmd, parsed.direction)
end

-- toggle the current devcontainer window
function M.toggle()
utils.toggle()
end

-- bring up the devcontainer in the current project directory
function M.up()
devcontainer_utils.bringup(vim.loop.cwd())
utils.bringup()
end

-- Thanks to the autocommand executed after leaving the UI, after closing the
Expand Down
Loading

0 comments on commit 97da018

Please sign in to comment.