diff --git a/app.go b/app.go index 3d185d0e..62e3cd51 100644 --- a/app.go +++ b/app.go @@ -7,6 +7,7 @@ import ( "log" "os" "os/exec" + "path/filepath" "strconv" "strings" "time" @@ -26,6 +27,7 @@ type app struct { cmdIn io.WriteCloser cmdOutBuf []byte cmdHistory []cmdItem + cmdHistoryBeg int cmdHistoryInd int } @@ -62,6 +64,70 @@ func (app *app) readFile(path string) { } } +func (app *app) readHistory() error { + f, err := os.Open(gHistoryPath) + if os.IsNotExist(err) { + return nil + } + if err != nil { + return fmt.Errorf("opening history file: %s", err) + } + defer f.Close() + + scanner := bufio.NewScanner(f) + for scanner.Scan() { + toks := strings.SplitN(scanner.Text(), " ", 2) + app.cmdHistory = append(app.cmdHistory, cmdItem{toks[0], toks[1]}) + } + + app.cmdHistoryBeg = len(app.cmdHistory) + + if err := scanner.Err(); err != nil { + return fmt.Errorf("reading history file: %s", err) + } + + return nil +} + +func (app *app) writeHistory() error { + if len(app.cmdHistory) == 0 { + return nil + } + + local := make([]cmdItem, len(app.cmdHistory)-app.cmdHistoryBeg) + copy(local, app.cmdHistory[app.cmdHistoryBeg:]) + app.cmdHistory = nil + + if err := app.readHistory(); err != nil { + return fmt.Errorf("reading history file: %s", err) + } + + app.cmdHistory = append(app.cmdHistory, local...) + + if err := os.MkdirAll(filepath.Dir(gHistoryPath), os.ModePerm); err != nil { + return fmt.Errorf("creating data directory: %s", err) + } + + f, err := os.Create(gHistoryPath) + if err != nil { + return fmt.Errorf("creating history file: %s", err) + } + defer f.Close() + + if len(app.cmdHistory) > 1000 { + app.cmdHistory = app.cmdHistory[len(app.cmdHistory)-1000:] + } + + for _, cmd := range app.cmdHistory { + _, err = f.WriteString(fmt.Sprintf("%s %s\n", cmd.prefix, cmd.value)) + if err != nil { + return fmt.Errorf("writing history file: %s", err) + } + } + + return nil +} + // This is the main event loop of the application. Expressions are read from // the client and the server on separate goroutines and sent here over channels // for evaluation. Similarly directories and regular files are also read in @@ -79,6 +145,10 @@ func (app *app) loop() { log.Printf("writing marks file: %s", err) } + if err := app.writeHistory(); err != nil { + log.Printf("writing history file: %s", err) + } + if gLastDirPath != "" { f, err := os.Create(gLastDirPath) if err != nil { diff --git a/client.go b/client.go index 33ea3f69..72b0880a 100644 --- a/client.go +++ b/client.go @@ -42,6 +42,10 @@ func run() { app.ui.printf("reading marks file: %s", err) } + if err := app.readHistory(); err != nil { + app.ui.printf("reading history file: %s", err) + } + app.loop() } diff --git a/os.go b/os.go index 4f33a337..c6b0ce0b 100644 --- a/os.go +++ b/os.go @@ -29,6 +29,7 @@ var ( gUser *user.User gConfigPaths []string gMarksPath string + gHistoryPath string ) func init() { @@ -74,6 +75,7 @@ func init() { } gMarksPath = filepath.Join(data, "lf", "marks") + gHistoryPath = filepath.Join(data, "lf", "history") gDefaultSocketPath = filepath.Join(os.TempDir(), fmt.Sprintf("lf.%s.sock", gUser.Username)) } diff --git a/os_windows.go b/os_windows.go index d8fa3fc7..15f7aaf6 100644 --- a/os_windows.go +++ b/os_windows.go @@ -30,6 +30,7 @@ var ( gUser *user.User gConfigPaths []string gMarksPath string + gHistoryPath string ) func init() { @@ -58,12 +59,15 @@ func init() { // remove domain prefix gUser.Username = strings.Split(gUser.Username, `\`)[1] + data := os.Getenv("LOCALAPPDATA") + gConfigPaths = []string{ filepath.Join(os.Getenv("ProgramData"), "lf", "lfrc"), - filepath.Join(os.Getenv("LOCALAPPDATA"), "lf", "lfrc"), + filepath.Join(data, "lf", "lfrc"), } - gMarksPath = filepath.Join(os.Getenv("LOCALAPPDATA"), "lf", "marks") + gMarksPath = filepath.Join(data, "lf", "marks") + gHistoryPath = filepath.Join(data, "lf", "history") } func pauseCommand() *exec.Cmd {