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