Skip to content

Commit

Permalink
Merge branch 'johan/textstyles'
Browse files Browse the repository at this point in the history
This moves text styling code into its own package.
  • Loading branch information
walles committed Jan 5, 2024
2 parents 192838a + c702282 commit 54c88d3
Show file tree
Hide file tree
Showing 10 changed files with 161 additions and 121 deletions.
63 changes: 63 additions & 0 deletions m/line.go
Original file line number Diff line number Diff line change
@@ -0,0 +1,63 @@
package m

import (
"regexp"

"github.com/walles/moar/textstyles"
"github.com/walles/moar/twin"
)

// A Line represents a line of text that can / will be paged
type Line struct {
raw string
plain *string
}

// NewLine creates a new Line from a (potentially ANSI / man page formatted) string
func NewLine(raw string) Line {
return Line{
raw: raw,
plain: nil,
}
}

// Returns a representation of the string split into styled tokens. Any regexp
// matches are highlighted. A nil regexp means no highlighting.
//
//revive:disable-next-line:unexported-return
func (line *Line) HighlightedTokens(linePrefix string, search *regexp.Regexp, lineNumberOneBased *int) textstyles.CellsWithTrailer {
plain := line.Plain(lineNumberOneBased)
matchRanges := getMatchRanges(&plain, search)

fromString := textstyles.CellsFromString(linePrefix+line.raw, lineNumberOneBased)
returnCells := make([]twin.Cell, 0, len(fromString.Cells))
for _, token := range fromString.Cells {
style := token.Style
if matchRanges.InRange(len(returnCells)) {
if standoutStyle != nil {
style = *standoutStyle
} else {
style = style.WithAttr(twin.AttrReverse)
}
}

returnCells = append(returnCells, twin.Cell{
Rune: token.Rune,
Style: style,
})
}

return textstyles.CellsWithTrailer{
Cells: returnCells,
Trailer: fromString.Trailer,
}
}

// Plain returns a plain text representation of the initial string
func (line *Line) Plain(lineNumberOneBased *int) string {
if line.plain == nil {
plain := textstyles.WithoutFormatting(line.raw, lineNumberOneBased)
line.plain = &plain
}
return *line.plain
}
17 changes: 3 additions & 14 deletions m/pager.go
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ import (

"github.com/alecthomas/chroma/v2"
log "github.com/sirupsen/logrus"
"github.com/walles/moar/textstyles"
"github.com/walles/moar/twin"
)

Expand All @@ -32,18 +33,6 @@ const (
STATUSBAR_STYLE_BOLD
)

// How do we render unprintable characters?
type UnprintableStyle int

const (
//revive:disable-next-line:var-naming
UNPRINTABLE_STYLE_HIGHLIGHT UnprintableStyle = iota
//revive:disable-next-line:var-naming
UNPRINTABLE_STYLE_WHITESPACE
)

var unprintableStyle UnprintableStyle

type eventSpinnerUpdate struct {
spinner string
}
Expand Down Expand Up @@ -79,7 +68,7 @@ type Pager struct {
StatusBarStyle StatusBarOption
ShowStatusBar bool

UnprintableStyle UnprintableStyle
UnprintableStyle textstyles.UnprintableStyleT

WrapLongLines bool

Expand Down Expand Up @@ -478,7 +467,7 @@ func (p *Pager) StartPaging(screen twin.Screen, chromaStyle *chroma.Style, chrom
}
}()

unprintableStyle = p.UnprintableStyle
textstyles.UnprintableStyle = p.UnprintableStyle
consumeLessTermcapEnvs(chromaStyle, chromaFormatter)
styleUI(chromaStyle, chromaFormatter, p.StatusBarStyle)

Expand Down
5 changes: 3 additions & 2 deletions m/pager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,6 +13,7 @@ import (
"github.com/alecthomas/chroma/v2/lexers"
"github.com/alecthomas/chroma/v2/styles"
"github.com/google/go-cmp/cmp"
"github.com/walles/moar/textstyles"
"github.com/walles/moar/twin"
"gotest.tools/v3/assert"
)
Expand Down Expand Up @@ -208,8 +209,8 @@ func TestUnicodePrivateUse(t *testing.T) {
}

func resetManPageFormat() {
manPageBold = twin.StyleDefault.WithAttr(twin.AttrBold)
manPageUnderline = twin.StyleDefault.WithAttr(twin.AttrUnderline)
textstyles.ManPageBold = twin.StyleDefault.WithAttr(twin.AttrBold)
textstyles.ManPageUnderline = twin.StyleDefault.WithAttr(twin.AttrUnderline)
}

