peterh / liner

Pure Go line editor with history, inspired by linenoise
MIT License
1.04k stars 132 forks source link

Implemented history retrieval. #57

Closed pires closed 8 years ago

pires commented 8 years ago

Fixes #56

peterh commented 8 years ago

Returning an internal slice is not safe, even if you take the requisite mutex.

I recommend using a bytes.Buffer with the existing WriteHistory to retrieve the current history state.

pires commented 8 years ago

@peterh so instead of returning int, error it could return int, bytes.Buffer, error or just bytes.Buffer, error?

peterh commented 8 years ago

I mean instead of adding a function to liner (which already has too many functions), you could add an example to the documentation.

So in line_test.go you could add

func ExampleWriteHistory() {
   // Untested, please test and fix before submitting patch
   buf := new(bytes.Buffer)
   _, err := line.WriteHistory(buf)
   if err != nil {
      history := strings.Split(buf.String(), "\n")
      for i, line := range history {
         fmt.Println("History entry ", i, ": ", line)
      }
   }
}

and that example would show up in the documentation. See https://blog.golang.org/examples

pires commented 8 years ago

@peterh WriteHistory didn't seem like the method to use but it is and it was right in my face. Thanks for doing most of the work for me. PR on the way.