Created
November 17, 2024 21:51
-
-
Save Reecepbcups/a832bb9875513decfbdcca174f0d5cbb to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
#!/usr/bin/env bash | |
: ' | |
Reece Williams | Nov 2024 | |
# Requires: fzf | |
# - https://archlinux.org/packages/extra/x86_64/fzf/ | |
Opens folders in your programming mono local repo in your editor or explorer. | |
# source .bashrc -> the folder where open is now | |
open [search_term][/] | |
examples: | |
- open # prompts for search query | |
- open go/ # with a trailing /, it opens up the folder in your EXPLORER (must be in FILE_LOCATION) | |
- open juno # searches for any folders or subfolders which contain juno, if so, opens up a picker to select which one to open | |
# Original Python version: | |
# https://gist.github.com/Reecepbcups/b64920fcfb7a151049884c9df6753076 | |
# | |
# This version: | |
# https://gist.github.com/Reecepbcups/a832bb9875513decfbdcca174f0d5cbb | |
' | |
EDITOR="code" | |
EXPLORER="/usr/bin/nautilus" | |
# Where the root of the files are. Would be nice for this to be an array in the future | |
FILE_LOCATION="${HOME}/Desktop/Programming" | |
if [[ $# -eq 1 ]]; then | |
# if $1 ends with a /, open the folder in the explorer | |
# i.e. Go/ opens the FILE_LOCATION/Go/ in the explorer | |
if [[ $1 == */ ]]; then | |
echo "Opening $1 in explorer" | |
# open without issue or printing | |
$EXPLORER -w $FILE_LOCATION/$1 > /dev/null 2>&1 & | |
exit 0 | |
fi | |
# find all possible files with the search term somewhere in it | |
matches=`find $FILE_LOCATION -mindepth 2 -maxdepth 2 -type d -wholename "*$1*"` | |
if [[ -z $matches ]]; then | |
echo "No matches found for '$1'" | |
exit 1 | |
fi | |
# if there is only 1 match, select it as the working | |
# i.e. len of 1 match; `ibc-app` matches `ibc-apps`; nothing else matches. select it | |
if [[ $(echo $matches | wc -w) -eq 1 ]]; then | |
selected=$matches | |
else | |
# search all possible folders for a possible direct match. | |
# i.e. `juno`; searches and finds a folder called `juno` even though `juno-ts` and `juno-frontend` exist | |
# as exact matches are pushed through | |
for match in $matches; do | |
lastItem=$(echo $match | rev | cut -d'/' -f1 | rev) | |
if [[ $lastItem == $1 ]]; then | |
selected=$match | |
break | |
fi | |
done | |
fi | |
# if selected is empty, we have not found yet. Need to prompt for multi-selecti-on input | |
if [[ -z $selected ]]; then | |
# remove prefix as it is redundant | |
selected=$(echo $matches | tr ' ' '\n' | sed "s#${FILE_LOCATION}/##g" | fzf) | |
selected="${FILE_LOCATION}/${selected}" # re-add the prefix of file loc | |
fi | |
else | |
# no input, search within the terminal itself | |
selected=$(find ${FILE_LOCATION} -mindepth 1 -maxdepth 2 -type d | sed "s#${FILE_LOCATION}/##g" | fzf) | |
selected="${FILE_LOCATION}/${selected}" # re-add the prefix of file loc | |
fi | |
# selected matches the root of all files, ignore | |
if [[ "${selected}" == "${FILE_LOCATION}/" ]]; then | |
echo "No folder selected" | |
exit 1 | |
fi | |
if [[ -z $selected ]]; then | |
echo "No folder selected" | |
exit 1 | |
fi | |
if [[ ! -d $selected ]]; then | |
echo "Selected is not a folder" | |
exit 1 | |
fi | |
$EDITOR $selected # open here / tmux it |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment