extract logic for reading maxwidth config into a function: now safely handles fallbacks for missing or malformed configuration

This commit is contained in:
R. Aidan Campbell 2022-02-07 21:54:12 -07:00
parent dac13e1669
commit 070f7eb6ba
No known key found for this signature in database
GPG Key ID: 0985399E9CD6A99B
2 changed files with 20 additions and 17 deletions

View File

@ -73,8 +73,7 @@ func (c *client) Draw() {
screen.WriteString("\033[0m") screen.WriteString("\033[0m")
screen.WriteString(c.TopBar.Render(c.Width, c.Options["theme"])) screen.WriteString(c.TopBar.Render(c.Width, c.Options["theme"]))
screen.WriteString("\n") screen.WriteString("\n")
maxWidth, _ := strconv.Atoi(c.Options["maxwidth"]) pageContent := c.PageState.Render(c.Height, c.Width-1, getMaxWidth(c.Options), (c.Options["theme"] == "color"))
pageContent := c.PageState.Render(c.Height, c.Width-1, maxWidth, (c.Options["theme"] == "color"))
var re *regexp.Regexp var re *regexp.Regexp
if c.Options["theme"] == "inverse" { if c.Options["theme"] == "inverse" {
screen.WriteString("\033[7m") screen.WriteString("\033[7m")
@ -259,8 +258,7 @@ func (c *client) TakeControlInput() {
} }
err = c.NextSearchItem(0) err = c.NextSearchItem(0)
if err != nil { if err != nil {
maxWidth, _ := strconv.Atoi(c.Options["maxwidth"]) c.PageState.History[c.PageState.Position].WrapContent(c.Width-1,getMaxWidth(c.Options),(c.Options["theme"] == "color"))
c.PageState.History[c.PageState.Position].WrapContent(c.Width-1,maxWidth,(c.Options["theme"] == "color"))
c.Draw() c.Draw()
} }
case ':', ' ': case ':', ' ':
@ -990,8 +988,7 @@ func (c *client) handleGopher(u Url) {
} else { } else {
pg.FileType = "text" pg.FileType = "text"
} }
maxWidth, _ := strconv.Atoi(c.Options["maxwidth"]) pg.WrapContent(c.Width-1, getMaxWidth(c.Options), (c.Options["theme"] == "color"))
pg.WrapContent(c.Width-1, maxWidth, (c.Options["theme"] == "color"))
c.PageState.Add(pg) c.PageState.Add(pg)
c.SetPercentRead() c.SetPercentRead()
c.ClearMessage() c.ClearMessage()
@ -1018,8 +1015,7 @@ func (c *client) handleGemini(u Url) {
u.Mime = capsule.MimeMin u.Mime = capsule.MimeMin
pg := MakePage(u, capsule.Content, capsule.Links) pg := MakePage(u, capsule.Content, capsule.Links)
pg.FileType = capsule.MimeMaj pg.FileType = capsule.MimeMaj
maxWidth, _ := strconv.Atoi(c.Options["maxwidth"]) pg.WrapContent(c.Width-1, getMaxWidth(c.Options), (c.Options["theme"] == "color"))
pg.WrapContent(c.Width-1, maxWidth, (c.Options["theme"] == "color"))
c.PageState.Add(pg) c.PageState.Add(pg)
c.SetPercentRead() c.SetPercentRead()
c.ClearMessage() c.ClearMessage()
@ -1085,8 +1081,7 @@ func (c *client) handleLocal(u Url) {
if ext == ".jpg" || ext == ".jpeg" || ext == ".gif" || ext == ".png" { if ext == ".jpg" || ext == ".jpeg" || ext == ".gif" || ext == ".png" {
pg.FileType = "image" pg.FileType = "image"
} }
maxWidth, _ := strconv.Atoi(c.Options["maxwidth"]) pg.WrapContent(c.Width-1, getMaxWidth(c.Options), (c.Options["theme"] == "color"))
pg.WrapContent(c.Width-1, maxWidth, (c.Options["theme"] == "color"))
c.PageState.Add(pg) c.PageState.Add(pg)
c.SetPercentRead() c.SetPercentRead()
c.ClearMessage() c.ClearMessage()
@ -1102,8 +1097,7 @@ func (c *client) handleFinger(u Url) {
return return
} }
pg := MakePage(u, content, []string{}) pg := MakePage(u, content, []string{})
maxWidth, _ := strconv.Atoi(c.Options["maxwidth"]) pg.WrapContent(c.Width-1, getMaxWidth(c.Options), (c.Options["theme"] == "color"))
pg.WrapContent(c.Width-1, maxWidth, (c.Options["theme"] == "color"))
c.PageState.Add(pg) c.PageState.Add(pg)
c.SetPercentRead() c.SetPercentRead()
c.ClearMessage() c.ClearMessage()
@ -1123,8 +1117,7 @@ func (c *client) handleWeb(u Url) {
return return
} }
pg := MakePage(u, page.Content, page.Links) pg := MakePage(u, page.Content, page.Links)
maxWidth, _ := strconv.Atoi(c.Options["maxwidth"]) pg.WrapContent(c.Width-1, getMaxWidth(c.Options), (c.Options["theme"] == "color"))
pg.WrapContent(c.Width-1, maxWidth, (c.Options["theme"] == "color"))
c.PageState.Add(pg) c.PageState.Add(pg)
c.SetPercentRead() c.SetPercentRead()
c.ClearMessage() c.ClearMessage()
@ -1265,3 +1258,16 @@ func updateTimeouts(timeoutString string) error {
return nil return nil
} }
// getMaxWidth looks through the given options map and will safely return a max width to render
// if the option is missing or malformed, it will default to 100. A sane minimum of 10 is enforced.
func getMaxWidth(options map[string]string) int {
out, err := strconv.Atoi(options["maxwidth"])
if err != nil {
out = 100
}
if out < 10 {
out = 10
}
return out
}

View File

@ -70,9 +70,6 @@ func (p *Page) WrapContent(width, maxWidth int, color bool) {
p.RenderImage(width) p.RenderImage(width)
return return
} }
if maxWidth < 100 {
maxWidth = 100
}
width = min(width, maxWidth) width = min(width, maxWidth)
counter := 0 counter := 0
spacer := "" spacer := ""