Prevent link lines (and other types) from being wider than the max_width setting

Fixes #280
This commit is contained in:
makeworld 2021-12-22 19:35:07 -05:00
parent 40865f977e
commit 3823a46152
2 changed files with 17 additions and 8 deletions

View File

@ -11,6 +11,7 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
### Fixed ### Fixed
- Modal can't be closed when opening non-gemini text URLs from the commandline (#283, #284) - Modal can't be closed when opening non-gemini text URLs from the commandline (#283, #284)
- External programs started by Amfora remain as zombie processes (#219) - External programs started by Amfora remain as zombie processes (#219)
- Prevent link lines (and other types) from being wider than the `max_width` setting (#280)
## [1.9.2] - 2021-12-10 ## [1.9.2] - 2021-12-10

View File

@ -58,6 +58,10 @@ func RenderPlainText(s string) string {
// //
// Set includeFirst to true if the prefix and suffix should be applied to the first wrapped line as well // Set includeFirst to true if the prefix and suffix should be applied to the first wrapped line as well
func wrapLine(line string, width int, prefix, suffix string, includeFirst bool) []string { func wrapLine(line string, width int, prefix, suffix string, includeFirst bool) []string {
if width < 1 {
width = 1
}
// Anonymous function to allow recovery from potential WordWrap panic // Anonymous function to allow recovery from potential WordWrap panic
var ret []string var ret []string
func() { func() {
@ -196,7 +200,7 @@ func convertRegularGemini(s string, numLinks, width int, proxied bool) (string,
// Add the link text in blue (in a region), and a gray link number to the left of it // Add the link text in blue (in a region), and a gray link number to the left of it
// Those are the default colors, anyway // Those are the default colors, anyway
wrappedLink = wrapLine(linkText, width, wrappedLink = wrapLine(linkText, width-indent,
strings.Repeat(" ", indent)+ strings.Repeat(" ", indent)+
`["`+strconv.Itoa(num-1)+`"][`+config.GetColorString("amfora_link")+`]`, `["`+strconv.Itoa(num-1)+`"][`+config.GetColorString("amfora_link")+`]`,
`[-][""]`, `[-][""]`,
@ -211,8 +215,8 @@ func convertRegularGemini(s string, numLinks, width int, proxied bool) (string,
} else { } else {
// No color // No color
wrappedLink = wrapLine(linkText, width, wrappedLink = wrapLine(linkText, width-indent,
strings.Repeat(" ", len(strconv.Itoa(num))+4)+ // +4 for spaces and brackets strings.Repeat(" ", indent)+ // +4 for spaces and brackets
`["`+strconv.Itoa(num-1)+`"]`, `["`+strconv.Itoa(num-1)+`"]`,
`[""]`, `[""]`,
false, // Don't indent the first line, it's the one with link number false, // Don't indent the first line, it's the one with link number
@ -228,7 +232,7 @@ func convertRegularGemini(s string, numLinks, width int, proxied bool) (string,
if viper.GetBool("a-general.color") { if viper.GetBool("a-general.color") {
// Color // Color
wrappedLink = wrapLine(linkText, width, wrappedLink = wrapLine(linkText, width-indent,
strings.Repeat(" ", indent)+ strings.Repeat(" ", indent)+
`["`+strconv.Itoa(num-1)+`"]`+linkTag, `["`+strconv.Itoa(num-1)+`"]`+linkTag,
`[-::-][""]`, `[-::-][""]`,
@ -242,7 +246,7 @@ func convertRegularGemini(s string, numLinks, width int, proxied bool) (string,
} else { } else {
// No color // No color
wrappedLink = wrapLine(linkText, width, wrappedLink = wrapLine(linkText, width-indent,
strings.Repeat(" ", indent)+ strings.Repeat(" ", indent)+
`["`+strconv.Itoa(num-1)+`"]`, `["`+strconv.Itoa(num-1)+`"]`,
`[::-][""]`, `[::-][""]`,
@ -261,7 +265,8 @@ func convertRegularGemini(s string, numLinks, width int, proxied bool) (string,
} else if strings.HasPrefix(lines[i], "* ") { } else if strings.HasPrefix(lines[i], "* ") {
if viper.GetBool("a-general.bullets") { if viper.GetBool("a-general.bullets") {
// Wrap list item, and indent wrapped lines past the bullet // Wrap list item, and indent wrapped lines past the bullet
wrappedItem := wrapLine(lines[i][1:], width, wrappedItem := wrapLine(lines[i][1:],
width-4, // Subtract the 4 indent spaces
fmt.Sprintf(" [%s]", config.GetColorString("list_text")), fmt.Sprintf(" [%s]", config.GetColorString("list_text")),
"[-]", false) "[-]", false)
// Add bullet // Add bullet
@ -269,7 +274,8 @@ func convertRegularGemini(s string, numLinks, width int, proxied bool) (string,
wrappedItem[0] + "[-]" wrappedItem[0] + "[-]"
wrappedLines = append(wrappedLines, wrappedItem...) wrappedLines = append(wrappedLines, wrappedItem...)
} else { } else {
wrappedItem := wrapLine(lines[i][1:], width, wrappedItem := wrapLine(lines[i][1:],
width-4, // Subtract the 4 indent spaces
fmt.Sprintf(" [%s]", config.GetColorString("list_text")), fmt.Sprintf(" [%s]", config.GetColorString("list_text")),
"[-]", false) "[-]", false)
// Add "*" // Add "*"
@ -290,7 +296,9 @@ func convertRegularGemini(s string, numLinks, width int, proxied bool) (string,
lines[i] = strings.TrimPrefix(lines[i], ">") lines[i] = strings.TrimPrefix(lines[i], ">")
lines[i] = strings.TrimPrefix(lines[i], " ") lines[i] = strings.TrimPrefix(lines[i], " ")
wrappedLines = append(wrappedLines, wrappedLines = append(wrappedLines,
wrapLine(lines[i], width, fmt.Sprintf("[%s::i]> ", config.GetColorString("quote_text")), wrapLine(lines[i],
width-2, // Subtract 2 for width of prefix string
fmt.Sprintf("[%s::i]> ", config.GetColorString("quote_text")),
"[-::-]", true)..., "[-::-]", true)...,
) )
} }