1
1
mirror of https://github.com/walles/moar.git synced 2024-11-12 17:22:53 +03:00

Handle background color changes

This commit is contained in:
Johan Walles 2019-06-16 20:57:03 +02:00
parent 0ba1efbe46
commit 4717624c89
2 changed files with 39 additions and 14 deletions

View File

@ -1,22 +1,24 @@
package m
import (
"log"
"regexp"
"strings"
"github.com/gdamore/tcell"
)
// Token is a rune with a style to be written to a cell on screen
type Token struct {
Rune rune
Style tcell.Style
}
// TokensFromString turns a string into a series of tokens
func TokensFromString(s string) []Token {
func TokensFromString(logger *log.Logger, s string) []Token {
var tokens []Token
for _, styledString := range _StyledStringsFromString(s) {
for _, styledString := range _StyledStringsFromString(logger, s) {
for _, char := range styledString.String {
tokens = append(tokens, Token{
Rune: char,
@ -33,7 +35,7 @@ type _StyledString struct {
Style tcell.Style
}
func _StyledStringsFromString(s string) []_StyledString {
func _StyledStringsFromString(logger *log.Logger, s string) []_StyledString {
// This function was inspired by the
// https://golang.org/pkg/regexp/#Regexp.Split source code
@ -58,7 +60,7 @@ func _StyledStringsFromString(s string) []_StyledString {
}
matchedPart := s[match[0]:match[1]]
style = _UpdateStyle(style, matchedPart)
style = _UpdateStyle(logger, style, matchedPart)
beg = match[1]
}
@ -74,11 +76,13 @@ func _StyledStringsFromString(s string) []_StyledString {
}
// _UpdateStyle parses a string of the form "ESC[33m" into changes to style
func _UpdateStyle(style tcell.Style, escapeSequence string) tcell.Style {
func _UpdateStyle(logger *log.Logger, style tcell.Style, escapeSequence string) tcell.Style {
for _, number := range strings.Split(escapeSequence[2:len(escapeSequence)-1], ";") {
switch number {
case "0":
case "", "0":
style = tcell.StyleDefault
// Foreground colors
case "30":
style = style.Foreground(tcell.ColorBlack)
case "31":
@ -95,6 +99,27 @@ func _UpdateStyle(style tcell.Style, escapeSequence string) tcell.Style {
style = style.Foreground(tcell.ColorTeal)
case "37":
style = style.Foreground(tcell.ColorWhite)
// Background colors
case "40":
style = style.Background(tcell.ColorBlack)
case "41":
style = style.Background(tcell.ColorRed)
case "42":
style = style.Background(tcell.ColorGreen)
case "43":
style = style.Background(tcell.ColorYellow)
case "44":
style = style.Background(tcell.ColorBlue)
case "45":
style = style.Background(tcell.ColorPurple)
case "46":
style = style.Background(tcell.ColorTeal)
case "47":
style = style.Background(tcell.ColorWhite)
default:
logger.Printf("Unrecognized ANSI SGI code <%s>", number)
}
}

View File

@ -25,13 +25,13 @@ func NewPager(r Reader) *_Pager {
}
}
func (p *_Pager) _AddLine(lineNumber int, line string) {
for pos, token := range TokensFromString(line) {
func (p *_Pager) _AddLine(logger *log.Logger, lineNumber int, line string) {
for pos, token := range TokensFromString(logger, line) {
p.screen.SetContent(pos, lineNumber, token.Rune, nil, token.Style)
}
}
func (p *_Pager) _AddLines() {
func (p *_Pager) _AddLines(logger *log.Logger) {
_, height := p.screen.Size()
wantedLineCount := height - 1
@ -44,7 +44,7 @@ func (p *_Pager) _AddLines() {
p.firstLineOneBased = lines.firstLineOneBased
for screenLineNumber, line := range lines.lines {
p._AddLine(screenLineNumber, line)
p._AddLine(logger, screenLineNumber, line)
}
}
@ -63,10 +63,10 @@ func (p *_Pager) _AddFooter() {
}
}
func (p *_Pager) _Redraw() {
func (p *_Pager) _Redraw(logger *log.Logger) {
p.screen.Clear()
p._AddLines()
p._AddLines(logger)
p._AddFooter()
p.screen.Show()
@ -151,7 +151,7 @@ func (p *_Pager) StartPaging(logger *log.Logger, screen tcell.Screen) {
p.screen = screen
screen.Show()
p._Redraw()
p._Redraw(logger)
// Main loop
for !p.quit {
@ -171,6 +171,6 @@ func (p *_Pager) StartPaging(logger *log.Logger, screen tcell.Screen) {
logger.Printf("Unhandled event type: %v", ev)
}
p._Redraw()
p._Redraw(logger)
}
}