# shellcheck shell=sh # lib/note.sh — quick notes, scoped to a target or global. Body can come from # -b, stdin, or $EDITOR. Depends on config.sh, ui.sh, db.sh, target.sh. note_add() { _tgt=""; _title=""; _body=""; _tags=""; _global=0; _havebody=0 while [ $# -gt 0 ]; do case $1 in --target|-T) pe_require_value "$@"; _tgt=$2; shift 2 ;; --title|-t) pe_require_value "$@"; _title=$2; shift 2 ;; --body|-b) pe_require_value "$@"; _body=$2; _havebody=1; shift 2 ;; --tags) pe_require_value "$@"; _tags=$2; shift 2 ;; --global|-g) _global=1; shift ;; *) [ -z "$_title" ] && { _title=$1; shift; } || pe_die "note add: unknown arg $1" ;; esac done [ -n "$_title" ] || pe_die "usage: pe note add -t [-b body] [--tags x] [--target T] [--global]" # body: -b, else piped stdin, else $EDITOR if [ $_havebody = 0 ]; then if [ ! -t 0 ]; then _body=$(cat) else _tmp=$(mktemp); ${EDITOR:-vi} "$_tmp" </dev/tty >/dev/tty 2>&1 _body=$(cat "$_tmp"); rm -f "$_tmp" fi fi if [ $_global = 1 ]; then _tid=NULL else _tid=$(pe_target_scope "$_tgt") || return 1 fi _nid=$(db_insert "INSERT INTO notes(target_id,title,body,tags) VALUES($_tid,$(db_quote "$_title"),$(db_quote "$_body"),$(db_quote "$_tags"));") || return 1 pe_ok "note #$_nid saved: $_title" } note_ls() { _tgt=""; _all=0; _glob=0 while [ $# -gt 0 ]; do case $1 in --target|-T) pe_require_value "$@"; _tgt=$2; shift 2 ;; --all|-a) _all=1; shift ;; --global|-g) _glob=1; shift ;; *) pe_die "note ls: unknown option $1" ;; esac done if [ $_all = 1 ]; then _where="" elif [ $_glob = 1 ]; then _where="WHERE n.target_id IS NULL" else _scope=$(pe_target_scope "$_tgt") || return 1 _where="WHERE n.target_id=$_scope" fi _rows=$(db_query "SELECT n.id,COALESCE(t.name,'(global)'),n.title,COALESCE(n.tags,'-'),n.updated_at FROM notes n LEFT JOIN targets t ON t.id=n.target_id $_where ORDER BY n.id;") || return 1 [ -n "$_rows" ] || { pe_empty "no notes"; return 0; } printf '%s\n' "$_rows" | pe_table "ID|TARGET|TITLE|TAGS|UPDATED" } note_show() { [ -n "$1" ] || pe_die "usage: pe note show <id>" pe_require_uint "note id" "$1" _meta=$(db_query "SELECT '# '||title||' ['||COALESCE((SELECT name FROM targets WHERE id=notes.target_id),'global')||']' FROM notes WHERE id=$1;") || return 1 [ -n "$_meta" ] || pe_die "no note #$1" printf '%s%s%s\n\n' "$(pe_fg "$PE_RP_GOLD")$(pe_bold)" "$_meta" "$(pe_reset)" db_query "SELECT body FROM notes WHERE id=$1;" || return 1 } note_edit() { [ -n "$1" ] || pe_die "usage: pe note edit <id> [-t title] [-b body] [--tags x]" pe_require_uint "note id" "$1" _nid=$1; shift _exists=$(db_query "SELECT id FROM notes WHERE id=$_nid;") || return 1 [ -n "$_exists" ] || pe_die "no note #$_nid" _set="" while [ $# -gt 0 ]; do case $1 in --title|-t) pe_require_value "$@"; _set="$_set, title=$(db_quote "$2")"; shift 2 ;; --body|-b) pe_require_value "$@"; _set="$_set, body=$(db_quote "$2")"; shift 2 ;; --tags) pe_require_value "$@"; _set="$_set, tags=$(db_quote "$2")"; shift 2 ;; *) pe_die "note edit: unknown option $1" ;; esac done [ -n "$_set" ] || pe_die "nothing to change" db_exec "UPDATE notes SET updated_at=datetime('now')$_set WHERE id=$_nid;" || return 1 pe_ok "updated note #$_nid" } note_search() { [ -n "$1" ] || pe_die "usage: pe note search <term>" _q=$(db_quote "%$1%") _rows=$(db_query "SELECT n.id,COALESCE(t.name,'(global)'),n.title FROM notes n LEFT JOIN targets t ON t.id=n.target_id WHERE n.title LIKE $_q OR n.body LIKE $_q OR n.tags LIKE $_q ORDER BY n.id;") || return 1 [ -n "$_rows" ] || { pe_empty "no match"; return 0; } printf '%s\n' "$_rows" | pe_table "ID|TARGET|TITLE" } note_rm() { [ -n "$1" ] || pe_die "usage: pe note rm <id>" pe_require_uint "note id" "$1" _exists=$(db_query "SELECT id FROM notes WHERE id=$1;") || return 1 [ -n "$_exists" ] || pe_die "no note #$1" db_exec "DELETE FROM notes WHERE id=$1;" || return 1 pe_ok "deleted note #$1" } note_main() { _verb=${1:-ls}; [ $# -gt 0 ] && shift case $_verb in add) note_add "$@" ;; ls|list) note_ls "$@" ;; show|cat) note_show "$@" ;; edit) note_edit "$@" ;; search|find) note_search "$@" ;; rm|del|delete) note_rm "$@" ;; -h|--help|help) printf 'pe note <add|ls|show|edit|search|rm>\n' ;; *) pe_die "unknown: pe note $_verb" ;; esac }