command autocompletion & file suggestion

This commit is contained in:
Dimitris Marakomihelakis 2023-08-21 17:48:30 +03:00
parent 332c70a72d
commit 78be120770
8 changed files with 171 additions and 22 deletions

View file

@ -44,4 +44,24 @@ function delnote() {
fi fi
fi fi
cd $DIR cd $DIR
} }
_delnote_completion() {
local cur prev
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
# Generate the list of files in the directory
files=("$DEFAULT_PATH"/*)
files_list="${files[@]##*/}" # Extract file names
case "$prev" in
delnote)
COMPREPLY=( $(compgen -W "${files_list}" -- "$cur") )
;;
*)
;;
esac
}
complete -F _delnote_completion delnote

View file

@ -30,6 +30,9 @@ function listnotes() {
-f|--folder ) -f|--folder )
. ~/.shellnotes/util/listnotes/f/f.sh . ~/.shellnotes/util/listnotes/f/f.sh
;; ;;
-v|--view )
. ~/.shellnotes/util/listnotes/v/v.sh
;;
*) *)
if [ -z $1 ]; then if [ -z $1 ]; then
if [ -z "$(ls -A $DEFAULT_PATH)" ]; then if [ -z "$(ls -A $DEFAULT_PATH)" ]; then
@ -44,3 +47,19 @@ function listnotes() {
return 0 return 0
cd $DIR cd $DIR
} }
_listnotes_completion() {
local cur prev
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
case "$prev" in
listnotes)
COMPREPLY=( $(compgen -W "-d -n -f -v" -- "$cur") )
;;
*)
;;
esac
}
complete -F _listnotes_completion listnotes

View file

@ -7,11 +7,12 @@ info
function newnote() { function newnote() {
DIR="$(pwd)" DIR="$(pwd)"
cd $DEFAULT_PATH cd $HOME
$NOTES_EDITOR # echo $1
clear NOTES_EDITOR
str="Note created in $DEFAULT_PATH" # clear
underlines=$(echo "$str" | tr -c '\010' '-') # str="Note created in $DEFAULT_PATH"
echo "$str\n$underlines" # underlines=$(echo "$str" | tr -c '\010' '-')
# echo "$str\n$underlines"
cd $DIR cd $DIR
} }

View file

@ -213,3 +213,25 @@ function notegrep() {
cd $DIR cd $DIR
return 0 return 0
} }
_notegrep_completion() {
local cur prev
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}" # Get the previous argument
# Generate the list of files in the directory
files=("$DEFAULT_PATH"/*)
files_list="${files[@]##*/}" # Extract file names
case "$prev" in
notegrep)
COMPREPLY=( $(compgen -W "-cf -cl -m -sc -sf -sl -ws" -- "$cur") )
;;
*)
COMPREPLY=( $(compgen -W "${files_list}" -- "$cur") )
;;
esac
}
complete -F _notegrep_completion notegrep

View file

@ -28,4 +28,24 @@ function notewc() {
cd $DIR cd $DIR
} }
_notewc_completion() {
local cur prev
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
# Generate the list of files in the directory
files=("$DEFAULT_PATH"/*)
files_list="${files[@]##*/}" # Extract file names
case "$prev" in
notewc)
COMPREPLY=( $(compgen -W "${files_list}" -- "$cur") )
;;
*)
;;
esac
}
complete -F _notewc_completion notewc

View file

@ -55,4 +55,26 @@ function quickread() {
fi fi
cd $DIR cd $DIR
} }
_quickread_completion() {
local cur prev
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}" # Get the previous argument
# Generate the list of files in the directory
files=("$DEFAULT_PATH"/*)
files_list="${files[@]##*/}" # Extract file names
case "$prev" in
quickread)
COMPREPLY=( $(compgen -W "-l -r -s" -- "$cur") )
;;
*)
COMPREPLY=( $(compgen -W "${files_list}" -- "$cur") )
;;
esac
}
complete -F _quickread_completion quickread

View file

@ -10,32 +10,56 @@ function readnote() {
if [ $# -eq 0 ]; then if [ $# -eq 0 ]; then
echo -n "Enter note name: " && read notename echo -n "Enter note name: " && read notename
else else
notename=$1 notename="$1"
fi fi
if [ -z $notename ]; then if [ -z "$notename" ]; then
echo "Invalid input." echo "Invalid input."
return 0 return 0
else else
cd $DEFAULT_PATH cd $DEFAULT_PATH
if [ -e $notename ]; then if [ -e "$notename" ]; then
chmod 0444 $notename cd "$HOME"
$NOTES_EDITOR $notename # chmod 0444 $notename
chmod +rw $notename NOTES_EDITOR "$notename"
# chmod +rw $notename
else else
clear clear
echo "No such note." echo "No such note."
echo -n "Do you want to create one?[Y/N]: " && read create; echo -n "Do you want to create one?[Y/N]: " && read create;
case $create in y|Y|YES|Yes|yes )
newnote
esac
fi fi
case $create in y|Y|YES|Yes|yes )
$NOTES_EDITOR
esac
clear # clear
cd $DIR cd "$DIR"
fi fi
} }
_readnote_completion() {
local cur prev
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
# Generate the list of files in the directory
files=("$DEFAULT_PATH"/*)
files_list="${files[@]##*/}" # Extract file names
case "$prev" in
readnote)
COMPREPLY=( $(compgen -W "${files_list}" -- "$cur") )
;;
*)
;;
esac
}
complete -F _readnote_completion readnote

View file

@ -47,4 +47,25 @@ function renamenote() {
cd $DIR cd $DIR
fi fi
} }
_renamenote_completion() {
local cur prev
cur="${COMP_WORDS[COMP_CWORD]}"
prev="${COMP_WORDS[COMP_CWORD-1]}"
# Generate the list of files in the directory
files=("$DEFAULT_PATH"/*)
files_list="${files[@]##*/}" # Extract file names
case "$prev" in
renamenote)
COMPREPLY=( $(compgen -W "${files_list}" -- "$cur") )
;;
*)
;;
esac
}
complete -F _renamenote_completion renamenote