mirror of https://github.com/curusarn/resh
parent
367263b28c
commit
67ab2ffaef
@ -0,0 +1,42 @@ |
|||||||
|
package main |
||||||
|
|
||||||
|
import ( |
||||||
|
"github.com/curusarn/resh/common" |
||||||
|
) |
||||||
|
|
||||||
|
type strategyDirectorySensitive struct { |
||||||
|
history map[string][]string |
||||||
|
lastPwd string |
||||||
|
} |
||||||
|
|
||||||
|
func (s *strategyDirectorySensitive) init() { |
||||||
|
s.history = map[string][]string{} |
||||||
|
} |
||||||
|
|
||||||
|
func (s *strategyDirectorySensitive) GetTitleAndDescription() (string, string) { |
||||||
|
return "directory sensitive (recent)", "Use recent commands executed is the same directory" |
||||||
|
} |
||||||
|
|
||||||
|
func (s *strategyDirectorySensitive) GetCandidates() []string { |
||||||
|
return s.history[s.lastPwd] |
||||||
|
} |
||||||
|
|
||||||
|
func (s *strategyDirectorySensitive) AddHistoryRecord(record *common.Record) error { |
||||||
|
// work on history for PWD
|
||||||
|
pwd := record.Pwd |
||||||
|
// remove previous occurance of record
|
||||||
|
for i, cmd := range s.history[pwd] { |
||||||
|
if cmd == record.CmdLine { |
||||||
|
s.history[pwd] = append(s.history[pwd][:i], s.history[pwd][i+1:]...) |
||||||
|
} |
||||||
|
} |
||||||
|
// append new record
|
||||||
|
s.history[pwd] = append([]string{record.CmdLine}, s.history[pwd]...) |
||||||
|
s.lastPwd = record.PwdAfter |
||||||
|
return nil |
||||||
|
} |
||||||
|
|
||||||
|
func (s *strategyDirectorySensitive) ResetHistory() error { |
||||||
|
s.history = map[string][]string{} |
||||||
|
return nil |
||||||
|
} |
||||||
@ -0,0 +1,47 @@ |
|||||||
|
package main |
||||||
|
|
||||||
|
import ( |
||||||
|
"sort" |
||||||
|
|
||||||
|
"github.com/curusarn/resh/common" |
||||||
|
) |
||||||
|
|
||||||
|
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 *common.Record) error { |
||||||
|
s.history[record.CmdLine]++ |
||||||
|
return nil |
||||||
|
} |
||||||
|
|
||||||
|
func (s *strategyFrequent) ResetHistory() error { |
||||||
|
s.history = map[string]int{} |
||||||
|
return nil |
||||||
|
} |
||||||
Loading…
Reference in new issue