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

pull/214/head
R. Aidan Campbell 10 months ago
parent dac13e1669
commit 070f7eb6ba
No known key found for this signature in database
GPG Key ID: 0985399E9CD6A99B
  1. 34
      client.go
  2. 3
      page.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
}

@ -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 := ""

Loading…
Cancel
Save