-
Notifications
You must be signed in to change notification settings - Fork 19
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
This moves text styling code into its own package.
- Loading branch information
Showing
10 changed files
with
161 additions
and
121 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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 | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.