-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstructural.go
105 lines (79 loc) · 1.99 KB
/
structural.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
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package hobocode
import (
"fmt"
"os"
"strings"
"unicode/utf8"
"github.com/jedib0t/go-pretty/v6/text"
)
/*
TODO: Header right justification gets wonky when the terminal width is odd
*/
const (
defaultWidth = 0
)
// Header prints a header title wrapped by "=" characters sized to the terminal width
//
// Stdout
func Header(title string) {
/*
TODO: I've wasted a lot of time here trying to get the header widths to reliably always
be the same across headers based on screen width but I am too stupid to get it to work in every
circumstance
*/
x, _, err := Size(os.Stdout)
if err != nil {
x = defaultWidth
}
// safety buffer
x = x - 1
//fmt.Printf("width: %d\n", x)
titleLength := utf8.RuneCountInString(title)
//fmt.Printf("title len: %d\n", titleLength)
n := (x - titleLength - 4) / 2
n2 := x - n*2
//fmt.Printf("Remainder: %d\n", n2)
//fmt.Printf("Difference: %d\n", titleLength*2-n2)
//fmt.Printf("half: %d\n", n)
offset := 0
if titleLength*2 > n2 {
offset--
}
if n%2 == 0 {
if titleLength%2 != 0 {
offset++
}
}
//fmt.Printf("Offset: %d\n", offset)
patternLeft := strings.Repeat("=", n)
patternRight := strings.Repeat("=", n+offset)
Icolor(0, os.Stdout, text.FgHiWhite, fmt.Sprintf("%s[ %s ]%s", patternLeft, title, patternRight))
}
// HeaderLeft prints a header title offset to the left wrapped by "=" characters sized to the terminal width
//
// Stdout
func HeaderLeft(title string) {
x, _, err := Size(os.Stdout)
if err != nil {
x = defaultWidth
}
x = x - 1
//fmt.Println(x)
titleLength := utf8.RuneCountInString(title)
//fmt.Printf("title len: %d\n", titleLength)
n := (x - titleLength) / 10
n2 := x - titleLength - n - 1
offset := 0
if titleLength%2 != 0 {
offset++
}
patternLeft := ""
if n-4 > 0 {
patternLeft = strings.Repeat("=", n-4)
}
patternRight := ""
if n-2+offset > 0 {
patternRight = strings.Repeat("=", n2-4+offset)
}
Icolor(0, os.Stdout, text.FgHiWhite, fmt.Sprintf("%s[ %s ]%s", patternLeft, title, patternRight))
}