1
0
forked from en2zmax/note-store

Compare commits

...

11 Commits

5 changed files with 164 additions and 20 deletions

View File

@ -2,7 +2,7 @@
Simple pass like notes manager on bash.
To-Do:
* [ ] Update help
* [ ] rm/del notes feature
* [ ] Autocomplite feature
* [x] Update help
* [x] rm/del notes feature
* [x] Autocomplite feature
* [ ] Sync with git

19
addon/wofi/menunote Normal file
View File

@ -0,0 +1,19 @@
#!/usr/bin/env bash
PN="$HOME/.note-store/"
touch $PN/readme.md
echo "# NotesMenu" > $PN/readme.md
TR="kitty sh -c"
ED="vim"
find $PN* -maxdepth 0 -type f ! -name *.md -delete
QT=$(ls $PN | wc -l)
CHOISE=$(find "$PN" -iname '*.md' -type f | awk -F 'store/' '{print $NF;}' | wofi -d) || exit 1
if [[ "$CHOISE" != *.md ]]; then
CHOISE="$CHOISE.md"
fi
CHOISE="$ED '$PN$CHOISE'"
env TEMP=xterm-256color $TR "$CHOISE"
exit 0

View File

@ -0,0 +1,63 @@
#compdef note
#
# Completion script for note
#
_note () {
local cmd
if (( CURRENT > 2)); then
cmd=${words[2]}
curcontext="${curcontext%:*:*}:note-$cmd"
(( CURRENT-- ))
shift words
case "${cmd}" in
init)
;;
find)
_note_complete_entries_with_subdirs
;;
show|ls|list|edit)
_note_complete_entries_with_subdirs
;;
cp|copy|mv|rename|rm)
_note_complete_entries_with_subdirs
;;
rm|delete|remove)
_note_complete_entries_with_subdirs
;;
esac
else
local -a subcommands
subcommands=(
"init:Initialize new note storage"
"ls:List note"
"find:Find note files or directories based on pattern"
"grep:Search note files for matching pattern"
"show:Print a note"
"edit:Edit a note with \$EDITOR"
"mv:Rename the note"
"cp:Copy the note"
"rm:Remove the note"
"version:Output version information"
"help:Output help message"
)
_describe -t commands 'note' subcommands
fi
}
_note_complete_entries_helper () {
local IFS=$'\n'
local notepath
zstyle -s ":completion:${curcontext}:" notepath notepath || notepath="${NOTE_STORE_DIR:-$HOME/.note-store}"
_values -C 'notes' ${$(find -L "$notepath" \( -name .git \) -prune -o $@ -print 2>/dev/null | sed -e "s#${notepath}/\{0,1\}##" -e 's#\.md##' -e 's#\\#\\\\#g' -e 's#:#\\:#g' | sort):-""}
}
_note_complete_entries_with_subdirs () {
_note_complete_entries_helper
}
_note_complete_entries () {
_note_complete_entries_helper -type f
}
_note

96
note
View File

