write history to history file on quit

Related #81
This commit is contained in:
Gokcehan committed 2018-07-12 21:22:36 +03:00
1 parent 211bcd0444
commit cdf4fcb810
4 files changed
+82 -2

No files matched your search

+70
View File
@@ -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 {
+4
View File
@@ -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()
}
+2
View File
@@ -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))
}
+6 -2
View File
@@ -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 {