blog-weight.nix (2165B)
1 { 2 pkgs, 3 vars, 4 ... 5 }: let 6 blog-weight = pkgs.writeShellScriptBin "blog-weight" '' 7 # variables 8 weight_filename="$HOME/vault/src/blog.${vars.user.domain}/content/weight.md" 9 if [ "$1" = "date" ]; then 10 printf "Writing empty dates... " 11 page_source="$(head -n -1 "$weight_filename")" 12 previous_date="$(printf %s "$page_source" | awk -F, 'END{print $1}')" 13 sequence_count="$((($(date --date="$(date +%F)" +%s) - $(date --date="$previous_date" +%s)) / (60 * 60 * 24)))" 14 { 15 printf "%s\\n" "$page_source" 16 printf "%s" "$(for i in $(seq $sequence_count); do printf "%s,\\n" "$(date -d "$previous_date+$i day" +%F)"; done)" 17 printf "\\n</pre></details>" 18 } >"$weight_filename" 19 printf "done\\n" 20 exit 0 21 fi 22 printf "Drawing graph... " 23 weight_rawdata="$(awk '/<pre>/{flag=1; next} /<\/pre>/{flag=0} flag' "$weight_filename" | sort -u)" 24 weight_dateinit="$(awk '/date:/ {print $2}' "$weight_filename")" 25 grep "^2[0-9]\{3\}-" <<<"$weight_rawdata" >temp.dat 26 ${pkgs.gnuplot}/bin/gnuplot <<-EOF 27 set grid 28 set datafile separator comma 29 set xlabel "Month" 30 set xdata time 31 set timefmt "%Y-%m-%d" 32 set xtics format "%b" 33 set ylabel "Weight (kg)" 34 set key off 35 set term svg font 'sans-serif,12' 36 set samples 200 37 set output "temp.svg" 38 set border lc rgb "#666666" 39 set xtics textcolor rgb "#AAAAAA" 40 set ytics textcolor rgb "#AAAAAA" 41 set xlabel textcolor rgb "#AAAAAA" 42 set ylabel textcolor rgb "#AAAAAA" 43 set grid lc rgb "#444444" 44 plot "temp.dat" using 1:2 smooth bezier with lines lc rgb "#6CB6FF" lw 2 45 EOF 46 printf "done\\nWriting page... " 47 { 48 printf -- "---\\ntitle: Weight\\nlayout: single\\ndate: %s\\nlastmod: %(%Y-%m-%dT%H:%M:00)T\\n---\\n\\n" "$weight_dateinit" -1 49 printf "%s\\n\\n" "$(${pkgs.scour}/bin/scour -i temp.svg --strip-xml-prolog --remove-descriptive-elements --create-groups --enable-id-stripping --enable-comment-stripping --shorten-ids --no-line-breaks)" 50 printf "<details><summary>Raw data</summary>\\n<pre>\\n%s\\n</pre></details>" "$weight_rawdata" 51 52 } >"$weight_filename" 53 printf "done\\nCleaning up... " 54 rm temp.{dat,svg} 55 printf "done\\n" 56 57 ''; 58 in { 59 environment.systemPackages = [blog-weight]; 60 }