Skip to content

Commit

Permalink
Add switch for terminal fg rather than style fg
Browse files Browse the repository at this point in the history
For plain text.

Fixes #255.
  • Loading branch information
walles committed Nov 16, 2024
1 parent 1a29aa8 commit 34bd176
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 2 deletions.
8 changes: 6 additions & 2 deletions m/pager.go
Original file line number Diff line number Diff line change
Expand Up @@ -93,7 +93,8 @@ type Pager struct {
// Optional ANSI to prefix each text line with. Initialised using
// ChromaStyle and ChromaFormatter. Used for coloring unstyled text lines
// based on the Chroma style.
linePrefix string
linePrefix string
WithTerminalFg bool // If true, don't set linePrefix

// Length of the longest line displayed. This is used for limiting scrolling to the right.
longestLineLength int
Expand Down Expand Up @@ -317,7 +318,10 @@ func (p *Pager) StartPaging(screen twin.Screen, chromaStyle *chroma.Style, chrom
styleUI(chromaStyle, chromaFormatter, p.StatusBarStyle)

p.screen = screen
p.linePrefix = getLineColorPrefix(chromaStyle, chromaFormatter)
if !p.WithTerminalFg {
// Use the plain text color from the theme
p.linePrefix = getLineColorPrefix(chromaStyle, chromaFormatter)
}
p.mode = PagerModeViewing{pager: p}
p.marks = make(map[rune]scrollPosition)

Expand Down
34 changes: 34 additions & 0 deletions m/pager_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,30 @@ func startPaging(t *testing.T, reader *Reader) *twin.FakeScreen {
return screen
}

// Set style to "native" and use the TTY16m formatter
func startPagingWithTerminalFg(t *testing.T, reader *Reader, withTerminalFg bool) *twin.FakeScreen {
err := reader._wait()
if err != nil {
t.Fatalf("Failed waiting for reader: %v", err)
}

screen := twin.NewFakeScreen(20, 10)
pager := NewPager(reader)
pager.ShowLineNumbers = false
pager.WithTerminalFg = withTerminalFg

// Tell our Pager to quit immediately
pager.Quit()

// Except for just quitting, this also associates our FakeScreen with the Pager
pager.StartPaging(screen, styles.Get("native"), &formatters.TTY16m)

// This makes sure at least one frame gets rendered
pager.redraw("")

return screen
}

// assertIndexOfFirstX verifies the (zero-based) index of the first 'x'
func assertIndexOfFirstX(t *testing.T, s string, expectedIndex int) {
reader := NewReaderFromText("", s)
Expand Down Expand Up @@ -682,6 +706,16 @@ func TestPageWideChars(t *testing.T) {
assert.Equal(t, "x"+monospaced18cells+">", renderTextLine("x"+monospaced24cells))
}

func TestTerminalFg(t *testing.T) {
reader := NewReaderFromText("", "x")

var styleAnswer = twin.NewStyledRune('x', twin.StyleDefault.WithForeground(twin.NewColor24Bit(0xd0, 0xd0, 0xd0)))
var terminalAnswer = twin.NewStyledRune('x', twin.StyleDefault)

assertRunesEqual(t, styleAnswer, startPagingWithTerminalFg(t, reader, false).GetRow(0)[0])
assertRunesEqual(t, terminalAnswer, startPagingWithTerminalFg(t, reader, true).GetRow(0)[0])
}

func benchmarkSearch(b *testing.B, highlighted bool) {
// Pick a go file so we get something with highlighting
_, sourceFilename, _, ok := runtime.Caller(0)
Expand Down
3 changes: 3 additions & 0 deletions moar.1
Original file line number Diff line number Diff line change
Expand Up @@ -105,6 +105,9 @@ Status bar style
\fB\-\-style\fR={\fBnative\fR | \fIstyle\fR}
Highlighting style from https://xyproto.github.io/splash/docs/longer/all.html
.TP
\fB\-\-terminal\-fg\fR
Use terminal foreground color rather than style foreground color for unstyled text
.TP
\fB\-\-trace\fR
Print trace logs after exiting, more verbose than
.B \-\-debug
Expand Down
2 changes: 2 additions & 0 deletions moar.go
Original file line number Diff line number Diff line change
Expand Up @@ -341,6 +341,7 @@ func pagerFromArgs(
lexer := flagSetFunc(flagSet,
"lang", nil,
"File contents, used for highlighting. Mime type or file extension (\"html\"). Default is to guess by filename.", parseLexerOption)
terminalFg := flagSet.Bool("terminal-fg", false, "Use terminal foreground color rather than style foreground for plain text")

defaultFormatter, err := parseColorsOption("auto")
if err != nil {
Expand Down Expand Up @@ -558,6 +559,7 @@ func pagerFromArgs(
pager.QuitIfOneScreen = *quitIfOneScreen
pager.StatusBarStyle = *statusBarStyle
pager.UnprintableStyle = *unprintableStyle
pager.WithTerminalFg = *terminalFg
pager.ScrollLeftHint = *scrollLeftHint
pager.ScrollRightHint = *scrollRightHint
pager.SideScrollAmount = int(*shift)
Expand Down

0 comments on commit 34bd176

Please sign in to comment.