Skip to content

Commit

Permalink
Merge branch 'walles/less-termcap'
Browse files Browse the repository at this point in the history
Fixes #14
  • Loading branch information
walles committed Oct 30, 2019
2 parents a4ec70f + 70eb01e commit 6b42190
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 3 deletions.
10 changes: 9 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,10 +21,18 @@ Doing the right thing includes:
* [Regexp](http://en.wikipedia.org/wiki/Regular_expression#Basic_concepts)
search if your search string is a valid regexp
* Supports displaying ANSI color coded texts (like the output from
"git diff" for example)
`git diff` for example)
* Supports UTF-8 input and output
* The position in the file is always shown

For compatibility reasons, `moar` uses the formats declared in these
environment variables when viewing man pages:

* `LESS_TERMCAP_md`: Bold
* `LESS_TERMCAP_us`: Underline

See [here](https://github.com/walles/moar/issues/14) for usage examples.

Installing
----------

Expand Down
33 changes: 31 additions & 2 deletions m/ansiTokenizer.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package m
import (
"fmt"
"log"
"os"
"regexp"
"strconv"
"strings"
Expand All @@ -12,12 +13,37 @@ import (

const _TabSize = 4

var manPageBold = tcell.StyleDefault.Bold(true)
var manPageUnderline = tcell.StyleDefault.Underline(true)

// Token is a rune with a style to be written to a cell on screen
type Token struct {
Rune rune
Style tcell.Style
}

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

lessTermcapMd := os.Getenv("LESS_TERMCAP_md")
if lessTermcapMd != "" {
manPageBold = _TermcapToStyle(logger, lessTermcapMd)
}

lessTermcapUs := os.Getenv("LESS_TERMCAP_us")
if lessTermcapUs != "" {
manPageUnderline = _TermcapToStyle(logger, lessTermcapUs)
}
}

func _TermcapToStyle(logger *log.Logger, termcap string) tcell.Style {
// Add a character to be sure we have one to take the format from
tokens, _ := TokensFromString(logger, termcap+"x")
return tokens[len(tokens)-1].Style
}

// TokensFromString turns a (formatted) string into a series of tokens,
// and an unformatted string
func TokensFromString(logger *log.Logger, s string) ([]Token, *string) {
Expand Down Expand Up @@ -80,14 +106,14 @@ func _TokensFromStyledString(styledString _StyledString) []Token {
if char == twoBack {
replacement = &Token{
Rune: twoBack,
Style: styledString.Style.Bold(true),
Style: manPageBold,
}
}

if twoBack == '_' {
replacement = &Token{
Rune: char,
Style: styledString.Style.Underline(true),
Style: manPageUnderline,
}
}

Expand All @@ -102,6 +128,9 @@ func _TokensFromStyledString(styledString _StyledString) []Token {
//
// Maybe the interpretation should be:
// "Make a bold +, then erase that and replace it with a bold o"?
//
// Used for bullet points, maybe we should just replace the whole thing with
// a unicode bullet point in bold?

if replacement != nil {
tokens = append(tokens[0:len(tokens)-2], *replacement)
Expand Down
2 changes: 2 additions & 0 deletions m/pager.go
Original file line number Diff line number Diff line change
Expand Up @@ -576,6 +576,8 @@ func (p *Pager) StartPaging(logger *log.Logger, screen tcell.Screen) {
// We want to match the terminal theme, see screen.Init() source code
os.Setenv("TCELL_TRUECOLOR", "disable")

SetManPageFormatFromEnv(logger)

if e := screen.Init(); e != nil {
fmt.Fprintf(os.Stderr, "%v\n", e)
os.Exit(1)
Expand Down

0 comments on commit 6b42190

Please sign in to comment.