Skip to content

Commit

Permalink
Quote/unquote, fixes #6
Browse files Browse the repository at this point in the history
  • Loading branch information
ldemailly committed Aug 9, 2024
1 parent 1b76e00 commit ddcb1f6
Showing 1 changed file with 10 additions and 2 deletions.
12 changes: 10 additions & 2 deletions terminal.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,6 +7,7 @@ import (
"io"
"os"
"slices"
"strconv"

"fortio.org/log"
"fortio.org/term"
Expand Down Expand Up @@ -144,7 +145,14 @@ func readOrCreateHistory(f string) ([]string, error) {
var lines []string
scanner := bufio.NewScanner(h)
for scanner.Scan() {
lines = append(lines, scanner.Text())
// unquote to get the actual command
rl := scanner.Text()
l, err := strconv.Unquote(rl)
if err != nil {
log.Errf("Error unquoting history file %s for %q: %v", f, rl, err)
return nil, err
}
lines = append(lines, l)

Check warning on line 155 in terminal.go

View check run for this annotation

Codecov / codecov/patch

terminal.go#L148-L155

Added lines #L148 - L155 were not covered by tests
}
if err := scanner.Err(); err != nil {
log.Errf("Error reading history file %s: %v", f, err)
Expand All @@ -163,7 +171,7 @@ func saveHistory(f string, h []string) {
defer hf.Close()
// write lines separated by \n
for _, l := range h {
_, err := hf.WriteString(l + "\n")
_, err := hf.WriteString(strconv.Quote(l) + "\n")

Check warning on line 174 in terminal.go

View check run for this annotation

Codecov / codecov/patch

terminal.go#L174

Added line #L174 was not covered by tests
if err != nil {
log.Errf("Error writing history file %s: %v", f, err)
return
Expand Down

0 comments on commit ddcb1f6

Please sign in to comment.