/
main.go
99 lines (81 loc) · 1.9 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
package main
import (
"bufio"
"fmt"
"os"
"github.com/paemuri/gridt"
"github.com/alecthomas/kingpin"
"golang.org/x/crypto/ssh/terminal"
)
const (
topToBottom = "top-to-bottom"
leftToRight = "left-to-right"
)
var (
args *[]string
file *string
separator *string
direction *string
)
func init() {
kingpin.CommandLine.HelpFlag.Short('h')
kingpin.Version("2.0.1").VersionFlag.Short('v')
file = kingpin.Flag("file", "Get values as lines from file.").Short('f').String()
separator = kingpin.
Flag("separator", "What separates every value column.").
Short('s').
Default(" ").
String()
direction = kingpin.
Flag("direction", `Whether it writes from "top-to-bottom" or "left-to-right".`).
Short('d').
Default(topToBottom).
Enum(topToBottom, leftToRight)
kingpin.Parse()
}
func main() {
width, _, err := terminal.GetSize(1)
if err != nil {
eprintf("Error getting terminal size: %s.", err.Error())
os.Exit(1)
}
var scanner *bufio.Scanner
if *file != "" {
f, err := os.Open(*file)
if err != nil {
eprintf("Error opening file: %s.", err.Error())
os.Exit(1)
}
defer f.Close()
scanner = bufio.NewScanner(f)
} else {
scanner = bufio.NewScanner(os.Stdin)
}
var values []string
if scanner != nil {
scanner.Split(bufio.ScanLines)
for scanner.Scan() {
values = append(values, scanner.Text())
}
}
if len(values) == 0 {
eprintln("Error: no values were given.")
os.Exit(1)
}
d := gridt.TopToBottom
if *direction == leftToRight {
d = gridt.LeftToRight
}
grid, ok := gridt.New(d, *separator, values...).FitIntoWidth(width)
if !ok {
eprintln("Error: the given values does not fit in the terminal width.")
os.Exit(1)
}
fmt.Println(grid.String())
}
func eprintln(a ...interface{}) (n int, err error) {
return fmt.Fprintln(os.Stderr, a...)
}
func eprintf(format string, a ...interface{}) (n int, err error) {
return fmt.Fprintf(os.Stderr, format, a...)
}