-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
106 lines (90 loc) · 2.42 KB
/
main.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
106
package main
import (
"flag"
"fmt"
"log"
"os"
"path/filepath"
"regexp"
"strings"
"github.com/keyneston/mktable/table"
)
var (
version = "dev"
commit = "none"
date = "unknown"
builtBy = "unknown"
)
type Config struct {
Format FormatValue
MaxPadding int
Reformat bool
Seperator string
SkipHeaders bool
Version bool
Alignments ParseAlignments
fset *flag.FlagSet
}
func (c *Config) Register(f *flag.FlagSet) *Config {
allFormats := strings.Join(table.AllFormats(), ",")
c.fset = f
c.fset.BoolVar(&c.SkipHeaders, "no-header", false, "Skip Setting Headers")
c.fset.StringVar(&c.Seperator, "s", `[ \t]*\t[ \t]*`, "Regexp of Delimiter to Build Table on")
c.fset.IntVar(&c.MaxPadding, "max-padding", -1, "Maximum units of padding. Set to a negative number for unlimited")
c.fset.BoolVar(&c.Reformat, "r", false, "Read in markdown table and reformat")
c.fset.BoolVar(&c.Reformat, "reformat", false, "Alias for -r")
c.fset.Var(&c.Format, "f", fmt.Sprintf("Set the format. Available formats: %v", allFormats))
c.fset.Var(&c.Format, "format", "Alias for -f")
c.fset.Var(&c.Alignments, "a", "Set column alignments; Can be called multiple times and/or comma separated. Arrow indicates direction '<' left, '>' right, '=' center; Columns are zero indexed; e.g. -a '0<,1>,2='")
c.fset.BoolVar(&c.Version, "v", false, "Print version info")
c.fset.BoolVar(&c.Version, "version", false, "Alias for -v")
return c
}
func (c *Config) Parse(args []string) error {
return c.fset.Parse(args)
}
func (c *Config) CompileSeperator() (*regexp.Regexp, error) {
return regexp.Compile(c.Seperator)
}
func main() {
fset := flag.NewFlagSet("", flag.ExitOnError)
c := (&Config{}).Register(fset)
if err := c.Parse(os.Args[1:]); err != nil {
log.Fatalf("Error: %v", err)
}
if c.Version {
PrintVersion(os.Args[0])
os.Exit(0)
}
tableConfig := table.TableConfig{
MaxPadding: c.MaxPadding,
SkipHeaders: c.SkipHeaders,
Alignments: c.Alignments.alignments,
Seperator: regexp.MustCompile(c.Seperator),
Format: c.Format.GetFormat(),
}
if c.Reformat {
tableConfig.Format = table.FormatMK
}
tb := table.NewTable(tableConfig)
tb.Read(os.Stdin)
tb.Write(os.Stdout)
}
func PrintVersion(bin string) {
fmt.Printf(strings.TrimLeft(`
%s
version: %s
commit: %v
built-on: %v
built-by: %v
formats: %v
`,
" \t\n"),
filepath.Base(bin),
version,
commit,
date,
builtBy,
strings.Join(table.AllFormats(), ", "),
)
}