From 99a0a449a19cc1cf32da70971ae77bafd26559e9 Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Sun, 3 Jan 2016 22:14:33 -0600 Subject: [PATCH 1/4] Still working on input field overflow --- termbox_inputfield.go | 38 +++++++++++++++++--------------------- 1 file changed, 17 insertions(+), 21 deletions(-) diff --git a/termbox_inputfield.go b/termbox_inputfield.go index ca072e8..34658d9 100644 --- a/termbox_inputfield.go +++ b/termbox_inputfield.go @@ -156,31 +156,27 @@ 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 // 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] + maxWidth := i.width + if i.bordered { + maxWidth -= 2 + } + cursorRune2 := cursorRune + if len(i.value) > maxWidth { + var chopLeft bool + for len(strPt1)+len(strPt2)+1 > maxWidth { + if chopLeft && len(strPt1) > 0 { + strPt1 = strPt1[1:] + } else if !chopLeft && len(strPt2) > 0 { + strPt2 = strPt2[:len(strPt2)-1] } + chopLeft = !chopLeft } } 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, cursorRune2, i.bg, i.fg) DrawStringAtPoint(strPt2, x+1, y, i.fg, i.bg) + termbox.SetCell(x, y+1, cursorRune, i.bg, i.fg) } From 31b785172d1e3953a9461aa443979f114f1c6f30 Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Mon, 4 Jan 2016 11:01:22 -0600 Subject: [PATCH 2/4] Starting work on Wrapping --- termbox_inputfield.go | 49 ++++++++++++++++++++++++++++++++----------- 1 file changed, 37 insertions(+), 12 deletions(-) diff --git a/termbox_inputfield.go b/termbox_inputfield.go index 34658d9..83ce9f9 100644 --- a/termbox_inputfield.go +++ b/termbox_inputfield.go @@ -158,25 +158,50 @@ func (i *InputField) Draw() { } // strPt1, strPt2 = all of the text before, after the cursor // cursorRune is the rune on the cursor - // Check if the value is longer than the width maxWidth := i.width if i.bordered { - maxWidth -= 2 + maxWidth-- } - cursorRune2 := cursorRune - if len(i.value) > maxWidth { - var chopLeft bool + 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 + } + 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 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 { + x, y = DrawStringAtPoint(strPt2[:maxWidth], x, y, i.fg, i.bg) + strPt2 = strPt2[maxWidth+1:] + continue + } + 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 chopLeft && len(strPt1) > 0 { + if len(strPt1) >= len(strPt2) { + if len(strPt1) == 0 { + break + } strPt1 = strPt1[1:] - } else if !chopLeft && len(strPt2) > 0 { + } else { strPt2 = strPt2[:len(strPt2)-1] } - chopLeft = !chopLeft } + 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) } - x, y := DrawStringAtPoint(strPt1, i.x+1, i.y+1, i.fg, i.bg) - termbox.SetCell(x, y, cursorRune2, i.bg, i.fg) - DrawStringAtPoint(strPt2, x+1, y, i.fg, i.bg) - termbox.SetCell(x, y+1, cursorRune, i.bg, i.fg) } From 9026f1a1ffa0b24a122302c75f91a32f1103b868 Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Mon, 4 Jan 2016 18:25:07 -0600 Subject: [PATCH 3/4] Working on Wrapping Still --- termbox_inputfield.go | 49 ++++++++++++++++++++++++++----------------- 1 file changed, 30 insertions(+), 19 deletions(-) diff --git a/termbox_inputfield.go b/termbox_inputfield.go index 83ce9f9..17cc4af 100644 --- a/termbox_inputfield.go +++ b/termbox_inputfield.go @@ -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, x, y, i.fg, i.bg) + 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 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 { + 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+1:] - continue + strPt2 = strPt2[maxWidth:] } x, y = DrawStringAtPoint(strPt2, x, y, i.fg, i.bg) } From ddda3d345033be317f4bd5e5c5dd8a7ad1e2b8e4 Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Wed, 6 Jan 2016 22:05:28 -0600 Subject: [PATCH 4/4] gofmt --- termbox_menu.go | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/termbox_menu.go b/termbox_menu.go index 44e7485..7d3b0e3 100644 --- a/termbox_menu.go +++ b/termbox_menu.go @@ -15,7 +15,7 @@ type Menu struct { disabledBg, disabledFg termbox.Attribute isDone bool bordered bool - vimMode bool + vimMode bool } // CreateMenu Creates a menu with the specified attributes