func testManPageFormatting(t *testing.T, input string, expected twin.Cell) {
Expand Down
3 changes: 2 additions & 1 deletion m/screenLines.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package m
import (
"fmt"

"github.com/walles/moar/textstyles"
"github.com/walles/moar/twin"
)

Expand Down Expand Up @@ -52,7 +53,7 @@ func (p *Pager) redraw(spinner string) overflowState {
// This happens when we're done
eofSpinner = "---"
}
spinnerLine := cellsFromString(_EofMarkerFormat+eofSpinner, nil).Cells
spinnerLine := textstyles.CellsFromString(_EofMarkerFormat+eofSpinner, nil).Cells
for column, cell := range spinnerLine {
p.screen.SetCell(column, lastUpdatedScreenLineNumber+1, cell)
}
Expand Down
12 changes: 5 additions & 7 deletions m/styling.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,15 +7,13 @@ import (

"github.com/alecthomas/chroma/v2"
log "github.com/sirupsen/logrus"
"github.com/walles/moar/textstyles"
"github.com/walles/moar/twin"
)

// From LESS_TERMCAP_so, overrides statusbarStyle from the Chroma style if set
var standoutStyle *twin.Style

var manPageBold = twin.StyleDefault.WithAttr(twin.AttrBold)
var manPageUnderline = twin.StyleDefault.WithAttr(twin.AttrUnderline)

var lineNumbersStyle = twin.StyleDefault.WithAttr(twin.AttrDim)
var statusbarStyle = twin.StyleDefault.WithAttr(twin.AttrReverse)

Expand Down Expand Up @@ -46,7 +44,7 @@ func twinStyleFromChroma(chromaStyle *chroma.Style, chromaFormatter *chroma.Form
}

formatted := stringBuilder.String()
cells := cellsFromString(formatted, nil).Cells
cells := textstyles.CellsFromString(formatted, nil).Cells
if len(cells) != 1 {
log.Warnf("Chroma formatter didn't return exactly one cell: %#v", cells)
return nil
Expand All @@ -60,8 +58,8 @@ func twinStyleFromChroma(chromaStyle *chroma.Style, chromaFormatter *chroma.Form
func consumeLessTermcapEnvs(chromaStyle *chroma.Style, chromaFormatter *chroma.Formatter) {
// Requested here: https://github.com/walles/moar/issues/14

setStyle(&manPageBold, "LESS_TERMCAP_md", twinStyleFromChroma(chromaStyle, chromaFormatter, chroma.GenericStrong))
setStyle(&manPageUnderline, "LESS_TERMCAP_us", twinStyleFromChroma(chromaStyle, chromaFormatter, chroma.GenericUnderline))
setStyle(&textstyles.ManPageBold, "LESS_TERMCAP_md", twinStyleFromChroma(chromaStyle, chromaFormatter, chroma.GenericStrong))
setStyle(&textstyles.ManPageUnderline, "LESS_TERMCAP_us", twinStyleFromChroma(chromaStyle, chromaFormatter, chroma.GenericUnderline))

// 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
Expand Down Expand Up @@ -114,6 +112,6 @@ func styleUI(chromaStyle *chroma.Style, chromaFormatter *chroma.Formatter, statu

func termcapToStyle(termcap string) twin.Style {
// Add a character to be sure we have one to take the format from
cells := cellsFromString(termcap+"x", nil).Cells
cells := textstyles.CellsFromString(termcap+"x", nil).Cells
return cells[len(cells)-1].Style
}
9 changes: 5 additions & 4 deletions moar.go
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@ import (
"golang.org/x/term"

"github.com/walles/moar/m"
"github.com/walles/moar/textstyles"
"github.com/walles/moar/twin"
)

Expand Down Expand Up @@ -222,12 +223,12 @@ func parseStatusBarStyle(styleOption string) (m.StatusBarOption, error) {
return 0, fmt.Errorf("Good ones are inverse, plain and bold")
}

func parseUnprintableStyle(styleOption string) (m.UnprintableStyle, error) {
func parseUnprintableStyle(styleOption string) (textstyles.UnprintableStyleT, error) {
if styleOption == "highlight" {
return m.UNPRINTABLE_STYLE_HIGHLIGHT, nil
return textstyles.UNPRINTABLE_STYLE_HIGHLIGHT, nil
}
if styleOption == "whitespace" {
return m.UNPRINTABLE_STYLE_WHITESPACE, nil
return textstyles.UNPRINTABLE_STYLE_WHITESPACE, nil
}

return 0, fmt.Errorf("Good ones are highlight or whitespace")
Expand Down Expand Up @@ -416,7 +417,7 @@ func main() {
noClearOnExit := flagSet.Bool("no-clear-on-exit", false, "Retain screen contents when exiting moar")
statusBarStyle := flagSetFunc(flagSet, "statusbar", m.STATUSBAR_STYLE_INVERSE,
"Status bar style: inverse, plain or bold", parseStatusBarStyle)
unprintableStyle := flagSetFunc(flagSet, "render-unprintable", m.UNPRINTABLE_STYLE_HIGHLIGHT,
unprintableStyle := flagSetFunc(flagSet, "render-unprintable", textstyles.UNPRINTABLE_STYLE_HIGHLIGHT,
"How unprintable characters are rendered: highlight or whitespace", parseUnprintableStyle)
scrollLeftHint := flagSetFunc(flagSet, "scroll-left-hint",
twin.NewCell('<', twin.StyleDefault.WithAttr(twin.AttrReverse)),
Expand Down
Loading

0 comments on commit 54c88d3

Please sign in to comment.