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.
68 lines
1.6 KiB
68 lines
1.6 KiB
package main
|
|
|
|
import (
|
|
"sort"
|
|
"strconv"
|
|
|
|
"github.com/curusarn/resh/pkg/records"
|
|
)
|
|
|
|
type strategyRecordDistance struct {
|
|
history []records.EnrichedRecord
|
|
distParams records.DistParams
|
|
maxDepth int
|
|
label string
|
|
}
|
|
|
|
type strDistEntry struct {
|
|
cmdLine string
|
|
distance float64
|
|
}
|
|
|
|
func (s *strategyRecordDistance) init() {
|
|
s.history = nil
|
|
}
|
|
|
|
func (s *strategyRecordDistance) GetTitleAndDescription() (string, string) {
|
|
return "record distance (depth:" + strconv.Itoa(s.maxDepth) + ";" + s.label + ")", "Use record distance to recommend commands"
|
|
}
|
|
|
|
func (s *strategyRecordDistance) GetCandidates() []string {
|
|
if len(s.history) == 0 {
|
|
return nil
|
|
}
|
|
var prevRecord records.EnrichedRecord
|
|
prevRecord = s.history[0]
|
|
prevRecord.SetCmdLine("")
|
|
prevRecord.SetBeforeToAfter()
|
|
var mapItems []strDistEntry
|
|
for i, record := range s.history {
|
|
if s.maxDepth != 0 && i > s.maxDepth {
|
|
break
|
|
}
|
|
distance := record.DistanceTo(prevRecord, s.distParams)
|
|
mapItems = append(mapItems, strDistEntry{record.CmdLine, distance})
|
|
}
|
|
sort.SliceStable(mapItems, func(i int, j int) bool { return mapItems[i].distance < mapItems[j].distance })
|
|
var hist []string
|
|
histSet := map[string]bool{}
|
|
for _, item := range mapItems {
|
|
if histSet[item.cmdLine] {
|
|
continue
|
|
}
|
|
histSet[item.cmdLine] = true
|
|
hist = append(hist, item.cmdLine)
|
|
}
|
|
return hist
|
|
}
|
|
|
|
func (s *strategyRecordDistance) AddHistoryRecord(record *records.EnrichedRecord) error {
|
|
// append record to front
|
|
s.history = append([]records.EnrichedRecord{*record}, s.history...)
|
|
return nil
|
|
}
|
|
|
|
func (s *strategyRecordDistance) ResetHistory() error {
|
|
s.init()
|
|
return nil
|
|
}
|
|
|