-
Notifications
You must be signed in to change notification settings - Fork 0
/
line.go
77 lines (64 loc) · 1.4 KB
/
line.go
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
package teq
import "strings"
func lineOf(text string) line {
return line{text: text}
}
func linesOf(texts ...string) lines {
result := make(lines, 0, len(texts))
for _, text := range texts {
result = append(result, line{text: text})
}
return result
}
type line struct {
indentDepth int
text string
}
type lines []line
func (l line) indent() line {
return line{
indentDepth: l.indentDepth + 1,
text: l.text,
}
}
func (ls lines) indent() lines {
result := make(lines, len(ls))
for i, l := range ls {
result[i] = l.indent()
}
return result
}
func (ls lines) diffSequence() []string {
result := make([]string, len(ls))
for i, l := range ls {
result[i] = strings.Repeat(" ", l.indentDepth) + l.text + "\n"
}
return result
}
func (ls lines) ledBy(s string) lines {
result := ls.clone()
result[0].text = s + result[0].text
return result
}
func (ls lines) followedBy(s string) lines {
result := ls.clone()
result[len(result)-1].text = result[len(result)-1].text + s
return result
}
func (ls lines) clone() lines {
result := make(lines, len(ls))
copy(result, ls)
return result
}
func (ls lines) key() string {
ts := make([]string, len(ls))
for i, l := range ls {
ts[i] = l.text
}
return strings.Join(ts, "")
}
func keyValue(key lines, value lines) lines {
result := key.followedBy(": " + value[0].text)
result = append(result, value[1:]...)
return result.followedBy(",")
}