mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2025-03-05 20:49:04 -08:00
refactor: move title to module
This commit is contained in:
parent
9e7abe4541
commit
c0f4b6d6f0
|
@ -8,6 +8,7 @@ import (
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"oh-my-posh/color"
|
"oh-my-posh/color"
|
||||||
|
"oh-my-posh/console"
|
||||||
"oh-my-posh/environment"
|
"oh-my-posh/environment"
|
||||||
"oh-my-posh/properties"
|
"oh-my-posh/properties"
|
||||||
"oh-my-posh/segments"
|
"oh-my-posh/segments"
|
||||||
|
@ -24,16 +25,16 @@ import (
|
||||||
|
|
||||||
// Config holds all the theme for rendering the prompt
|
// Config holds all the theme for rendering the prompt
|
||||||
type Config struct {
|
type Config struct {
|
||||||
FinalSpace bool `config:"final_space"`
|
FinalSpace bool `config:"final_space"`
|
||||||
OSC99 bool `config:"osc99"`
|
OSC99 bool `config:"osc99"`
|
||||||
ConsoleTitle bool `config:"console_title"`
|
ConsoleTitle bool `config:"console_title"`
|
||||||
ConsoleTitleStyle ConsoleTitleStyle `config:"console_title_style"`
|
ConsoleTitleStyle console.Style `config:"console_title_style"`
|
||||||
ConsoleTitleTemplate string `config:"console_title_template"`
|
ConsoleTitleTemplate string `config:"console_title_template"`
|
||||||
TerminalBackground string `config:"terminal_background"`
|
TerminalBackground string `config:"terminal_background"`
|
||||||
Blocks []*Block `config:"blocks"`
|
Blocks []*Block `config:"blocks"`
|
||||||
Tooltips []*Segment `config:"tooltips"`
|
Tooltips []*Segment `config:"tooltips"`
|
||||||
TransientPrompt *TransientPrompt `config:"transient_prompt"`
|
TransientPrompt *TransientPrompt `config:"transient_prompt"`
|
||||||
Palette color.Palette `config:"palette"`
|
Palette color.Palette `config:"palette"`
|
||||||
}
|
}
|
||||||
|
|
||||||
// MakeColors creates instance of AnsiColors to use in AnsiWriter according to
|
// MakeColors creates instance of AnsiColors to use in AnsiWriter according to
|
||||||
|
@ -168,7 +169,7 @@ func getDefaultConfig(info string) *Config {
|
||||||
cfg := &Config{
|
cfg := &Config{
|
||||||
FinalSpace: true,
|
FinalSpace: true,
|
||||||
ConsoleTitle: true,
|
ConsoleTitle: true,
|
||||||
ConsoleTitleStyle: FolderName,
|
ConsoleTitleStyle: console.FolderName,
|
||||||
Blocks: []*Block{
|
Blocks: []*Block{
|
||||||
{
|
{
|
||||||
Type: Prompt,
|
Type: Prompt,
|
||||||
|
|
60
src/console/title.go
Normal file
60
src/console/title.go
Normal file
|
@ -0,0 +1,60 @@
|
||||||
|
package console
|
||||||
|
|
||||||
|
import (
|
||||||
|
"oh-my-posh/color"
|
||||||
|
"oh-my-posh/environment"
|
||||||
|
"oh-my-posh/template"
|
||||||
|
"strings"
|
||||||
|
)
|
||||||
|
|
||||||
|
type Title struct {
|
||||||
|
Env environment.Environment
|
||||||
|
Ansi *color.Ansi
|
||||||
|
Style Style
|
||||||
|
Template string
|
||||||
|
}
|
||||||
|
|
||||||
|
// Style defines how to show the title in the console window
|
||||||
|
type Style string
|
||||||
|
|
||||||
|
const (
|
||||||
|
// FolderName show the current folder name
|
||||||
|
FolderName Style = "folder"
|
||||||
|
// FullPath show the current path
|
||||||
|
FullPath Style = "path"
|
||||||
|
// Template allows a more powerful custom string
|
||||||
|
Template Style = "template"
|
||||||
|
)
|
||||||
|
|
||||||
|
func (t *Title) GetTitle() string {
|
||||||
|
var title string
|
||||||
|
switch t.Style {
|
||||||
|
case FullPath:
|
||||||
|
title = t.getPwd()
|
||||||
|
case Template:
|
||||||
|
title = t.getTitleTemplateText()
|
||||||
|
case FolderName:
|
||||||
|
fallthrough
|
||||||
|
default:
|
||||||
|
title = environment.Base(t.Env, t.getPwd())
|
||||||
|
}
|
||||||
|
title = t.Ansi.EscapeText(title)
|
||||||
|
return t.Ansi.Title(title)
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Title) getTitleTemplateText() string {
|
||||||
|
tmpl := &template.Text{
|
||||||
|
Template: t.Template,
|
||||||
|
Env: t.Env,
|
||||||
|
}
|
||||||
|
if text, err := tmpl.Render(); err == nil {
|
||||||
|
return text
|
||||||
|
}
|
||||||
|
return ""
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t *Title) getPwd() string {
|
||||||
|
pwd := t.Env.Pwd()
|
||||||
|
pwd = strings.Replace(pwd, t.Env.Home(), "~", 1)
|
||||||
|
return pwd
|
||||||
|
}
|
|
@ -1,4 +1,4 @@
|
||||||
package main
|
package console
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"oh-my-posh/color"
|
"oh-my-posh/color"
|
||||||
|
@ -9,9 +9,9 @@ import (
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestGetConsoleTitle(t *testing.T) {
|
func TestGetTitle(t *testing.T) {
|
||||||
cases := []struct {
|
cases := []struct {
|
||||||
Style ConsoleTitleStyle
|
Style Style
|
||||||
Template string
|
Template string
|
||||||
Root bool
|
Root bool
|
||||||
User string
|
User string
|
||||||
|
@ -51,10 +51,6 @@ func TestGetConsoleTitle(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range cases {
|
for _, tc := range cases {
|
||||||
config := &Config{
|
|
||||||
ConsoleTitleStyle: tc.Style,
|
|
||||||
ConsoleTitleTemplate: tc.Template,
|
|
||||||
}
|
|
||||||
env := new(mock.MockedEnvironment)
|
env := new(mock.MockedEnvironment)
|
||||||
env.On("Pwd").Return(tc.Cwd)
|
env.On("Pwd").Return(tc.Cwd)
|
||||||
env.On("Home").Return("/usr/home")
|
env.On("Home").Return("/usr/home")
|
||||||
|
@ -72,19 +68,20 @@ func TestGetConsoleTitle(t *testing.T) {
|
||||||
})
|
})
|
||||||
ansi := &color.Ansi{}
|
ansi := &color.Ansi{}
|
||||||
ansi.Init(tc.ShellName)
|
ansi.Init(tc.ShellName)
|
||||||
ct := &consoleTitle{
|
ct := &Title{
|
||||||
env: env,
|
Env: env,
|
||||||
config: config,
|
Ansi: ansi,
|
||||||
ansi: ansi,
|
Style: tc.Style,
|
||||||
|
Template: tc.Template,
|
||||||
}
|
}
|
||||||
got := ct.getConsoleTitle()
|
got := ct.GetTitle()
|
||||||
assert.Equal(t, tc.Expected, got)
|
assert.Equal(t, tc.Expected, got)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func TestGetConsoleTitleIfGethostnameReturnsError(t *testing.T) {
|
func TestGetConsoleTitleIfGethostnameReturnsError(t *testing.T) {
|
||||||
cases := []struct {
|
cases := []struct {
|
||||||
Style ConsoleTitleStyle
|
Style Style
|
||||||
Template string
|
Template string
|
||||||
Root bool
|
Root bool
|
||||||
User string
|
User string
|
||||||
|
@ -112,10 +109,6 @@ func TestGetConsoleTitleIfGethostnameReturnsError(t *testing.T) {
|
||||||
}
|
}
|
||||||
|
|
||||||
for _, tc := range cases {
|
for _, tc := range cases {
|
||||||
config := &Config{
|
|
||||||
ConsoleTitleStyle: tc.Style,
|
|
||||||
ConsoleTitleTemplate: tc.Template,
|
|
||||||
}
|
|
||||||
env := new(mock.MockedEnvironment)
|
env := new(mock.MockedEnvironment)
|
||||||
env.On("Pwd").Return(tc.Cwd)
|
env.On("Pwd").Return(tc.Cwd)
|
||||||
env.On("Home").Return("/usr/home")
|
env.On("Home").Return("/usr/home")
|
||||||
|
@ -130,12 +123,13 @@ func TestGetConsoleTitleIfGethostnameReturnsError(t *testing.T) {
|
||||||
})
|
})
|
||||||
ansi := &color.Ansi{}
|
ansi := &color.Ansi{}
|
||||||
ansi.Init(tc.ShellName)
|
ansi.Init(tc.ShellName)
|
||||||
ct := &consoleTitle{
|
ct := &Title{
|
||||||
env: env,
|
Env: env,
|
||||||
config: config,
|
Ansi: ansi,
|
||||||
ansi: ansi,
|
Style: tc.Style,
|
||||||
|
Template: tc.Template,
|
||||||
}
|
}
|
||||||
got := ct.getConsoleTitle()
|
got := ct.GetTitle()
|
||||||
assert.Equal(t, tc.Expected, got)
|
assert.Equal(t, tc.Expected, got)
|
||||||
}
|
}
|
||||||
}
|
}
|
|
@ -1,59 +0,0 @@
|
||||||
package main
|
|
||||||
|
|
||||||
import (
|
|
||||||
"oh-my-posh/color"
|
|
||||||
"oh-my-posh/environment"
|
|
||||||
"oh-my-posh/template"
|
|
||||||
"strings"
|
|
||||||
)
|
|
||||||
|
|
||||||
type consoleTitle struct {
|
|
||||||
env environment.Environment
|
|
||||||
config *Config
|
|
||||||
ansi *color.Ansi
|
|
||||||
}
|
|
||||||
|
|
||||||
// ConsoleTitleStyle defines how to show the title in the console window
|
|
||||||
type ConsoleTitleStyle string
|
|
||||||
|
|
||||||
const (
|
|
||||||
// FolderName show the current folder name
|
|
||||||
FolderName ConsoleTitleStyle = "folder"
|
|
||||||
// FullPath show the current path
|
|
||||||
FullPath ConsoleTitleStyle = "path"
|
|
||||||
// Template allows a more powerful custom string
|
|
||||||
Template ConsoleTitleStyle = "template"
|
|
||||||
)
|
|
||||||
|
|
||||||
func (t *consoleTitle) getConsoleTitle() string {
|
|
||||||
var title string
|
|
||||||
switch t.config.ConsoleTitleStyle {
|
|
||||||
case FullPath:
|
|
||||||
title = t.getPwd()
|
|
||||||
case Template:
|
|
||||||
title = t.getTemplateText()
|
|
||||||
case FolderName:
|
|
||||||
fallthrough
|
|
||||||
default:
|
|
||||||
title = environment.Base(t.env, t.getPwd())
|
|
||||||
}
|
|
||||||
title = t.ansi.EscapeText(title)
|
|
||||||
return t.ansi.Title(title)
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *consoleTitle) getTemplateText() string {
|
|
||||||
tmpl := &template.Text{
|
|
||||||
Template: t.config.ConsoleTitleTemplate,
|
|
||||||
Env: t.env,
|
|
||||||
}
|
|
||||||
if text, err := tmpl.Render(); err == nil {
|
|
||||||
return text
|
|
||||||
}
|
|
||||||
return ""
|
|
||||||
}
|
|
||||||
|
|
||||||
func (t *consoleTitle) getPwd() string {
|
|
||||||
pwd := t.env.Pwd()
|
|
||||||
pwd = strings.Replace(pwd, t.env.Home(), "~", 1)
|
|
||||||
return pwd
|
|
||||||
}
|
|
|
@ -3,6 +3,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"fmt"
|
||||||
"oh-my-posh/color"
|
"oh-my-posh/color"
|
||||||
|
"oh-my-posh/console"
|
||||||
"oh-my-posh/environment"
|
"oh-my-posh/environment"
|
||||||
"oh-my-posh/template"
|
"oh-my-posh/template"
|
||||||
"strings"
|
"strings"
|
||||||
|
@ -14,7 +15,7 @@ type engine struct {
|
||||||
env environment.Environment
|
env environment.Environment
|
||||||
writer color.Writer
|
writer color.Writer
|
||||||
ansi *color.Ansi
|
ansi *color.Ansi
|
||||||
consoleTitle *consoleTitle
|
consoleTitle *console.Title
|
||||||
plain bool
|
plain bool
|
||||||
|
|
||||||
console strings.Builder
|
console strings.Builder
|
||||||
|
@ -59,7 +60,7 @@ func (e *engine) render() string {
|
||||||
e.renderBlock(block)
|
e.renderBlock(block)
|
||||||
}
|
}
|
||||||
if e.config.ConsoleTitle {
|
if e.config.ConsoleTitle {
|
||||||
e.writeANSI(e.consoleTitle.getConsoleTitle())
|
e.writeANSI(e.consoleTitle.GetTitle())
|
||||||
}
|
}
|
||||||
e.writeANSI(e.ansi.ColorReset())
|
e.writeANSI(e.ansi.ColorReset())
|
||||||
if e.config.FinalSpace {
|
if e.config.FinalSpace {
|
||||||
|
@ -132,7 +133,7 @@ func (e *engine) debug() string {
|
||||||
e.write("\n\x1b[1mSegments:\x1b[0m\n\n")
|
e.write("\n\x1b[1mSegments:\x1b[0m\n\n")
|
||||||
// console title timing
|
// console title timing
|
||||||
start := time.Now()
|
start := time.Now()
|
||||||
consoleTitle := e.consoleTitle.getTemplateText()
|
consoleTitle := e.consoleTitle.GetTitle()
|
||||||
duration := time.Since(start)
|
duration := time.Since(start)
|
||||||
segmentTiming := &SegmentTiming{
|
segmentTiming := &SegmentTiming{
|
||||||
name: "ConsoleTitle",
|
name: "ConsoleTitle",
|
||||||
|
|
|
@ -3,6 +3,7 @@ package main
|
||||||
import (
|
import (
|
||||||
"errors"
|
"errors"
|
||||||
"oh-my-posh/color"
|
"oh-my-posh/color"
|
||||||
|
"oh-my-posh/console"
|
||||||
"oh-my-posh/environment"
|
"oh-my-posh/environment"
|
||||||
"oh-my-posh/mock"
|
"oh-my-posh/mock"
|
||||||
"os"
|
"os"
|
||||||
|
@ -103,16 +104,17 @@ func engineRender(configPath string) error {
|
||||||
TerminalBackground: getConsoleBackgroundColor(env, cfg.TerminalBackground),
|
TerminalBackground: getConsoleBackgroundColor(env, cfg.TerminalBackground),
|
||||||
AnsiColors: writerColors,
|
AnsiColors: writerColors,
|
||||||
}
|
}
|
||||||
title := &consoleTitle{
|
consoleTitle := &console.Title{
|
||||||
env: env,
|
Env: env,
|
||||||
config: cfg,
|
Ansi: ansi,
|
||||||
ansi: ansi,
|
Style: cfg.ConsoleTitleStyle,
|
||||||
|
Template: cfg.ConsoleTitleTemplate,
|
||||||
}
|
}
|
||||||
engine := &engine{
|
engine := &engine{
|
||||||
config: cfg,
|
config: cfg,
|
||||||
env: env,
|
env: env,
|
||||||
writer: writer,
|
writer: writer,
|
||||||
consoleTitle: title,
|
consoleTitle: consoleTitle,
|
||||||
ansi: ansi,
|
ansi: ansi,
|
||||||
plain: *args.Plain,
|
plain: *args.Plain,
|
||||||
}
|
}
|
||||||
|
|
|
@ -65,7 +65,7 @@ const (
|
||||||
left = "left"
|
left = "left"
|
||||||
osc99 = "osc99"
|
osc99 = "osc99"
|
||||||
lineChange = "linechange"
|
lineChange = "linechange"
|
||||||
title = "title"
|
consoleTitle = "title"
|
||||||
link = "link"
|
link = "link"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
@ -181,7 +181,7 @@ func (ir *ImageRenderer) init() {
|
||||||
left: `^(?P<STR>\x1b\[(\d{1,3})D)`,
|
left: `^(?P<STR>\x1b\[(\d{1,3})D)`,
|
||||||
osc99: `^(?P<STR>\x1b\]9;9;(.+)\x1b\\)`,
|
osc99: `^(?P<STR>\x1b\]9;9;(.+)\x1b\\)`,
|
||||||
lineChange: `^(?P<STR>\x1b\[(\d)[FB])`,
|
lineChange: `^(?P<STR>\x1b\[(\d)[FB])`,
|
||||||
title: `^(?P<STR>\x1b\]0;(.+)\007)`,
|
consoleTitle: `^(?P<STR>\x1b\]0;(.+)\007)`,
|
||||||
link: `^(?P<STR>\x1b]8;;file:\/\/(.+)\x1b\\(?P<URL>.+)\x1b]8;;\x1b\\)`,
|
link: `^(?P<STR>\x1b]8;;file:\/\/(.+)\x1b\\(?P<URL>.+)\x1b]8;;\x1b\\)`,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -457,7 +457,7 @@ func (ir *ImageRenderer) shouldPrint() bool {
|
||||||
case boldReset, italicReset, underlineReset:
|
case boldReset, italicReset, underlineReset:
|
||||||
ir.style = ""
|
ir.style = ""
|
||||||
return false
|
return false
|
||||||
case strikethrough, strikethroughReset, left, osc99, lineChange, title:
|
case strikethrough, strikethroughReset, left, osc99, lineChange, consoleTitle:
|
||||||
return false
|
return false
|
||||||
case color16:
|
case color16:
|
||||||
ir.setBase16Color(match[fg])
|
ir.setBase16Color(match[fg])
|
||||||
|
|
12
src/main.go
12
src/main.go
|
@ -5,6 +5,7 @@ import (
|
||||||
"flag"
|
"flag"
|
||||||
"fmt"
|
"fmt"
|
||||||
"oh-my-posh/color"
|
"oh-my-posh/color"
|
||||||
|
"oh-my-posh/console"
|
||||||
"oh-my-posh/environment"
|
"oh-my-posh/environment"
|
||||||
"oh-my-posh/regex"
|
"oh-my-posh/regex"
|
||||||
"oh-my-posh/template"
|
"oh-my-posh/template"
|
||||||
|
@ -200,16 +201,17 @@ func main() {
|
||||||
AnsiColors: writerColors,
|
AnsiColors: writerColors,
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
title := &consoleTitle{
|
consoleTitle := &console.Title{
|
||||||
env: env,
|
Env: env,
|
||||||
config: cfg,
|
Ansi: ansi,
|
||||||
ansi: ansi,
|
Template: cfg.ConsoleTitleTemplate,
|
||||||
|
Style: cfg.ConsoleTitleStyle,
|
||||||
}
|
}
|
||||||
engine := &engine{
|
engine := &engine{
|
||||||
config: cfg,
|
config: cfg,
|
||||||
env: env,
|
env: env,
|
||||||
writer: writer,
|
writer: writer,
|
||||||
consoleTitle: title,
|
consoleTitle: consoleTitle,
|
||||||
ansi: ansi,
|
ansi: ansi,
|
||||||
plain: *args.Plain,
|
plain: *args.Plain,
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue