1
0
mirror of https://github.com/br0xen/termbox-util.git synced 2024-11-25 23:03:13 +00:00

Working on Wrapping Still

This commit is contained in:
Brian Buller 2016-01-04 18:25:07 -06:00
parent 31b785172d
commit 9026f1a1ff

View File

@ -2,6 +2,8 @@ package termboxUtil
import (
"fmt"
"strconv"
"strings"
"github.com/nsf/termbox-go"
)
@ -88,9 +90,7 @@ func (i *InputField) SetWrap(b bool) *InputField {
// HandleKeyPress accepts the termbox event and returns whether it was consumed
func (i *InputField) HandleKeyPress(event termbox.Event) bool {
if event.Key == termbox.KeyEnter {
// Done editing
} else if event.Key == termbox.KeyBackspace || event.Key == termbox.KeyBackspace2 {
if event.Key == termbox.KeyBackspace || event.Key == termbox.KeyBackspace2 {
if len(i.value) > 0 {
i.value = i.value[:len(i.value)-1]
}
@ -103,8 +103,8 @@ func (i *InputField) HandleKeyPress(event termbox.Event) bool {
i.cursor++
}
} else if event.Key == termbox.KeyCtrlU {
// Ctrl+U Clears the Input
i.value = ""
// Ctrl+U Clears the Input (before the cursor)
i.value = i.value[i.cursor:]
} else {
// Get the rune to add to our value. Space and Tab are special cases where
// we can't use the event's rune directly
@ -114,6 +114,8 @@ func (i *InputField) HandleKeyPress(event termbox.Event) bool {
ch = " "
case termbox.KeyTab:
ch = "\t"
case termbox.KeyEnter:
ch = "\n"
default:
ch = string(event.Ch)
}
@ -162,29 +164,38 @@ func (i *InputField) Draw() {
if i.bordered {
maxWidth--
}
DrawStringAtPoint(strconv.Itoa(strings.Count(i.value, "\n")), i.x, i.y, i.fg, i.bg)
if i.wrap {
// Split the text into maxWidth chunks
x, y := i.x+1, i.y+1
for len(strPt1) > 0 {
if len(strPt1) > maxWidth {
x, y = DrawStringAtPoint(strPt1[:maxWidth], x, y, i.fg, i.bg)
strPt1 = strPt1[maxWidth+1:]
continue
nlCount := strings.Count(strPt1, "\n")
for len(strPt1) > maxWidth || nlCount > 0 {
nlIdx := strings.Index(strPt1, "\n")
breakAt := maxWidth
if nlIdx < maxWidth {
breakAt = nlIdx + 1
}
x, y = DrawStringAtPoint(strPt1[:breakAt], x, y, i.fg, i.bg)
strPt1 = strPt1[breakAt:]
if len(strPt1) > 0 {
x = i.x + 1
y++
}
nlCount = strings.Count(strPt1, "\n")
}
x, y = DrawStringAtPoint(strPt1, x, y, i.fg, i.bg)
}
if maxWidth-len(strPt1) <= 0 {
termbox.SetCell(x, y, cursorRune, i.bg, i.fg)
}
if len(strPt2) > 0 {
if maxWidth-len(strPt1)-1 > 0 {
DrawStringAtPoint(strPt2[:(maxWidth-len(strPt1)-1)], x+1, y, i.fg, i.bg)
strPt2 = strPt2[(maxWidth - len(strPt1)):]
}
for len(strPt2) > 0 {
if len(strPt2) > maxWidth {
nlCount := strings.Count(strPt2, "\n")
for len(strPt2) > maxWidth || nlCount > 0 {
x, y = DrawStringAtPoint(strPt2[:maxWidth], x, y, i.fg, i.bg)
strPt2 = strPt2[maxWidth+1:]
continue
strPt2 = strPt2[maxWidth:]
}
x, y = DrawStringAtPoint(strPt2, x, y, i.fg, i.bg)
}