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

Add Wrapping Flags to Input Fields

This commit is contained in:
Brian Buller 2016-01-01 12:15:09 -06:00
parent c5ed6dc0ba
commit 9f978a3ec0
2 changed files with 41 additions and 0 deletions

View File

@ -13,6 +13,7 @@ type InputField struct {
cursor int
fg, bg termbox.Attribute
bordered bool
wrap bool
}
// CreateInputField creates an input field at x, y that is w by h
@ -75,6 +76,16 @@ func (i *InputField) SetBordered(b bool) *InputField {
return i
}
// DoesWrap returns true or false if this input field wraps text
func (i *InputField) DoesWrap() bool { return i.wrap }
// SetWrap sets whether we wrap the text at width.
// If 'wrap' is set, we automatically increase the height when we need to.
func (i *InputField) SetWrap(b bool) *InputField {
i.wrap = b
return i
}
// HandleKeyPress accepts the termbox event and returns whether it was consumed
func (i *InputField) HandleKeyPress(event termbox.Event) bool {
if event.Key == termbox.KeyEnter {
@ -145,6 +156,30 @@ func (i *InputField) Draw() {
} else {
strPt1, strPt2, cursorRune = "", "", ' '
}
// Check if the value is longer than the width
if len(i.value) > i.width {
if i.wrap {
// If we're wrapping the text, figure out how that goes
} else {
// Not wrapping, so figure out what we need to trim
// We have i.width/2 space for each strPt
if len(strPt1) > i.width/2 {
if len(strPt2) > i.width/2 {
// Both sides are too long, center the cursor
} else {
// Just side 1 is too long, figure out how much we can show
tmp := i.width - 1
tmp -= len(strPt2)
strPt1 = strPt1[tmp:]
}
} else if len(strPt2) > i.width/2 {
// Just side 2 is too long, figure out how much we can show
tmp := i.width - 1
tmp -= len(strPt1)
strPt2 = strPt2[:tmp]
}
}
}
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)

View File

@ -139,6 +139,12 @@ func (i *InputModal) SetValue(s string) *InputModal {
return i
}
// SetInputWrap sets whether the input field will wrap long text or not
func (i *InputModal) SetInputWrap(b bool) *InputModal {
i.input.SetWrap(b)
return i
}
// Clear Resets all non-positional parameters of the modal
func (i *InputModal) Clear() *InputModal {
i.title = ""