@ -1,4 +1,4 @@
#!/bin/bash
#!/usr/bin/env bash
VER=0.0.1
NOTESPATH="$HOME/.note-store"
@ -6,7 +6,7 @@ CMD_INSERT(){
path=$1
mkdir -p -v "$NOTESPATH/$(dirname -- "$path")" &> /dev/null
path="$NOTESPATH/$path.md"
$EDITOR $path
$EDITOR "$path"
}
CMD_HELP(){
@ -18,10 +18,12 @@ CMD_HELP(){
Usage:
$PROGRAM init
Initialize new note storage.
$PROGRAM [ls]
$PROGRAM [ls] [subfolder]
List notes.
$PROGRAM [edit] note-name
$PROGRAM edit note-name
Insert a new note or edit an existing note using editor.
$PROGRAM rm [--recursive,-r] [--force,-f] note-name
Remove existing note or directory, optionally forcefully.
$PROGRAM help
Show this text.
$PROGRAM version
@ -32,19 +34,31 @@ _EOF
}
CMD_INIT(){
test -d $NOTESPATH && echo "Note Store already exist" && exit 0 ||
mkdir $NOTESPATH &> /dev/null
echo "New note storage creted to: $NOTESPATH"
local path="${1%/}"
local notefile="$NOTESPATH/$path.md"
check_sneaky_paths "$path"
[[ $force -eq 0 && -e $passfile ]] && yesno "An entry already exists for $path. Overwrite it?"
mkdir -p -v "$NOTESPATH/$(dirname -- "$path")"
echo "New note storage creted to: "$NOTESPATH""
exit 0
}
CMD_SHOWTREE(){
if [ ! -z $1 ]; then
glow -p $NOTESPATH/$1.md
else
echo "Note Store"
tree -N -C -l --noreport $NOTESPATH | tail -n +2 |\
local path="$1"
local notefile="$NOTESPATH/$path.md"
if [ -d $NOTESPATH/$path ]; then
if [[ -z $path ]]; then
echo "Note Store"
else
echo "${path%\/}"
fi
tree -N -C -l --noreport "$NOTESPATH/$path" | tail -n +2 |\
sed -E 's/\.md(\x1B\[[0-9]+m)?( ->|$)/\1\2/g'
else
glow -p $NOTESPATH/$path.md
fi
exit 0
}
@ -61,6 +75,32 @@ CMD_GREP(){
echo "add to-do grep"
}
CMD_DELETE(){
local opts recursive="" force=0
te
opts="$($GETOPT -o rf -l recursive,force -n "$PROGRAM" -- "$@")"
local err=$?
eval set -- "$opts"
while true; do case $1 in
-r|--recursive) recursive="-r"; shift ;;
-f|--force) force=1; shift ;;
--) shift; break ;;
esac done
[[ $# -ne 1 ]] && die "Usage: $PROGRAM $COMMAND [--recursive,-r] [--force,-f] note-name"
local path="$1"
check_sneaky_paths "$path"
local notedir="$NOTESPATH/${path%/}"
local notefile="$NOTESPATH/$path.md"
[[ -f $notefile && -d $notedir && $path == */ || ! -f $notefile ]] && notefile="${notedir%/}/"
[[ -e $notefile ]] || die "Error: $path is not in the note store."
[[ $force -eq 1 ]] || yesno "Are you sure you would like to delete $path?"
rm $recursive -f -v "$notefile"
rmdir -p "${notefile%/*}" 2>/dev/null
}
CMD_COPY_MOVE() {
move=1
[[ $1 == "copy" ]] && move=0
@ -79,15 +119,36 @@ CMD_SHOWVERSION(){
echo $PROGRAM version: $VER
}
if [[ ! $1 = init ]]; then
test -d $NOTESPATH || CMD_HELP
if [[ ! "$1" = init ]]; then
test -d "$NOTESPATH" || CMD_HELP
fi
if [ -z $1 ]; then
if [ -z "$1" ]; then
CMD_SHOWTREE
fi
check_sneaky_paths() {
local path
for path in "$@"; do
[[ $path =~ /\.\.$ || $path =~ ^\.\./ || $path =~ /\.\./ || $path =~ ^\.\.$ ]] && die "Error: You've attempted to note a sneaky path to note. Go home."
done
}
die() {
echo "$@" >&2
exit 1
}
yesno() {
[[ -t 0 ]] || return 0
local response
read -r -p "$1 [y/N] " response
[[ $response == [yY] ]] || exit 1
}
PROGRAM="${0##*/}"
COMMAND="$1"
GETOPT="getopt"
SHRED="shred -f -z"
case "$1" in
init) shift; CMD_INIT "$@" ;;
@ -95,13 +156,14 @@ case "$1" in
show|ls|list) shift; CMD_SHOWTREE "$@" ;;
find|search) shift; CMD_FIND "$@" ;;
grep) shift; CMD_GREP "$@" ;;
delete|rm|remove) shift; CMD_DELETE "$@" ;;
copy|cp) shift; CMD_COPY_MOVE "copy" "$@" ;;
rename|mv) shift; CMD_COPY_MOVE "move" "$@" ;;
edit) shift; CMD_INSERT "$@" ;;
edit|add) shift; CMD_INSERT "$@" ;;
version|-v) CMD_SHOWVERSION ;;
*) CMD_HELP "$@" ;;
esac
find $NOTESPATH* -maxdepth 0 -type f ! -name *.md -delete &> /dev/null
find "$NOTESPATH"* -maxdepth 0 -type f ! -name *.md -delete &> /dev/null
exit 0