From 5bb62f7a80b611ef8c7be790decd9988dd54a38d Mon Sep 17 00:00:00 2001 From: makeworld Date: Mon, 29 Jun 2020 12:54:36 -0400 Subject: [PATCH] =?UTF-8?q?=F0=9F=90=9B=20Use=20recover()=20on=20WordWrap?= =?UTF-8?q?=20-=20fixes=20#20?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- CHANGELOG.md | 1 + NOTES.md | 5 ++++- renderer/renderer.go | 32 ++++++++++++++++++++++++++------ 3 files changed, 31 insertions(+), 7 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 99284c7..40ccca3 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 ### Fixed - Actual unicode bullet symbol is used for lists: U+2022 - Performance when loading very long cached pages improved (#26) +- Doesn't crash when wrapping certain complex lines (#20) ## [1.1.0] - 2020-06-24 ### Added diff --git a/NOTES.md b/NOTES.md index 4635bfd..a5335c1 100644 --- a/NOTES.md +++ b/NOTES.md @@ -4,8 +4,11 @@ - And then just one single map of tab number to `tab` ## Bugs -- Wrapping is messed up on CHAZ post, but nothing else +- Wrapping messes up on brackets - Filed [issue 23](https://gitlab.com/tslocum/cview/-/issues/23) +- Wrapping panics on strings with brackets and Asian characters + - Filed cview [issue 27](https://gitlab.com/tslocum/cview/-/issues/27) + - The panicking was reported and fixed in Amfora [issue 20](https://github.com/makeworld-the-better-one/amfora/issues/20), but the lines are now just not wrapped - Text background not reset on ANSI pages - Filed [issue 25](https://gitlab.com/tslocum/cview/-/issues/25) - Modal styling messed up when wrapped - example occurence is the error modal for a long unsupported scheme URL diff --git a/renderer/renderer.go b/renderer/renderer.go index 4a2c55d..156edef 100644 --- a/renderer/renderer.go +++ b/renderer/renderer.go @@ -124,13 +124,33 @@ func convertRegularGemini(s string, numLinks int, width int) (string, []string) lines[i] = strings.TrimPrefix(lines[i], ">") lines[i] = strings.TrimPrefix(lines[i], " ") - temp := cview.WordWrap(lines[i], width) - for i := range temp { - temp[i] = "> " + temp[i] - } - wrappedLines = append(wrappedLines, temp...) + // Anonymous function to allow recovery from potential WordWrap panic + func() { + defer func() { + if r := recover(); r != nil { + // Add unwrapped line instead + wrappedLines = append(wrappedLines, "> "+lines[i]) + } + }() + + temp := cview.WordWrap(lines[i], width) + for i := range temp { + temp[i] = "> " + temp[i] + } + wrappedLines = append(wrappedLines, temp...) + }() } else { - wrappedLines = append(wrappedLines, cview.WordWrap(lines[i], width)...) + // Anonymous function to allow recovery from potential WordWrap panic + func() { + defer func() { + if r := recover(); r != nil { + // Add unwrapped line instead + wrappedLines = append(wrappedLines, lines[i]) + } + }() + + wrappedLines = append(wrappedLines, cview.WordWrap(lines[i], width)...) + }() } } }