Skip to content

Instantly share code, notes, and snippets.

@haskie-lambda
Last active July 23, 2021 09:53
Show Gist options
  • Save haskie-lambda/6ee6372c7916e3e7c42012a9a44fb6e4 to your computer and use it in GitHub Desktop.
Save haskie-lambda/6ee6372c7916e3e7c42012a9a44fb6e4 to your computer and use it in GitHub Desktop.
zsh-search-utilities specialized for haskell for .zshrc
# sudo enhancements
svim() {
t=$(ls -l $1 | awk '{ print $3 }');
if [[ "$t" == "$USER" ]];
then vim $@;
else sudo vim $@;
fi
}
# finding and opening files
countIndent() {
sed -e 's/\(.\)/\1\n/g' |
{i=0;
while read res; do
if [[ -z "$res" ]]; then
{ i=$i+1; }
else { break; }
fi;
done;
echo "$i"}
}
findhsfunc () {
findhs "$1 *::" $2 | removeAnsi |
{
read res;
split=( "${(s/:/)res}" );
file=$split[1];
indent=`echo "$split[3]" | countIndent`;
lineNum=$split[2];
echo "$file:$lineNum:";
cat $file |
tail -n +"$lineNum" |
awk -v name="^$split[3]" -v mindent="^ {$indent,}" -v maxindent="^ {0,$((indent))}[a-z]+" '$0 ~ name || $0 ~ mindent{ print $0 } ($0 !~ name) && $0 ~ maxindent{ exit }'
}
}
findhso () {
findhs $1 $2 | awk '{ print(NR ": " $0) }'
RC=$?
if (( $RC==0 )); then
vared -p "Number: " -c n
findhs $1 $2 | openn $n
fi
return $RC
}
removeAnsi() {
cat | sed $'s/\e\\[[0-9;:]*[a-zA-Z]//g'
}
openn() {
file_line=`cat | removeAnsi | awk "NR==$1{print \\$1}"`
split=("${(@s/:/)file_line}")
vim $split[1] +:$split[2]
}
findin(){
in="."
[[ ! -z "$3" ]] && in="$3"
grep --include="$1" -rnw -E "$2" "$in"
}
findhs(){
set +e
in="."
[[ ! -z "$2" ]] && in="$2"
grep --color=always --include=\*.hs --exclude-dir={dist-newstyle,stack-work,dist} -rnw -E "$1" "$in"
return $?
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment