-
Notifications
You must be signed in to change notification settings - Fork 0
/
parsealign.go
65 lines (53 loc) · 1.13 KB
/
parsealign.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
package main
import (
"fmt"
"strings"
"github.com/keyneston/mktable/table"
)
type ParseAlignments struct {
alignments map[int]table.Alignment
}
func (p *ParseAlignments) String() string {
list := []string{}
for k, v := range p.alignments {
arrow := ""
switch v {
case table.AlignCenter:
arrow = "<>"
case table.AlignRight:
arrow = ">"
case table.AlignLeft:
arrow = "<"
default:
arrow = "="
}
list = append(list, fmt.Sprintf("%d%s", k, arrow))
}
return strings.Join(list, ",")
}
func (p *ParseAlignments) Set(input string) error {
if p.alignments == nil {
p.alignments = map[int]table.Alignment{}
}
sep := strings.Split(input, ",")
for _, cur := range sep {
var num int
var arrow string
if _, err := fmt.Sscanf(cur, "%d%s", &num, &arrow); err != nil {
return fmt.Errorf("Error parsing %q: %v", cur, err)
}
align := table.AlignDefault
switch arrow {
case "<":
align = table.AlignLeft
case ">":
align = table.AlignRight
case "=":
align = table.AlignCenter
default:
return fmt.Errorf("Unknown alignment: \"%d%s\"", num, arrow)
}
p.alignments[num] = align
}
return nil
}