Skip to content

Commit

Permalink
Open NVIM immediately and eliminate extra script
Browse files Browse the repository at this point in the history
  • Loading branch information
erichlf committed May 22, 2024
1 parent a8a56de commit 1f8ba3a
Show file tree
Hide file tree
Showing 5 changed files with 57 additions and 77 deletions.
31 changes: 13 additions & 18 deletions bin/connect_to_devcontainer.sh
Original file line number Diff line number Diff line change
Expand Up @@ -7,36 +7,31 @@ set -e
# TODO: Give the possibility of using multiple terminals, not only gnome-terminal
# TODO: Give the possibility of opening a new tmux page instead of opening a new container

# Get the folder of the current file
function get_script_dir {
# SOURCE: https://stackoverflow.com/a/246128/10491337
local SOURCE="${BASH_SOURCE[0]}"
local DIR=""
while [ -h "${SOURCE}" ]; do
DIR="$(cd -P "$(dirname "${SOURCE}")" >/dev/null 2>&1 && pwd)"
SOURCE="$(readlink "${SOURCE}")"
[[ "${SOURCE}" != /* ]] && SOURCE="${DIR}/${SOURCE}"
done
cd -P "$(dirname "${SOURCE}")" >/dev/null 2>&1 && pwd
}
if [ -n "$TMUX" ]; then
tmux_open="tmux split-window -h -t \"$TMUX_PANE\" "
else
tmux_open=""
fi

SCRIPT_DIR=$(get_script_dir)
# Execute the command for opening the devcontainer in the following terminal:
if [ -x "$(command -v alacritty)" ]; then
# ALACRITTY TERMINAL EMULATOR
REPOSTORY_NAME=$(basename "$(pwd)")
TERMINAL_TITLE="Devcontainer [${REPOSTORY_NAME}] - You are inside a Docker Container now...!"
alacritty --working-directory . --title "${TERMINAL_TITLE}" -e "${SCRIPT_DIR}"/open_shell_in_devcontainer.sh &
TERMINAL_TITLE="Devcontainer [${REPOSTORY_NAME}]"
command="alacritty --working-directory . --title "${TERMINAL_TITLE}" -e ${tmux_open}$@ &"
elif [ -x "$(command -v gnome-terminal)" ]; then
# GNOME TERMINAL
gnome-terminal -- bash -c "${SCRIPT_DIR}"/open_shell_in_devcontainer.sh
command="gnome-terminal -- ${tmux_open}$@"
elif [ "$(uname)" == "Darwin" ] && [ -x "$(command -v iTerm)" ]; then
# MAC ITERM2 TERMINAL EMULATOR
open -a iTerm.app "${SCRIPT_DIR}"/open_shell_in_devcontainer.sh
command="open -a iTerm.app ${tmux_open}$@"
elif [ "$(uname)" == "Darwin" ] && [ -x "$(command -v Terminal)" ]; then
# MAC TERMINAL
open -a Terminal.app "${SCRIPT_DIR}"/open_shell_in_devcontainer.sh
command="open -a Terminal.app ${tmux_open}$@"
else
# TERMINAL NO DEFINED
echo "ERROR: No compatible emulators found!"
exit 1
fi

eval "${command}"
34 changes: 0 additions & 34 deletions bin/open_shell_in_devcontainer.sh

This file was deleted.

7 changes: 6 additions & 1 deletion lua/devcontainer-cli/config/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -35,17 +35,22 @@ local default_config = {
dotfiles_repository = "[email protected]:erichlf/dotfiles",
-- branch to checkout for repositories (this is a feature not supported by
-- devcontainers in general, but we do)
dotfiles_repository = "devcontainer",
dotfiles_branch = "main",
-- directory for the setup environment
dotfiles_targetPath = "~/dotfiles",
-- command that's executed for installed the dependencies from the
-- setup_environment_repo
dotfiles_installCommand = "install.sh",
-- The number of columns to wrap text at
terminal_columns = 80,
-- The particular binary to use for connecting to in the devcontainer
-- Most likely this should remain nvim
nvim_binary = "nvim",
-- The shell to use for executing command. Available sh, bash, zsh or any
-- other that uses '-c' to signify a command is to follow
shell = 'bash',
-- The name of the socket file to use
port = "7777",
}

local options
Expand Down
32 changes: 8 additions & 24 deletions lua/devcontainer-cli/devcontainer_cli.lua
Original file line number Diff line number Diff line change
@@ -1,46 +1,26 @@
-- 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 utils = require("devcontainer-cli.devcontainer_utils")

local M = {}

local function define_autocommands()
local au_id = vim.api.nvim_create_augroup("devcontainer.docker.terminal", {})
vim.api.nvim_create_autocmd("UILeave", {
group = au_id,
callback = function()
-- 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
)
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)
Expand Down Expand Up @@ -82,7 +62,11 @@ end
-- Thanks to the autocommand executed after leaving the UI, after closing the
-- neovim window the devcontainer will be automatically open in a new terminal
function M.connect()
define_autocommands()
if not utils.create_connect_cmd() then
vim.notify("Failed to create autocommand", vim.log.levels.ERROR)
return
end

vim.cmd("wqa")
end

Expand Down
30 changes: 30 additions & 0 deletions lua/devcontainer-cli/devcontainer_utils.lua
Original file line number Diff line number Diff line change
Expand Up @@ -333,4 +333,34 @@ function M.toggle()
_terminal:toggle()
end

-- create the necessary functions needed to connect to nvim in a devcontainer
function M.create_connect_cmd()
local au_id = vim.api.nvim_create_augroup("devcontainer-cli.connect", {})
local dev_command = _devcontainer_command("exec")
if dev_command == nil then
return false
end
dev_command = dev_command .. " " .. config.nvim_binary

vim.api.nvim_create_autocmd(
"UILeave",
{
group = au_id,
callback =
function()
local connect_command = {config.nvim_plugin_folder .. "/bin/connect_to_devcontainer.sh"}
table.insert(connect_command, dev_command)
local command = table.concat(connect_command, " ")
vim.schedule(
function()
vim.fn.jobstart(command, { detach = true })
end
)
end
}
)

return true
end

return M

0 comments on commit 1f8ba3a

Please sign in to comment.