Skip to content

Commit

Permalink
Be clearer when LESS_TERMCAP_xx are invalid
Browse files Browse the repository at this point in the history
Relates to #170.
  • Loading branch information
walles committed Jan 14, 2024
1 parent f6571b3 commit 2aa5dd3
Show file tree
Hide file tree
Showing 2 changed files with 40 additions and 11 deletions.
32 changes: 22 additions & 10 deletions m/styling.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -85,20 +90,19 @@ 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.
//
// 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)
}
}
}

Expand All @@ -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
Expand Down Expand Up @@ -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
}
19 changes: 18 additions & 1 deletion moar.go
Original file line number Diff line number Diff line change
Expand Up @@ -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,
)
}

Expand Down

0 comments on commit 2aa5dd3

Please sign in to comment.