Improve autocompletion for t command

main
Ensar Sarajčić 2023-08-12 20:54:10 +02:00
parent b249e237b6
commit e1157e455f
2 changed files with 10 additions and 13 deletions

View File

@ -120,20 +120,15 @@ if [ -z "$TIMESHEET_LEDGER_HOME" ]; then
fi fi
time_in () { time_in () {
PROJECT=$1
COMMENT=$2
LAST_ENTRY=$(grep "$PROJECT" "$TIMESHEET_LEDGER_HOME/timesheets/$(date +%Y).journal" | tail -1 | cut -f1 -d ' ') LAST_ENTRY=$(grep "$PROJECT" "$TIMESHEET_LEDGER_HOME/timesheets/$(date +%Y).journal" | tail -1 | cut -f1 -d ' ')
if [ "$LAST_ENTRY" = "i" ]; then if [ "$LAST_ENTRY" = "i" ]; then
echo "Timer for project $PROJECT was already started. Aborting..." echo "Timer for project $PROJECT was already started. Aborting..."
exit 1 exit 1
fi fi
echo "i $(date '+%Y-%m-%d %H:%M:%S') $PROJECT $COMMENT" >> "$TIMESHEET_LEDGER_HOME/timesheets/$(date +%Y).journal" echo "i $(date '+%Y-%m-%d %H:%M:%S') $PROJECT $COMMENT" >> "$TIMESHEET_LEDGER_HOME/timesheets/$(date +%Y).journal"
} }
time_out () { time_out () {
PROJECT=$1
LAST_ENTRY=$(grep "$PROJECT" "$TIMESHEET_LEDGER_HOME/timesheets/$(date +%Y).journal" | tail -1 | cut -f1 -d ' ') LAST_ENTRY=$(grep "$PROJECT" "$TIMESHEET_LEDGER_HOME/timesheets/$(date +%Y).journal" | tail -1 | cut -f1 -d ' ')
if [ -z "$LAST_ENTRY" ]; then if [ -z "$LAST_ENTRY" ]; then
echo "Timer for project $PROJECT is not started. Aborting..." echo "Timer for project $PROJECT is not started. Aborting..."
@ -147,20 +142,18 @@ time_out () {
} }
time_bal () { time_bal () {
PROJECT=$1 ledger -f "$TIMESHEET_LEDGER_HOME/main.journal" bal $PROJECT
ledger -f "$TIMESHEET_LEDGER_HOME/main.journal" bal $1
} }
case $COMMAND in case $COMMAND in
in) in)
time_in $PROJECT $COMMENT time_in
;; ;;
out) out)
time_out $PROJECT time_out
;; ;;
bal) bal)
time_bal $PROJECT time_bal
;; ;;
*) *)
usage $COMMAND; exit 1 ;; usage $COMMAND; exit 1 ;;

View File

@ -2,6 +2,10 @@ function __fish-timesheets-available-projects
grep 'account Project:.*' "$TIMESHEET_LEDGER_HOME/config/02_accounts.dat" | cut -f2 -d ' ' grep 'account Project:.*' "$TIMESHEET_LEDGER_HOME/config/02_accounts.dat" | cut -f2 -d ' '
end end
function __fish-timesheets-running-projects
cat "$TIMESHEET_LEDGER_HOME/timesheets/$(date +%Y).journal" | grep -v ';' | awk '{ last[$4] = $1 } END { for (i in last) { if (last[i] == "i") { print i } } }'
end
function __fish-timesheets-existing-comments function __fish-timesheets-existing-comments
ledger -f "$TIMESHEET_LEDGER_HOME/main.journal" payees ^Project ledger -f "$TIMESHEET_LEDGER_HOME/main.journal" payees ^Project
end end
@ -22,7 +26,7 @@ complete -c t -x -n "__fish_seen_subcommand_from in" -n "__fish_timesheets_arg_n
complete -c t -x -n "__fish_seen_subcommand_from in" -n "__fish_timesheets_arg_number 3" -a '(__fish-timesheets-existing-comments)' complete -c t -x -n "__fish_seen_subcommand_from in" -n "__fish_timesheets_arg_number 3" -a '(__fish-timesheets-existing-comments)'
# Out # Out
complete -c t -x -n "__fish_seen_subcommand_from out" -a '(__fish-timesheets-available-projects)' complete -c t -x -n "__fish_seen_subcommand_from out" -a '(__fish-timesheets-running-projects)'
# Bal # Bal
complete -c t -x -n "__fish_seen_subcommand_from bal" -a '(__fish-timesheets-available-projects)' complete -c t -x -n "__fish_seen_subcommand_from bal" -a '(__fish-timesheets-available-projects)'