mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2024-11-09 20:44:03 -08:00
refactor(color): move background Template to colors
This commit is contained in:
parent
c1f99f8e6b
commit
ef16b1cd3a
|
@ -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{
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
|
|
|
@ -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)
|
||||
}
|
||||
}
|
||||
|
|
|
@ -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
|
||||
|
||||
|
|
|
@ -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{
|
||||
|
|
|
@ -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
|
||||
|
|
Loading…
Reference in a new issue