/
main.go
105 lines (89 loc) · 2.16 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
package main
import (
"flag"
"fmt"
"log"
"os"
"path"
"sort"
"github.com/BurntSushi/xgb"
"github.com/BurntSushi/xgb/randr"
)
type commandInfo struct {
f func(*config, heads)
usage string
}
var (
command = "table"
commands = map[string]commandInfo{
"set": {set, "set HEAD-NAME [ HEAD-NAME ... ]"},
"primary": {primary, "primary HEAD-NAME"},
"table": {table, "table"},
"tabs": {tabs, "tabs"},
"list": {list, "list"},
"query": {query, "query [ HEAD-NAME | primary ]"},
"connected": {connected, "connected"},
"all": {all, "all"},
}
flagBaseline string
flagConfig string
flagHeader bool
flagTest bool
flagVertical bool
)
func init() {
log.SetPrefix("[gohead] ")
flag.StringVar(&flagConfig, "config", "",
"Override the default location of the config file.")
flag.BoolVar(&flagHeader, "header", false,
"If set, column headers will be shown in the 'tabs' and 'table' "+
"commands.")
flag.BoolVar(&flagTest, "test", false,
"If set, commands will be echoed but not executed.")
flag.BoolVar(&flagVertical, "vertical", false,
"If set, monitors will be aligned vertically instead of horizontally.")
flag.Usage = usage(commands)
flag.Parse()
if flag.NArg() >= 1 {
command = flag.Arg(0)
}
}
func usage(commands map[string]commandInfo) func() {
return func() {
fmt.Fprintf(os.Stderr, "Usage: %s [ flags ] [ command ]\n",
path.Base(os.Args[0]))
fmt.Fprint(os.Stderr, "Where 'command' (default: 'table') is one of:\n")
printCommands(commands)
flag.PrintDefaults()
os.Exit(1)
}
}
func printCommands(commands map[string]commandInfo) {
lines := make([]string, 0, len(commands))
for _, cmd := range commands {
lines = append(lines, cmd.usage)
}
sort.Sort(sort.StringSlice(lines))
for _, line := range lines {
fmt.Fprintf(os.Stderr, "\t%s\n", line)
}
}
func main() {
X, err := xgb.NewConn()
if err != nil {
log.Fatal(err)
}
err = randr.Init(X)
if err != nil {
log.Fatal(err)
}
conf := newConfig()
hds := newHeads(X)
if cmdInfo, ok := commands[command]; ok {
cmdInfo.f(conf, hds)
} else {
fmt.Fprintf(os.Stderr, "Unknown command '%s'.\n", command)
flag.Usage()
}
os.Exit(0)
}