From 78be12077079f332816e466ffacc47d5b2b498b0 Mon Sep 17 00:00:00 2001 From: Dimitris Marakomihelakis Date: Mon, 21 Aug 2023 17:48:30 +0300 Subject: [PATCH] command autocompletion & file suggestion --- util/delnote.sh | 22 ++++++++++++++++++++- util/listnotes.sh | 19 ++++++++++++++++++ util/newnote.sh | 13 +++++++------ util/notegrep.sh | 22 +++++++++++++++++++++ util/notewc.sh | 22 ++++++++++++++++++++- util/quickread.sh | 24 ++++++++++++++++++++++- util/readnote.sh | 48 ++++++++++++++++++++++++++++++++++------------ util/renamenote.sh | 23 +++++++++++++++++++++- 8 files changed, 171 insertions(+), 22 deletions(-) diff --git a/util/delnote.sh b/util/delnote.sh index b5b0fd4..544c0a0 100644 --- a/util/delnote.sh +++ b/util/delnote.sh @@ -44,4 +44,24 @@ function delnote() { fi fi cd $DIR -} \ No newline at end of file +} + +_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 \ No newline at end of file diff --git a/util/listnotes.sh b/util/listnotes.sh index 5a7c539..51eb9fb 100644 --- a/util/listnotes.sh +++ b/util/listnotes.sh @@ -30,6 +30,9 @@ function listnotes() { -f|--folder ) . ~/.shellnotes/util/listnotes/f/f.sh ;; + -v|--view ) + . ~/.shellnotes/util/listnotes/v/v.sh + ;; *) if [ -z $1 ]; then if [ -z "$(ls -A $DEFAULT_PATH)" ]; then @@ -44,3 +47,19 @@ function listnotes() { return 0 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 \ No newline at end of file diff --git a/util/newnote.sh b/util/newnote.sh index c903e38..ac2654f 100644 --- a/util/newnote.sh +++ b/util/newnote.sh @@ -7,11 +7,12 @@ info function newnote() { DIR="$(pwd)" - cd $DEFAULT_PATH - $NOTES_EDITOR - clear - str="Note created in $DEFAULT_PATH" - underlines=$(echo "$str" | tr -c '\010' '-') - echo "$str\n$underlines" + cd $HOME + # echo $1 + NOTES_EDITOR + # clear + # str="Note created in $DEFAULT_PATH" + # underlines=$(echo "$str" | tr -c '\010' '-') + # echo "$str\n$underlines" cd $DIR } \ No newline at end of file diff --git a/util/notegrep.sh b/util/notegrep.sh index c446714..f3ebe73 100644 --- a/util/notegrep.sh +++ b/util/notegrep.sh @@ -213,3 +213,25 @@ function notegrep() { cd $DIR 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 \ No newline at end of file diff --git a/util/notewc.sh b/util/notewc.sh index bac9337..94d7059 100644 --- a/util/notewc.sh +++ b/util/notewc.sh @@ -28,4 +28,24 @@ function notewc() { cd $DIR -} \ No newline at end of file +} + +_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 \ No newline at end of file diff --git a/util/quickread.sh b/util/quickread.sh index f8bdd7e..b0318f7 100644 --- a/util/quickread.sh +++ b/util/quickread.sh @@ -55,4 +55,26 @@ function quickread() { fi cd $DIR -} \ No newline at end of file +} + + +_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 \ No newline at end of file diff --git a/util/readnote.sh b/util/readnote.sh index b92f218..0d5072f 100644 --- a/util/readnote.sh +++ b/util/readnote.sh @@ -10,32 +10,56 @@ function readnote() { if [ $# -eq 0 ]; then echo -n "Enter note name: " && read notename else - notename=$1 + notename="$1" fi - if [ -z $notename ]; then + if [ -z "$notename" ]; then echo "Invalid input." return 0 else cd $DEFAULT_PATH - if [ -e $notename ]; then - chmod 0444 $notename - $NOTES_EDITOR $notename - chmod +rw $notename + if [ -e "$notename" ]; then + cd "$HOME" + # chmod 0444 $notename + NOTES_EDITOR "$notename" + # chmod +rw $notename else clear echo "No such note." echo -n "Do you want to create one?[Y/N]: " && read create; + + case $create in y|Y|YES|Yes|yes ) + newnote + esac fi - case $create in y|Y|YES|Yes|yes ) - $NOTES_EDITOR - esac + - clear - cd $DIR + # clear + cd "$DIR" fi -} \ No newline at end of file +} + + +_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 \ No newline at end of file diff --git a/util/renamenote.sh b/util/renamenote.sh index c0eafea..c7f8f1a 100644 --- a/util/renamenote.sh +++ b/util/renamenote.sh @@ -47,4 +47,25 @@ function renamenote() { cd $DIR fi -} \ No newline at end of file +} + + +_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 \ No newline at end of file