Files
dev_prod/10x_project_access.sh
Pratik Tripathy 0758973e20 fix: Fix project quick open scripts
- Existing projects: search 4 directory deep for `.git`
- New projects: ensure project opens in neovim after it completes
  download
2025-08-19 20:34:46 +05:30

80 lines
2.5 KiB
Bash
Executable File

#!/bin/sh
# TODO:
# - Create fuzzy finder for cht.sh & tldr + system man pages + learnxinyminutes github
# Quick open project files in NVIM
op() {
if ! command -v fzf >/dev/null; then
echo "Install fzf to use this function."
return 127
fi
# %T => Sort by last modified
# @ => time displayed in seconds (since 1.1.1970)
# %p => Directory path
project_dir="$(find -L ~/Code -mindepth 1 -maxdepth 4 -name ".git" -not -path '*/.stversions/*' -printf "%T@ %p\n" | sort -nr | cut -d ' ' -f 2- | xargs dirname | fzf --prompt='Select a project: ')"
# Do nothing and return if user cancelled out
if [ "$project_dir" = "" ]; then
return 0
fi
cd "$project_dir" || return 126
nvim
}
# Quick create new projects
pnew() {
if ! command -v fzf >/dev/null; then
echo "Install fzf to use this function."
return 127
fi
directory="$(find -L ~/Code/ -mindepth 1 -maxdepth 1 -not -path '*/.*' -type d | sed '/Squbix\|KeepWorks\|Supra/d' | sort | fzf --prompt='Creating new project. In which directory? ')"
# Do nothing and return if user cancelled out
if [ "$directory" = "" ]; then
return 0
fi
is_clone=$(printf 'no\nyes' | fzf --prompt='Clone from remote repo?')
# Do nothing and return if user cancelled out
if [ "$is_clone" = "" ]; then
return 0
fi
if [ "$is_clone" = "yes" ]; then
# Clone the repo and open nvim on it
printf "Git repo URL: "
read -r git_url
cd "$directory" || return 126
git clone "$git_url" || return
cd "$(basename "$git_url" | cut -d '.' -f 1)" || return 126
else
# Loop until user provides a valid directory name or cancels out
while [ -d "$directory/$project_name" ]; do
project_name=$(find -L "$directory" -mindepth 1 -maxdepth 1 -not -path '*/.*' -type d | fzf --print-query -e +i --prompt='Project name (AVOID EXISTING ONES BELOW): ' | head -1)
if [ "$project_name" = "" ]; then
echo "Cancelling..."
return 0
fi
if [ -d "$directory/$project_name" ] || [ -e "$directory/$project_name" ]; then
echo "$directory/$project_name already exists"
fi
done
if [ ! -d "$directory/$project_name" ] && [ ! -e "$directory/$project_name" ]; then
mkdir -p "$directory/$project_name" || return 126
fi
cd "$directory/$project_name" || return 126
git init || return 126
fi
nvim
}