intercept line rendering using a 'Painter' (#116)

This commit is contained in:
Thomas Bradford 2017-10-02 05:58:28 +02:00 committed by chzyer
parent 9f56defe66
commit 707fd8ecaa
3 changed files with 19 additions and 3 deletions

View File

@ -504,3 +504,13 @@ func (d *DumpListener) OnChange(line []rune, pos int, key rune) (newLine []rune,
type Listener interface {
OnChange(line []rune, pos int, key rune) (newLine []rune, newPos int, ok bool)
}
type Painter interface {
Paint(line []rune, pos int) []rune
}
type defaultPainter struct{}
func (p *defaultPainter) Paint(line []rune, _ int) []rune {
return line
}

View File

@ -44,6 +44,8 @@ type Config struct {
// NOTE: Listener will be triggered by (nil, 0, 0) immediately
Listener Listener
Painter Painter
// If VimMode is true, readline will in vim.insert mode by default
VimMode bool
@ -149,6 +151,10 @@ func (c *Config) SetListener(f func(line []rune, pos int, key rune) (newLine []r
c.Listener = FuncListener(f)
}
func (c *Config) SetPainter(p Painter) {
c.Painter = p
}
func NewEx(cfg *Config) (*Instance, error) {
t, err := NewTerminal(cfg)
if err != nil {

View File

@ -490,11 +490,11 @@ func (r *RuneBuffer) output() []byte {
}
} else {
for idx := range r.buf {
if r.buf[idx] == '\t' {
for _, e := range r.cfg.Painter.Paint(r.buf, r.idx) {
if e == '\t' {
buf.WriteString(strings.Repeat(" ", TabWidth))
} else {
buf.WriteRune(r.buf[idx])
buf.WriteRune(e)
}
}
if r.isInLineEdge() {