forked from kencx/keyb
-
Notifications
You must be signed in to change notification settings - Fork 0
/
main.go
150 lines (121 loc) · 3.18 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
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
package main
import (
"flag"
"fmt"
"log"
"os"
"path"
tea "github.com/charmbracelet/bubbletea"
"github.com/kencx/keyb/config"
"github.com/kencx/keyb/output"
"github.com/kencx/keyb/ui"
)
const (
help = `usage: keyb [options] <command>
Options:
-p, --print Print to stdout
-e, --export Export to file
-k, --key Key bindings at custom path
-c, --config Config file at custom path
-v, --version Version info
-h, --help Show help
Commands:
a, add Add keybind to keyb file
`
addHelp = `usage: keyb [-k file] add [app; name; key]
Options:
-k, --key Key bindings file at custom path
-b, --binding Key binding
-p, --prefix Ignore prefix
`
)
var version string
func main() {
log.SetPrefix("keyb: ")
log.SetFlags(log.Lshortfile)
var (
stdout bool
exportFile string
keybFile string
configFile string
addBind string
addPrefix bool
)
baseDir, err := config.GetBaseDir()
if err != nil {
log.Fatalf("os not supported: %v", err)
}
defaultConfig := path.Join(baseDir, "keyb", "config.yml")
shortVersion := flag.Bool("v", false, "version information")
longVersion := flag.Bool("version", false, "version information")
flag.BoolVar(&stdout, "p", false, "print to stdout")
flag.BoolVar(&stdout, "print", false, "print to stdout")
flag.StringVar(&exportFile, "e", "", "export to file")
flag.StringVar(&exportFile, "export", "", "export to file")
flag.StringVar(&keybFile, "k", "", "keybindings file")
flag.StringVar(&keybFile, "key", "", "keybindings file")
flag.StringVar(&configFile, "c", defaultConfig, "config file")
flag.StringVar(&configFile, "config", defaultConfig, "config file")
addCmd := flag.NewFlagSet("add", flag.ExitOnError)
addCmd.StringVar(&addBind, "b", "", "keybind")
addCmd.StringVar(&addBind, "binding", "", "keybind")
addCmd.BoolVar(&addPrefix, "p", false, "prefix")
addCmd.BoolVar(&addPrefix, "prefix", false, "prefix")
flag.Usage = func() { os.Stdout.Write([]byte(help)) }
flag.Parse()
if *shortVersion || *longVersion {
fmt.Println(version)
os.Exit(0)
}
keys, cfg, err := config.Parse(keybFile, configFile)
if err != nil {
log.Fatal(err)
}
args := flag.Args()
if len(args) > 1 {
switch args[0] {
case "add", "a":
addCmd.Usage = func() { os.Stdout.Write([]byte(addHelp)) }
addCmd.Parse(args[1:])
var addFile string
if keybFile != "" {
// use flag -k path
addFile = keybFile
} else {
// use default path in config
addFile = cfg.KeybPath
}
if err := config.AddEntry(addFile, addBind, addPrefix); err != nil {
log.Fatal(err)
}
fmt.Printf("%s added to %s", addBind, addFile)
os.Exit(0)
default:
fmt.Print(help)
os.Exit(1)
}
}
m := ui.NewModel(keys, cfg)
if stdout {
if err := output.ToStdout(m); err != nil {
log.Fatal(err)
}
os.Exit(0)
}
if exportFile != "" {
if err := output.ToFile(m, exportFile); err != nil {
log.Fatal(err)
}
os.Exit(0)
}
if err := start(m); err != nil {
log.Fatal(err)
}
}
func start(m *ui.Model) error {
p := tea.NewProgram(m, tea.WithMouseCellMotion())
if _, err := p.Run(); err != nil {
return fmt.Errorf("failed to start: %w", err)
}
return nil
}