app/vlogscli: store incompletely written lines in the history

This commit is contained in:
Aliaksandr Valialkin 2024-10-07 10:57:31 +02:00
parent 6c9e643ea8
commit 2d8785fdf6
No known key found for this signature in database
GPG key ID: 52C003EE2BCDB9EB

View file

@ -51,12 +51,12 @@ func main() {
} }
headers = hes headers = hes
isEmptyLine := true incompleteLine := ""
cfg := &readline.Config{ cfg := &readline.Config{
Prompt: firstLinePrompt, Prompt: firstLinePrompt,
DisableAutoSaveHistory: true, DisableAutoSaveHistory: true,
Listener: func(line []rune, pos int, _ rune) ([]rune, int, bool) { Listener: func(line []rune, pos int, _ rune) ([]rune, int, bool) {
isEmptyLine = len(line) == 0 incompleteLine = string(line)
return line, pos, false return line, pos, false
}, },
} }
@ -67,7 +67,7 @@ func main() {
fmt.Fprintf(rl, "sending queries to %s\n", *datasourceURL) fmt.Fprintf(rl, "sending queries to %s\n", *datasourceURL)
runReadlineLoop(rl, &isEmptyLine) runReadlineLoop(rl, &incompleteLine)
if err := rl.Close(); err != nil { if err := rl.Close(); err != nil {
fatalf("cannot close readline: %s", err) fatalf("cannot close readline: %s", err)
@ -75,7 +75,7 @@ func main() {
} }
func runReadlineLoop(rl *readline.Instance, isEmptyLine *bool) { func runReadlineLoop(rl *readline.Instance, incompleteLine *string) {
historyLines, err := loadFromHistory(*historyFile) historyLines, err := loadFromHistory(*historyFile)
if err != nil { if err != nil {
fatalf("cannot load query history: %s", err) fatalf("cannot load query history: %s", err)
@ -100,11 +100,13 @@ func runReadlineLoop(rl *readline.Instance, isEmptyLine *bool) {
} }
return return
case readline.ErrInterrupt: case readline.ErrInterrupt:
if s == "" && *isEmptyLine { if s == "" && *incompleteLine == "" {
fmt.Fprintf(rl, "interrupted\n") fmt.Fprintf(rl, "interrupted\n")
os.Exit(128 + int(syscall.SIGINT)) os.Exit(128 + int(syscall.SIGINT))
} }
// Default value for Ctrl+C - clear the prompt // Default value for Ctrl+C - clear the prompt and store the incompletely entered line into history
s += *incompleteLine
historyLines = pushToHistory(rl, historyLines, s)
s = "" s = ""
rl.SetPrompt(firstLinePrompt) rl.SetPrompt(firstLinePrompt)
continue continue
@ -143,24 +145,29 @@ func runReadlineLoop(rl *readline.Instance, isEmptyLine *bool) {
// Save queries in the history even if they weren't finished successfully // Save queries in the history even if they weren't finished successfully
} }
s = strings.TrimSpace(s) historyLines = pushToHistory(rl, historyLines, s)
if len(historyLines) == 0 || historyLines[len(historyLines)-1] != s {
historyLines = append(historyLines, s)
if len(historyLines) > 500 {
historyLines = historyLines[len(historyLines)-500:]
}
if err := saveToHistory(*historyFile, historyLines); err != nil {
fatalf("cannot save query history: %s", err)
}
}
if err := rl.SaveToHistory(s); err != nil {
fatalf("cannot update query history: %s", err)
}
s = "" s = ""
rl.SetPrompt(firstLinePrompt) rl.SetPrompt(firstLinePrompt)
} }
} }
func pushToHistory(rl *readline.Instance, historyLines []string, s string) []string {
s = strings.TrimSpace(s)
if len(historyLines) == 0 || historyLines[len(historyLines)-1] != s {
historyLines = append(historyLines, s)
if len(historyLines) > 500 {
historyLines = historyLines[len(historyLines)-500:]
}
if err := saveToHistory(*historyFile, historyLines); err != nil {
fatalf("cannot save query history: %s", err)
}
}
if err := rl.SaveToHistory(s); err != nil {
fatalf("cannot update query history: %s", err)
}
return historyLines
}
func loadFromHistory(filePath string) ([]string, error) { func loadFromHistory(filePath string) ([]string, error) {
data, err := os.ReadFile(filePath) data, err := os.ReadFile(filePath)
if err != nil { if err != nil {