From 8637587b82b9af209bd0bea3ba9df85a30824285 Mon Sep 17 00:00:00 2001 From: Jesse Duffield Date: Sun, 23 Jul 2023 11:43:10 +1000 Subject: [PATCH] Better word wrap Word wrapping has been pretty bad so far so let's fix that. --- go.mod | 2 +- go.sum | 4 +- vendor/github.com/jesseduffield/gocui/view.go | 46 +++++++++++++++++-- vendor/modules.txt | 2 +- 4 files changed, 46 insertions(+), 8 deletions(-) diff --git a/go.mod b/go.mod index d46597ee5de..84f269a2f14 100644 --- a/go.mod +++ b/go.mod @@ -18,7 +18,7 @@ require ( github.com/integrii/flaggy v1.4.0 github.com/jesseduffield/generics v0.0.0-20220320043834-727e535cbe68 github.com/jesseduffield/go-git/v5 v5.1.2-0.20221018185014-fdd53fef665d - github.com/jesseduffield/gocui v0.3.1-0.20230719120401-398f4965241f + github.com/jesseduffield/gocui v0.3.1-0.20230723014157-03e858e46144 github.com/jesseduffield/kill v0.0.0-20220618033138-bfbe04675d10 github.com/jesseduffield/lazycore v0.0.0-20221012050358-03d2e40243c5 github.com/jesseduffield/minimal/gitignore v0.3.3-0.20211018110810-9cde264e6b1e diff --git a/go.sum b/go.sum index 41f5f3b47cf..8affcb7a94a 100644 --- a/go.sum +++ b/go.sum @@ -72,8 +72,8 @@ github.com/jesseduffield/generics v0.0.0-20220320043834-727e535cbe68 h1:EQP2Tv8T github.com/jesseduffield/generics v0.0.0-20220320043834-727e535cbe68/go.mod h1:+LLj9/WUPAP8LqCchs7P+7X0R98HiFujVFANdNaxhGk= github.com/jesseduffield/go-git/v5 v5.1.2-0.20221018185014-fdd53fef665d h1:bO+OmbreIv91rCe8NmscRwhFSqkDJtzWCPV4Y+SQuXE= github.com/jesseduffield/go-git/v5 v5.1.2-0.20221018185014-fdd53fef665d/go.mod h1:nGNEErzf+NRznT+N2SWqmHnDnF9aLgANB1CUNEan09o= -github.com/jesseduffield/gocui v0.3.1-0.20230719120401-398f4965241f h1:w/pxI34XepTAx4HwxUu8ipimbVRgSTS+7ahmgFQwH80= -github.com/jesseduffield/gocui v0.3.1-0.20230719120401-398f4965241f/go.mod h1:dJ/BEUt3OWtaRg/PmuJWendRqREhre9JQ1SLvqrVJ8s= +github.com/jesseduffield/gocui v0.3.1-0.20230723014157-03e858e46144 h1:gwy5JzP6+PhcPFG1obkUSLGcTkUY88sLKlCPOFjwtak= +github.com/jesseduffield/gocui v0.3.1-0.20230723014157-03e858e46144/go.mod h1:dJ/BEUt3OWtaRg/PmuJWendRqREhre9JQ1SLvqrVJ8s= github.com/jesseduffield/kill v0.0.0-20220618033138-bfbe04675d10 h1:jmpr7KpX2+2GRiE91zTgfq49QvgiqB0nbmlwZ8UnOx0= github.com/jesseduffield/kill v0.0.0-20220618033138-bfbe04675d10/go.mod h1:aA97kHeNA+sj2Hbki0pvLslmE4CbDyhBeSSTUUnOuVo= github.com/jesseduffield/lazycore v0.0.0-20221012050358-03d2e40243c5 h1:CDuQmfOjAtb1Gms6a1p5L2P8RhbLUq5t8aL7PiQd2uY= diff --git a/vendor/github.com/jesseduffield/gocui/view.go b/vendor/github.com/jesseduffield/gocui/view.go index 3577c9ccf3d..d4a27f736b5 100644 --- a/vendor/github.com/jesseduffield/gocui/view.go +++ b/vendor/github.com/jesseduffield/gocui/view.go @@ -1265,14 +1265,52 @@ func lineWrap(line []cell, columns int) [][]cell { var n int var offset int + lastWhitespaceIndex := -1 lines := make([][]cell, 0, 1) for i := range line { - rw := runewidth.RuneWidth(line[i].chr) + currChr := line[i].chr + rw := runewidth.RuneWidth(currChr) n += rw + // if currChr == 'g' { + // panic(n) + // } if n > columns { - n = rw - lines = append(lines, line[offset:i]) - offset = i + // This code is convoluted but we've got comprehensive tests so feel free to do whatever you want + // to the code to simplify it so long as our tests still pass. + if currChr == ' ' { + // if the line ends in a space, we'll omit it. This means there'll be no + // way to distinguish between a clean break and a mid-word break, but + // I think it's worth it. + lines = append(lines, line[offset:i]) + offset = i + 1 + n = 0 + } else if currChr == '-' { + // if the last character is hyphen and the width of line is equal to the columns + lines = append(lines, line[offset:i]) + offset = i + n = rw + } else if lastWhitespaceIndex != -1 && lastWhitespaceIndex+1 != i { + // if there is a space in the line and the line is not breaking at a space/hyphen + if line[lastWhitespaceIndex].chr == '-' { + // if break occurs at hyphen, we'll retain the hyphen + lines = append(lines, line[offset:lastWhitespaceIndex+1]) + offset = lastWhitespaceIndex + 1 + n = i - offset + } else { + // if break occurs at space, we'll omit the space + lines = append(lines, line[offset:lastWhitespaceIndex]) + offset = lastWhitespaceIndex + 1 + n = i - offset + 1 + } + } else { + // in this case we're breaking mid-word + lines = append(lines, line[offset:i]) + offset = i + n = rw + } + lastWhitespaceIndex = -1 + } else if line[i].chr == ' ' || line[i].chr == '-' { + lastWhitespaceIndex = i } } diff --git a/vendor/modules.txt b/vendor/modules.txt index 5aa0a325b6a..88efe8b86d0 100644 --- a/vendor/modules.txt +++ b/vendor/modules.txt @@ -172,7 +172,7 @@ github.com/jesseduffield/go-git/v5/utils/merkletrie/filesystem github.com/jesseduffield/go-git/v5/utils/merkletrie/index github.com/jesseduffield/go-git/v5/utils/merkletrie/internal/frame github.com/jesseduffield/go-git/v5/utils/merkletrie/noder -# github.com/jesseduffield/gocui v0.3.1-0.20230719120401-398f4965241f +# github.com/jesseduffield/gocui v0.3.1-0.20230723014157-03e858e46144 ## explicit; go 1.12 github.com/jesseduffield/gocui # github.com/jesseduffield/kill v0.0.0-20220618033138-bfbe04675d10