mirror of
https://github.com/walles/moar.git
synced 2024-11-25 19:56:20 +03:00
Merge branch 'johan/ui-colors-from-chroma'
This commit is contained in:
commit
ed491742a1
43
m/styling.go
43
m/styling.go
@ -55,6 +55,42 @@ func twinStyleFromChroma(chromaStyle *chroma.Style, chromaFormatter *chroma.Form
|
||||
return &cells[0].Style
|
||||
}
|
||||
|
||||
func backgroundStyleFromChroma(chromaStyle chroma.Style) twin.Style {
|
||||
backgroundEntry := chromaStyle.Get(chroma.Background)
|
||||
|
||||
backgroundColor := twin.ColorDefault
|
||||
if backgroundEntry.Background.IsSet() {
|
||||
backgroundColor = twin.NewColor24Bit(
|
||||
backgroundEntry.Background.Red(),
|
||||
backgroundEntry.Background.Green(),
|
||||
backgroundEntry.Background.Blue())
|
||||
}
|
||||
|
||||
foregroundColor := twin.ColorDefault
|
||||
if backgroundEntry.Colour.IsSet() {
|
||||
foregroundColor = twin.NewColor24Bit(
|
||||
backgroundEntry.Colour.Red(),
|
||||
backgroundEntry.Colour.Green(),
|
||||
backgroundEntry.Colour.Blue())
|
||||
}
|
||||
|
||||
returnMe := twin.StyleDefault.
|
||||
WithBackground(backgroundColor).
|
||||
WithForeground(foregroundColor)
|
||||
|
||||
if backgroundEntry.Bold == chroma.Yes {
|
||||
returnMe = returnMe.WithAttr(twin.AttrBold)
|
||||
}
|
||||
if backgroundEntry.Italic == chroma.Yes {
|
||||
returnMe = returnMe.WithAttr(twin.AttrItalic)
|
||||
}
|
||||
if backgroundEntry.Underline == chroma.Yes {
|
||||
returnMe = returnMe.WithAttr(twin.AttrUnderline)
|
||||
}
|
||||
|
||||
return returnMe
|
||||
}
|
||||
|
||||
// consumeLessTermcapEnvs parses LESS_TERMCAP_xx environment variables and
|
||||
// adapts the moar output accordingly.
|
||||
func consumeLessTermcapEnvs(chromaStyle *chroma.Style, chromaFormatter *chroma.Formatter) {
|
||||
@ -88,8 +124,11 @@ func styleUi(chromaStyle *chroma.Style, chromaFormatter *chroma.Formatter, statu
|
||||
if standoutStyle != nil {
|
||||
statusbarStyle = *standoutStyle
|
||||
} else if statusbarOption == STATUSBAR_STYLE_INVERSE {
|
||||
// FIXME: Get this from the Chroma style
|
||||
statusbarStyle = twin.StyleDefault.WithAttr(twin.AttrReverse)
|
||||
if chromaStyle != nil {
|
||||
statusbarStyle = backgroundStyleFromChroma(*chromaStyle).WithAttr(twin.AttrReverse)
|
||||
} else {
|
||||
statusbarStyle = twin.StyleDefault.WithAttr(twin.AttrReverse)
|
||||
}
|
||||
} else if statusbarOption == STATUSBAR_STYLE_PLAIN {
|
||||
plain := twinStyleFromChroma(chromaStyle, chromaFormatter, chroma.None)
|
||||
if plain != nil {
|
||||
|
54
m/styling_test.go
Normal file
54
m/styling_test.go
Normal file
@ -0,0 +1,54 @@
|
||||
package m
|
||||
|
||||
import (
|
||||
"testing"
|
||||
|
||||
"github.com/alecthomas/chroma/v2/styles"
|
||||
"github.com/walles/moar/twin"
|
||||
"gotest.tools/v3/assert"
|
||||
)
|
||||
|
||||
func TestBackgroundStyleFromChromaGithub(t *testing.T) {
|
||||
style := backgroundStyleFromChroma(*styles.Get("github"))
|
||||
|
||||
assert.Equal(t, style.String(), "Default color on #ffffff")
|
||||
}
|
||||
|
||||
func TestBackgroundStyleFromChromaAverage(t *testing.T) {
|
||||
// Verify we get the right values out of a style with both Text and Background.
|
||||
style := backgroundStyleFromChroma(*styles.Get("average"))
|
||||
assert.Equal(t, style.String(), "#757575 on #000000")
|
||||
}
|
||||
|
||||
func TestBackgroundStyleFromChromaNative(t *testing.T) {
|
||||
// Verify we get the right values out of a style where Background comes with
|
||||
// both foreground and background.
|
||||
style := backgroundStyleFromChroma(*styles.Get("native"))
|
||||
assert.Equal(t, style.String(), "#d0d0d0 on #202020")
|
||||
}
|
||||
|
||||
// Loop over all styles and check that the contrast we get in
|
||||
// backgroundStyleFromChroma is good enough.
|
||||
func TestBackgroundStyleContrast(t *testing.T) {
|
||||
for _, style := range styles.Registry {
|
||||
t.Run(style.Name, func(t *testing.T) {
|
||||
backgroundStyle := backgroundStyleFromChroma(*style)
|
||||
|
||||
if backgroundStyle.Foreground().ColorType() == twin.ColorTypeDefault {
|
||||
return
|
||||
}
|
||||
|
||||
if backgroundStyle.Background().ColorType() == twin.ColorTypeDefault {
|
||||
return
|
||||
}
|
||||
|
||||
distance := backgroundStyle.Background().Distance(backgroundStyle.Foreground())
|
||||
|
||||
// 0.4 feels low, but as of Chroma v2.12.0 this is the distance we
|
||||
// get for some styles (solarized-dark, solarized-light, average).
|
||||
//
|
||||
// Let's at least verify it doesn't get worse than this.
|
||||
assert.Check(t, distance > 0.4, "distance=%f", distance)
|
||||
})
|
||||
}
|
||||
}
|
@ -117,6 +117,14 @@ func (attr AttrMask) has(attrs AttrMask) bool {
|
||||
return attr&attrs != 0
|
||||
}
|
||||
|
||||
func (style Style) Background() Color {
|
||||
return style.bg
|
||||
}
|
||||
|
||||
func (style Style) Foreground() Color {
|
||||
return style.fg
|
||||
}
|
||||
|
||||
func (style Style) WithBackground(color Color) Style {
|
||||
return Style{
|
||||
fg: style.fg,
|
||||
|
Loading…
Reference in New Issue
Block a user