aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorFlorian Fischer <florian.fl.fischer@fau.de>2016-03-22 17:22:26 +0100
committerFlorian Fischer <florian.fl.fischer@fau.de>2016-03-22 17:22:26 +0100
commit552edcc7f97b8eddbd6a97481b4d04f0704748ce (patch)
tree7f97f7a9b5b5af8e2af8cbf8c213aadc27b9dfe9
parent1083cfd3fdee3185406c9b41680a6db250584405 (diff)
downloadmark-552edcc7f97b8eddbd6a97481b4d04f0704748ce.tar.gz
mark-552edcc7f97b8eddbd6a97481b4d04f0704748ce.zip
add function to get information out of a mark store
__mark_get: + function to get marks and information about marks + arguments: + "-y= | -m= | -d=" date options + "-s=" store location + "-r=" range of days + behaviour: + if no specific date argument is given the current date is used. + if day is set to 0 a small summary of the month is printed + if range is given all marks in the range are printed
-rw-r--r--functions/__mark_get.fish127
1 files changed, 127 insertions, 0 deletions
diff --git a/functions/__mark_get.fish b/functions/__mark_get.fish
new file mode 100644
index 0000000..ecd3d91
--- /dev/null
+++ b/functions/__mark_get.fish
@@ -0,0 +1,127 @@
+function __mark_get --description 'extract information from a mark_store'
+ # defaults
+ set mark_store $__mark_store
+ set year (date "+%Y")
+ set month (math (date "+%m") "*1")
+ set day (math (date "+%d") "*1")
+ # range of marks retunrned around date
+ set range 0
+
+ # parse argv
+ for i in (seq 1 (count $argv))
+ if string match -q -- "-y=*" "$argv[$i]"
+ set year (string replace -- "-y=" "" $argv[$i])
+ else if string match -q -- "-m=*" "$argv[$i]"
+ set month (string replace -- "-m=" "" $argv[$i])
+ else if string match -q -- "-d=*" "$argv[$i]"
+ set day (string replace -- "-d=" "" $argv[$i])
+ else if string match -q -- "-s=*" "$argv[$i]"
+ set mark_store (string replace -- "-s=" "" "$argv[$i]")
+ else if string match -q -- "-r=*" "$argv[$i]"
+ set range (string replace -- "-r=" "" "$argv[$i]")
+ else
+ echo unrecognised option: $argv[$i] >&2
+ return 1
+ end
+ end
+
+ if not test -f $mark_store
+ return 1
+ end
+
+ set marks (__mark_print_block store $mark_store)
+ set marks (string split " " $marks)
+
+ # find year in marks
+ for i in (seq 1 (count $marks))
+ if string match -q -r "^$year:\$" $marks[$i]
+ set year_offset $i
+ break
+ end
+ end
+
+ # return if year not in the data
+ if not set -q year_offset
+ echo year $year not found in the store >&2
+ return 1
+ end
+
+ if test $day -eq 0
+ # collect all marks in month
+ set marks_list
+ for m in (string split "" (string split ':' $marks[(math $month+$year_offset)])[2])
+ # check if $m is a mark
+ if test $m != '?'
+ set incremented false
+ # check if a count for $m is already stored in $marks_list
+ for e in $marks_list
+ if test $e = mark_$m
+ set incremented true
+ # build count variable name
+ set cur mark_$m
+ # increment mark count
+ set $cur (math $$cur + 1)
+ end
+ end
+ # add count for $m if it was not in $marks_list
+ if test $incremented = false
+ set mark_$m 1
+ set marks_list $marks_list mark_$m
+ end
+ end
+ end
+ # print result
+ echo (count $marks_list) different marks found:
+ for e in $marks_list
+ echo (string split '_' $e)[2]:$$e
+ end
+ else
+ # get range
+ if test $range -ne 0
+ set range (math $range - 1)
+ set start_s (date -d "$year-$month-$day - $range days" +%s)
+ set start_d (string split "-" (date -d @$start_s +%Y-%m-%d))
+
+ set year_diff (math $year - $start_d[1])
+ set first_year_offset (math "$year_offset + 12*$year_diff")
+
+ if test $first_year_offset -gt (count marks)
+ echo range is to big for available data! >&2
+ return 1
+ end
+
+ # collect relevant marks
+ # $s = oldest month to add
+ set s $start_d[2]
+ set e 12
+ for y in (seq $first_year_offset -12 $year_offset)
+ # set e if last year is reached
+ if test $y -eq $year_offset
+ set e $month
+ end
+ for i in (seq $s $e)
+ set range_marks $range_marks (string split ":" $marks[(math $y + $i)])[2]
+ end
+ # reset s -> start with january
+ set s 0
+ end
+
+ set marks (string split "" $range_marks[1])
+
+ if test (count $range_marks) -gt 1
+ set marks $marks[$start_d[3]..-1]
+ for i in (seq 2 (math (count $range_marks) - 1))
+ set marks $marks (string split "" $range_marks[$i])
+ end
+ set tmp_marks (string split "" $range_marks[-1])
+ set marks $marks $tmp_marks[1..$day]
+ else
+ set marks $marks[$start_d[3]..$day]
+ end
+ echo (string join "" $marks)
+ else
+ set marks (string split "" (string split ":" $marks[(math $month+$year_offset)])[2])
+ echo $marks[$day]
+ end
+ end
+end