type-theory/scripts/build-table
2024-07-22 14:43:01 -05:00

121 lines
3.6 KiB
Plaintext
Executable file

#!/usr/bin/env nu
let chapters = {
2: 7,
3: 9,
4: 19,
6: 15,
7: 21,
8: 22,
}
let gradients = [
[0, [60, 0, 0]],
[0.65, [180, 180, 0]],
[1, [0, 120, 0]],
]
let formatColor = {|color| $"rgb\(($color.0), ($color.1), ($color.2)\)" }
let interpColor = {|p|
let next = ($gradients
| enumerate
| skip until { $p <= $in.item.0 }
| get index).0
if $next == 0 { return (do $formatColor ($gradients.0.1)) }
let prev = ($gradients | get ($next - 1))
let next = ($gradients | get $next)
let pp = ($p - $prev.0) / ($next.0 - $prev.0)
let color = ($prev.1 | zip $next.1 | each {|a| $a.0 * (1 - $pp) + $a.1 * $pp})
do $formatColor $color
}
let viz = { |k, n, cell|
let ratio = $k / $n
let percent = $ratio * 100 | math round --precision 1
let bgColorOpacity = if $k == 0 { 50 } else { 100 }
let bgColor = if ($n == 0 or $k == 0) { "gray" } else { $"color-mix\(in srgb, (do $interpColor $ratio) ($bgColorOpacity)%, transparent\)" }
let textColor = if $k == $n { "gold" } else if $k == 0 { "lightgray" } else { "white" }
let isExercise = $cell.0.section == "Exercise"
let percentStr = $"<small style=\"font-size: 0.7rem\">($percent)%</small>"
$"<div class=\"cell\" style=\"
background-color: ($bgColor);
color: ($textColor);
\">
<span>($k)&nbsp;/&nbsp;($n)</span>
(if ($isExercise) { $percentStr })
</div>" | str replace -a "\n" ""
# $n | math round --precision 1
}
let vizCh = { |n, table|
let noExercises = $table | where type != "Exercise"
let completed = $noExercises | where completed == "x" | length
let total = $noExercises | where type != "Exercise" | length
let ratio = if $total != 0 { $completed / $total } else { 0 }
let percent = ($ratio * 100 | math round --precision 1)
let color = if $n == 0 { "gray" } else { do $interpColor $ratio }
let textColor = if $completed == $total { "gold" } else { "white" }
let url = $"https://git.mzhang.io/school/type-theory/issues/($chapters | get $n)"
$"<a data-ch=\"($n)\" class=\"cell\" style=\"
background-color: ($color);
color: ($textColor);
\" href=\"($url)\" target=\"_blank\">
<h3>($n)</h3>
<small>($completed) / ($total)</small>
<small style=\"font-size: 0.5rem;\">($percent)%</small>
</a>" | str replace -a "\n" ""
}
let vizChapter = { |n|
let nStr = $n | into string
let url = $"https://git.mzhang.io/api/v1/repos/school/type-theory/issues/($chapters | get $nStr)"
let table = curl -s $url
| jq -r .body
| lines
| rg -o $"\\[\(x| \)\\] \(.*\) \(\\d+\(\\.\\d+\)*\)" -r "$1,$2,$3"
| from csv --noheaders
| rename completed type number
| insert section { if $in.type == "Exercise" { "Exercise" } else { $in.number | split row "." | get 1 } }
let table2 = $table
| group-by section
| transpose
| each { $in | update column1 {
do $viz ($in | where completed == "x" | length) ($in | length) $in
} }
| sort-by -n column0
let newColumn1 = { column0: "Ch", column1: (do $vizCh $n $table) }
let newColumn2 = { column0: "Chnum", column1: $n }
let table3 = if ($table2 | is-empty) {
[$newColumn1, $newColumn2]
} else {
$table2 | prepend $newColumn1 | prepend $newColumn2
}
$table3
| transpose -r
}
$chapters
| columns
| each { do $vizChapter $in }
| reduce {|it, acc| $acc | append $it }
| sort-by Chnum
| reject Chnum
| transpose
| sort-by -n column0
| transpose -r
| move Ch --before 0
| to md
| tee { save -f html/src/generated/Progress.md }
# open breakdown.json
# | transpose
# | each {|row| [$row.column0, $row.column1.ratioCompleted] }