Skip to content

Commit

Permalink
Add timelog start
Browse files Browse the repository at this point in the history
This closes #65
  • Loading branch information
Lilja committed Aug 11, 2017
1 parent 98aa501 commit 69b5788
Show file tree
Hide file tree
Showing 2 changed files with 155 additions and 5 deletions.
120 changes: 115 additions & 5 deletions bin/timelog
Original file line number Diff line number Diff line change
Expand Up @@ -143,6 +143,64 @@ function week_stats {
fi
}

function save_time_to_disk() {
# format timestamp-of-day;time;project_id
# $1 = kind of operation, if break or start
# $2 = time in 'hhmm' format, like 1220 for 12:20
# $3 = YYYY-mm-dd
operation="$1"
timestamp="$2"

if [ -z "${operation:+foo}" ]; then
echo "Error, no operation supplied"
exit 1
fi
if [ -z "${timestamp:+foo}" ]; then
echo "Error, no timestamp supplied"
exit 1
fi
if [ -z "${proj_name:+foo}" ]; then
echo "Error, no project supplied"
exit 1
fi

if [ -f "$log_path/saved_log_times" ]; then
phrase="${proj_name};${operation};${timestamp}"
logger_debug "Saving '$phrase' to '$log_path/saved_log_times'"
echo "$phrase" >> "$log_path/saved_log_times"
exit 0
else
echo "No such file: saved_log_times"
exit 1
fi
}

function read_start_time_for_disk() {
# format timestamp-of-day;time;project_id
# $1 = kind of operation, if break or start
# $2 = time in 'hhmm' format, like 1220 for 12:20
# $3 = YYYY-mm-dd
operation="$1"

if [ -z "${proj_name:+foo}" ]; then
exit 1
fi
if [ -z "${operation:+foo}" ]; then
exit 1
fi

if [ -f "$log_path/saved_log_times" ]; then
timestamp=$(grep "^$proj_name;$operation" "$log_path/saved_log_times" | tail -n1 | tr ';' '\n' | tail -n1)
if [ ! -z "$timestamp" ]; then
date +%H%M -d "$timestamp"
else
exit 1
fi
else
exit 1
fi
}

