Skip to content

Commit

Permalink
Merge branch 'johan/less-termcap-so'
Browse files Browse the repository at this point in the history
Fixes #114.
  • Loading branch information
walles committed Dec 31, 2022
2 parents 871ac3b + 4d285bd commit a7f2033
Show file tree
Hide file tree
Showing 4 changed files with 36 additions and 18 deletions.
7 changes: 4 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -34,10 +34,11 @@ Doing the right thing includes:
just like `tail -f`

[For compatibility reasons](https://github.com/walles/moar/issues/14), `moar`
uses the formats declared in these environment variables when viewing man pages:
uses the formats declared in these environment variables if present:

- `LESS_TERMCAP_md`: Bold
- `LESS_TERMCAP_us`: Underline
- `LESS_TERMCAP_md`: Man page <b>bold</b>
- `LESS_TERMCAP_us`: Man page <u>underline</u>
- `LESS_TERMCAP_so`: [Status bar and search hits](https://github.com/walles/moar/issues/114)

For configurability reasons, `moar` reads extra command line options from the
`MOAR` environment variable.
Expand Down
36 changes: 24 additions & 12 deletions m/ansiTokenizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ const BACKSPACE = '\b'

var manPageBold = twin.StyleDefault.WithAttr(twin.AttrBold)
var manPageUnderline = twin.StyleDefault.WithAttr(twin.AttrUnderline)
var standoutStyle *twin.Style = nil
var unprintableStyle UnprintableStyle = UNPRINTABLE_STYLE_HIGHLIGHT

// A Line represents a line of text that can / will be paged
Expand All @@ -40,7 +41,7 @@ func NewLine(raw string) Line {
}

// Returns a representation of the string split into styled tokens. Any regexp
// matches are highlighted in inverse video. A nil regexp means no highlighting.
// matches are highlighted. A nil regexp means no highlighting.
func (line *Line) HighlightedTokens(search *regexp.Regexp) cellsWithTrailer {
plain := line.Plain()
matchRanges := getMatchRanges(&plain, search)
Expand All @@ -50,8 +51,11 @@ func (line *Line) HighlightedTokens(search *regexp.Regexp) cellsWithTrailer {
for _, token := range fromString.Cells {
style := token.Style
if matchRanges.InRange(len(returnCells)) {
// Search hits in reverse video
style = style.WithAttr(twin.AttrReverse)
if standoutStyle != nil {
style = *standoutStyle
} else {
style = style.WithAttr(twin.AttrReverse)
}
}

returnCells = append(returnCells, twin.Cell{
Expand All @@ -75,19 +79,27 @@ func (line *Line) Plain() string {
return *line.plain
}

// SetManPageFormatFromEnv parses LESS_TERMCAP_xx environment variables and
func setStyleFromEnv(updateMe *twin.Style, envVarName string) {
envValue := os.Getenv(envVarName)
if envValue == "" {
return
}

*updateMe = termcapToStyle(envValue)
}

// ConsumeLessTermcapEnvs parses LESS_TERMCAP_xx environment variables and
// adapts the moar output accordingly.
func SetManPageFormatFromEnv() {
func ConsumeLessTermcapEnvs() {
// Requested here: https://github.com/walles/moar/issues/14

lessTermcapMd := os.Getenv("LESS_TERMCAP_md")
if lessTermcapMd != "" {
manPageBold = termcapToStyle(lessTermcapMd)
}
setStyleFromEnv(&manPageBold, "LESS_TERMCAP_md")
setStyleFromEnv(&manPageUnderline, "LESS_TERMCAP_us")

lessTermcapUs := os.Getenv("LESS_TERMCAP_us")
if lessTermcapUs != "" {
manPageUnderline = termcapToStyle(lessTermcapUs)
value := os.Getenv("LESS_TERMCAP_so")
if value != "" {
_standoutStyle := termcapToStyle(value)
standoutStyle = &_standoutStyle
}
}

Expand Down
6 changes: 4 additions & 2 deletions m/pager.go
Original file line number Diff line number Diff line change
Expand Up @@ -166,7 +166,9 @@ func (p *Pager) setFooter(footer string) {

pos := 0
var footerStyle twin.Style
if p.StatusBarStyle == STATUSBAR_STYLE_INVERSE {
if standoutStyle != nil {
footerStyle = *standoutStyle
} else if p.StatusBarStyle == STATUSBAR_STYLE_INVERSE {
footerStyle = twin.StyleDefault.WithAttr(twin.AttrReverse)
} else if p.StatusBarStyle == STATUSBAR_STYLE_PLAIN {
footerStyle = twin.StyleDefault
Expand Down Expand Up @@ -400,7 +402,7 @@ func (p *Pager) onRune(char rune) {
// StartPaging brings up the pager on screen
func (p *Pager) StartPaging(screen twin.Screen) {
unprintableStyle = p.UnprintableStyle
SetManPageFormatFromEnv()
ConsumeLessTermcapEnvs()

p.screen = screen

Expand Down
5 changes: 4 additions & 1 deletion m/pager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -196,14 +196,17 @@ func resetManPageFormat() {
func testManPageFormatting(t *testing.T, input string, expected twin.Cell) {
reader := NewReaderFromStream("", strings.NewReader(input))

// Without these three lines the man page tests will fail if either of these
// Without these lines the man page tests will fail if either of these
// environment variables are set when the tests are run.
if err := os.Setenv("LESS_TERMCAP_md", ""); err != nil {
panic(err)
}
if err := os.Setenv("LESS_TERMCAP_us", ""); err != nil {
panic(err)
}
if err := os.Setenv("LESS_TERMCAP_so", ""); err != nil {
panic(err)
}
resetManPageFormat()

contents := startPaging(t, reader).GetRow(0)
Expand Down

0 comments on commit a7f2033

Please sign in to comment.