Import initial history from fish too

This commit is contained in:
David Dworken 2022-11-03 22:32:55 -07:00
parent c5aff4085d
commit d4ca466314
No known key found for this signature in database
3 changed files with 23 additions and 2 deletions

View File

@ -533,6 +533,11 @@ func ImportHistory(ctx *context.Context, shouldReadStdin bool) (int, error) {
return 0, fmt.Errorf("failed to parse zsh history: %v", err)
}
historyEntries = append(historyEntries, extraEntries...)
extraEntries, err = parseFishHistory(homedir)
if err != nil {
return 0, fmt.Errorf("failed to parse fish history: %v", err)
}
historyEntries = append(historyEntries, extraEntries...)
if shouldReadStdin {
extraEntries, err = readStdin()
if err != nil {
@ -550,7 +555,7 @@ func ImportHistory(ctx *context.Context, shouldReadStdin bool) (int, error) {
return 0, err
}
for _, cmd := range historyEntries {
if isZshWeirdness(cmd) || isBashWeirdness(cmd) {
if isZshWeirdness(cmd) || isBashWeirdness(cmd) || strings.HasPrefix(cmd, " ") {
// Skip it
continue
}
@ -601,6 +606,21 @@ func readStdin() ([]string, error) {
return ret, nil
}
func parseFishHistory(homedir string) ([]string, error) {
lines, err := readFileToArray(filepath.Join(homedir, ".local/share/fish/fish_history"))
if err != nil {
return nil, err
}
ret := make([]string, 0)
for _, line := range lines {
line = strings.TrimSpace(line)
if strings.HasPrefix(line, "- cmd: ") {
ret = append(ret, strings.SplitN(line, ": ", 2)[1])
}
}
return ret, nil
}
func parseBashHistory(homedir string) ([]string, error) {
return readFileToArray(filepath.Join(homedir, ".bash_history"))
}

View File

@ -79,7 +79,6 @@ func main() {
fmt.Printf("Imported %v history entries from your existing shell history\n", numImported)
}
}
case "install":
lib.CheckFatalError(lib.Install())
if os.Getenv("HISHTORY_TEST") == "" {

View File

@ -62,6 +62,7 @@ func BackupAndRestoreWithId(t *testing.T, id string) func() {
path.Join(homedir, data.HISHTORY_PATH, "config.fish"),
path.Join(homedir, ".bash_history"),
path.Join(homedir, ".zsh_history"),
path.Join(homedir, ".local/share/fish/fish_history"),
}
for _, file := range renameFiles {
touchFile(file)
@ -79,6 +80,7 @@ func BackupAndRestoreWithId(t *testing.T, id string) func() {
configureZshrc(homedir)
touchFile(path.Join(homedir, ".bash_history"))
touchFile(path.Join(homedir, ".zsh_history"))
touchFile(path.Join(homedir, ".local/share/fish/fish_history"))
return func() {
Check(t, os.MkdirAll(path.Join(homedir, data.HISHTORY_PATH), os.ModePerm))
for _, file := range renameFiles {