1
0
mirror of https://github.com/br0xen/termbox-util.git synced 2024-11-22 21:43:14 +00:00

Text wrapping

no newlines though.
This commit is contained in:
Brian Buller 2016-01-10 20:42:51 -06:00
parent 53306b1f2e
commit e93feb0dde

View File

@ -2,7 +2,6 @@ package termboxUtil
import ( import (
"fmt" "fmt"
"strconv"
"strings" "strings"
"github.com/nsf/termbox-go" "github.com/nsf/termbox-go"
@ -135,8 +134,15 @@ func (i *InputField) HandleKeyPress(event termbox.Event) bool {
// Draw outputs the input field on the screen // Draw outputs the input field on the screen
func (i *InputField) Draw() { func (i *InputField) Draw() {
maxWidth := i.width
x, y := i.x, i.y
startX := i.x
if i.bordered { if i.bordered {
DrawBorder(i.x, i.y, i.x+i.width, i.y+i.height, i.fg, i.bg) DrawBorder(i.x, i.y, i.x+i.width, i.y+i.height, i.fg, i.bg)
maxWidth--
x++
y++
startX++
} }
var strPt1, strPt2 string var strPt1, strPt2 string
@ -160,28 +166,14 @@ func (i *InputField) Draw() {
} }
// strPt1, strPt2 = all of the text before, after the cursor // strPt1, strPt2 = all of the text before, after the cursor
// cursorRune is the rune on the cursor // cursorRune is the rune on the cursor
maxWidth := i.width
if i.bordered {
maxWidth--
}
DrawStringAtPoint(strconv.Itoa(strings.Count(i.value, "\n")), i.x, i.y, i.fg, i.bg)
if i.wrap { if i.wrap {
// Split the text into maxWidth chunks // Split the text into maxWidth chunks
x, y := i.x+1, i.y+1 for len(strPt1) > maxWidth { //|| nlCount > 0 {
nlCount := strings.Count(strPt1, "\n")
for len(strPt1) > maxWidth || nlCount > 0 {
nlIdx := strings.Index(strPt1, "\n")
breakAt := maxWidth breakAt := maxWidth
if nlIdx < maxWidth { DrawStringAtPoint(strPt1[:breakAt], x, y, i.fg, i.bg)
breakAt = nlIdx + 1 x = startX
}
x, y = DrawStringAtPoint(strPt1[:breakAt], x, y, i.fg, i.bg)
strPt1 = strPt1[breakAt:]
if len(strPt1) > 0 {
x = i.x + 1
y++ y++
} strPt1 = strPt1[breakAt:]
nlCount = strings.Count(strPt1, "\n")
} }
x, y = DrawStringAtPoint(strPt1, x, y, i.fg, i.bg) x, y = DrawStringAtPoint(strPt1, x, y, i.fg, i.bg)
if maxWidth-len(strPt1) <= 0 { if maxWidth-len(strPt1) <= 0 {