-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathcellwidth.go
More file actions
62 lines (56 loc) · 1011 Bytes
/
cellwidth.go
File metadata and controls
62 lines (56 loc) · 1011 Bytes
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
package csvi
import (
"strconv"
"strings"
)
type CellWidth struct {
Default int
Option map[int]int
}
func NewCellWidth() *CellWidth {
cw := &CellWidth{
Default: 14,
Option: map[int]int{},
}
return cw
}
func (cw *CellWidth) Set(at, value int) bool {
if value == cw.Default {
delete(cw.Option, at)
} else {
cw.Option[at] = value
}
return true
}
func (cw *CellWidth) Get(n int) int {
if val, ok := cw.Option[n]; ok {
return val
}
return cw.Default
}
func (cw *CellWidth) Parse(s string) error {
var p string
cont := true
for cont {
p, s, cont = strings.Cut(s, ",")
left, right, ok := strings.Cut(p, ":")
if ok {
leftN, err := strconv.ParseUint(left, 10, 64)
if err != nil {
return err
}
rightN, err := strconv.ParseUint(right, 10, 64)
if err != nil {
return err
}
cw.Option[int(leftN)] = int(rightN)
} else {
value, err := strconv.ParseUint(p, 10, 64)
if err != nil {
return err
}
cw.Default = int(value)
}
}
return nil
}