type-theory/scripts/build-table

121 lines
3.6 KiB
Text
Raw Permalink Normal View History

2024-07-10 22:03:51 +00:00
#!/usr/bin/env nu
let chapters = {
2: 7,
3: 9,
4: 19,
2024-07-11 15:15:26 +00:00
6: 15,
7: 21,
8: 22,
2024-07-10 22:03:51 +00:00
}
let gradients = [
2024-07-12 08:07:03 +00:00
[0, [60, 0, 0]],
2024-07-11 04:06:20 +00:00
[0.65, [180, 180, 0]],
2024-07-12 08:07:03 +00:00
[1, [0, 120, 0]],
2024-07-10 22:03:51 +00:00
]
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
}
2024-07-12 08:07:03 +00:00
let viz = { |k, n, cell|
let ratio = $k / $n
let percent = $ratio * 100 | math round --precision 1
2024-07-22 19:43:01 +00:00
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" }
2024-07-12 08:07:03 +00:00
let isExercise = $cell.0.section == "Exercise"
let percentStr = $"<small style=\"font-size: 0.7rem\">($percent)%</small>"
2024-07-10 22:03:51 +00:00
2024-07-11 04:06:20 +00:00
$"<div class=\"cell\" style=\"
2024-07-22 19:43:01 +00:00
background-color: ($bgColor);
2024-07-10 22:03:51 +00:00
color: ($textColor);
\">
2024-07-12 08:07:03 +00:00
<span>($k)&nbsp;/&nbsp;($n)</span>
(if ($isExercise) { $percentStr })
2024-07-10 22:03:51 +00:00
</div>" | str replace -a "\n" ""
# $n | math round --precision 1
}
2024-07-11 15:15:26 +00:00
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" }
2024-07-22 19:43:01 +00:00
let url = $"https://git.mzhang.io/school/type-theory/issues/($chapters | get $n)"
2024-07-11 15:15:26 +00:00
2024-07-22 19:43:01 +00:00
$"<a data-ch=\"($n)\" class=\"cell\" style=\"
2024-07-11 15:15:26 +00:00
background-color: ($color);
color: ($textColor);
2024-07-22 19:43:01 +00:00
\" href=\"($url)\" target=\"_blank\">
2024-07-11 15:15:26 +00:00
<h3>($n)</h3>
<small>($completed) / ($total)</small>
<small style=\"font-size: 0.5rem;\">($percent)%</small>
2024-07-22 19:43:01 +00:00
</a>" | str replace -a "\n" ""
2024-07-11 15:15:26 +00:00
}
2024-07-10 22:03:51 +00:00
let vizChapter = { |n|
let nStr = $n | into string
let url = $"https://git.mzhang.io/api/v1/repos/school/type-theory/issues/($chapters | get $nStr)"
2024-07-11 15:15:26 +00:00
let table = curl -s $url
2024-07-10 22:03:51 +00:00
| 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 } }
2024-07-11 15:15:26 +00:00
let table2 = $table
2024-07-10 22:03:51 +00:00
| group-by section
| transpose
2024-07-12 08:07:03 +00:00
| each { $in | update column1 {
do $viz ($in | where completed == "x" | length) ($in | length) $in
} }
2024-07-10 22:03:51 +00:00
| sort-by -n column0
2024-07-11 15:15:26 +00:00
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
2024-07-10 22:03:51 +00:00
| transpose -r
}
$chapters
| columns
| each { do $vizChapter $in }
| reduce {|it, acc| $acc | append $it }
2024-07-11 15:15:26 +00:00
| sort-by Chnum
| reject Chnum
| transpose
| sort-by -n column0
| transpose -r
| move Ch --before 0
| to md
2024-07-10 22:03:51 +00:00
| tee { save -f html/src/generated/Progress.md }
# open breakdown.json
# | transpose
# | each {|row| [$row.column0, $row.column1.ratioCompleted] }