Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Switch to using toggleterm #13

Merged
merged 6 commits into from
May 14, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
20 changes: 20 additions & 0 deletions Dockerfile
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
# Copyright (c) 2024 Erich L Foster
#
# Permission is hereby granted, free of charge, to any person obtaining a copy of
# this software and associated documentation files (the "Software"), to deal in
# the Software without restriction, including without limitation the rights to
# use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
# of the Software, and to permit persons to whom the Software is furnished to do
# so, subject to the following conditions:
#
# The above copyright notice and this permission notice shall be included in all
# copies or substantial portions of the Software.
#
# THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
# IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
# FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
# AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
# LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
# OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
# SOFTWARE.

FROM ubuntu:22.04 as builder

ENV USER_NAME=my-app
Expand Down
16 changes: 7 additions & 9 deletions LICENSE
Original file line number Diff line number Diff line change
@@ -1,13 +1,11 @@
MIT License
Copyright (c) 2024 Erich L Foster

Copyright (c) 2023 Ellison

Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
Permission is hereby granted, free of charge, to any person obtaining a copy of
this software and associated documentation files (the "Software"), to deal in
the Software without restriction, including without limitation the rights to
use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
of the Software, and to permit persons to whom the Software is furnished to do
so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
Expand Down
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 direction='vertical' size='40'<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 cmd='cd build && make test' direction='horizontal'<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
20 changes: 20 additions & 0 deletions lua/devcontainer-cli/config/init.lua
Original file line number Diff line number Diff line change
@@ -1,3 +1,23 @@
-- Copyright (c) 2024 Erich L Foster
--
-- Permission is hereby granted, free of charge, to any person obtaining a copy of
-- this software and associated documentation files (the "Software"), to deal in
-- the Software without restriction, including without limitation the rights to
-- use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
-- of the Software, and to permit persons to whom the Software is furnished to do
-- so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in all
-- copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-- SOFTWARE.

local ConfigModule = {}
local file_path = debug.getinfo(1).source:sub(2)
local default_config = {
Expand Down
66 changes: 55 additions & 11 deletions lua/devcontainer-cli/devcontainer_cli.lua
Original file line number Diff line number Diff line change
@@ -1,5 +1,25 @@
-- Copyright (c) 2024 Erich L Foster
--
-- Permission is hereby granted, free of charge, to any person obtaining a copy of
-- this software and associated documentation files (the "Software"), to deal in
-- the Software without restriction, including without limitation the rights to
-- use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies
-- of the Software, and to permit persons to whom the Software is furnished to do
-- so, subject to the following conditions:
--
-- The above copyright notice and this permission notice shall be included in all
-- copies or substantial portions of the Software.
--
-- THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
-- IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
-- FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
-- AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
-- LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
-- OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
-- SOFTWARE.

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 +31,52 @@ 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,
size = nil,
}

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

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

utils.exec(parsed.cmd, parsed.direction, parsed.size)
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
Loading