-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathframe.gleam
More file actions
99 lines (88 loc) · 2.41 KB
/
frame.gleam
File metadata and controls
99 lines (88 loc) · 2.41 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
import gleam/list
import lustre/attribute as a
import lustre/element.{text}
import lustre/element/html as h
// component, block panel
// enclosed bare clad
// prepend spans doesnt work on bare item
pub type Frame(a) {
Multiline(
List(element.Element(a)),
List(element.Element(a)),
List(element.Element(a)),
)
Inline(List(element.Element(a)))
Statements(List(element.Element(a)))
}
fn is_inline(m) {
case m {
Inline(spans) -> Ok(spans)
_ -> Error(Nil)
}
}
pub fn line_height(frame) {
case frame {
Inline(_) -> 1
Multiline(_, inner, _) -> list.length(inner)
Statements(inner) -> list.length(inner)
}
}
pub fn all_inline(ms) {
list.try_map(ms, is_inline)
}
pub fn prepend_spans(new, m) {
case m {
Inline(spans) -> Inline(list.append(new, spans))
Multiline(pre, inner, post) -> Multiline(list.append(new, pre), inner, post)
Statements(inner) ->
prepend_spans(new, Multiline([text("{")], inner, [text("}")]))
}
}
pub fn append_spans(m, new) {
case m {
Inline(spans) -> Inline(list.append(spans, new))
Multiline(pre, inner, post) -> Multiline(pre, inner, list.append(post, new))
Statements(inner) ->
append_spans(Multiline([text("{")], inner, [text("}")]), new)
}
}
pub fn join(a, b) {
case a, b {
Inline(spans), b -> prepend_spans(spans, b)
a, Inline(spans) -> append_spans(a, spans)
Multiline(pre, inner_a, span_a), Multiline(span_b, inner_b, post) -> {
let middle = h.div([], list.append(span_a, span_b))
let inner = list.flatten([inner_a, [middle], inner_b])
Multiline(pre, inner, post)
}
_, _ -> todo as "shouldn't join statements"
}
}
pub fn delimit(frames, delimiter) {
case list.reverse(frames) {
[] -> []
[last, ..rest] -> {
let rest = list.map(rest, append_spans(_, [text(delimiter)]))
list.reverse([last, ..rest])
}
}
}
// Call to_divs but its a single div
// could return unwrapped divs but what about indent.
// have indent as an option
// wrap each line thing in it's own div
pub fn to_fat_line(exp) {
case exp {
Inline(spans) -> h.div([], spans)
Multiline([], inner, []) -> h.div([], inner)
Multiline(pre, inner, post) ->
h.div([], [h.div([], pre), indent(inner), h.div([], post)])
Statements(inner) -> h.div([], inner)
}
}
fn indent(inner) {
h.div([a.style([#("padding-left", "2ch")])], inner)
}
pub fn to_fat_lines(lines) {
list.map(lines, to_fat_line)
}