mirror of
https://github.com/br0xen/termbox-util.git
synced 2024-11-26 07:03:14 +00:00
Merge branch 'master' of gogs.bullercodeworks.com:brian/termbox-util
This commit is contained in:
commit
53306b1f2e
@ -2,6 +2,8 @@ package termboxUtil
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
|
"strconv"
|
||||||
|
"strings"
|
||||||
|
|
||||||
"github.com/nsf/termbox-go"
|
"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
|
// HandleKeyPress accepts the termbox event and returns whether it was consumed
|
||||||
func (i *InputField) HandleKeyPress(event termbox.Event) bool {
|
func (i *InputField) HandleKeyPress(event termbox.Event) bool {
|
||||||
if event.Key == termbox.KeyEnter {
|
if event.Key == termbox.KeyBackspace || event.Key == termbox.KeyBackspace2 {
|
||||||
// Done editing
|
|
||||||
} else if event.Key == termbox.KeyBackspace || event.Key == termbox.KeyBackspace2 {
|
|
||||||
if len(i.value) > 0 {
|
if len(i.value) > 0 {
|
||||||
i.value = i.value[:len(i.value)-1]
|
i.value = i.value[:len(i.value)-1]
|
||||||
}
|
}
|
||||||
@ -103,8 +103,8 @@ func (i *InputField) HandleKeyPress(event termbox.Event) bool {
|
|||||||
i.cursor++
|
i.cursor++
|
||||||
}
|
}
|
||||||
} else if event.Key == termbox.KeyCtrlU {
|
} else if event.Key == termbox.KeyCtrlU {
|
||||||
// Ctrl+U Clears the Input
|
// Ctrl+U Clears the Input (before the cursor)
|
||||||
i.value = ""
|
i.value = i.value[i.cursor:]
|
||||||
} else {
|
} else {
|
||||||
// Get the rune to add to our value. Space and Tab are special cases where
|
// Get the rune to add to our value. Space and Tab are special cases where
|
||||||
// we can't use the event's rune directly
|
// we can't use the event's rune directly
|
||||||
@ -114,6 +114,8 @@ func (i *InputField) HandleKeyPress(event termbox.Event) bool {
|
|||||||
ch = " "
|
ch = " "
|
||||||
case termbox.KeyTab:
|
case termbox.KeyTab:
|
||||||
ch = "\t"
|
ch = "\t"
|
||||||
|
case termbox.KeyEnter:
|
||||||
|
ch = "\n"
|
||||||
default:
|
default:
|
||||||
ch = string(event.Ch)
|
ch = string(event.Ch)
|
||||||
}
|
}
|
||||||
@ -156,31 +158,61 @@ func (i *InputField) Draw() {
|
|||||||
} else {
|
} else {
|
||||||
strPt1, strPt2, cursorRune = "", "", ' '
|
strPt1, strPt2, cursorRune = "", "", ' '
|
||||||
}
|
}
|
||||||
// Check if the value is longer than the width
|
// strPt1, strPt2 = all of the text before, after the cursor
|
||||||
if len(i.value) > i.width {
|
// 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 {
|
||||||
// If we're wrapping the text, figure out how that goes
|
// Split the text into maxWidth chunks
|
||||||
} else {
|
x, y := i.x+1, i.y+1
|
||||||
// Not wrapping, so figure out what we need to trim
|
nlCount := strings.Count(strPt1, "\n")
|
||||||
// We have i.width/2 space for each strPt
|
for len(strPt1) > maxWidth || nlCount > 0 {
|
||||||
if len(strPt1) > i.width/2 {
|
nlIdx := strings.Index(strPt1, "\n")
|
||||||
if len(strPt2) > i.width/2 {
|
breakAt := maxWidth
|
||||||
// Both sides are too long, center the cursor
|
if nlIdx < maxWidth {
|
||||||
} else {
|
breakAt = nlIdx + 1
|
||||||
// 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 {
|
x, y = DrawStringAtPoint(strPt1[:breakAt], x, y, i.fg, i.bg)
|
||||||
// Just side 2 is too long, figure out how much we can show
|
strPt1 = strPt1[breakAt:]
|
||||||
tmp := i.width - 1
|
if len(strPt1) > 0 {
|
||||||
tmp -= len(strPt1)
|
x = i.x + 1
|
||||||
strPt2 = strPt2[:tmp]
|
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)):]
|
||||||
|
}
|
||||||
|
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:]
|
||||||
|
}
|
||||||
|
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 {
|
||||||
|
break
|
||||||
|
}
|
||||||
|
strPt1 = strPt1[1:]
|
||||||
|
} else {
|
||||||
|
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)
|
termbox.SetCell(x, y, cursorRune, i.bg, i.fg)
|
||||||
DrawStringAtPoint(strPt2, x+1, y, i.fg, i.bg)
|
DrawStringAtPoint(strPt2, x+1, y, i.fg, i.bg)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user