Skip to content

unexpected history behavior #1

@emicklei

Description

@emicklei

I am working on a small example that uses the readline package

When I enter each number 1,2,3 and use the arrow keys to navigate the history, I get the sequence 2,3,2,1

$ go run cli.go
>1
1
>2
2
>3
3
1

When I restart the program and enter A then this entry is not showing in the history. I was expecting this.

Maybe I setup the program wrong. (what is the role of the ps2 argument in NewLine?)

package main

import (
    "fmt"
    "log"
    "os"
    "path"

    "github.com/kless/term"
    "github.com/kless/term/readline"
)

var historyFile = path.Join(os.TempDir(), "test_history")

func main() {
    hist, err := readline.NewHistoryOfSize(historyFile, 10)
    if err != nil {
        log.Fatal("could not create history", err)
    }
    ter, _ := term.New()
    defer func() {
        if err := ter.Restore(); err != nil {
            log.Print(err)
        }
    }()
    l, err := readline.NewLine(ter, ">", "?", 1, hist)
    if err != nil {
        log.Fatal("NewLine failed", err)
    }
    for {
        entry, err := l.Read()
        if err != nil {
            log.Fatal("Read failed", err)
        }
        if entry == "exit" || entry == "quit" {
            break
        }
        fmt.Println(entry)
    }

}

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions