Files
notestorm/notestorm

343 lines
10 KiB
Bash
Executable File

#!/bin/bash
# notestorm
# Description: Easily take and read notes from the CLI.
#
# Copyright 2019, F123 Consulting, <information@f123.org>
# Copyright 2019, Storm Dragon, <storm_dragon@linux-a11y.org>
#
# This is free software; you can redistribute it and/or modify it under the
# terms of the GNU General Public License as published by the Free
# Software Foundation; either version 3, or (at your option) any later
# version.
#
# This software is distributed in the hope that it will be useful,
# but WITHOUT ANY WARRANTY; without even the implied warranty of
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
# General Public License for more details.
#
# You should have received a copy of the GNU General Public License
# along with this package; see the file COPYING. If not, write to the Free
# Software Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
# 02110-1301, USA.
#
#--code--
# the gettext essentials
export TEXTDOMAIN=notestorm
export TEXTDOMAINDIR=/usr/share/locale
source gettext.sh
# Log writing function
log() {
# Usage: command | log for just stdout.
# Or command |& log for stderr and stdout.
while read -r line ; do
echo "$line" | tee -a "$logFile" &> /dev/null
done
}
# Log file name is ~/.cache/scriptname
logFile="${HOME}/.cache/${0##*/}"
# Functions section
infobox() {
# Returns: None
# Shows the provided message on the screen with no buttons.
local continue
dialog --infobox "$*" 10 80
read -n1 -t $messageTimeout continue
}
inputbox() {
# Returns: text entered by the user
# Args 1, Instructions for box.
# args: 2 initial text (optional)
dialog --clear --backtitle "$(gettext "Enter text and press enter.")" \
--inputbox "$1" 0 0 "$2" --stdout
}
yesno() {
# Returns: Yes or No
# Args: Question to user.
# Called in if $(yesno) == "Yes"
# Or variable=$(yesno)
dialog --clear --backtitle "$(gettext "Press 'Enter' for \"yes\" or 'Escape' for \"no\".")" --yesno "$*" 10 80 --stdout
if [[ $? -eq 0 ]]; then
echo "Yes"
else
echo "No"
fi
}
menulist() {
# Args: Tag, description.
# returns: selected tag
dialog --backtitle "$(gettext "Use the up and down arrow keys to find the option you want, then press enter to select it.")" \
--clear \
--cancel-label "$(gettext "Exit")" \
--extra-button \
--extra-label "$(gettext "New")" \
--help-button \
--help-label "$(gettext "More")" \
--ok-label "$(gettext "View")" \
--no-tags \
--menu "$(gettext "Please select one")" 0 0 0 $@ --stdout
}
more_menu() {
# Options for the submenu go in the options array, see options+=
declare -a options
if [[ "${1##*.}" == "gpg" ]]; then
options+=("decrypt" "$(gettext "Decrypt")")
else
options=("encrypt" "$(gettext "Encrypt")")
fi
options+=(
"edit" "$(gettext "Edit")"
"delete" "$(gettext "Delete")")
local action="$(dialog --backtitle "$(gettext "Use the up and down arrow keys to find the option you want, then press enter to select it.")" \
--no-tags \
--menu "$(gettext "Please select one")" \
0 0 0 ${options[@]} --stdout)"
case "$action" in
"decrypt") decrypt_note "$1";;
"delete") delete_note "$1";;
"edit") edit_note "$1";;
"encrypt") encrypt_note "$1";;
*) return;;
esac
}
add_note() {
# Notes are named based on number of seconds since 1970.
# If the note exists it is incremented until the name is available.
local noteName=$(date '+%s')
while [ -f "${xdgPath}/notestorm/notes/${noteName}.md" ]; do
((noteName++))
done
$editor "${xdgPath}/notestorm/notes/${noteName}.md"
if [ -f "${xdgPath}/notestorm/notes/${noteName}.md" ]; then
infobox "Note added."
else
infobox "Note canceled."
fi
}
decrypt_note() {
#returns the note to its unencrypted state.
if gpg -o "${1%.gpg}" -d "$1" ; then
rm -f "$1"
infobox "$(gettext "Note decrypted and saved as: ") \"${1%.gpg}\""
return
fi
infobox "$(gettext "Decryption failed.")"
}
delete_note() {
local text="$(head -1 "$1")"
if [[ "${1##*.}" == "gpg" ]]; then
local answer="$(yesno "$(gettext "Really delete note named:") \"${1##*/}\"?")"
else
local answer="$(yesno "$(gettext "Really delete note starting with text:") \"$text\"?")"
fi
if [ "$answer" == "Yes" ]; then
rm -f "$1"
infobox "$(gettext "Note deleted.")"
else
infobox "$(gettext "Action canceled.")"
fi
}
display_note() {
if [[ "$1" =~ [0-9]+ ]]; then
mapfile -t notes < <(find "$xdgPath/notestorm/notes" -type f -iname '*.md')
if [[ $1 -ge "${#notes[@]}" ]]; then
gettext "The requested note could not be found. Try using -l to get a list."
echo "($1)"
exit 1
fi
cat "${notes[1]}"
exit 0
fi
if [[ "${1##*.}" == "gpg" ]]; then
gpg -d "$1" | markdown | eval "$pager"
else
markdown "$1" | eval "$pager"
fi
}
edit_note() {
local oldMd5="$(md5sum "$1")"
$editor "$1"
local newMd5="$(md5sum "$1")"
if [ "$oldMd5" != "$newMd5" ]; then
infobox "$(gettext "Changes saved.")"
else
infobox "$(gettext "Changes discarded.")"
fi
}
encrypt_note() {
# function requires exactly 1 argument.
if [[ $# -ne 1 ]]; then
return
fi
# Get a human readable name for the new note.
local noteName="$(inputbox "$(gettext "Please enter a descriptive name for the encrypted note:")")"
if [[ -z "$noteName" ]]; then
# No name supplied, so return
return
fi
# Encrypt the given file and if successful, remove the original.
if gpg -o "${1%/*}/${noteName%.md*}.md.gpg" -c "$1" ; then
rm -f "$1"
infobox "$(gettext "Note encrypted.")"
return
fi
infobox "$(gettext "Encryption failed.")"
}
list_notes() {
# Return a numbered list of unencrypted notes.
mapfile -t notes < <(find "$xdgPath/notestorm/notes" -type f -iname '*.md')
for i in "${!notes[@]}" ; do
echo "$i: ${notes[i]##*/}"
done
}
original_note() {
# If no notes are present this will try to copy the README.md file into the notes directory so the menu won't fail.
mapfile -t notes < <(find "$xdgPath/notestorm/notes" -type f -iname '*.md' -o -iname '*.gpg')
if [[ "${#notes[@]}" -gt 0 ]]; then
return
fi
if [[ -f "README.md" ]]; then
cp "README.md" "${xdgPath}/notestorm/notes/README.md"
elif [[ -f "/usr/share/doc/notestorm/README.md" ]]; then
cp "README.md" "${xdgPath}/notestorm/notes/README.md"
else
echo "The README file is missing. Pleae visit the notestorm git page for usage information." > "${xdgPath}/notestorm/notes/README.md"
fi
}
# Configuration section
# Available arguments in both long and short versions stored in associative array.
declare -A argList=(
[l]="list"
[n]="new")
# Make the args a continuous string.
short="${!argList[*]}"
short="${short// /}"
long="${argList[*]}"
long="${long// /}"
editor="${EDITOR:-nano}"
messageTimeout=${messageTimeout:-1}
# Set pager
if command -v w3m &> /dev/null ; then
pager="w3m -T text/html"
elif command -v elinks &> /dev/null ; then
pager="elinks -force-html"
elif command -v lynx &> /dev/null ; then
pager="lynx -force_html"
else
pager="${PAGER:-more}"
fi
xdgPath="${XDG_CONFIG_HOME:-$HOME/.config}"
# set up the notes directory
mkdir -p "${xdgPath}/notestorm/notes"
original_note
# Keep track of the number of arguments passed to the program.
argNum=$#
# Settings to improve accessibility of dialog.
export DIALOGOPTS='--insecure --no-lines --visit-items'
# Dialog custom configuration file
export DIALOGRC="${xdgPath}/notestorm/dialogrc"
# Generate dialogrc if it does not exist.
if ! [ -e "$DIALOGRC" ]; then
dialog --create-rc "$DIALOGRC"
# Add keybindings to dialogrc
echo >> "$DIALOGRC"
echo "# Edit keybinding" >> "$DIALOGRC"
echo "bindkey menubox ^E EXTRA" >> "$DIALOGRC"
fi
# Code section
# If the only arg is a number, display that note, then exit.
if [[ "$*" =~ [0-9]+ ]]; then
display_note $*
fi
# Parse non-numeric command line args.
if ! options=$(getopt -o "$short" -l "$long" -n "notestorm" -- "$@"); then
gettext -e "Usage: notestorm launch interactive session.\n"
gettext -e -- "-n or --new add a new note without opening an interactive session.\n"
echo
gettext -e "You can use markdown syntax in notes.\n"
gettext -e "Notes are named numerically. they can be renamed and will still show up so long as they end with a .md extension.\n"
gettext -e "To get a numbered list, instead of the interactive menu, use the -l option.\n"
gettext -e "To show a note without using the interactive interface and pager, give the notes number as the only argument.\n"
gettext "Notes are saved in "
echo "${xdgPath}/notestorm/notes"
exit 1
fi
eval set -- "$options"
while [ $# -gt 0 ]; do
case "$1" in
"-n"|"--new") add_note;;
"-l"|"--list") list_notes;;
esac
shift
done
# If there were args, the program was controled by those and we do not need the menu.
if [ $argNum -gt 0 ]; then
exit 0
fi
while [ "$action" != "exit" ]; do
# Get a list of notes
mapfile -t notes < <(find "$xdgPath/notestorm/notes" -type f -iname '*.md' -o -iname '*.gpg')
# Generate the note menu:
unset noteMenu
declare -a noteMenu
for i in "${notes[@]}" ; do
noteMenu+=("$i")
if [[ "${i##*.}" == "gpg" ]]; then
noteMenu+=("$(gettext "[enc]") $(basename "${i%.md.gpg}")")
else
noteMenu+=("$(head -1 "${i}")")
fi
done
# Create menu of actions and notes.
ifs="$IFS"
IFS=$'\n'
action="$(menulist "${noteMenu[@]}")"
dialogCode=$?
IFS="$ifs"
if [ -z "$action" ]; then
action="exit"
fi
# Run selected action
case "${action}" in
"add_note") add_note;;
"exit") exit 0;;
*)
case $dialogCode in
0) display_note "${action}";;
2) more_menu "${action#HELP }";;
3) add_note;;
esac;;
esac
done
exit 0