feat: remove console title style

This commit is contained in:
Jan De Dobbeleer 2022-03-14 19:14:10 +01:00 committed by Jan De Dobbeleer
parent d796ff166e
commit 096cb8e997
38 changed files with 25 additions and 150 deletions

View file

@ -14,23 +14,8 @@ sidebar_label: Console title
}
```
To manipulate the console title, you can make use of the following properties:
- console_title: `boolean` - when true sets the current location as the console title
- console_title_style: `string` - the title to set in the console - defaults to `folder`
- console_title_template: `string` - the template to use when `"console_title_style" = "template"`
### Console Title Style
- `folder`: show the current folder name
- `path`: show the current path
- `template`: show a custom template
### Console Title Template
You can create a more custom console title with the use of `"console_title_style" = "template"`.
When this is set, a `console_title_template` is also expected, otherwise, the title will remain empty.
The following examples illustrate possible contents for `console_title_template`, provided
the current working directory is `/usr/home/omp` and the shell is `zsh`.

View file

@ -61,7 +61,6 @@ You can tweak the output by using additional flags:
Env: env,
Ansi: ansi,
Template: cfg.ConsoleTitleTemplate,
Style: cfg.ConsoleTitleStyle,
}
eng := &engine.Engine{
Config: cfg,

View file

@ -62,7 +62,11 @@ var printCmd = &cobra.Command{
defer env.Close()
cfg := engine.LoadConfig(env)
ansi := &color.Ansi{}
ansi.Init(env.Shell())
shell := env.Shell()
if debug {
shell = "shell"
}
ansi.Init(shell)
var writer color.Writer
if plain {
writer = &color.PlainWriter{}
@ -78,7 +82,6 @@ var printCmd = &cobra.Command{
Env: env,
Ansi: ansi,
Template: cfg.ConsoleTitleTemplate,
Style: cfg.ConsoleTitleStyle,
}
eng := &engine.Engine{
Config: cfg,

View file

@ -4,40 +4,16 @@ 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.getTitleTemplateText()
title = t.Ansi.EscapeText(title)
return t.Ansi.Title(title)
}
@ -52,9 +28,3 @@ func (t *Title) getTitleTemplateText() string {
}
return ""
}
func (t *Title) getPwd() string {
pwd := t.Env.Pwd()
pwd = strings.Replace(pwd, t.Env.Home(), "~", 1)
return pwd
}

View file

@ -11,7 +11,6 @@ import (
func TestGetTitle(t *testing.T) {
cases := []struct {
Style Style
Template string
Root bool
User string
@ -20,10 +19,7 @@ func TestGetTitle(t *testing.T) {
ShellName string
Expected string
}{
{Style: FolderName, Cwd: "/usr/home", PathSeparator: "/", ShellName: "default", Expected: "\x1b]0;~\a"},
{Style: FullPath, Cwd: "/usr/home/jan", PathSeparator: "/", ShellName: "default", Expected: "\x1b]0;~/jan\a"},
{
Style: Template,
Template: "{{.Env.USERDOMAIN}} :: {{.PWD}}{{if .Root}} :: Admin{{end}} :: {{.Shell}}",
Cwd: "C:\\vagrant",
PathSeparator: "\\",
@ -32,7 +28,6 @@ func TestGetTitle(t *testing.T) {
Expected: "\x1b]0;MyCompany :: C:\\vagrant :: Admin :: PowerShell\a",
},
{
Style: Template,
Template: "{{.Folder}}{{if .Root}} :: Admin{{end}} :: {{.Shell}}",
Cwd: "C:\\vagrant",
PathSeparator: "\\",
@ -40,7 +35,6 @@ func TestGetTitle(t *testing.T) {
Expected: "\x1b]0;vagrant :: PowerShell\a",
},
{
Style: Template,
Template: "{{.UserName}}@{{.HostName}}{{if .Root}} :: Admin{{end}} :: {{.Shell}}",
Root: true,
User: "MyUser",
@ -71,7 +65,6 @@ func TestGetTitle(t *testing.T) {
ct := &Title{
Env: env,
Ansi: ansi,
Style: tc.Style,
Template: tc.Template,
}
got := ct.GetTitle()
@ -81,7 +74,6 @@ func TestGetTitle(t *testing.T) {
func TestGetConsoleTitleIfGethostnameReturnsError(t *testing.T) {
cases := []struct {
Style Style
Template string
Root bool
User string
@ -91,7 +83,6 @@ func TestGetConsoleTitleIfGethostnameReturnsError(t *testing.T) {
Expected string
}{
{
Style: Template,
Template: "Not using Host only {{.UserName}} and {{.Shell}}",
User: "MyUser",
PathSeparator: "\\",
@ -99,7 +90,6 @@ func TestGetConsoleTitleIfGethostnameReturnsError(t *testing.T) {
Expected: "\x1b]0;Not using Host only MyUser and PowerShell\a",
},
{
Style: Template,
Template: "{{.UserName}}@{{.HostName}} :: {{.Shell}}",
User: "MyUser",
PathSeparator: "\\",
@ -126,7 +116,6 @@ func TestGetConsoleTitleIfGethostnameReturnsError(t *testing.T) {
ct := &Title{
Env: env,
Ansi: ansi,
Style: tc.Style,
Template: tc.Template,
}
got := ct.GetTitle()

View file

@ -6,7 +6,6 @@ import (
"fmt"
"io"
"oh-my-posh/color"
"oh-my-posh/console"
"oh-my-posh/environment"
"oh-my-posh/properties"
"os"
@ -33,8 +32,6 @@ type Config struct {
Version int `json:"version"`
FinalSpace bool `json:"final_space,omitempty"`
OSC99 bool `json:"osc99,omitempty"`
ConsoleTitle bool `json:"console_title,omitempty"`
ConsoleTitleStyle console.Style `json:"console_title_style,omitempty"`
ConsoleTitleTemplate string `json:"console_title_template,omitempty"`
TerminalBackground string `json:"terminal_background,omitempty"`
Blocks []*Block `json:"blocks,omitempty"`

View file

@ -60,7 +60,7 @@ func (e *Engine) PrintPrimary() string {
for _, block := range e.Config.Blocks {
e.renderBlock(block)
}
if e.Config.ConsoleTitle {
if len(e.Config.ConsoleTitleTemplate) > 0 {
e.writeANSI(e.ConsoleTitle.GetTitle())
}
e.writeANSI(e.Ansi.ColorReset())
@ -167,13 +167,15 @@ func (e *Engine) PrintDebug(version string) string {
e.write("\n\x1b[1mSegments:\x1b[0m\n\n")
// console title timing
start := time.Now()
consoleTitle := e.ConsoleTitle.GetTitle()
title := e.ConsoleTitle.GetTitle()
title = strings.TrimPrefix(title, "\x1b]0;")
title = strings.TrimSuffix(title, "\a")
duration := time.Since(start)
segmentTiming := &SegmentTiming{
name: "ConsoleTitle",
nameLength: 12,
active: e.Config.ConsoleTitle,
text: consoleTitle,
active: len(e.Config.ConsoleTitleTemplate) > 0,
text: title,
duration: duration,
}
segmentTimings = append(segmentTimings, segmentTiming)

View file

@ -67,7 +67,6 @@ func engineRender() {
consoleTitle := &console.Title{
Env: env,
Ansi: ansi,
Style: cfg.ConsoleTitleStyle,
Template: cfg.ConsoleTitleTemplate,
}
engine := &Engine{

View file

@ -229,8 +229,6 @@
"type": "rprompt"
}
],
"console_title": true,
"console_title_style": "template",
"console_title_template": "{{ .Shell }} in {{ .Folder }}",
"final_space": true,
"palette": {

View file

@ -229,8 +229,6 @@
"type": "rprompt"
}
],
"console_title": true,
"console_title_style": "template",
"console_title_template": "{{ .Shell }} in {{ .Folder }}",
"final_space": true,
"version": 1

View file

@ -127,8 +127,6 @@
"type": "prompt"
}
],
"console_title": true,
"console_title_style": "template",
"console_title_template": "{{if .Root}} \u26a1 {{end}}{{.Folder | replace \"~\" \"🏚\" }} @ {{.HostName}}",
"osc99": true,
"version": 1

View file

@ -42,7 +42,7 @@
},
{
"background": "#C678DD",
"foreground": "ffffff",
"foreground": "#ffffff",
"powerline_symbol": "\ue0b0",
"properties": {
"folder_icon": "\uf115",
@ -68,8 +68,6 @@
"type": "prompt"
}
],
"console_title": true,
"console_title_style": "template",
"console_title_template": "{{if .Root}} \u26a1 {{end}}{{.UserName}} \u2794 📁{{.Folder}}",
"final_space": true,
"version": 1

View file

@ -143,8 +143,6 @@
"type": "prompt"
}
],
"console_title": true,
"console_title_style": "template",
"console_title_template": "{{ .Folder }}",
"osc99": true,
"transient_prompt": {

View file

@ -154,8 +154,6 @@
"type": "prompt"
}
],
"console_title": true,
"console_title_style": "template",
"console_title_template": "{{ .Shell }} in {{ .Folder }}",
"final_space": true,
"version": 1

View file

@ -114,8 +114,6 @@
"type": "prompt"
}
],
"console_title": true,
"console_title_style": "template",
"console_title_template": "{{if .Root}}(Admin) {{end}}{{.Folder}}",
"version": 1
}

View file

@ -248,8 +248,6 @@
"type": "rprompt"
}
],
"console_title": true,
"console_title_style": "template",
"console_title_template": "{{if .Root}}Admin: {{end}} {{.Folder}}",
"final_space": true,
"version": 1

View file

@ -125,8 +125,6 @@
"type": "prompt"
}
],
"console_title": true,
"console_title_style": "template",
"console_title_template": "{{if .Root}} \u26a1 {{end}}{{.Folder | replace \"~\" \"🏚\" }} @ {{.HostName}}",
"osc99": true,
"version": 1

View file

@ -53,8 +53,6 @@
"type": "prompt"
}
],
"console_title": true,
"console_title_style": "template",
"console_title_template": "{{.Folder}}{{if .Root}} :: root{{end}} :: {{.Shell}}",
"final_space": true,
"osc99": true,

View file

@ -170,8 +170,6 @@
"type": "prompt"
}
],
"console_title": true,
"console_title_style": "template",
"console_title_template": "{{ .Folder }}",
"osc99": true,
"transient_prompt": {

View file

@ -142,8 +142,6 @@
"type": "prompt"
}
],
"console_title": true,
"console_title_style": "template",
"console_title_template": "{{ .Shell }} in {{ .Folder }}",
"final_space": true,
"transient_prompt": {

View file

@ -229,8 +229,6 @@
"type": "rprompt"
}
],
"console_title": true,
"console_title_style": "template",
"console_title_template": "{{ .Shell }} in {{ .Folder }}",
"final_space": true,
"version": 1

View file

@ -100,8 +100,6 @@
"type": "prompt"
}
],
"console_title": true,
"console_title_style": "template",
"console_title_template": "{{if .Root}} \u26a1 {{end}}{{.Folder | replace \"~\" \"🏠\"}} @ {{.HostName}}",
"final_space": true,
"osc99": true,

View file

@ -157,8 +157,6 @@
"type": "prompt"
}
],
"console_title": true,
"console_title_style": "template",
"console_title_template": "{{if .Root}}root :: {{end}}{{.Shell}} :: {{.Folder}}",
"final_space": true,
"version": 1

View file

@ -181,8 +181,6 @@
"type": "rprompt"
}
],
"console_title": true,
"console_title_style": "template",
"console_title_template": "{{if .Root}}Admin: {{end}} {{.Folder}}",
"final_space": true,
"version": 1

View file

@ -98,8 +98,6 @@
"type": "prompt"
}
],
"console_title": true,
"console_title_style": "template",
"console_title_template": "{{ .Shell }} in {{ .Folder }}",
"final_space": true,
"version": 1

View file

@ -123,8 +123,6 @@
"type": "prompt"
}
],
"console_title": true,
"console_title_style": "template",
"console_title_template": "{{if .Root}}\u26a1 {{end}}{{.Folder}}",
"final_space": true,
"osc99": true,

View file

@ -144,8 +144,6 @@
"type": "rprompt"
}
],
"console_title": true,
"console_title_style": "template",
"console_title_template": "{{.UserName}}@{{.HostName}} : {{.Folder}}",
"final_space": true,
"version": 1

View file

@ -317,8 +317,6 @@
"type": "prompt"
}
],
"console_title": true,
"console_title_style": "template",
"console_title_template": "{{ .Folder }}",
"osc99": true,
"transient_prompt": {

View file

@ -93,8 +93,6 @@
"foreground": "#100e23",
"template": " \uf061 "
},
"console_title": true,
"console_title_style": "template",
"console_title_template": "{{if .Root}} \u26a1 {{end}}{{.UserName}} \u2794 📁{{.Folder}}",
"final_space": true,
"version": 1

View file

@ -153,8 +153,6 @@
"type": "prompt"
}
],
"console_title": true,
"console_title_style": "template",
"console_title_template": "{{if .Root}}root :: {{end}}{{.Shell}} :: {{.Folder}}",
"final_space": true,
"version": 1

View file

@ -222,8 +222,6 @@
"type": "rprompt"
}
],
"console_title": true,
"console_title_style": "template",
"console_title_template": "{{ .Shell }} in {{ .Folder }}",
"final_space": true,
"version": 1

View file

@ -80,8 +80,6 @@
"type": "prompt"
}
],
"console_title": true,
"console_title_style": "template",
"console_title_template": "{{if .Root}}(Admin){{end}} {{.PWD}}",
"version": 1
}

View file

@ -2112,19 +2112,6 @@
"description": "https://ohmyposh.dev/docs/config-overview#general-settings",
"default": false
},
"console_title": {
"type": "boolean",
"title": "Console Title",
"description": "https://ohmyposh.dev/docs/config-overview#general-settings",
"default": true
},
"console_title_style": {
"type": "string",
"title": "Console Title Style",
"description": "https://ohmyposh.dev/docs/config-title#console-title-style",
"enum": ["folder", "path", "template"],
"default": "folder"
},
"console_title_template": {
"type": "string",
"title": "Console Title Template",

View file

@ -196,8 +196,6 @@
"type": "rprompt"
}
],
"console_title": true,
"console_title_style": "template",
"console_title_template": "{{if .Root}}root :: {{end}}{{.Shell}} :: {{.Folder}}",
"final_space": true,
"version": 1

View file

@ -194,8 +194,6 @@
"type": "rprompt"
}
],
"console_title": true,
"console_title_style": "template",
"console_title_template": "{{if .Root}}root :: {{end}}{{.Shell}} :: {{.Folder}}",
"final_space": true,
"version": 1

View file

@ -112,6 +112,6 @@
"type": "prompt"
}
],
"console_title_style": "folder",
"console_title_template": "{{ .Folder }}",
"version": 1
}

View file

@ -95,8 +95,6 @@
"type": "prompt"
}
],
"console_title": true,
"console_title_style": "template",
"console_title_template": "{{.UserName}}@{{.HostName}} in {{ .PWD }}",
"final_space": true,
"version": 1

View file

@ -164,8 +164,6 @@
"type": "prompt"
}
],
"console_title": true,
"console_title_style": "template",
"console_title_template": "{{ .Shell }} - {{ .Folder }}",
"final_space": true,
"osc99": true,