diff options
author | Michael Muré <batolettre@gmail.com> | 2018-08-03 17:29:11 +0200 |
---|---|---|
committer | Michael Muré <batolettre@gmail.com> | 2018-08-03 17:29:11 +0200 |
commit | 5c86164f22a4939a871810685cd161a2561deebc (patch) | |
tree | 329769209d2c5a6bc00c741acd5a1bb150f777bc /termui | |
parent | d88d59e9c56d6077cb5b827992d6717f98823530 (diff) | |
download | git-bug-5c86164f22a4939a871810685cd161a2561deebc.tar.gz |
util: add a text wrapping function
Diffstat (limited to 'termui')
-rw-r--r-- | termui/error_popup.go | 26 |
1 files changed, 2 insertions, 24 deletions
diff --git a/termui/error_popup.go b/termui/error_popup.go index e46a00e7..855bd05d 100644 --- a/termui/error_popup.go +++ b/termui/error_popup.go @@ -2,8 +2,8 @@ package termui import ( "fmt" + "github.com/MichaelMure/git-bug/util" "github.com/jroimartin/gocui" - "strings" ) const errorPopupView = "errorPopupView" @@ -37,7 +37,7 @@ func (ep *errorPopup) layout(g *gocui.Gui) error { maxX, maxY := g.Size() width := minInt(30, maxX) - wrapped, nblines := word_wrap(ep.message, width-2) + wrapped, nblines := util.WordWrap(ep.message, width-2) height := minInt(nblines+2, maxY) x0 := (maxX - width) / 2 y0 := (maxY - height) / 2 @@ -69,25 +69,3 @@ func (ep *errorPopup) close(g *gocui.Gui, v *gocui.View) error { func (ep *errorPopup) activate(message string) { ep.message = message } - -func word_wrap(text string, lineWidth int) (string, int) { - words := strings.Fields(strings.TrimSpace(text)) - if len(words) == 0 { - return text, 1 - } - lines := 1 - wrapped := words[0] - spaceLeft := lineWidth - len(wrapped) - for _, word := range words[1:] { - if len(word)+1 > spaceLeft { - wrapped += "\n" + word - spaceLeft = lineWidth - len(word) - lines++ - } else { - wrapped += " " + word - spaceLeft -= 1 + len(word) - } - } - - return wrapped, lines -} |