Skip to content

v1.6.0

Compare
Choose a tag to compare
@hymkor hymkor released this 04 Nov 13:49
· 11 commits to master since this release
  • Enable to replace the function to predict (入力予想機能の予想関数を差し替えられるようにした)
package main

import (
	"context"
	"fmt"
	"strings"

	"github.com/nyaosorg/go-readline-ny"
	"github.com/nyaosorg/go-readline-ny/simplehistory"
)

func predictByHistory(B *readline.Buffer) string {
	current := strings.TrimSpace(B.String())
	for i := B.History.Len() - 1; i >= 0; i-- {
		h := strings.TrimSpace(B.History.At(i))
		if strings.HasPrefix(h, current) {
			return h[len(current):]
		}
	}
	return ""
}

func main() {
	history := simplehistory.New()

	editor := &readline.Editor{
		PredictColor: [...]string{"\x1B[3;22;34m", "\x1B[23;39m"},
		Predictor:    predictByHistory,
		History:      history,
	}
	for {
		text, err := editor.ReadLine(context.Background())
		if err != nil {
			fmt.Printf("ERR=%s\n", err.Error())
			return
		}
		fmt.Printf("TEXT=%s\n", text)
		history.Add(text)
	}
}

__example160