mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2025-03-05 20:49:04 -08:00
feat: set console title with template
This commit is contained in:
parent
b427a0e8a4
commit
9e7d901fb6
|
@ -55,6 +55,8 @@ boxes with question marks, [set up your terminal][setupterm] to use a supported
|
|||
- final_space: `boolean` - when true adds a space at the end of the prompt
|
||||
- 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"` - defaults
|
||||
to `{{ .Shell }} in {{ .Folder }}`
|
||||
|
||||
> "I Like The Way You Speak Words" - Gary Goodspeed
|
||||
|
||||
|
@ -62,6 +64,33 @@ boxes with question marks, [set up your terminal][setupterm] to use a supported
|
|||
|
||||
- `folder`: show the current folder name
|
||||
- `path`: show the current path
|
||||
- `template`: show the current path
|
||||
|
||||
### 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.
|
||||
Under the hood this uses go's text/template feature and offers a few standard properties to work with.
|
||||
|
||||
- `.Root`: `boolean` - is the current user root/admin or not
|
||||
- `.Path`: `string` - the current working directory
|
||||
- `.Folder`: `string` - the current working folder
|
||||
- `.Shell`: `string` - the current shell name
|
||||
|
||||
A `boolean` can be used for conditional display purposes, a `string` can be displayed.
|
||||
The following examples illustrate possible contents for `console_title_template`, provided
|
||||
the current working directory is `/usr/home/omp` and the shell is `zsh`.
|
||||
|
||||
```json
|
||||
{
|
||||
"console_title_template" = "{{.Folder}}{{if .Root}} :: root{{end}} :: {{.Shell}}",
|
||||
// outputs:
|
||||
// when root == false: omp :: zsh
|
||||
// when root == true: omp :: root :: zsh
|
||||
"console_title_template" = "{{.Folder}}", // outputs: omp
|
||||
"console_title_template" = "{{.Shell}} in {{.Path}}", // outputs: zsh in /usr/home/omp
|
||||
}
|
||||
```
|
||||
|
||||
## Block
|
||||
|
||||
|
|
|
@ -1,6 +1,10 @@
|
|||
package main
|
||||
|
||||
import "fmt"
|
||||
import (
|
||||
"bytes"
|
||||
"fmt"
|
||||
"text/template"
|
||||
)
|
||||
|
||||
type consoleTitle struct {
|
||||
env environmentInfo
|
||||
|
@ -16,13 +20,28 @@ const (
|
|||
FolderName ConsoleTitleStyle = "folder"
|
||||
// FullPath show the current path
|
||||
FullPath ConsoleTitleStyle = "path"
|
||||
// Template allows a more powerful custom string
|
||||
Template ConsoleTitleStyle = "template"
|
||||
// Errors to show when the template handling fails
|
||||
invalidTitleTemplate = "invalid template title text"
|
||||
incorrectTitleTemplate = "unable to create title based on template"
|
||||
)
|
||||
|
||||
// TitleTemplateContext defines what can be aded to the title when using the template
|
||||
type TitleTemplateContext struct {
|
||||
Root bool
|
||||
Path string
|
||||
Folder string
|
||||
Shell string
|
||||
}
|
||||
|
||||
func (t *consoleTitle) getConsoleTitle() string {
|
||||
var title string
|
||||
switch t.settings.ConsoleTitleStyle {
|
||||
case FullPath:
|
||||
title = t.env.getcwd()
|
||||
case Template:
|
||||
title = t.getTemplateText()
|
||||
case FolderName:
|
||||
fallthrough
|
||||
default:
|
||||
|
@ -30,3 +49,23 @@ func (t *consoleTitle) getConsoleTitle() string {
|
|||
}
|
||||
return fmt.Sprintf(t.formats.title, title)
|
||||
}
|
||||
|
||||
func (t *consoleTitle) getTemplateText() string {
|
||||
context := &TitleTemplateContext{
|
||||
Root: t.env.isRunningAsRoot(),
|
||||
Path: t.env.getcwd(),
|
||||
Folder: base(t.env.getcwd(), t.env),
|
||||
Shell: t.env.getShellName(),
|
||||
}
|
||||
tmpl, err := template.New("title").Parse(t.settings.ConsoleTitleTemplate)
|
||||
if err != nil {
|
||||
return invalidTitleTemplate
|
||||
}
|
||||
buffer := new(bytes.Buffer)
|
||||
defer buffer.Reset()
|
||||
err = tmpl.Execute(buffer, context)
|
||||
if err != nil {
|
||||
return incorrectTitleTemplate
|
||||
}
|
||||
return buffer.String()
|
||||
}
|
||||
|
|
|
@ -9,6 +9,8 @@ import (
|
|||
func TestGetConsoleTitle(t *testing.T) {
|
||||
cases := []struct {
|
||||
Style ConsoleTitleStyle
|
||||
Template string
|
||||
Root bool
|
||||
Cwd string
|
||||
PathSeperator string
|
||||
ShellName string
|
||||
|
@ -16,16 +18,36 @@ func TestGetConsoleTitle(t *testing.T) {
|
|||
}{
|
||||
{Style: FolderName, Cwd: "/usr/home", PathSeperator: "/", ShellName: "default", Expected: "\x1b]0;home\a"},
|
||||
{Style: FullPath, Cwd: "/usr/home/jan", PathSeperator: "/", ShellName: "default", Expected: "\x1b]0;/usr/home/jan\a"},
|
||||
{
|
||||
Style: Template,
|
||||
Template: "{{.Path}}{{if .Root}} :: Admin{{end}} :: {{.Shell}}",
|
||||
Cwd: "C:\\vagrant",
|
||||
PathSeperator: "\\",
|
||||
ShellName: "PowerShell",
|
||||
Root: true,
|
||||
Expected: "\x1b]0;C:\\vagrant :: Admin :: PowerShell\a",
|
||||
},
|
||||
{
|
||||
Style: Template,
|
||||
Template: "{{.Folder}}{{if .Root}} :: Admin{{end}} :: {{.Shell}}",
|
||||
Cwd: "C:\\vagrant",
|
||||
PathSeperator: "\\",
|
||||
ShellName: "PowerShell",
|
||||
Expected: "\x1b]0;vagrant :: PowerShell\a",
|
||||
},
|
||||
}
|
||||
|
||||
for _, tc := range cases {
|
||||
settings := &Settings{
|
||||
ConsoleTitleStyle: tc.Style,
|
||||
ConsoleTitleStyle: tc.Style,
|
||||
ConsoleTitleTemplate: tc.Template,
|
||||
}
|
||||
env := new(MockedEnvironment)
|
||||
env.On("getcwd", nil).Return(tc.Cwd)
|
||||
env.On("homeDir", nil).Return("/usr/home")
|
||||
env.On("getPathSeperator", nil).Return(tc.PathSeperator)
|
||||
env.On("isRunningAsRoot", nil).Return(tc.Root)
|
||||
env.On("getShellName", nil).Return(tc.ShellName)
|
||||
formats := &ansiFormats{}
|
||||
formats.init(tc.ShellName)
|
||||
ct := &consoleTitle{
|
||||
|
|
|
@ -10,10 +10,11 @@ import (
|
|||
|
||||
// Settings holds all the theme for rendering the prompt
|
||||
type Settings struct {
|
||||
FinalSpace bool `json:"final_space"`
|
||||
ConsoleTitle bool `json:"console_title"`
|
||||
ConsoleTitleStyle ConsoleTitleStyle `json:"console_title_style"`
|
||||
Blocks []*Block `json:"blocks"`
|
||||
FinalSpace bool `json:"final_space"`
|
||||
ConsoleTitle bool `json:"console_title"`
|
||||
ConsoleTitleStyle ConsoleTitleStyle `json:"console_title_style"`
|
||||
ConsoleTitleTemplate string `json:"console_title_template"`
|
||||
Blocks []*Block `json:"blocks"`
|
||||
}
|
||||
|
||||
// BlockType type of block
|
||||
|
|
|
@ -1209,23 +1209,29 @@
|
|||
"properties": {
|
||||
"final_space": {
|
||||
"type": "boolean",
|
||||
"title": "The final_space schema",
|
||||
"description": "An explanation about the purpose of this instance.",
|
||||
"title": "Final Space",
|
||||
"description": "https://ohmyposh.dev/docs/configure#general-settings",
|
||||
"default": true
|
||||
},
|
||||
"console_title": {
|
||||
"type": "boolean",
|
||||
"title": "The console_title schema",
|
||||
"description": "An explanation about the purpose of this instance.",
|
||||
"title": "Console Title",
|
||||
"description": "https://ohmyposh.dev/docs/configure#general-settings",
|
||||
"default": true
|
||||
},
|
||||
"console_title_style": {
|
||||
"type": "string",
|
||||
"title": "The console_title_style schema",
|
||||
"description": "An explanation about the purpose of this instance.",
|
||||
"enum": ["folder", "path"],
|
||||
"title": "Console Title Style",
|
||||
"description": "https://ohmyposh.dev/docs/configure#console-title-style",
|
||||
"enum": ["folder", "path", "template"],
|
||||
"default": "folder"
|
||||
},
|
||||
"console_title_template": {
|
||||
"type": "string",
|
||||
"title": "Console Title Template",
|
||||
"description": "https://ohmyposh.dev/docs/configure#console-title-template",
|
||||
"default": "{{ .Shell }} in {{ .Folder }}"
|
||||
},
|
||||
"blocks": {
|
||||
"type": "array",
|
||||
"title": "Block array",
|
||||
|
|
Loading…
Reference in a new issue