From 5a23b5884621f7deca543bd22dfae7f8c36f663d Mon Sep 17 00:00:00 2001 From: Pikatsuto Date: Sat, 3 Aug 2024 01:21:24 +0200 Subject: [PATCH] feat: add ssh support --- package.nix | 2 ++ src/ide | 59 ++++++++++++++++++++++++++++++++++++++++++++--------- 2 files changed, 51 insertions(+), 10 deletions(-) diff --git a/package.nix b/package.nix index 0f1f0fe..9711cb8 100644 --- a/package.nix +++ b/package.nix @@ -12,6 +12,7 @@ wl-clipboard, nil, neovim, + sshfs, ... }: ############ @@ -72,6 +73,7 @@ stdenv.mkDerivation (finalAttrs: { wl-clipboard nil neovim + sshfs ]} ''; # ----------------------------------------------------------------- # diff --git a/src/ide b/src/ide index eeb3ba8..774a632 100755 --- a/src/ide +++ b/src/ide @@ -1,25 +1,64 @@ #! /usr/bin/env bash -rm -rf ${HOME}/.config/nvim -cp -r /Applications/ide/nvim ${HOME}/.config/ -sudo chown -R $(id -u):$(id -g) ${HOME}/.config/nvim -sudo chmod -R 755 ${HOME}/.config/nvim +sudo rm -rf ${HOME}/.config/nvim +cp -r \ + /Applications/ide/nvim \ + ${HOME}/.config/ +sudo chown -R $(id -u):$(id -g) \ + ${HOME}/.config/nvim +sudo chmod -R 755 \ + ${HOME}/.config/nvim FolderIdeCommand() { - nvim -c "ToggleTerm direction=horizontal" -c "Neotree" + nvim \ + -c "ToggleTerm direction=horizontal" \ + -c "Neotree" } FileIdeCommand() { - nvim -c "ToggleTerm direction=horizontal" ${1} + nvim \ + -c "ToggleTerm direction=horizontal" \ + ${1} } ideCommand="FileIdeCommand ${1}" -if [ -d "${1}" ] 2> /dev/null; then +[[ -d "${1}" && "${1}x" != *"@"* ]] && cd ${1} + +[[ -d "${1}" || "${1}x" == "x" ]] && ideCommand="FolderIdeCommand" -fi -if [ "${1}x" == "x" ] 2> /dev/null; then - ideCommand="FolderIdeCommand" + +if [[ "${1}x" == *"@"* ]]; then + port=22 + [[ "${2}x" == "-px" && "${3}x" != "x" ]] && + port="${3}" + + dir="${HOME}/.cache/ide/$(echo ${1} | cut -d ':' -f 2)" + mkdir -p ${dir} + umount ${dir} 2> /dev/null + sshfs -p ${port} ${1} ${dir} || exit + + while ! $(mountpoint -q ${dir}); do + sleep 0.5 + done + cd ${dir} + + workDir=$(echo ${1} | cut -d ':' -f 2) + sshLogin="$(echo ${1} | cut -d ':' -f 1)" + choiceShell="zsh 2> /dev/null || fish 2> /dev/null || bash 2> /dev/null;" + sshArgs="bash -c \\\"cd ${workDir}; clear; ${choiceShell}\\\"" + sshCommand="/usr/bin/env ssh -t -p ${port} ${sshLogin} \"${sshArgs}\"" + SSHIdeCommand() { + nvim \ + -c "ToggleTerm direction=horizontal dir=~" \ + -c "Neotree" \ + -c "TermExec cmd='${sshCommand}'" + } + + ideCommand="SSHIdeCommand" fi $ideCommand +pkill -f $(findmnt -T ${dir} \ + | grep -v "TARGET" \ + | cut -d " " -f 2)