Have hishtory import also read from stdin

This commit is contained in:
David Dworken
2022-09-30 23:50:25 -07:00
parent 18ddbf2ca9
commit 6cd7fa00fc
2 changed files with 26 additions and 2 deletions

View File

@ -407,6 +407,11 @@ func ImportHistory(ctx *context.Context) (int, error) {
return 0, fmt.Errorf("failed to parse zsh history: %v", err)
}
historyEntries = append(historyEntries, extraEntries...)
extraEntries, err = readStdin()
if err != nil {
return 0, fmt.Errorf("failed to read stdin: %v", err)
}
historyEntries = append(historyEntries, extraEntries...)
db := hctx.GetDb(ctx)
currentUser, err := user.Current()
if err != nil {
@ -445,6 +450,25 @@ func ImportHistory(ctx *context.Context) (int, error) {
return len(historyEntries), nil
}
func readStdin() ([]string, error) {
ret := make([]string, 0)
in := bufio.NewReader(os.Stdin)
for {
s, err := in.ReadString('\n')
if err != nil {
if err != io.EOF {
return nil, err
}
break
}
s = strings.TrimSpace(s)
if s != "" {
ret = append(ret, s)
}
}
return ret, nil
}
func parseBashHistory(homedir string) ([]string, error) {
return readFileToArray(filepath.Join(homedir, ".bash_history"))
}