From 070f7eb6ba261866395706f709d12dff3ead2e74 Mon Sep 17 00:00:00 2001 From: "R. Aidan Campbell" Date: Mon, 7 Feb 2022 21:54:12 -0700 Subject: [PATCH] extract logic for reading maxwidth config into a function: now safely handles fallbacks for missing or malformed configuration --- client.go | 34 ++++++++++++++++++++-------------- page.go | 3 --- 2 files changed, 20 insertions(+), 17 deletions(-) diff --git a/client.go b/client.go index 1141030..123ddf6 100644 --- a/client.go +++ b/client.go @@ -73,8 +73,7 @@ func (c *client) Draw() { screen.WriteString("\033[0m") screen.WriteString(c.TopBar.Render(c.Width, c.Options["theme"])) screen.WriteString("\n") - maxWidth, _ := strconv.Atoi(c.Options["maxwidth"]) - pageContent := c.PageState.Render(c.Height, c.Width-1, maxWidth, (c.Options["theme"] == "color")) + pageContent := c.PageState.Render(c.Height, c.Width-1, getMaxWidth(c.Options), (c.Options["theme"] == "color")) var re *regexp.Regexp if c.Options["theme"] == "inverse" { screen.WriteString("\033[7m") @@ -259,8 +258,7 @@ func (c *client) TakeControlInput() { } err = c.NextSearchItem(0) if err != nil { - maxWidth, _ := strconv.Atoi(c.Options["maxwidth"]) - c.PageState.History[c.PageState.Position].WrapContent(c.Width-1,maxWidth,(c.Options["theme"] == "color")) + c.PageState.History[c.PageState.Position].WrapContent(c.Width-1,getMaxWidth(c.Options),(c.Options["theme"] == "color")) c.Draw() } case ':', ' ': @@ -990,8 +988,7 @@ func (c *client) handleGopher(u Url) { } else { pg.FileType = "text" } - maxWidth, _ := strconv.Atoi(c.Options["maxwidth"]) - pg.WrapContent(c.Width-1, maxWidth, (c.Options["theme"] == "color")) + pg.WrapContent(c.Width-1, getMaxWidth(c.Options), (c.Options["theme"] == "color")) c.PageState.Add(pg) c.SetPercentRead() c.ClearMessage() @@ -1018,8 +1015,7 @@ func (c *client) handleGemini(u Url) { u.Mime = capsule.MimeMin pg := MakePage(u, capsule.Content, capsule.Links) pg.FileType = capsule.MimeMaj - maxWidth, _ := strconv.Atoi(c.Options["maxwidth"]) - pg.WrapContent(c.Width-1, maxWidth, (c.Options["theme"] == "color")) + pg.WrapContent(c.Width-1, getMaxWidth(c.Options), (c.Options["theme"] == "color")) c.PageState.Add(pg) c.SetPercentRead() c.ClearMessage() @@ -1085,8 +1081,7 @@ func (c *client) handleLocal(u Url) { if ext == ".jpg" || ext == ".jpeg" || ext == ".gif" || ext == ".png" { pg.FileType = "image" } - maxWidth, _ := strconv.Atoi(c.Options["maxwidth"]) - pg.WrapContent(c.Width-1, maxWidth, (c.Options["theme"] == "color")) + pg.WrapContent(c.Width-1, getMaxWidth(c.Options), (c.Options["theme"] == "color")) c.PageState.Add(pg) c.SetPercentRead() c.ClearMessage() @@ -1102,8 +1097,7 @@ func (c *client) handleFinger(u Url) { return } pg := MakePage(u, content, []string{}) - maxWidth, _ := strconv.Atoi(c.Options["maxwidth"]) - pg.WrapContent(c.Width-1, maxWidth, (c.Options["theme"] == "color")) + pg.WrapContent(c.Width-1, getMaxWidth(c.Options), (c.Options["theme"] == "color")) c.PageState.Add(pg) c.SetPercentRead() c.ClearMessage() @@ -1123,8 +1117,7 @@ func (c *client) handleWeb(u Url) { return } pg := MakePage(u, page.Content, page.Links) - maxWidth, _ := strconv.Atoi(c.Options["maxwidth"]) - pg.WrapContent(c.Width-1, maxWidth, (c.Options["theme"] == "color")) + pg.WrapContent(c.Width-1, getMaxWidth(c.Options), (c.Options["theme"] == "color")) c.PageState.Add(pg) c.SetPercentRead() c.ClearMessage() @@ -1265,3 +1258,16 @@ func updateTimeouts(timeoutString string) error { 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 +} \ No newline at end of file diff --git a/page.go b/page.go index d45184a..7fc01c1 100644 --- a/page.go +++ b/page.go @@ -70,9 +70,6 @@ func (p *Page) WrapContent(width, maxWidth int, color bool) { p.RenderImage(width) return } - if maxWidth < 100 { - maxWidth = 100 - } width = min(width, maxWidth) counter := 0 spacer := ""