fix tabbing

This commit is contained in:
Liam Galvin 2018-12-03 12:00:36 +00:00
parent adee2d2284
commit 03fd345079
2 changed files with 30 additions and 4 deletions

View File

@ -709,10 +709,7 @@ func (buffer *Buffer) CarriageReturn() {
func (buffer *Buffer) Tab() {
tabSize := 4
shift := int(buffer.cursorX-1) % tabSize
if shift == 0 {
shift = tabSize
}
shift := tabSize - (int(buffer.cursorX+1) % tabSize)
for i := 0; i < shift; i++ {
buffer.Write(' ')
}

View File

@ -9,6 +9,35 @@ import (
"github.com/stretchr/testify/assert"
)
func TestTabbing(t *testing.T) {
b := NewBuffer(30, 3, CellAttributes{})
b.Write([]rune("hello")...)
b.Tab()
b.Write([]rune("x")...)
b.Tab()
b.Write([]rune("goodbye")...)
b.CarriageReturn()
b.NewLine()
b.Write([]rune("hell")...)
b.Tab()
b.Write([]rune("xxx")...)
b.Tab()
b.Write([]rune("good")...)
b.CarriageReturn()
b.NewLine()
expected := `
hello x goodbye
hell xxx good
`
lines := b.GetVisibleLines()
strs := []string{}
for _, l := range lines {
strs = append(strs, l.String())
}
require.Equal(t, strings.TrimSpace(expected), strings.Join(strs, "\n"))
}
func TestOffsets(t *testing.T) {
b := NewBuffer(10, 3, CellAttributes{})
b.Write([]rune("hello")...)