mirror of https://github.com/curusarn/resh
You can not select more than 25 topics
Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
47 lines
968 B
47 lines
968 B
package main
|
|
|
|
import (
|
|
"sort"
|
|
|
|
"github.com/curusarn/resh/pkg/records"
|
|
)
|
|
|
|
type strategyFrequent struct {
|
|
history map[string]int
|
|
}
|
|
|
|
type strFrqEntry struct {
|
|
cmdLine string
|
|
count int
|
|
}
|
|
|
|
func (s *strategyFrequent) init() {
|
|
s.history = map[string]int{}
|
|
}
|
|
|
|
func (s *strategyFrequent) GetTitleAndDescription() (string, string) {
|
|
return "frequent", "Use frequent commands"
|
|
}
|
|
|
|
func (s *strategyFrequent) GetCandidates() []string {
|
|
var mapItems []strFrqEntry
|
|
for cmdLine, count := range s.history {
|
|
mapItems = append(mapItems, strFrqEntry{cmdLine, count})
|
|
}
|
|
sort.Slice(mapItems, func(i int, j int) bool { return mapItems[i].count > mapItems[j].count })
|
|
var hist []string
|
|
for _, item := range mapItems {
|
|
hist = append(hist, item.cmdLine)
|
|
}
|
|
return hist
|
|
}
|
|
|
|
func (s *strategyFrequent) AddHistoryRecord(record *records.EnrichedRecord) error {
|
|
s.history[record.CmdLine]++
|
|
return nil
|
|
}
|
|
|
|
func (s *strategyFrequent) ResetHistory() error {
|
|
s.init()
|
|
return nil
|
|
}
|
|
|