damshote

damshote stands for dash msh note. It is a single dash script to manage plain text notes in a folder using neovim. Put this script in a folder that you want your plain text notes to reside, cd into the folder and run the script.

#!/bin/dash

# check if it is run as ROOT and exit.
ROOID=$(id -u)
if [ $((ROOID)) -eq 0 ]; then
    exit 0
fi
# check if it is running in terminal. otherwise exit.
case "$(ps -o stat= -p $$)" in
    *+*) clear; echo "\ndamshote continue..." ;;
    *) notify-send -t 2700 "clean exit" "please run it in terminal." ;;
esac
# check it is already running and exit.
SCRIPTNAME="$(basename -- "$0")"
if pidof -x "$SCRIPTNAME" -o $$ > /dev/null 2>&1; then
    echo "Oop! it is already running. clean exit"
    exit 0
fi

HEREDIR="$(dirname "$(readlink -f "$0")")"
NOTEDIR=""$HEREDIR"/damshote-notes-dir"
TRASHDIR=""$HEREDIR"/damshote-trash-dir"
mkdir -p "$NOTEDIR"
mkdir -p "$TRASHDIR"
GENFILE=""$HEREDIR"/contentcpmvfodesgen"

# generate list of files and folders in the current directory
# content generate
contentgen_func () {
    if [ -f "$GENFILE" ]; then
        rm "$GENFILE"
    fi
    if [ "$PWD" = "$NOTEDIR" ]; then
        local FIFORAW=$(echo "EXIT\nNew-FILE\nNew-FOLDER" && ls --group-directories-first --file-type .)
    else
        local FIFORAW=$(echo "EXIT\nNew-FILE\nNew-FOLDER" && ls -a --group-directories-first --file-type . | grep -v "^\.[0-9a-zA-Z]\|^\./$")
    fi
    local NUM=0
    for FIFO in $FIFORAW; do
        local ZLNUM=$(printf "%03d" "$NUM")
        echo ""$ZLNUM". "$FIFO" @ "$ZLNUM""
        local NUM=$((NUM+1))
    done > "$GENFILE"
    echo; cat "$GENFILE"; echo
}

# cp mv folder destination generate
cpmvfodesgen_func () {
    if [ -f "$GENFILE" ]; then
        rm "$GENFILE"
    fi
    local FOLIST=$(echo "EXIT\n../" && ls -d */ 2>/dev/null)
    local NUM=0
    for FOLDER in $FOLIST; do
        local ZLNUM=$(printf "%03d" "$NUM")
        echo ""$ZLNUM". "$FOLDER" @ "$ZLNUM""
        local NUM=$((NUM+1))
    done > "$GENFILE"
    echo; cat "$GENFILE"; echo
}

# selecting by number
selection_func () {
    while true; do
        read -p "Select Number: " SELNUN
        # correct selected number
        unset CORRECNUM
        case "$SELNUN" in
            [0-9]|[0-9][0-9]) CORRECNUM=$(printf "%03d" "$SELNUN") ;;
            [0-9][0-9][0-9]) CORRECNUM="$SELNUN" ;;
            e|exit|q|quit) exit 0 ;;
            *) echo "Oop! invalid selection. try again."; continue ;;
        esac
        SELECTED=$(cat "$GENFILE" | grep "^$CORRECNUM" | cut -d ' ' -f 2)
        if [ -z "$SELECTED" ]; then
            echo "Oop! selection is empty. try again."; continue
        fi
        break
    done
}

# copy and move function
copymove_func () {
    local SELFILE=$(readlink -f "$SELECTED")
    unset SELECTED
    cpmvfodesgen_func
    selection_func
    local SELDEST=$(readlink -f "$SELECTED")
    if [ "$1" = "copy" ]; then
        cp "$SELFILE" "$SELDEST"/
    fi
    if [ "$1" = "move" ]; then
        mv "$SELFILE" "$SELDEST"/
    fi
}
# open and go to directory
opengoto_func () {
    if [ -d "$SELECTED" ]; then
        cd "$SELECTED"
    fi
    if [ -f "$SELECTED" ]; then
        nvim "$SELECTED"
    fi
}
# read file
readnotefile_func () {
    if [ -f "$SELECTED" ]; then
        less "$SELECTED"
    fi
}
# rename file or folder
renamefifo_func () {
    read -p "Type New Name: " NEWNAME
    mv "$SELECTED" $(date +'%Y%m%d-%H%M%S')___"$NEWNAME"
}
# delete file or folder
deletefifo_func () {
    mv "$SELECTED" "$TRASHDIR"/
}
# copy content to clipboard
copycontentclipboard_func () {
    if [ -f "$SELECTED" ]; then
        cat "$SELECTED" | wl-copy
    fi
}

# step one operations
steponeopera_func () {
    unset STEPTWO
    if [ "$SELECTED" = "EXIT" ]; then
        exit 0
    fi
    if [ "$SELECTED" = "New-FILE" ]; then
        read -p "Type New-FILE name without space: " FILENAME
        nvim "$(date +'%Y%m%d-%H%M%S')___"$FILENAME""
        STEPTWO=0
        return
    fi
    if [ "$SELECTED" = "New-FOLDER" ]; then
        read -p "Type New-FOLDER name without space: " FOLDERNAME
        mkdir "$(date +'%Y%m%d-%H%M%S')___"$FOLDERNAME""
        STEPTWO=0
        return
    fi
    if [ "$SELECTED" = "BACK" ]; then
        cd ../
        STEPTWO=0
        return
    fi
    STEPTWO=1
}

# step two operations
steptwoopera_func () {
    if [ $((STEPTWO)) -eq 0 ]; then
        return
    fi
    echo "
    o -> Open/GoTo
    a -> Read
    c -> Copy
    m -> Move
    r -> Rename
    d -> Delete
    b -> Go-BACK
    p -> Copy Content to Clipboard
    q -> EXIT/QUIT
    e -> EXIT/QUIT
    "
    read -p "Type Character: " SELCHAR
    case "$SELCHAR" in
        o)
            opengoto_func
            ;;
        a)
            readnotefile_func
            ;;
        c)
            copymove_func copy
            ;;
        m)
            copymove_func move
            ;;
        r)
            renamefifo_func
            ;;
        d)
            deletefifo_func
            ;;
        b)
            echo "\ndamshote continue..."
            ;;
        p)
            copycontentclipboard_func
            ;;
        q)
            exit 0
            ;;
        e)
            exit 0
            ;;
        *)
            opengoto_func
            ;;
    esac
}

# start main
cd "$NOTEDIR"
while true; do
    contentgen_func
    selection_func
    steponeopera_func
    steptwoopera_func
    clear
    echo "\ndamshote continue..."
done

Comments

Popular posts from this blog

printf - Printing with format

rebase or no-rebase for git pull

dmmsh-wlr for managing outputs in Wayland using wlr-randr