diff --git a/m/styling.go b/m/styling.go index 96ef6a28..18e444be 100644 --- a/m/styling.go +++ b/m/styling.go @@ -26,7 +26,12 @@ func setStyle(updateMe *twin.Style, envVarName string, fallback *twin.Style) { return } - *updateMe = termcapToStyle(envValue) + style, err := TermcapToStyle(envValue) + if err != nil { + *updateMe = style + } + + log.Debug("Ignoring invalid ", envVarName, ": ", strings.ReplaceAll(envValue, "\x1b", "ESC"), ": ", err) } // With exact set, only return a style if the Chroma formatter has an explicit @@ -85,11 +90,6 @@ func consumeLessTermcapEnvs(chromaStyle *chroma.Style, chromaFormatter *chroma.F twinStyleFromChroma(chromaStyle, chromaFormatter, chroma.GenericUnderline, false), ) - headingStyle := twinStyleFromChroma(chromaStyle, chromaFormatter, chroma.GenericHeading, true) - if headingStyle != nil { - textstyles.ManPageHeading = *headingStyle - } - // Since standoutStyle defaults to nil we can't just pass it to setStyle(). // Instead we give it special treatment here and set it only if its // environment variable is set. @@ -97,8 +97,12 @@ func consumeLessTermcapEnvs(chromaStyle *chroma.Style, chromaFormatter *chroma.F // Ref: https://github.com/walles/moar/issues/171 envValue := os.Getenv("LESS_TERMCAP_so") if envValue != "" { - style := termcapToStyle(envValue) - standoutStyle = &style + style, err := TermcapToStyle(envValue) + if err == nil { + standoutStyle = &style + } else { + log.Debug("Ignoring invalid LESS_TERMCAP_so: ", strings.ReplaceAll(envValue, "\x1b", "ESC"), ": ", err) + } } } @@ -107,6 +111,11 @@ func styleUI(chromaStyle *chroma.Style, chromaFormatter *chroma.Formatter, statu return } + headingStyle := twinStyleFromChroma(chromaStyle, chromaFormatter, chroma.GenericHeading, true) + if headingStyle != nil { + textstyles.ManPageHeading = *headingStyle + } + chromaLineNumbers := twinStyleFromChroma(chromaStyle, chromaFormatter, chroma.LineNumbers, true) if chromaLineNumbers != nil { // If somebody can provide an example where not-dimmed line numbers @@ -139,8 +148,11 @@ func styleUI(chromaStyle *chroma.Style, chromaFormatter *chroma.Formatter, statu } } -func termcapToStyle(termcap string) twin.Style { +func TermcapToStyle(termcap string) (twin.Style, error) { // Add a character to be sure we have one to take the format from cells := textstyles.CellsFromString("", termcap+"x", nil).Cells - return cells[len(cells)-1].Style + if len(cells) != 1 { + return twin.StyleDefault, fmt.Errorf("Expected styling only and no text") + } + return cells[len(cells)-1].Style, nil } diff --git a/moar.go b/moar.go index 87e8baf5..9a16c200 100644 --- a/moar.go +++ b/moar.go @@ -33,10 +33,27 @@ func printUsageEnvVar(output io.Writer, envVarName string, description string) { return } + style, err := m.TermcapToStyle(value) + if err != nil { + bold := twin.StyleDefault.WithAttr(twin.AttrBold).RenderUpdateFrom(twin.StyleDefault, twin.ColorType256) + notBold := twin.StyleDefault.RenderUpdateFrom(twin.StyleDefault.WithAttr(twin.AttrBold), twin.ColorType256) + _, _ = fmt.Fprintf(output, " %s (%s): %s %s<- Error: %v%s\n", + envVarName, + description, + strings.ReplaceAll(value, "\x1b", "ESC"), + bold, + err, + notBold, + ) + return + } + + prefix := style.RenderUpdateFrom(twin.StyleDefault, twin.ColorType256) + suffix := twin.StyleDefault.RenderUpdateFrom(style, twin.ColorType256) _, _ = fmt.Fprintf(output, " %s (%s): %s\n", envVarName, description, - strings.ReplaceAll(value, "\x1b", "ESC"), + prefix+strings.ReplaceAll(value, "\x1b", "ESC")+suffix, ) }