-
Notifications
You must be signed in to change notification settings - Fork 61
/
print.go
82 lines (69 loc) · 1.39 KB
/
print.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
78
79
80
81
82
// Package debug provides simple routines for debugging continued fractions.
package debug
import (
"bytes"
"math"
)
type Term struct {
A string
B string
}
// TODO(eric): drop this once 1.10 becomes our minimum supported version
func round(x float64) float64 {
t := math.Trunc(x)
if math.Abs(x-t) >= 0.5 {
return t + math.Copysign(1, x)
}
return t
}
func Dump(t []Term) string {
max := len(t)
if max >= 25 {
max = 25
}
var length int
for _, m := range t[:max] {
length += len(m.B) + len(" + ")
}
const ddots = "⋱"
if max < len(t) {
length += len(ddots)
}
var (
buf bytes.Buffer
lpad int
spaces = bytes.Repeat([]byte{' '}, length)
dashes = bytes.Repeat([]byte{'-'}, length)
)
for _, m := range t[:max] {
a, b := m.A, m.B
buf.Write(spaces[:lpad])
neg := a[0] == '-'
if neg {
a = a[1:]
}
half := int(round(float64(len(b)+length)/2.0) + round(float64(len(a))/2))
buf.Write(spaces[:half])
buf.WriteString(a)
buf.Write(spaces[:half])
buf.WriteByte('\n')
buf.Write(spaces[:lpad])
buf.WriteString(b)
if neg {
buf.WriteString(" - ")
} else {
buf.WriteString(" + ")
}
buf.Write(dashes[:length])
buf.WriteByte('\n')
lpad += len(b) + len(" + ")
length -= len(b) + len(" + ")
}
if max < len(t) {
buf.Write(spaces[:lpad])
buf.WriteString(t[max].B)
buf.WriteString(" + ")
buf.WriteString(ddots)
}
return buf.String()
}