refactor(color): move background Template to colors

This commit is contained in:
Jan De Dobbeleer 2024-07-24 13:17:09 +02:00 committed by Jan De Dobbeleer
parent c1f99f8e6b
commit ef16b1cd3a
7 changed files with 58 additions and 32 deletions

View file

@ -70,7 +70,7 @@ Exports the config to an image file using customized output options.`,
env.Var = cfg.Var
terminal.Init(shell.GENERIC)
terminal.BackgroundColor = shell.ConsoleBackgroundColor(env, cfg.TerminalBackground)
terminal.BackgroundColor = cfg.TerminalBackground.ResolveTemplate(env)
terminal.Colors = cfg.MakeColors()
eng := &prompt.Engine{

View file

@ -42,7 +42,7 @@ var debugCmd = &cobra.Command{
env.Var = cfg.Var
terminal.Init(shell.GENERIC)
terminal.BackgroundColor = shell.ConsoleBackgroundColor(env, cfg.TerminalBackground)
terminal.BackgroundColor = cfg.TerminalBackground.ResolveTemplate(env)
terminal.Colors = cfg.MakeColors()
terminal.Plain = plain

View file

@ -7,6 +7,7 @@ import (
"github.com/gookit/color"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
"github.com/jandedobbeleer/oh-my-posh/src/template"
)
var TrueColor = true
@ -103,6 +104,29 @@ func (c Ansi) ToForeground() Ansi {
return c
}
func (c Ansi) ResolveTemplate(env runtime.Environment) Ansi {
if c.IsEmpty() {
return c
}
if c.IsTransparent() {
return emptyColor
}
tmpl := &template.Text{
Template: string(c),
Context: nil,
Env: env,
}
text, err := tmpl.Render()
if err != nil {
return Transparent
}
return Ansi(text)
}
func (c Ansi) String() string {
return string(c)
}

View file

@ -5,8 +5,11 @@ import (
"testing"
"github.com/alecthomas/assert"
"github.com/jandedobbeleer/oh-my-posh/src/cache"
"github.com/jandedobbeleer/oh-my-posh/src/runtime"
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
testify_ "github.com/stretchr/testify/mock"
)
func TestGetAnsiFromColorString(t *testing.T) {
@ -57,3 +60,30 @@ func TestMakeColors(t *testing.T) {
assert.IsType(t, &PaletteColors{}, colors.(*Cached).ansiColors)
assert.IsType(t, &Defaults{}, colors.(*Cached).ansiColors.(*PaletteColors).ansiColors)
}
func TestAnsiRender(t *testing.T) {
cases := []struct {
Case string
Expected Ansi
Term string
}{
{Case: "Inside vscode", Expected: "#123456", Term: "vscode"},
{Case: "Outside vscode", Expected: "", Term: "windowsterminal"},
}
for _, tc := range cases {
env := new(mock.Environment)
env.On("DebugF", testify_.Anything, testify_.Anything).Return(nil)
env.On("TemplateCache").Return(&cache.Template{
Env: map[string]string{
"TERM_PROGRAM": tc.Term,
},
})
env.On("Flags").Return(&runtime.Flags{})
ansi := Ansi("{{ if eq \"vscode\" .Env.TERM_PROGRAM }}#123456{{end}}")
got := ansi.ResolveTemplate(env)
assert.Equal(t, tc.Expected, got, tc.Case)
}
}

View file

@ -518,7 +518,7 @@ func New(flags *runtime.Flags) *Engine {
flags.HasTransient = cfg.TransientPrompt != nil
terminal.Init(env.Shell())
terminal.BackgroundColor = shell.ConsoleBackgroundColor(env, cfg.TerminalBackground)
terminal.BackgroundColor = cfg.TerminalBackground.ResolveTemplate(env)
terminal.Colors = cfg.MakeColors()
terminal.Plain = flags.Plain

View file

@ -120,7 +120,7 @@ func engineRender() {
cfg := config.Load(env)
terminal.Init(shell.GENERIC)
terminal.BackgroundColor = shell.ConsoleBackgroundColor(env, cfg.TerminalBackground)
terminal.BackgroundColor = cfg.TerminalBackground.ResolveTemplate(env)
terminal.Colors = cfg.MakeColors()
engine := &Engine{

View file

@ -4,37 +4,9 @@ import (
"fmt"
"testing"
"github.com/jandedobbeleer/oh-my-posh/src/cache"
"github.com/jandedobbeleer/oh-my-posh/src/color"
"github.com/jandedobbeleer/oh-my-posh/src/runtime/mock"
"github.com/stretchr/testify/assert"
testify_ "github.com/stretchr/testify/mock"
)
func TestConsoleBackgroundColorTemplate(t *testing.T) {
cases := []struct {
Case string
Expected string
Term string
}{
{Case: "Inside vscode", Expected: "#123456", Term: "vscode"},
{Case: "Outside vscode", Expected: "", Term: "windowsterminal"},
}
for _, tc := range cases {
env := new(mock.Environment)
env.On("DebugF", testify_.Anything, testify_.Anything).Return(nil)
env.On("TemplateCache").Return(&cache.Template{
Env: map[string]string{
"TERM_PROGRAM": tc.Term,
},
})
bgColor := ConsoleBackgroundColor(env, "{{ if eq \"vscode\" .Env.TERM_PROGRAM }}#123456{{end}}")
assert.Equal(t, color.Ansi(tc.Expected), bgColor, tc.Case)
}
}
func TestQuotePwshStr(t *testing.T) {
tests := []struct {
str string