peterh / liner

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

panic: runtime error: integer divide by zero #52

Closed michaelmacinnis closed 8 years ago

michaelmacinnis commented 8 years ago

Long completions can cause liner to panic when the tab completion style is set to liner.TabPrints.

Run the modified example code below. When prompted for your name, type 'p' and then hit Tab twice.

package main

import (
        "log"
        "os"
        "strings"

        "github.com/peterh/liner"
)

var (
        history_fn = "/tmp/.liner_history"
        names      = []string{
                "john",
                "james",
                "mary",
                "nancy",
                "probably not a very common name but a useful string to demonstrate what currently happens with a completion longer than the terminal width",
                "panic appears to happen when tab completion style is TabPrints",
        }
)

func main() {
        line := liner.NewLiner()
        defer line.Close()

        line.SetCtrlCAborts(true)
        line.SetTabCompletionStyle(liner.TabPrints)

// Remainder unchanged.

        line.SetCompleter(func(line string) (c []string) {
                for _, n := range names {
                        if strings.HasPrefix(n, strings.ToLower(line)) {
                                c = append(c, n)
                        }
                }
                return
        })

        if f, err := os.Open(history_fn); err == nil {
                line.ReadHistory(f)
                f.Close()
        }

        if name, err := line.Prompt("What is your name? "); err == nil {
                log.Print("Got: ", name)
                line.AppendHistory(name)
        } else if err == liner.ErrPromptAborted {
                log.Print("Aborted")
        } else {
                log.Print("Error reading line: ", err)
        }

        if f, err := os.Create(history_fn); err != nil {
                log.Print("Error writing history file: ", err)
        } else {
                line.WriteHistory(f)
                f.Close()
        }
}
peterh commented 8 years ago

Thanks for the bug report.