-
Notifications
You must be signed in to change notification settings - Fork 6
/
Copy pathcommand.go
59 lines (49 loc) · 1.12 KB
/
command.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
package cortana
import (
"strings"
"github.com/google/btree"
)
// Command is an executive unit
type Command struct {
Path string
Proc func()
Brief string
Alias bool
order int // the order is the sequence of invoking add command
}
type command Command
func (c *command) Less(than btree.Item) bool {
t := than.(*command)
return strings.Compare(c.Path, t.Path) < 0
}
type commands struct {
t *btree.BTree
}
func (c commands) scan(prefix string) []*command {
var cmds []*command
begin := &command{Path: prefix}
end := &command{Path: prefix + "\xFF"}
c.t.AscendRange(begin, end, func(i btree.Item) bool {
cmds = append(cmds, i.(*command))
return true
})
return cmds
}
func (c commands) get(path string) *command {
i := c.t.Get(&command{Path: path})
if i != nil {
return i.(*command)
}
return nil
}
// orderedCommands keep the order of adding a command
type orderedCommands []*command
func (cmds orderedCommands) Len() int {
return len(cmds)
}
func (cmds orderedCommands) Less(i, j int) bool {
return cmds[i].order < cmds[j].order
}
func (cmds orderedCommands) Swap(i, j int) {
cmds[i], cmds[j] = cmds[j], cmds[i]
}