Created
April 1, 2020 22:17
-
-
Save SirJson/08b4674f3c22eeef8a97f115e09c722e to your computer and use it in GitHub Desktop.
Skim + Ripgrep + VSCode = Code search jumping
This file contains 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
#!/bin/bash | |
# shellcheck disable=SC2068 | |
set -Euo pipefail | |
_fail() { | |
printf '\e[0m' | |
printf '\n\e[38;2;200;0;0m%s\e[0m\n' "FAIL: $1" | |
exit 1 | |
} | |
_info() { | |
printf '\e[0m' | |
printf '\e[38;2;0;200;0m%s\e[0m\n' "$1" | |
} | |
_terminate_handler() { | |
_fail "Caught terminate signal" | |
} | |
_error_handler() { | |
ORIGIN=$1 | |
ERRNO=$2 | |
printf '\e[0m' | |
printf '\n\e[1m\e[48;2;236;240;241m\e[38;2;200;0;0m %s \e[0m' "!! > Internal failure. Command '$ORIGIN' failed with $ERRNO" | |
printf '\n' | |
exit 1 | |
} | |
trap '_terminate_handler' SIGINT SIGTERM | |
trap '_error_handler $BASH_COMMAND $?' ERR | |
OPTIND=1 # Reset in case getopts has been used previously in the shell. | |
BATVIEW="bat --style=numbers,changes,snip,plain,header --color=always --pager=never" | |
PYGMENTSVIEW="pygmentize" | |
FALLBACKVIEW="cat" | |
_installed() { | |
command -v "$1" &> /dev/null | |
return $? | |
} | |
if ! _installed "code"; then | |
_fail "At the moment this script only support vscode" | |
fi | |
if ! _installed "sk"; then | |
_fail "You need skim (https://github.com/lotabout/skim) to run this script!" | |
fi | |
codesk_search() { | |
result=$(SKIM_DEFAULT_COMMAND="fd --type f || git ls-tree -r --name-only HEAD || rg --files || find .";sk --ansi -i -c 'rg --color=always --line-number "{}"' --preview='codesk -p {}' || true) | |
if [[ -n "$result" ]]; then | |
target=$(echo "$result" | awk -F':' '{printf "%s:%s",$1,$2}') | |
code -g "$target" | |
else | |
_info "Search exited without result" | |
fi | |
} | |
codesk_preview() { | |
local viewer | |
if _installed "bat"; then | |
viewer=$BATVIEW | |
elif _installed "pygmentize"; then | |
viewer=$PYGMENTSVIEW | |
else | |
viewer=$FALLBACKVIEW | |
fi | |
filepath=$(echo "$1" | awk -F':' '{printf "%s",$1}' | xargs readlink -f) | |
$viewer "$filepath" | |
} | |
while getopts 'p:' c | |
do | |
case $c in | |
p) codesk_preview "$OPTARG" && exit 0;; | |
*) ;; | |
esac | |
done | |
if [[ $OPTIND == 1 ]]; then | |
codesk_search | |
fi |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment