Skip to content

Commit

Permalink
feat(dev): prettify paths in fzf
Browse files Browse the repository at this point in the history
  • Loading branch information
csvenke committed Sep 16, 2024
1 parent 86a8fd9 commit e42b744
Show file tree
Hide file tree
Showing 2 changed files with 37 additions and 20 deletions.
6 changes: 3 additions & 3 deletions flake.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

51 changes: 34 additions & 17 deletions packages/dev/script.bash
Original file line number Diff line number Diff line change
Expand Up @@ -11,21 +11,7 @@ function main() {
local selected_path
selected_path=$(select_path "$project_paths")

if [[ -z "$selected_path" ]]; then
exit 1
fi

cd "$selected_path" || exit 1

if [ -n "$VISUAL" ]; then
"$VISUAL" .
exit 0
fi

if [ -n "$EDITOR" ]; then
"$EDITOR" .
exit 0
fi
open_path "$selected_path"
}

function find_search_paths() {
Expand All @@ -40,8 +26,39 @@ function find_project_paths() {
}

function select_path() {
local paths="$1"
echo "$paths" | sed 's|.*/\([^/]*\)|\1 (\0)|' | fzf --ansi --border=none | sed -n 's/.*(\(.*\)).*/\1/p'
local project_paths="$1"
local pretty_project_paths
pretty_project_paths=$(make_pretty_paths "$project_paths")

echo "$pretty_project_paths" |
fzf --ansi --border=none --info=inline |
sed -n 's/.*(\(.*\)).*/\1/p'
}

function make_pretty_paths() {
echo "$1" |
awk '{ cmd = "basename " $1; cmd | getline base; close(cmd); printf "%s (%s)\n", base, $1 }' |
awk 'BEGIN { gray="\033[90m"; blue="\033[34m"; reset="\033[0m"; folderIcon=" "; } { print blue folderIcon $1 reset " " gray ""$2"" reset }'
}

function open_path() {
local path="$1"

if [[ -z "$path" ]]; then
exit 1
fi

cd "$path" || exit 1

if [ -n "$VISUAL" ]; then
"$VISUAL" .
exit 0
fi

if [ -n "$EDITOR" ]; then
"$EDITOR" .
exit 0
fi
}

main

0 comments on commit e42b744

Please sign in to comment.