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(strippedRecord records.EnrichedRecord) []string { if len(s.history) == 0 { return nil } var mapItems []strDistEntry for i, record := range s.history { if s.maxDepth != 0 && i > s.maxDepth { break } distance := record.DistanceTo(strippedRecord, 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 }