function weekly_stats_days {
# $1 = Contents of the log file
# $2 = what week to search for
Expand Down Expand Up @@ -257,11 +315,19 @@ function get_project_from_id {
echo "$all_projects" | grep "\[$1\]" | grep -o ':\ .*\[' | sed 's#^:\ *\(.*\)\[$#\1#' | sed 's/*//;s/ *$//'
}

function has_illegal_characters {
echo "$1" | grep -q '\(;\|\[\|\]\)\+'
}


function create_project {
echo "Creating a new project"
echo "What would you like to call it?"
read -r project_name
if has_illegal_characters "$project_name"; then
echo "You can't have brackets([]) in the project name"
exit 1
fi

echo "What is an ID that you would call it?"
echo "(This is used to specify which project you would like to submit time to)"
Expand Down Expand Up @@ -315,10 +381,19 @@ function create_project {

function init_program {
logger_debug "Initalizing the program. Creating folder and config."
mkdir -p "$log_path"; logger_debug "Created '$log_path'";
mkdir "$log_path/def/"; logger_debug "Created '$log_path/def'";
touch "$log_path/config"
echo "default_project=" > "$log_path/config"
if [ ! -d "$log_path" ]; then
mkdir -p "$log_path"; logger_debug "Created '$log_path'";
fi
if [ ! -d "$log_path/def" ]; then
mkdir "$log_path/def/"; logger_debug "Created '$log_path/def'";
fi
if [ ! -d "$log_path/saved_log_times" ]; then
touch "$log_path/saved_log_times"; logger_debug "Created '$log_path/saved_log_times'";
fi
if [ ! -f "$log_path/config" ]; then
touch "$log_path/config"
echo "default_project=" > "$log_path/config"
fi
}

function get_default_project {
Expand All @@ -334,7 +409,8 @@ function get_project {
}

function is_program_inited {
if [ -f $log_path/config ] ; then exit 0
if [ -f "$log_path/config" ] && [ -d "$log_path/def/" ] && [ -f "$log_path/saved_log_times" ]; then
exit 0
else exit 1
fi
}
Expand Down Expand Up @@ -683,6 +759,20 @@ while [[ $# -ge 1 ]]; do
esac
shift
;;
start)
logger_debug "Start matched"
log_start_time="y"
specify_project="y"
proj_name=$(get_project_from_id "$2")
action="log start time for"
if [ ! -z "$proj_name" ]; then
logger_debug "Project id specified from arguments"
specify_project="n"
id_matched="y"
shift
fi
shift
;;
edit)
case "$2" in
logs)
Expand Down Expand Up @@ -801,6 +891,15 @@ if [ ! -z "$list_projects" ]; then
fi
exit 0
fi
#############################################################
# Log start time
#############################################################
if [ ! -z "$log_start_time" ]; then
logger_debug "About to store date from now to disk"
now=$(date +%Y-%m-%d\ %H:%M)
save_time_to_disk "start" "$now"
fi


#############################################################
# Delete project
Expand Down Expand Up @@ -843,10 +942,21 @@ elif [ ! -z "$log_time" ]; then
# See if we need to prompt the user for timestamps
format="8, 800, 8.00, 8:00, 0800, 08.00, 08:00"

# Read potential start time
read_start_time=$(read_start_time_for_disk "start")
if [ $? -ne 0 ]; then
logger_debug "read_start_time_for_disk did get anything '$read_start_time'"
read_start_time=
fi

# Start time
if [[ ! -z "${maybeStart:+foo}" ]]; then
logger_debug "'$maybeStart' was a timestamp"
parsed_start_timestamp=$(parse_timestamp "$maybeStart")
elif [ ! -z "${read_start_time:+foo}" ]; then
echo "Using start time $read_start_time that is saved from earlier."
logger_debug "Using '$read_start_time' as start time"
parsed_start_timestamp=$(parse_timestamp "$read_start_time")
else
echo "What start hour:minute? (e.g. 8, 8.05, 8:10, 08:25, 0835)"
read start_timestamp
Expand Down
40 changes: 40 additions & 0 deletions test/unittest.sh
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#!/bin/bash
start=$(date +%s)

if [[ $1 = "-v" ]]; then debug="-v"; shift
else debug=""; fi
Expand Down Expand Up @@ -105,6 +106,19 @@ testCreateProjectWithFaultyParams() {
code=$?
assertTrue "Exit code for faulty create project was not 1" "[ $code -eq 1 ]"

createProjectWithParams "Test ]"
code=$?
assertTrue "Exit code for faulty create project was not 1 when having a [ in the project name" "[ $code -eq 1 ]"

createProjectWithParams "Test ["
code=$?
assertTrue "Exit code for faulty create project was not 1 when having a [ in the project name" "[ $code -eq 1 ]"

createProjectWithParams "Test ;"
code=$?
assertTrue "Exit code for faulty create project was not 1 when having a ; in the project name" "[ $code -eq 1 ]"


createProjectWithParams "Test" "test2" "40" "40d"
code=$?
assertTrue "Exit code for faulty create project was not 1" "[ $code -eq 1 ]"
Expand Down Expand Up @@ -357,6 +371,27 @@ END
deleteProject
}

testLogStart() {
createProjectWithParams "Test1" "ts1" "40" "140" "kr"

now_in_one_hour=$(date +%H%M -d "$(($(date +%H)+1))$(date +%M)")
timelog $debug --dev "$dir" start ts1 >/dev/null

timelog $debug --dev "$dir" log ts1>/dev/null << END
$now_in_one_hour
0
y
END

logs=$(timelog $debug --dev "$dir" show logs ts1 $(date +%V))
remaining_hours=$(echo "$logs" | grep -o 'You have 39 hours more to work')
worked_hours=$(echo "$logs" | grep -o 'You have worked for 1 hours')
assertTrue "Remaining hours was not 39" "[ ! -z '$remaining_hours' ]"
assertTrue "Worked hours was not 1" "[ ! -z '$worked_hours' ]"

deleteProject
}

testLogWithNote() {
createProjectTest
current_week=$(date +%V)
Expand Down Expand Up @@ -450,3 +485,8 @@ END
}

. shunit2-2.1.6/src/shunit2
end=$(date +%s)
diff=$((end-start))
minutes=$((diff/60%60))
seconds=$((diff%60))
echo "Unit tests took $minutes min(s), $seconds second(s) to run"

0 comments on commit 69b5788

Please sign in to comment.