feat: improve completions, rename & extract rc/src to dfe/dfs

This commit is contained in:
2024-08-06 13:32:51 +03:00
parent fe33bf01cc
commit a4a6e8df0a
5 changed files with 159 additions and 132 deletions

View File

@@ -3,6 +3,10 @@ autoload_completions() {
autoload bashcompinit
bashcompinit
autoload -Uz compinit
# autocompletions
for i in $(ls $DOTFILES/completions); do
autoload $i
done
compinit
}

View File

@@ -1,4 +1,4 @@
#compdef rc src
#compdef dfe dfs
dfpath="$(wd path df)"
out=()

View File

@@ -1,4 +1,4 @@
#compdef prc sprc
#compdef dfpe dfps
dfpath="$(wd path df)"
out=()

View File

@@ -0,0 +1,147 @@
#!/usr/bin/env zsh
# edit a dotfile script and source if there were changes
# supports autocomplete for any editable files
dfe() {
if [[ $# -eq 0 ]]; then
echo "Usage: df [-n] [-q] <dotfile>"
echo "Edit a dotfile script and source if there were changes"
echo "For .zsh files, the extension is optional"
echo " -n: do not source the file after editing"
echo " -q: do not print any messages"
return 1
fi
no_src=0
quiet=0
while [[ $# -gt 1 ]]; do
case $1 in
-n)
no_src=1
;;
-q)
quiet=1
;;
esac
shift
done
if [[ -f "$DOTFILES/$1.sh" ]]; then
file="$DOTFILES/$1.sh"
elif [[ -f "$DOTFILES/$1.zsh" ]]; then
file="$DOTFILES/$1.zsh"
else
file="$DOTFILES/$1"
fi
if [[ -f $file ]]; then
hash=$(md5 $file)
echo "Opening $(strip-home $file)..."
nvim $file
newhash=$(md5 $file)
if [[ $? -eq 0 && $hash != $newhash ]]; then
if [[ $no_src -ne 1 ]]; then
if [[ $quiet -ne 1 ]]; then
src $1
else
src -q $1
fi
fi
else
echo "No changes made"
return 2
fi
return 0
fi
echo_red "File not found: $(strip-home $file)"
return 1
}
# source a dotfile script
# supports autocomplete for any editable files
dfs() {
if [[ $# -eq 0 ]]; then
echo "Usage: dfs [-q] <dotfile>"
echo "Source a dotfile script"
echo " -q: do not print any messages"
return 1
fi
while [[ $# -gt 1 ]]; do
case $1 in
-q)
quiet=1
;;
esac
shift
done
if [[ -f "$DOTFILES/$1.sh" ]]; then
file="$DOTFILES/$1.sh"
elif [[ -f "$DOTFILES/$1.zsh" ]]; then
file="$DOTFILES/$1.zsh"
else
file="$DOTFILES/$1"
fi
if [[ -f $file ]]; then
if [[ $quiet -ne 1 ]]; then
echo "Reloading $(strip-home $file)..."
fi
source "$file"
return 0
fi
echo_red "File not found: $(strip-home $file)"
return 1
}
# same as rc, but for plugin files
dfpe() {
local flags=()
# Parse arguments
while [[ "$#" -gt 1 ]]; do
flags+=("$1")
shift
done
local file="$1"
# Ensure a file argument was passed
if [[ -z $file ]]; then
echo "Usage: dfp [-n] [-q] <plugin file>"
echo "Edit a dotfile plugin script and source if there were changes"
echo "For .zsh files, the extension is optional"
echo " -n: do not source the file after editing"
echo " -q: do not print any messages"
return 1
fi
# Call dfe with flags and transformed file argument
dfe "${flags[@]}" "plugins/$file.plugin"
return $?
}
# same as src, but for plugin files
dfps() {
local flags=()
# Parse arguments
while [[ "$#" -gt 1 ]]; do
flags+=("$1")
shift
done
local file="$1"
# Ensure a file argument was passed
if [[ -z $file ]]; then
echo "Usage: dfps [-q] <plugin file>"
echo "Source a dotfile plugin script"
echo " -q: do not print any messages"
return 1
fi
# Call dfs with flags and transformed file argument
dfs "${flags[@]}" "plugins/$file.plugin"
return $?
}

View File

@@ -78,108 +78,6 @@ is_linux() {
return $?
}
# edit a dotfile script and source if there were changes
# supports autocomplete for any editable files
rc() {
if [[ $# -eq 0 ]]; then
echo_red "Usage: rc [-n] [-q] <dotfile>"
return 1
fi
no_src=0
quiet=0
while [[ $# -gt 1 ]]; do
case $1 in
-n)
no_src=1
;;
-q)
quiet=1
;;
esac
shift
done
if [[ -f "$DOTFILES/$1.sh" ]]; then
file="$DOTFILES/$1.sh"
elif [[ -f "$DOTFILES/$1.zsh" ]]; then
file="$DOTFILES/$1.zsh"
else
file="$DOTFILES/$1"
fi
if [[ -f $file ]]; then
hash=$(md5 $file)
echo "Opening $file..."
nvim $file
newhash=$(md5 $file)
if [[ $? -eq 0 && $hash != $newhash ]]; then
if [[ $no_src -ne 1 ]]; then
if [[ $quiet -ne 1 ]]; then
src $1
else
src -q $1
fi
fi
else
echo "No changes made"
return 2
fi
return 0
fi
echo_red "File not found: $file"
return 1
}
# source a dotfile script
# supports autocomplete for any editable files
src() {
if [[ $# -eq 0 ]]; then
echo_red "Usage: src [-q] <dotfile>"
return 1
fi
while [[ $# -gt 1 ]]; do
case $1 in
-q)
quiet=1
;;
esac
shift
done
if [[ -f "$DOTFILES/$1.sh" ]]; then
file="$DOTFILES/$1.sh"
elif [[ -f "$DOTFILES/$1.zsh" ]]; then
file="$DOTFILES/$1.zsh"
else
file="$DOTFILES/$1"
fi
if [[ -f $file ]]; then
if [[ $quiet -ne 1 ]]; then
echo "Reloading $file..."
fi
source "$file"
return 0
fi
echo_red "File not found: $file"
return 1
}
# same as rc, but for plugin files
prc() {
rc "plugins/$1.plugin"
return $?
}
# same as src, but for plugin files
sprc() {
src "plugins/$1.plugin"
return $?
}
# select random number between min and max
rand() {
if [[ $# -eq 0 ]]; then
@@ -196,10 +94,11 @@ rand() {
echo $(($RANDOM % ($max - $min + 1) + $min))
}
# select random element from list
# select random line from file
randline() {
if [[ $# -eq 0 ]]; then
echo_red "Usage: randline <file>"
echo_red "Select a random line from a file"
return 1
fi
linenum=$(($RANDOM % $(wc -l <$1) + 1))
@@ -219,25 +118,7 @@ find-replace() {
sed "s/$find/$replace/g" $file
}
# find $1 and replace with $2 in file $3, output to file
find-replace-file() {
if [[ $# -lt 3 || $1 == '-h' ]]; then
echo_red "Find and replace text in file. Modifies the file."
echo_red "Usage: find-replace-file <find> <replace> <file> [file]..."
return 1
fi
files=( "${@:3}" )
find=$1
replace=$2
for file in $files; do
echo "Replacing $find with $replace in $file..."
out=$(find-replace $find $replace $file)
echo $out >$file
done
}
# runs all scripts in directory $1 in order
# same as run-parts from debian, but for osx
if is_mac; then
run-parts() {
@@ -265,7 +146,8 @@ fi
# search for a file in a directory
search-file() {
if [[ $# -eq 0 ]]; then
echo "Usage: find-file [dir] <file>"
echo "Usage: search-file [dir] <file>"
echo "Search for a file in a directory (recursively)"
return 1
fi
if [[ $# -eq 1 ]]; then
@@ -284,6 +166,7 @@ search-file() {
find-up() {
if [[ $# -eq 0 ]]; then
echo "Usage: find-up <file>"
echo "Finds a file in the current directory or on one of its ancestors"
return 1
fi
file=$1
@@ -362,13 +245,6 @@ docker-volume-cd() {
cd $(docker-volume-path "$image")
}
# autocompletions
autoload _docker-exec
autoload _docker-volume-path
autoload _prj
autoload _src
autoload _psrc
# reload entire shell
reload-zsh() {
source $HOME/.zshrc