diff --git a/README.md b/README.md index 11c12a78..f0580772 100644 --- a/README.md +++ b/README.md @@ -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 ---------- diff --git a/m/ansiTokenizer.go b/m/ansiTokenizer.go index a114ec59..18c463f0 100644 --- a/m/ansiTokenizer.go +++ b/m/ansiTokenizer.go @@ -3,6 +3,7 @@ package m import ( "fmt" "log" + "os" "regexp" "strconv" "strings" @@ -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) { @@ -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, } } @@ -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) diff --git a/m/pager.go b/m/pager.go index f2eb302e..f60588f0 100644 --- a/m/pager.go +++ b/m/pager.go @@ -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)