#!/usr/bin/env bash # Source: https://github.com/agorf/jot # The MIT License # # Copyright (c) 2020 Angelos Orfanakos # # Permission is hereby granted, free of charge, to any person obtaining a copy # of # this software and associated documentation files (the "Software"), to # deal in the Software without restriction, including without limitation the # rights to use, copy, modify, merge, publish, distribute, sublicense, and/or # sell copies of the Software, and to permit persons to whom the Software is # furnished to do so, subject to the following conditions: # # The above copyright notice and this permission notice shall be included in all # copies or substantial portions of the Software. # # THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR # IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, # FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE # AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER # LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, # OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE # SOFTWARE. JOT_HOME=${JOT_HOME:-$HOME/jot} JOT_HOOKS=${JOT_HOOKS:-$JOT_HOME/hooks} JOT_EDITOR=${JOT_EDITOR:-$EDITOR} JOT_DATE_FMT=${JOT_DATE_FMT:-%a, %d %b %Y} JOT_EXT=${JOT_EXT:-txt} DATE_FILE_FMT='%Y-%m-%d' VERSION=0.8.0 SOURCE_URL=https://raw.githubusercontent.com/agorf/jot/master/jot # Drop trailing / JOT_HOME=${JOT_HOME%/} JOT_HOOKS=${JOT_HOOKS%/} mkdir_home() { mkdir -p "$JOT_HOME" } prev_day_with_notes() { local today_date=$(date +"$DATE_FILE_FMT") local file=$(ls -1 "$JOT_HOME"/*.$JOT_EXT | grep -v "$today_date" | tail -n 1) date_from_file "$file" } prev_workday() { day_of_week=$(date +%w) if [[ $day_of_week == '0' ]]; then days_offset=2 elif [[ $day_of_week == '1' ]]; then days_offset=3 else days_offset=1 fi date -d "today - $days_offset days" +"$DATE_FILE_FMT" } next_workday() { day_of_week=$(date +%w) if [[ $day_of_week == '5' ]]; then days_offset=3 elif [[ $day_of_week == '6' ]]; then days_offset=2 else days_offset=1 fi date -d "today + $days_offset days" +"$DATE_FILE_FMT" } parse_date() { local date=$1 case $date in td|'') date=today ;; yd) date=yesterday ;; tm) date=tomorrow ;; esac case $date in prev) prev_day_with_notes ;; pwd) prev_workday ;; nwd) next_workday ;; all) echo '*' ;; dir) ;; *) date -d "$date" +"$DATE_FILE_FMT" ;; esac } date_from_file() { basename "$1" .$JOT_EXT } file_from_date() { date=$(parse_date "$1") [[ -n "$date" ]] && echo "$JOT_HOME/$date.$JOT_EXT" } hook() { local hook_path="$JOT_HOOKS/$1" [[ -x "$hook_path" ]] && sh -c "cd $JOT_HOME && $hook_path" } usage() { cat <<-EOF Usage: jot [ [...]] jot [ -- [...]] command: e[dit] edit notes for one or more dates (default) c[opy] copy notes for a date l[ist] list notes help print this text version print the version update update jot to latest version date: td or today yd or yesterday tm or tomorrow prev previous day with notes pwd previous working day nwd next working day "3 days ago" "2 days" 2 days ahead 2020-04-28 absolute date all all dates dir target \$JOT_HOME Any other command will be executed and passed any notes after -- For example: jot rm -v -- 2020-04-28 jot cat -- yd td jot less -- tm jot ls -lh jot grep --color Jane -- all jot git status -- dir EOF } execute() { hook 'pre' local seen_separator=false local seen_dir=false local cmd='' local files='' for arg in "$@"; do if [[ $seen_separator == 'false' ]]; then if [[ "$arg" == '--' ]]; then seen_separator=true else cmd="$cmd$arg " fi else [[ "$arg" == 'dir' ]] && seen_dir=true local file=$(file_from_date "$arg") files="$files $file" fi done if [[ -z "$files" ]]; then files=$(file_from_date td) fi if [[ "$seen_dir" == 'true' ]]; then files='' fi sh -c "cd $JOT_HOME && $cmd $files" [[ $? -eq 0 ]] && hook 'post' } set_copy_command() { if [[ -z "$JOT_COPY" ]]; then for cmd in xclip xsel xcut sselp; do if command -v $cmd >/dev/null 2>&1; then JOT_COPY=$cmd break fi done fi } copy() { set_copy_command if [[ -z "$JOT_COPY" ]]; then echo 'Please set JOT_COPY to an X selection manipulation program' exit 1 fi local date=$(parse_date "$1") local display_date=$(date -d "$date" +"$JOT_DATE_FMT") local file=$(file_from_date "$date") if [[ ! -f "$file" ]]; then echo "There are no notes for $display_date" exit 1 fi grep -v '^#' "$file" | sed -z '$ s/\n$//' | $JOT_COPY if [[ $? -eq 0 ]]; then local lines=$(grep -v '^#' "$file" | wc -l) echo "Copied $lines line(s) to clipboard for $display_date" fi } edit() { execute "$JOT_EDITOR" "$JOT_EDITOR_OPTS" -- "$@" } list() { local today_date=$(date +"$JOT_DATE_FMT") for file in "$JOT_HOME"/*.$JOT_EXT; do file_date=$(date_from_file "$file") date=$(date -d "$file_date" +"$JOT_DATE_FMT" | tr -d '\n') lines=$(wc -l "$file" | cut -d ' ' -f 1) if [[ "$date" == "$today_date" ]]; then echo -n '> ' else echo -n ' ' fi echo -n "$date" printf ' (%2d) ' $lines echo "$file" done } update() { echo "Current version: $VERSION" if command -v wget >/dev/null 2>&1; then wget -qO - $SOURCE_URL >$0 elif command -v curl >/dev/null 2>&1; then curl -s $SOURCE_URL >$0 else echo "wget or curl must be installed in order to update" fi echo "New version: $(jot version)" } case $1 in e|edit|'') mkdir_home shift edit "$@" ;; c|copy) shift copy "$1" ;; l|list) mkdir_home list ;; update) update ;; version) echo $VERSION ;; help|-h|--help) usage ;; *) mkdir_home execute "$@" ;; esac