# Pantry can be much easier to use when you have shell functions to
# help you. These are the functions I use. They might be usable
# as-is, but I encourage you to modify them for your needs. These
# are not fully documented--that is, you'll need to examine the
# functions themselves to know exactly what they do and how they
# work. The interface to these functions is quite non-standard--for
# instance, the pq function's first non-option argument is required,
# while the second two are optional. This works, but is hacky.
#
# These functions are written for the Z Shell. Every major Linux
# distribution makes zsh available in its package repository, and it
# is a standard part of recent releases of Mac OS X. You can also
# get zsh from its home page, http://www.zsh.org
#
# These functions would work in bash, with modifications. Most
# importantly, zsh and bash have different word-splitting behavior.
#
# Paths to your master, quick, and diary files - change so it fits
# your site:
master=/home/massysett/pantry-data/master
quick=/home/massysett/pantry-data/quick
diary=/home/massysett/pantry-data/diary

# use shortList when printing a brief nutrient report--for
# example, when using the pq function. Use longList
# when printing a long nutrient report.
shortList=eatless-short
longList=eatless

function pq {
    # This function is used to quickly add files to a diary. It
    # looks up foods by name in your quick file, and then adds the
    # food to your diary file, with the date trait modified to
    # today's date. The meal trait is modified to the argument of
    # the -M option, if you used it.
    #
    # This function is easily used with aliases, so that you can
    # type (for example) 'ab milk' to invoke 'pq -M Breakfast milk'.
    #
    # arguments:
    # $1 (required) food name, or group name
    # $2 (optional) quantity
    # $3 (optional) units
    #
    # options:
    # -g look up by group name, not by food name
    # -R use refuse
    # -M change meal 
    # -K change by calories
    local panCmd useGroup meal matches
    panCmd=(pantry --ignore-case --add $diary --c-date $(date +%F)
	--print traits-nuts-blank --nutrient-list $shortList --auto-order)
    while getopts 'gRM:K:' arg; do
	case $arg in
	    (g) 
		useGroup=true;;
	    (R)
		panCmd=($panCmd --refuse);;
	    (M)
		meal=$OPTARG;;
            (K)
                calories=$OPTARG;;
	esac
    done
    if [[ -n $meal ]]; then
	panCmd=($panCmd --c-meal $meal)
    fi
    if [[ -n $calories ]]; then
        panCmd=($panCmd --by-cal $calories)
    fi

    shift $(( $OPTIND - 1 ))
    if [[ $# -lt 1 || $# -gt 3 ]]; then
	echo 'pq: invalid number of options entered.' 1>&2
	return
    fi

    if [[ $useGroup == true ]]; then
	panCmd=($panCmd --group $1)
    else
        matches=$(pantry -in $1 -p names $quick)
        matches=(${(f)matches})
	if [[ -z ${matches} ]]; then
	    echo "No matches." 1>&2
	    return
	elif [[ ${#matches} -ne 1 ]]; then
	    echo "More than one match. Matches:" 1>&2
	    echo ${(F)matches} 1>&2
	    return
	fi
	panCmd=($panCmd --name $1)
    fi
    shift
    case $# in
	(2)
	    panCmd=($panCmd --c-unit $2);&
	(1)
	    panCmd=($panCmd --c-qty $1);;
    esac

    $panCmd $quick
    pantry -d $(date +%F) -p sum -l $shortList $diary
}

# Aliases to use with the pq function.
# to use these aliases with the -g, -R, or -K options, just
# type them in on invocation, e.g. 'ab -R apples 2 large'
# or 'ab -g yogurt'
alias ab="pq -M Breakfast"
alias al="pq -M Lunch"
alias ad="pq -M Dinner"

function plookup {
    # Look up a food in the file given in $1.
    # Second argument, required, is the name.
    # Third argument, optional, is the group.
    #
    # Designed to be used with GNU Screen. Also, best used with
    # aliases, which follow this function definition.
    local panCmd filename
    if [[ $TERM != screen ]]; then
	echo 'start screen first.' 1>&2
	return
    fi
    filename=${1:?no filename given}
    shift
    panCmd=( pantry -in ${1:?name not given} -p paste )
    shift
    if [[ -n $1 ]]; then
	panCmd=($panCmd -g $1)
	shift
    fi

    $panCmd $@ $filename | sort | less
}

# aliases to use with plookup function
alias pmfind="plookup $master"
alias pqfind="plookup $quick"

# Alias for abbreviation for grams. This way I type G instead of
# '^g$' when I need to change a food's unit trait to g.
# This is a global alias. zsh has this feature; bash does not.
# Perhaps you could do this in bash using shell parameters.
alias -g G='\^g\$'

function pnuts {
    # using results pasted in to the command line from paste
    # report, get nutrient report.
    # $1-$5 come form paste report.
    # $6 is qty.
    if [[ $# -ne 6 ]]; then
	echo 'Invalid number of arguments.' 1>&2
	return
    fi
    pantry $@[1,5] -Q $6 -p tr-nu $master
}

function pqadd {
    # using results pasted in to the command line from paste
    # report, add food to quick file from master file.
    # $1-$5 come form paste report.
    # $6 is qty.
    if [[ $# -ne 6 ]]; then
	echo 'Invalid number of arguments.' 1>&2
	return
    fi
    pantry $@[1,5] -Q $6 -p tr -a $quick $master
}

function ptoday {
    # get report of today's food intake. You'll want to change the
    # -l option to meet your needs.
    pantry -d $(date +%F) -p tr-nu-bl-su -l $longList -s mo $diary | less
}

# This binds ctrl-t to insert today's date; very helpful when
# getting reports about today's food intake, or if I make a mistake
# and need to delete or edit a food, or...
#
# No idea how you'd do this in bash.
function today {
    now=$(date +%F)
    RBUFFER="${now}${RBUFFER}"
    (( CURSOR += 10 ))
}
zle -N today
bindkey '\C-t' today

