1
0
mirror of https://github.com/br0xen/termbox-util.git synced 2024-11-22 13:33:15 +00:00

Adjusting input field

This commit is contained in:
Brian Buller 2016-01-14 09:48:46 -06:00
parent e843ea92b9
commit b2f731cbbb

View File

@ -1,10 +1,6 @@
package termboxUtil
import (
"strings"
"github.com/nsf/termbox-go"
)
import "github.com/nsf/termbox-go"
// InputField is a field for inputting text
type InputField struct {
@ -121,10 +117,12 @@ func (i *InputField) HandleKeyPress(event termbox.Event) bool {
ch = " "
case termbox.KeyTab:
ch = "\t"
/* Multiline is disabled right now
case termbox.KeyEnter:
if i.multiline {
ch = "\n"
}
*/
default:
if KeyIsAlphaNumeric(event) || KeyIsSymbol(event) {
ch = string(event.Ch)
@ -161,61 +159,6 @@ func (i *InputField) Draw() {
startY++
}
var valSplit []string //, cursorLine []string
if i.multiline {
valSplit = strings.Split(i.value, "\n")
} else {
if i.wrap {
var j int
for j < len(i.value) {
l, h := j, j+i.width
//if l >= i.cursor && h <= i.cursor {
// cursorLine = append(cursorLine, i.value[l:i.cursor])
// cursorLine = append(cursorLine, i.value[i.cursor]...)
// cursorLine = append(cursorLine, i.value[i.cursor:h])
//} else {
valSplit = append(valSplit, i.value[l:h])
//}
j = h
}
}
}
for j := range valSplit {
DrawStringAtPoint(valSplit[j], x, y+j, i.fg, i.bg)
}
//var valSplit []string
// if it's not multiline, new lines aren't allowed in the input
/*
multiSplit := strings.Split(i.value, "\n")
var cursCount int
for j := range multiSplit {
for k := range multiSplit[j] {
if cursCount == i.cursor {
termbox.SetCell(x+k, y+j, rune(multiSplit[j][k]), i.fg, i.bg)
} else {
termbox.SetCell(x+k, y+j, rune(multiSplit[j][k]), i.bg, i.fg)
}
cursCount++
}
}
*/
/*
if i.wrap {
// Automatically wrap the text
for j := range multiSplit {
for len(multiSplit[j]) > maxWidth {
valSplit = append(valSplit, multiSplit[j][:maxWidth])
multiSplit[j] = multiSplit[j][maxWidth:]
}
}
} else {
valSplit = multiSplit
}
for j := range valSplit {
DrawStringAtPoint(valSplit[j], x, y+j, i.fg, i.bg)
}
/*
var strPt1, strPt2 string
var cursorRune rune
if len(i.value) > 0 {
@ -235,8 +178,12 @@ func (i *InputField) Draw() {
} else {
strPt1, strPt2, cursorRune = "", "", ' '
}
// strPt1, strPt2 = all of the text before, after the cursor
// cursorRune is the rune on the cursor
// Original:
/*
x, y = DrawStringAtPoint(strPt1, i.x+1, i.y+1, i.fg, i.bg)
termbox.SetCell(x, y, cursorRune, i.bg, i.fg)
DrawStringAtPoint(strPt2, x+1, y, i.fg, i.bg)
*/
if i.wrap {
// Split the text into maxWidth chunks
for len(strPt1) > maxWidth {
@ -269,7 +216,6 @@ func (i *InputField) Draw() {
x, y = DrawStringAtPoint(strPt2, x, y, i.fg, i.bg)
}
} else {
// Not wrapping, just adjust the viewport
for len(strPt1)+len(strPt2)+1 > maxWidth {
if len(strPt1) >= len(strPt2) {
if len(strPt1) == 0 {
@ -280,9 +226,8 @@ func (i *InputField) Draw() {
strPt2 = strPt2[:len(strPt2)-1]
}
}
x, y := DrawStringAtPoint(strPt1, i.x+1, i.y+1, i.fg, i.bg)
x, y = DrawStringAtPoint(strPt1, i.x+1, i.y+1, i.fg, i.bg)
termbox.SetCell(x, y, cursorRune, i.bg, i.fg)
DrawStringAtPoint(strPt2, x+1, y, i.fg, i.bg)
}
*/
}