#!/bin/sh ############################################################################### # # MicroJournal v0.2 # # Personal microjournaling software # (c)2011-2012, by Branko Vukelic. All rights reserved. # Code relased under GNU GPL v3 (see LICENSE) # ############################################################################### BACK_YELLOW='\e[43m' BACK_GREEN='\e[42m' FRONT_BLACK='\e[30m' RED='\e[31m' GREEN='\e[32m' YELLOW='\e[33m' NORMAL='\e[0m' VERSION=0.2 TITLE="${BACK_YELLOW}$FRONT_BLACK MicroJournal $BACK_GREEN v$VERSION $NORMAL" BASEDIR="$HOME/.microj" FULL=off AWK_SEP=' | ' SEPARATOR="---------------------------------------------------------------" # Save terminal tput smcup # Exit and restore terminal function quit { tput rmcup exit ${1:0} } # Reset journal variables function set_journalvars { JOURNALDATE=`date -d "$1" +"%A %F"` JOURNALDIR="$BASEDIR/`date -d $1 +%Y`/`date -d $1 +%m`" JOURNALFILE="$JOURNALDIR/`date -d $1 +%d`.microj" } # Wait for one character entry function anykey { read -n 1 } # Add new entry to current journal file function add_entry { set_journalvars today echo echo "`date +%H:%M` | $1" >> $JOURNALFILE } # Change the current journal dir and journal file function switch_journalfile { set_journalvars $1 # Create the journal dir if it doesn't exist if [ ! -d $JOURNALDIR ] then mkdir -p $JOURNALDIR fi } # Show menu function show_menu { echo "[r] Read today's journal entries" echo "[f] Toggle full mode (currently $FULL)" echo "[y] Read yesterday's journal entries" echo "[d] Read entries for a specific date" echo "[g] Search current date by keywords (grep)" echo "[G] Search all dates by keywords (grep all)" echo "[n] New journal entry" echo "[q] Quit" } function awk_col { awk -F $AWK_SEP '{print $'$1'};' } # Process a single line of journal using sed function process_entry { while read line; do date=`echo $line | awk_col 1` text=`echo $line | awk_col 2` echo -e "${YELLOW}${date}${NORMAL} | $text" done; } # Show journal entry: pass a string of any length to show full version function show_journal { if [ -z "$1" ] then if [ -f $JOURNALFILE ] then echo echo $SEPARATOR if [ $FULL == on ] then cat $JOURNALFILE | process_entry else cat $JOURNALFILE | process_entry fi echo $SEPARATOR echo else echo -e " ... ${RED}not found$NORMAL" echo $SEPARATOR echo fi else echo $SEPARATOR if [ -z "$2" ] then echo No results found. else echo $2 | process_entry fi echo $SEPARATOR fi } # Set default journaldir and journalfile switch_journalfile today # If there's a command line argument, add it to journal and quit if [ ! -z "$1" ] then add_entry "$1" exit 0 fi # Main loop while : do clear echo -e $TITLE echo $SEPARATOR echo -ne "${GREEN}${JOURNALDATE}$NORMAL" show_journal show_menu echo echo -n "What would you like to do? " read -n 1 -s menu_option echo case "$menu_option" in r) switch_journalfile today ;; f) if [ $FULL == on ] then FULL=off else FULL=on fi ;; y) switch_journalfile yesterday ;; d) echo echo -n "Which date? (e.g. '2011 Jul 11', '-2 days', 'next Firday') " read -r date_option switch_journalfile "$date_option" ;; g) echo echo -n "Search for: " read -r search_keywords clear echo -e "Searching today's entries for ${GREEN}${search_keywords}$NORMAL:" show_journal search "`grep -i "$search_keywords" $JOURNALFILE`" echo echo -n "Press any key to return to menu..." anykey ;; G) echo echo -n "Search for: " read -r search_keywords clear journal_files=`find $BASEDIR -name *.microj -type f` echo -e "Searching all dates for ${GREEN}${search_keywords}$NORMAL:" for search_file in $journal_files do search_result=`cat "$search_file" | grep -i "$search_keywords"` if [ ! -z "$search_result" ] then echo echo -e "In ${YELLOW}${search_file}$NORMAL" show_journal search "$search_result" fi done echo echo -n "Press any key to return to menu..." anykey ;; n) echo echo -n "Type in your entry: " read -r new_entry add_entry "$new_entry" ;; q) clear quit 0 ;; *) echo -n "Invalid option '$menu_option'. Press any key to go back to menu..." anykey ;; esac done