-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtimeline.go
More file actions
253 lines (212 loc) · 5.08 KB
/
Copy pathtimeline.go
File metadata and controls
253 lines (212 loc) · 5.08 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
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
/* **********************************************************************************************100
Example JSON input:
{
"Title": "My Timeline",
"Nodes": [
{"Start": -5000, "End": -4000, "Label": "Ancient Era"},
{"Start": -3000, "End": -2000, "Label": "Bronze Age"},
{"Start": 0, "End": 2024, "Label": "Common Era"},
{"Now": true, "Mark": true, "Label": "Today"}
]
}
Each node can specify a start and end year, or use "Now" to indicate the current year.
If "Mark" is true, it will print a single point instead of a range.
Compile and run with:
cat timeline.json | go run timeline.go
format: gofmt -d timeline.go
*/
package main
import (
"encoding/json"
"fmt"
"io"
"os"
"strings"
"syscall"
"time"
"unsafe"
)
// ***************************************************************************80
// MARK: Structures
const (
RuneCkBoard = '▒'
RuneUnderline = '─'
RuneVertical = '│'
RuneIntersection = '┼'
RuneDiamond = '◆'
RuneLArrow = '←'
)
type TimeLine struct {
Title string
Nodes []TimeLineNode
}
type TimeLineNode struct {
Start int
Now bool
Mark bool
End int
Label string
}
type win_size struct {
Row uint16
Col uint16
Xpixel uint16
Ypixel uint16
}
// ***************************************************************************80
// MARK: Functions
func GetWidth() int {
ws := &win_size{}
retCode, _, errno := syscall.Syscall(syscall.SYS_IOCTL,
uintptr(syscall.Stdout),
uintptr(syscall.TIOCGWINSZ),
uintptr(unsafe.Pointer(ws)))
if int(retCode) == -1 {
panic(errno)
}
return int(ws.Col)
}
func abs(x int) int {
if x < 0 {
return -x
}
return x
}
/** limit string to width */
func limit(str string, width int) string {
if len(str) <= width {
return str
}
return str[:width]
}
/** print string limited to width */
func print(str string, width int) {
fmt.Printf("%s", limit(str, width))
}
// ***********************************40
// MARK: JSON Helpers
func JsonToStruct[Target any](bytes []byte) (Target, error) {
var data Target
err := json.Unmarshal(bytes, &data)
if err != nil {
return data, err
}
return data, nil
}
func StructToJson[T any](data T, useIndent bool) ([]byte, error) {
var bytes []byte
var err error
if useIndent {
bytes, err = json.MarshalIndent(data, "", strings.Repeat(" ", 4))
} else {
bytes, err = json.Marshal(data)
}
if err != nil {
return nil, err
}
return bytes, nil
}
// ***************************************************************************80
// MARK: application functions
func toAstronomical(year int) int {
// AD years (1 AD = 1, etc.)
if year >= 1 {
return year
}
// BC years: input year = -N means "N BC"
// But astronomical year 0 = 1 BC
// So: N BC -> astronomical year = -(N - 1)
// Example: -500 -> 500 BC -> astronomical year = -(500 - 1) = -499
return -(abs(year) - 1)
}
func toHolocene(year int) int {
astYear := toAstronomical(year)
return astYear + 10000
}
func printHeader() {
max_width := (GetWidth() - 7) * 100
for i := 0; i < max_width; i++ {
mark := i % 1000
if mark != 0 {
continue
}
fmt.Printf("│%-9d", i) // 1+8 is 9 chars wide
}
fmt.Println()
for i := 0; i < (max_width / 100); i++ {
mark := i % 10
if mark != 0 {
fmt.Printf(string(RuneUnderline))
} else {
fmt.Printf(string(RuneIntersection))
}
}
fmt.Println()
}
func processTimeLineRow(node TimeLineNode) int {
width := GetWidth()
scale := 100 //number of years per column
if node.Now {
node.Start = time.Now().Year()
if node.Label == "" {
node.Label = "Today"
}
}
if node.Mark {
node.End = node.Start
}
astYear := toAstronomical(node.Start)
holocene := astYear + 10000
astEnd := toAstronomical(node.End)
holoceneEnd := astEnd + 10000
durration := holoceneEnd - holocene
// calculate positions, maybe round in the future
mark1 := max(0, holocene/scale)
mark2 := max(0, durration/scale)
spacer := strings.Repeat(" ", mark1)
span := strings.Repeat(string(RuneCkBoard), mark2)
if node.Mark {
print(fmt.Sprintf("%s◆ <-- %d : %s", spacer, holocene, node.Label), width)
} else {
print(fmt.Sprintf("%s│%s│ <-- %d to %d (%d years): %s",
spacer, span, holocene, holoceneEnd, durration, node.Label), width)
}
fmt.Println()
return durration
}
// If stdin is from a pipe or file:
func hasInput() bool {
info, err := os.Stdin.Stat()
if err != nil {
panic(err)
}
return (info.Mode() & os.ModeCharDevice) == 0
}
func doTimeLine(timeline TimeLine) {
fmt.Printf("%s\n", timeline.Title)
printHeader()
total := 0
for _, seg := range timeline.Nodes {
total += processTimeLineRow(seg)
}
fmt.Printf("Average durration: %d years.\n", total/len(timeline.Nodes))
}
// ***************************************************************************80
// MARK: main
func main() {
if !hasInput() {
fmt.Println("Please provide timeline JSON data via stdin.")
return
}
data, err := io.ReadAll(os.Stdin)
if err != nil {
fmt.Println("Error reading stdin:", err)
return
}
timeline, err := JsonToStruct[TimeLine]([]byte(data))
if err != nil {
fmt.Println("Error parsing JSON:", err)
return
}
doTimeLine(timeline)
}