WordWrap does not wrap text containing brackets properly #27

Open
opened 2 years ago by makeworld-the-better-one · 18 comments
makeworld-the-better-one commented 2 years ago (Migrated from gitlab.com)
Owner
package main

import (
	"fmt"

	"gitlab.com/tslocum/cview"
)

func main() {
	lines := cview.WordWrap(cview.Escape("In [Chinese astronomy], the stars that correspond to Gemini are located in two areas: the [White Tiger of the West] (西方白虎, *Xī Fāng Bái Hǔ*) and the [Vermillion Bird of the South] (南方朱雀, *Nán Fāng Zhū Què*)."), 100)
	for i := range lines {
		fmt.Println(lines[i])
	}
}

The above code results in the following:

In [Chinese astronomy[], the stars that correspond to Gemini are located in two areas: the [White
Tiger of the West]
(西方白虎, *Xī Fāng Bái Hǔ*) and the [Vermillion Bird of the South[] (南方朱雀, *Nán Fāng Zhū Què*).

The expected result is:

In [Chinese astronomy[], the stars that correspond to Gemini are located in two areas: the [White
Tiger of the West[] (西方白虎, *Xī Fāng Bái Hǔ*) and the [Vermillion Bird of the South[] (南方朱雀, *Nán
Fāng Zhū Què*).
```go package main import ( "fmt" "gitlab.com/tslocum/cview" ) func main() { lines := cview.WordWrap(cview.Escape("In [Chinese astronomy], the stars that correspond to Gemini are located in two areas: the [White Tiger of the West] (西方白虎, *Xī Fāng Bái Hǔ*) and the [Vermillion Bird of the South] (南方朱雀, *Nán Fāng Zhū Què*)."), 100) for i := range lines { fmt.Println(lines[i]) } } ``` The above code results in the following: ``` In [Chinese astronomy[], the stars that correspond to Gemini are located in two areas: the [White Tiger of the West] (西方白虎, *Xī Fāng Bái Hǔ*) and the [Vermillion Bird of the South[] (南方朱雀, *Nán Fāng Zhū Què*). ``` The expected result is: ``` In [Chinese astronomy[], the stars that correspond to Gemini are located in two areas: the [White Tiger of the West[] (西方白虎, *Xī Fāng Bái Hǔ*) and the [Vermillion Bird of the South[] (南方朱雀, *Nán Fāng Zhū Què*). ```
makeworld-the-better-one commented 2 years ago (Migrated from gitlab.com)
Owner

changed title from WordWrap {-slice bounds out of range-} panic with brackets and asian characters to WordWrap {+"slice bounds out of range"+} panic with brackets and asian characters

changed title from **WordWrap {-slice bounds out of range-} panic with brackets and asian characters** to **WordWrap {+"slice bounds out of range"+} panic with brackets and asian characters**
makeworld-the-better-one commented 2 years ago (Migrated from gitlab.com)
Owner

Here's a very simple file that causes the error with the string I provided:

package main

import (
	"gitlab.com/tslocum/cview"
)

func main() {
	cview.WordWrap(cview.Escape("In [Chinese astronomy], the stars that correspond to Gemini are located in two areas: the [White Tiger of the West] (西方白虎, *Xī Fāng Bái Hǔ*) and the [Vermillion Bird of the South] (南方朱雀, *Nán Fāng Zhū Què*)."), 100)
}

The panic output:

panic: runtime error: slice bounds out of range [:17] with length 12

goroutine 1 [running]:
gitlab.com/tslocum/cview.WordWrap.func1(0xc000192062, 0xc, 0x62, 0x5f4e80, 0xc)
	/home/makeworld/go/src/gitlab.com/tslocum/cview/util.go:484 +0x117
gitlab.com/tslocum/cview.WordWrap.func2(0xc0000000e1, 0x0, 0x0, 0x0, 0xd1, 0x2, 0xc5, 0x1, 0xc000106a00)
	/home/makeworld/go/src/gitlab.com/tslocum/cview/util.go:532 +0x341
gitlab.com/tslocum/cview.iterateString(0xc0001921e0, 0xe7, 0xc00018de68, 0xffffffffffffffff)
	/home/makeworld/go/src/gitlab.com/tslocum/cview/util.go:591 +0x187
gitlab.com/tslocum/cview.WordWrap(0xc000192000, 0xea, 0x64, 0xc0000107c0, 0x1, 0x1)
	/home/makeworld/go/src/gitlab.com/tslocum/cview/util.go:489 +0x3a2
main.main()
	/tmp/ww.go:8 +0x78
exit status 2

Maybe this is related to #23?

Here's a very simple file that causes the error with the string I provided: ```go package main import ( "gitlab.com/tslocum/cview" ) func main() { cview.WordWrap(cview.Escape("In [Chinese astronomy], the stars that correspond to Gemini are located in two areas: the [White Tiger of the West] (西方白虎, *Xī Fāng Bái Hǔ*) and the [Vermillion Bird of the South] (南方朱雀, *Nán Fāng Zhū Què*)."), 100) } ``` The panic output: ``` panic: runtime error: slice bounds out of range [:17] with length 12 goroutine 1 [running]: gitlab.com/tslocum/cview.WordWrap.func1(0xc000192062, 0xc, 0x62, 0x5f4e80, 0xc) /home/makeworld/go/src/gitlab.com/tslocum/cview/util.go:484 +0x117 gitlab.com/tslocum/cview.WordWrap.func2(0xc0000000e1, 0x0, 0x0, 0x0, 0xd1, 0x2, 0xc5, 0x1, 0xc000106a00) /home/makeworld/go/src/gitlab.com/tslocum/cview/util.go:532 +0x341 gitlab.com/tslocum/cview.iterateString(0xc0001921e0, 0xe7, 0xc00018de68, 0xffffffffffffffff) /home/makeworld/go/src/gitlab.com/tslocum/cview/util.go:591 +0x187 gitlab.com/tslocum/cview.WordWrap(0xc000192000, 0xea, 0x64, 0xc0000107c0, 0x1, 0x1) /home/makeworld/go/src/gitlab.com/tslocum/cview/util.go:489 +0x3a2 main.main() /tmp/ww.go:8 +0x78 exit status 2 ``` Maybe this is related to #23?
makeworld-the-better-one commented 2 years ago (Migrated from gitlab.com)
Owner

I don't think this issue is fixed.

When I run the following file with the lastest cview commit:

package main

import (
	"fmt"

	"gitlab.com/tslocum/cview"
)

func main() {
	lines := cview.WordWrap(cview.Escape("In [Chinese astronomy], the stars that correspond to Gemini are located in two areas: the [White Tiger of the West] (西方白虎, *Xī Fāng Bái Hǔ*) and the [Vermillion Bird of the South] (南方朱雀, *Nán Fāng Zhū Què*)."), 100)
	for i := range lines {
		fmt.Println(lines[i])
	}
}

The output I get is:

In [Chinese astronomy[], the stars that correspond to Gemini are located in two areas: the [White
Tiger of the
West] (西方白虎, *Xī Fāng Bái Hǔ*) and the [Vermillion Bird of the South[] (南方朱雀, *Nán Fāng Zhū Q
uè*).

The expected output would be:

In [Chinese astronomy[], the stars that correspond to Gemini are located in two areas: the [White
Tiger of the West[] (西方白虎, *Xī Fāng Bái Hǔ*) and the [Vermillion Bird of the South[] (南方朱雀, *Nán
Fāng Zhū Què*).

Note how there is an additional bug in cview.Escape, where the text [White Tiger of the West] is left unchanged, when actually the brackets should be escaped: [White Tiger of the West[]. Weirdly, it works fine with the first bracketed section, [Chinese astronomy].

I don't think this issue is fixed. When I run the following file with the lastest cview commit: ```go package main import ( "fmt" "gitlab.com/tslocum/cview" ) func main() { lines := cview.WordWrap(cview.Escape("In [Chinese astronomy], the stars that correspond to Gemini are located in two areas: the [White Tiger of the West] (西方白虎, *Xī Fāng Bái Hǔ*) and the [Vermillion Bird of the South] (南方朱雀, *Nán Fāng Zhū Què*)."), 100) for i := range lines { fmt.Println(lines[i]) } } ``` The output I get is: ``` In [Chinese astronomy[], the stars that correspond to Gemini are located in two areas: the [White Tiger of the West] (西方白虎, *Xī Fāng Bái Hǔ*) and the [Vermillion Bird of the South[] (南方朱雀, *Nán Fāng Zhū Q uè*). ``` The expected output would be: ``` In [Chinese astronomy[], the stars that correspond to Gemini are located in two areas: the [White Tiger of the West[] (西方白虎, *Xī Fāng Bái Hǔ*) and the [Vermillion Bird of the South[] (南方朱雀, *Nán Fāng Zhū Què*). ``` Note how there is an additional bug in `cview.Escape`, where the text `[White Tiger of the West]` is left unchanged, when actually the brackets should be escaped: `[White Tiger of the West[]`. Weirdly, it works fine with the first bracketed section, `[Chinese astronomy]`.
tslocum commented 2 years ago (Migrated from gitlab.com)
Owner

Thanks, I'll look into this further.

Thanks, I'll look into this further.
makeworld-the-better-one commented 2 years ago (Migrated from gitlab.com)
Owner

Wait, how is this a solution? What should I be doing if I want to display text that is both escaped and wordwrapped?

Wait, how is this a solution? What should I be doing if I want to display text that is both escaped and wordwrapped?
tslocum commented 2 years ago (Migrated from gitlab.com)
Owner

From what I found Wordwrap was not intended for this use case. I think the best solution is to write escaped text to a TextView and make use of its internal wrapping. I've just added TextView.SetWrapWidth to facilitate this.

From what I found `Wordwrap` was not intended for this use case. I think the best solution is to write escaped text to a `TextView` and make use of its internal wrapping. I've just added `TextView.SetWrapWidth` to facilitate this.
makeworld-the-better-one commented 2 years ago (Migrated from gitlab.com)
Owner

Okay, thank you. But being able to use WordWrap for this would still be handy though, at least for my personal project. And I don't see why it should care whether the text is escaped before or not, is this not still a problem/bug?

Okay, thank you. But being able to use `WordWrap` for this would still be handy though, at least for my personal project. And I don't see why it should care whether the text is escaped before or not, is this not still a problem/bug?
makeworld-the-better-one commented 2 years ago (Migrated from gitlab.com)
Owner

mentioned in issue #23

mentioned in issue #23
tslocum commented 2 years ago (Migrated from gitlab.com)
Owner

Could you elaborate on why using SetWrapWidth is insufficient? I will look into another solution once I understand more clearly.

Regarding passing WordWrap escaped text, either:

  • decomposeText does parse and index escaped tags properly, or
  • WordWrap does not correctly process the indices of escaped tags returned by decomposeText

I've put some additional time into investigating this, but I'm still stumped. Assistance would be appreciated.

Could you elaborate on why using `SetWrapWidth` is insufficient? I will look into another solution once I understand more clearly. Regarding passing `WordWrap` escaped text, either: - `decomposeText` does parse and index escaped tags properly, or - `WordWrap` does not correctly process the indices of escaped tags returned by `decomposeText` I've put some additional time into investigating this, but I'm still stumped. Assistance would be appreciated.
tslocum commented 2 years ago (Migrated from gitlab.com)
Owner

changed title from WordWrap {-"slice bounds out of range" panic with brackets and asian characters-} to WordWrap {+does not wrap escaped text properly+}

changed title from **WordWrap {-"slice bounds out of range" panic with brackets and asian characters-}** to **WordWrap {+does not wrap escaped text properly+}**
tslocum commented 2 years ago (Migrated from gitlab.com)
Owner

changed the description

changed the description
makeworld-the-better-one commented 2 years ago (Migrated from gitlab.com)
Owner

Could you elaborate on why using SetWrapWidth is insufficient?

I was just surprised it wouldn't work here, but I guess it makes sense now, I see how tags (which are zero width) make things more complicated. I haven't implemented a solution using SetWrapWidth yet, but if there are problems when I do I'll let you know here. Thank you.

> Could you elaborate on why using `SetWrapWidth` is insufficient? I was just surprised it wouldn't work here, but I guess it makes sense now, I see how tags (which are zero width) make things more complicated. I haven't implemented a solution using `SetWrapWidth` yet, but if there are problems when I do I'll let you know here. Thank you.
makeworld-the-better-one commented 2 years ago (Migrated from gitlab.com)
Owner

Could you elaborate on why using SetWrapWidth is insufficient? I will look into another solution once I understand more clearly.

Now that I am actually trying to use this in Amfora (see here), I have a better answer to this.

Sometimes I want to wrap certain lines of text in different ways. For example, if I have a long line that's part of a bullet point, I wrap that line using WordWrap, and indent all the wrapped lines after the first one. And then continue to use normal wrapping for the following lines.

This isn't possible with SetWrapWidth, and so I need WordWrap to be able to handle all kinds of characters, like the ones mentioned in the original issue, as well as ones with brackets like described in #23 and in this upstream issue.

I've tried to look into this, but I'll admit I find the function pretty difficult to understand. Any work you could do on this would be appreciated.

> Could you elaborate on why using `SetWrapWidth` is insufficient? I will look into another solution once I understand more clearly. Now that I am actually trying to use this in Amfora ([see here](https://github.com/makeworld-the-better-one/amfora/pull/107)), I have a better answer to this. Sometimes I want to wrap certain lines of text in different ways. For example, if I have a long line that's part of a bullet point, I wrap that line using `WordWrap`, and indent all the wrapped lines after the first one. And then continue to use normal wrapping for the following lines. This isn't possible with `SetWrapWidth`, and so I need `WordWrap` to be able to handle all kinds of characters, like the ones mentioned in the original issue, as well as ones with brackets like described in #23 and in this [upstream issue](https://github.com/makeworld-the-better-one/amfora/issues/156). I've tried to look into this, but I'll admit I find the function pretty difficult to understand. Any work you could do on this would be appreciated.
makeworld-the-better-one commented 2 years ago (Migrated from gitlab.com)
Owner

@tslocum any update on this? Thanks.

@tslocum any update on this? Thanks.
tslocum added the
bug
label 2 years ago
Owner

I have spent some more time investigating this, there appears to be an issue with the escapeIndices returned by decomposeText when the input text contains square brackets, or the escapeIndices are not being used correctly in WordWrap.

Another example of incorrectly wrapped text is available in this comment: #72.

I've added tests for this on the wordwrap branch. Any help would be appreciated.

I have spent some more time investigating this, there appears to be an issue with the `escapeIndices` returned by `decomposeText` when the input text contains square brackets, or the `escapeIndices` are not being used correctly in `WordWrap`. Another example of incorrectly wrapped text is available in this comment: https://code.rocketnine.space/tslocum/cview/issues/72#issuecomment-3968. I've added tests for this on the `wordwrap` branch. Any help would be appreciated.
tslocum added the
help wanted
label 1 year ago
tslocum changed title from WordWrap does not wrap escaped text properly to WordWrap does not wrap text containing brackets properly 1 year ago
Owner

This blog post may be useful: https://www.foonathan.net/2021/02/column/

This blog post may be useful: https://www.foonathan.net/2021/02/column/

The downstream issue no longer occurs in Amfora, strangely enough. It is using commit 7e8817f20b of cview. Was this fixed without either of us knowing, could you test?

The [downstream issue](https://github.com/makeworld-the-better-one/amfora/issues/156#issuecomment-1198375655) no longer occurs in Amfora, strangely enough. It is using commit 7e8817f20bdc of cview. Was this fixed without either of us knowing, could you test?
Owner

When running the test case given in #27 I still get erroneous output, even after updating dependencies.

When running the test case given in https://code.rocketnine.space/tslocum/cview/issues/27#issuecomment-2502 I still get erroneous output, even after updating dependencies.
Sign in to join this conversation.
No Milestone
No Assignees
2 Participants
Notifications
Due Date

No due date set.

Dependencies

This issue currently doesn't have any dependencies.

Loading…
There is no content yet.