Rich Enhanced Shell History - Contextual shell history for zsh and bash
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.
 
 
 
resh/evaluate/strategy-directory-sensitiv...

40 lines
1.0 KiB

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.EnrichedRecord) 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
}