Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 6 additions & 5 deletions lib.typ
Original file line number Diff line number Diff line change
Expand Up @@ -20,9 +20,10 @@
template: (:),
// A stroke style to control the style of the default stroke, or a function taking two parameters `(x, y)` to control the stroke. The first row is the dates, and the first column is the times.
line-style: none,
datetime-format: "[year]-[month]-[day]"
date-format: "[year]-[month]-[day]",
time-format: "[hour]:[minute]"
) = {
let items = events-to-calendar-items(events, hour-range.at(0))
let items = events-to-calendar-items(events, hour-range)
let day-list = items.keys()
let days = day-list.len()
let hours = hour-range.at(1) - hour-range.at(0)
Expand All @@ -43,7 +44,7 @@
rows: (auto, ) + (minute-height,) * hours * 60 + (8pt,),
fill: white,
stroke: stroke-rule,
[], ..day-list.map(i => day(i)).map(d => (style.header)(d.display(datetime-format))),
[], ..day-list.map(i => day(i)).map(d => (style.header)(d.display(date-format))),
..array.range(hours * 60 + 1).map(y => {
array.range(days + 1).map(x => {
if x == 0 {
Expand All @@ -54,14 +55,14 @@
} else []
} else {
if items.at(day-list.at(x - 1)).keys().contains(str(y)) {
let (last, body) = items.at(day-list.at(x - 1)).at(str(y))
let (last, event) = items.at(day-list.at(x - 1)).at(str(y))
show: block.with(inset: (x: 2pt, y: 0pt), width: 100%)
place({
block(
width: 100%,
height: (last) * minute-height,
{
(style.event)(..(minutes-to-datetime(y + minutes-offset), body))
(style.event)(..(event, time-format))
}
)
})
Expand Down
5 changes: 3 additions & 2 deletions test/day-view.typ
Original file line number Diff line number Diff line change
Expand Up @@ -7,11 +7,12 @@
("2024-1-1", "10:15", "11:10", [Tutorial]),
("2024-1-1", (11, 35), (12, 35), [Shopping]),
("2024-1-1", 13.55, 15.00, [Lecture 2]),
("2024-1-2", 7.00, 9.00, [Lecture 2]),
("2024-1-2", 9.30, 11.30, [Lecture 2]),
("2024-1-2", 13.45, 14.30, [Tutorial]),
("2024-1-2", 13.45, 15.20, [Tutorial]),
)

#calendar(events, hour-range: (8, 15), datetime-format: "[day]/[month]/[year]")
#calendar(events, hour-range: (8, 15), date-format: "[day]/[month]/[year]")

= ICS

Expand Down
16 changes: 9 additions & 7 deletions util/utils.typ
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,8 @@
return datetime(hour: h, minute: m, second: 0)
}

#let events-to-calendar-items(events, start) = {
#let events-to-calendar-items(events, hour-range) = {
let (dstart, dend) = hour-range
let days = events.map(i => i.at(0)).dedup().map(i => if type(i) == datetime { i } else { day(..(i,).flatten()) })
let dict = days.map(i => (i.display("[year]-[month]-[day]"), (:))).to-dict()
for value in events {
Expand All @@ -18,11 +19,12 @@
let stime = if type(value.at(1)) == datetime { value.at(1) } else if type(value.at(1)) == array { time(..value.at(1)) } else { time(value.at(1)) }
let etime = if type(value.at(2)) == datetime { value.at(2) } else if type(value.at(2)) == array { time(..value.at(2)) } else { time(value.at(2)) }
let body = if value.len() > 3 { value.at(3) } else { none }
let istart = calc.min((stime.hour() - start), 24) * 60 + calc.min(stime.minute(), 60)
let iend = calc.min((etime.hour() - start), 24) * 60 + calc.min(etime.minute(), 60)
let istart = calc.clamp((stime.hour() - dstart), 0, 24) * 60 + calc.min(stime.minute(), 60)
let iend = calc.clamp((etime.hour() - dstart), 0, 24) * 60 + calc.min(etime.minute(), 60)
iend = calc.clamp(iend, istart + 1, (dend - dstart) * 60)
let ilast = iend - istart
if ilast > 0 {
dict.at(kday).insert(str(istart), (ilast, body))
dict.at(kday).insert(str(istart), (ilast, (stime, etime, ..value.slice(3,))))
}
}
dict
Expand All @@ -35,7 +37,7 @@
[#{day}]
}

#let default-item-style(time, body) = {
#let default-item-style(event, time-format) = {
show: block.with(
fill: white,
height: 100%,
Expand All @@ -51,10 +53,10 @@
set par(leading: 4pt)
if time != none {
terms(
terms.item(time.display("[hour]:[minute]"), body)
terms.item(event.at(0).display(time-format), event.last())
)
} else {
body
event.last()
}
}

Expand Down