dev-launcher 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218
  1. #!/usr/bin/env bash
  2. set -euo pipefail
  3. # Configuration
  4. CACHE_MAX_AGE=300
  5. MRU_SIZE=5
  6. # Debug logging (set DEBUG=1 to enable via environment variable)
  7. DEBUG="${DEBUG:-0}"
  8. debug() {
  9. [ "$DEBUG" = "1" ] && echo "[DEBUG] $*" >&2 || true
  10. }
  11. # Parse arguments - flags first, then DEV_DIR
  12. NO_CACHE=false
  13. CLEAR_CACHE=false
  14. EDITOR="${EDITOR:-nvim}"
  15. DEV_DIR=""
  16. debug "Starting dev-launcher with args: $*"
  17. for arg in "$@"; do
  18. case "$arg" in
  19. --help | -h)
  20. cat <<EOF
  21. Usage: $0 [DEV_DIR] [OPTIONS]
  22. Options:
  23. --no-cache Bypass cache and rescan repositories
  24. --clear-cache Clear cache and MRU files
  25. --help, -h Show this help message
  26. EOF
  27. exit 0
  28. ;;
  29. --no-cache)
  30. NO_CACHE=true
  31. ;;
  32. --clear-cache)
  33. CLEAR_CACHE=true
  34. ;;
  35. *)
  36. [ -z "$DEV_DIR" ] && DEV_DIR="$arg"
  37. ;;
  38. esac
  39. done
  40. DEV_DIR="${DEV_DIR:-$HOME/Dev}"
  41. debug "DEV_DIR: $DEV_DIR"
  42. debug "NO_CACHE: $NO_CACHE"
  43. debug "CLEAR_CACHE: $CLEAR_CACHE"
  44. # Setup cache files (unique per dev directory)
  45. DEV_DIR_HASH=$(echo -n "$DEV_DIR" | sha256sum | cut -d' ' -f1 | head -c 16)
  46. CACHE_FILE="$HOME/.cache/dev-launcher-cache-${DEV_DIR_HASH}"
  47. MRU_FILE="$HOME/.cache/dev-launcher-mru-${DEV_DIR_HASH}"
  48. debug "CACHE_FILE: $CACHE_FILE"
  49. debug "MRU_FILE: $MRU_FILE"
  50. # Handle --clear-cache
  51. if [ "$CLEAR_CACHE" = true ]; then
  52. rm -f "$CACHE_FILE" "$MRU_FILE"
  53. notify-send "Dev Launcher" "Cache cleared" 2>/dev/null || true
  54. exit 0
  55. fi
  56. # Find git repositories
  57. find_git_repos() {
  58. debug "find_git_repos: checking cache..."
  59. if [ "$NO_CACHE" = false ] && [ -f "$CACHE_FILE" ]; then
  60. local cache_age=$(($(date +%s) - $(stat -c %Y "$CACHE_FILE" 2>/dev/null || echo 0)))
  61. debug "Cache age: ${cache_age}s (max: ${CACHE_MAX_AGE}s)"
  62. if [ $cache_age -lt $CACHE_MAX_AGE ]; then
  63. debug "Using cached repos"
  64. cat "$CACHE_FILE"
  65. return
  66. fi
  67. debug "Cache expired, rescanning..."
  68. fi
  69. debug "Scanning for git repositories in $DEV_DIR..."
  70. if command -v fd >/dev/null 2>&1; then
  71. debug "Using fd for scanning"
  72. fd -H -t d "^\.git$" "$DEV_DIR" -d 3 -0 | xargs -0 -n1 dirname | sort -u >"$CACHE_FILE"
  73. else
  74. debug "Using find for scanning"
  75. find "$DEV_DIR" -maxdepth 3 -type d -name ".git" -print0 |
  76. xargs -0 -n1 dirname | sort -u >"$CACHE_FILE"
  77. fi
  78. local repo_count=$(wc -l <"$CACHE_FILE")
  79. debug "Found $repo_count repositories"
  80. cat "$CACHE_FILE"
  81. }
  82. # Get sanitized project name for tmux session
  83. get_project_name() {
  84. basename "$1" | tr '[:upper:]' '[:lower:]' | sed 's/[^a-z0-9]/-/g' | sed 's/--*/-/g' | sed 's/^-\|-$//g'
  85. }
  86. # Update MRU list
  87. update_mru() {
  88. local selected="$1"
  89. local temp_file
  90. temp_file=$(mktemp)
  91. echo "$selected" >"$temp_file"
  92. [ -f "$MRU_FILE" ] && grep -vFx "$selected" "$MRU_FILE" >>"$temp_file" || true
  93. head -n "$MRU_SIZE" "$temp_file" >"$MRU_FILE"
  94. rm -f "$temp_file"
  95. }
  96. # Sort by MRU (MRU items first with ⭐, then others)
  97. sort_by_mru() {
  98. local all_repos="$1"
  99. local mru_list
  100. local temp_file
  101. [ -f "$MRU_FILE" ] && mru_list=$(grep -v '^$' "$MRU_FILE") || mru_list=""
  102. # If MRU list is empty, just return sorted repos
  103. if [ -z "$mru_list" ]; then
  104. echo "$all_repos" | sort
  105. return
  106. fi
  107. # Output MRU items first with indicator
  108. echo "$mru_list" | sed 's/^/⭐ /'
  109. # Output non-MRU items using comm (fast set difference)
  110. temp_file=$(mktemp)
  111. echo "$all_repos" | sort >"$temp_file.all"
  112. echo "$mru_list" | sort >"$temp_file.mru"
  113. comm -23 "$temp_file.all" "$temp_file.mru" 2>/dev/null || cat "$temp_file.all"
  114. rm -f "$temp_file.all" "$temp_file.mru"
  115. }
  116. # Main execution
  117. REPOS=$(find_git_repos)
  118. if [ -z "$REPOS" ]; then
  119. debug "No repositories found!"
  120. notify-send "Dev Launcher" "No git repositories found in $DEV_DIR" 2>/dev/null || true
  121. exit 1
  122. fi
  123. debug "Found repositories, proceeding..."
  124. # Build display list (relative paths) and mapping
  125. DISPLAY_LIST=$(echo "$REPOS" | sed "s|^${DEV_DIR}/||")
  126. declare -A DISPLAY_TO_PATH
  127. debug "Building display mapping..."
  128. while IFS=$'\t' read -r repo display; do
  129. DISPLAY_TO_PATH["$display"]="$repo"
  130. done < <(paste <(echo "$REPOS") <(echo "$DISPLAY_LIST"))
  131. debug "Mapped ${#DISPLAY_TO_PATH[@]} projects"
  132. # Sort by MRU and present in tofi
  133. debug "Sorting by MRU..."
  134. SORTED_LIST=$(sort_by_mru "$DISPLAY_LIST")
  135. debug "Presenting in tofi..."
  136. SELECTED_DISPLAY=$(echo "$SORTED_LIST" | tofi \
  137. --prompt-text "💀 Poison: " \
  138. --fuzzy-match true \
  139. --width 30% \
  140. --height 50% \
  141. --anchor center \
  142. --padding-left 20 \
  143. --padding-right 20 \
  144. --padding-top 15 \
  145. --padding-bottom 15 \
  146. --border-width 2 \
  147. --outline-width 0 \
  148. --font "$(fc-match -f '%{family}' 2>/dev/null || echo 'sans')" \
  149. --font-size 16 \
  150. --background-color '#191724' \
  151. --text-color '#e0def4' \
  152. --selection-color '#31748f' \
  153. --selection-background '#1f1d2e' \
  154. --border-color '#31748f' \
  155. --prompt-color '#f6c177')
  156. debug "Selected display: '$SELECTED_DISPLAY'"
  157. [ -z "$SELECTED_DISPLAY" ] && debug "No selection, exiting" && exit 0
  158. # Remove star indicator and lookup path
  159. SELECTED_DISPLAY_CLEAN=$(echo "$SELECTED_DISPLAY" | sed 's/^⭐ //')
  160. SELECTED="${DISPLAY_TO_PATH[$SELECTED_DISPLAY_CLEAN]:-}"
  161. debug "Selected display (clean): '$SELECTED_DISPLAY_CLEAN'"
  162. debug "Selected path: '$SELECTED'"
  163. [ -z "$SELECTED" ] || [ ! -d "$SELECTED" ] && debug "Invalid selection, exiting" && exit 0
  164. # Update MRU and launch
  165. debug "Updating MRU..."
  166. update_mru "$SELECTED_DISPLAY_CLEAN"
  167. PROJECT_NAME=$(get_project_name "$SELECTED")
  168. SESSION_NAME="dev-${PROJECT_NAME}"
  169. CLASS_NAME="com.mzunino.dev.${PROJECT_NAME}"
  170. debug "Project name: $PROJECT_NAME"
  171. debug "Session name: $SESSION_NAME"
  172. debug "Class name: $CLASS_NAME"
  173. debug "Launching ghostty with launch-or-focus..."
  174. launch-or-focus ghostty \
  175. --class "$CLASS_NAME" \
  176. --title "$PROJECT_NAME" \
  177. --working-directory="$SELECTED" \
  178. -e bash -c "if tmux has-session -t '$SESSION_NAME' 2>/dev/null; then \
  179. tmux attach -t '$SESSION_NAME'; \
  180. else \
  181. tmux new-session -c '$SELECTED' -s '$SESSION_NAME' -d '$EDITOR'; \
  182. tmux split-window -h -c '$SELECTED' -t '$SESSION_NAME'; \
  183. tmux select-pane -t '$SESSION_NAME:0.0'; \
  184. tmux attach -t '$SESSION_NAME'; \
  185. fi"