40 lines
797 B
Go
40 lines
797 B
Go
package menu
|
|
|
|
import "fmt"
|
|
|
|
const (
|
|
ActionMenu = iota + 1
|
|
ActionExec
|
|
)
|
|
|
|
type Action uint8
|
|
|
|
type Option struct {
|
|
Name string
|
|
Options []Option
|
|
Action Action
|
|
Args []string
|
|
y int
|
|
parent *Option
|
|
}
|
|
|
|
func (o *Option) String() string {
|
|
ol := len(o.Options)
|
|
if ol > 1 {
|
|
return fmt.Sprintf("%s (%d)", o.Name, ol)
|
|
}
|
|
return o.Name
|
|
}
|
|
|
|
type ByName []Option
|
|
|
|
func (a ByName) Len() int { return len(a) }
|
|
func (a ByName) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
|
func (a ByName) Less(i, j int) bool { return a[i].Name < a[j].Name }
|
|
|
|
type ByOptions []Option
|
|
|
|
func (a ByOptions) Len() int { return len(a) }
|
|
func (a ByOptions) Swap(i, j int) { a[i], a[j] = a[j], a[i] }
|
|
func (a ByOptions) Less(i, j int) bool { return len(a[i].Options) > len(a[j].Options) }
|