-
Notifications
You must be signed in to change notification settings - Fork 3
/
commander.go
50 lines (41 loc) · 1.02 KB
/
commander.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
package commander
import (
"github.com/c-bata/go-prompt"
"github.com/aphistic/softcopy/internal/pkg/consts"
"github.com/aphistic/softcopy/pkg/proto"
)
type Commander struct {
client scproto.SoftcopyClient
writer Writer
parser *parser
}
func NewCommander(client scproto.SoftcopyClient) *Commander {
w := &consoleWriter{}
return &Commander{
client: client,
writer: w,
parser: newParser(map[string]ParserCmd{
"inbox": newCmdInbox(w, client),
"show": newCmdShow(w, client),
"exit": newCmdExit(),
}),
}
}
func (c *Commander) Startup() error {
c.writer.Printf("softcopy v%s\n", consts.ProcessVersion)
c.writer.Printf("Please use `exit` or `Ctrl-D` to exit.\n")
c.writer.Printf("\n")
return nil
}
func (c *Commander) Executor(s string) {
err := c.parser.RunExecution(s)
if err != nil {
c.writer.Printf("error: %s\n", err)
}
}
func (c *Commander) Completer(d prompt.Document) []prompt.Suggest {
return c.parser.CompileSuggestions(d)
}
func (c *Commander) LivePrefix() (string, bool) {
return "> ", true
}