feat: add the User and Host properties to the console title template

This commit is contained in:
Carlos Alberto Costa Beppler 2021-02-05 22:30:09 -03:00 committed by Jan De Dobbeleer
parent 55fb04335e
commit 3729dee16e
3 changed files with 76 additions and 0 deletions

View file

@ -76,6 +76,8 @@ properties to work with.
- `.Path`: `string` - the current working directory
- `.Folder`: `string` - the current working folder
- `.Shell`: `string` - the current shell name
- `.User`: `string` - the current user name
- `.Host`: `string` - the host name
- `.Env.VarName`: `string` - Any environment variable where `VarName` is the environment variable name
A `boolean` can be used for conditional display purposes, a `string` can be displayed.
@ -91,6 +93,7 @@ the current working directory is `/usr/home/omp` and the shell is `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
"console_title_template": "{{.User}}@{{.Host}} {{.Shell}} in {{.Path}}", // outputs: MyUser@MyMachine zsh in /usr/home/omp
"console_title_template": "{{.Env.USERDOMAIN}} {{.Shell}} in {{.Path}}", // outputs: MyCompany zsh in /usr/home/omp
}
```

View file

@ -50,6 +50,11 @@ func (t *consoleTitle) getTemplateText() string {
context["Path"] = t.getPwd()
context["Folder"] = base(t.getPwd(), t.env)
context["Shell"] = t.env.getShellName()
context["User"] = t.env.getCurrentUser()
context["Host"] = ""
if host, err := t.env.getHostName(); err == nil {
context["Host"] = host
}
// load environment variables into the map
envVars := map[string]string{}

View file

@ -1,6 +1,7 @@
package main
import (
"fmt"
"testing"
"github.com/stretchr/testify/assert"
@ -11,6 +12,7 @@ func TestGetConsoleTitle(t *testing.T) {
Style ConsoleTitleStyle
Template string
Root bool
User string
Cwd string
PathSeperator string
ShellName string
@ -35,6 +37,15 @@ func TestGetConsoleTitle(t *testing.T) {
ShellName: "PowerShell",
Expected: "\x1b]0;vagrant :: PowerShell\a",
},
{
Style: Template,
Template: "{{.User}}@{{.Host}}{{if .Root}} :: Admin{{end}} :: {{.Shell}}",
Root: true,
User: "MyUser",
PathSeperator: "\\",
ShellName: "PowerShell",
Expected: "\x1b]0;MyUser@MyHost :: Admin :: PowerShell\a",
},
}
for _, tc := range cases {
@ -49,6 +60,63 @@ func TestGetConsoleTitle(t *testing.T) {
env.On("isRunningAsRoot", nil).Return(tc.Root)
env.On("getShellName", nil).Return(tc.ShellName)
env.On("getenv", "USERDOMAIN").Return("MyCompany")
env.On("getCurrentUser", nil).Return("MyUser")
env.On("getHostName", nil).Return("MyHost", nil)
formats := &ansiFormats{}
formats.init(tc.ShellName)
ct := &consoleTitle{
env: env,
settings: settings,
formats: formats,
}
got := ct.getConsoleTitle()
assert.Equal(t, tc.Expected, got)
}
}
func TestGetConsoleTitleIfGethostnameReturnsError(t *testing.T) {
cases := []struct {
Style ConsoleTitleStyle
Template string
Root bool
User string
Cwd string
PathSeperator string
ShellName string
Expected string
}{
{
Style: Template,
Template: "Not using Host only {{.User}} and {{.Shell}}",
User: "MyUser",
PathSeperator: "\\",
ShellName: "PowerShell",
Expected: "\x1b]0;Not using Host only MyUser and PowerShell\a",
},
{
Style: Template,
Template: "{{.User}}@{{.Host}} :: {{.Shell}}",
User: "MyUser",
PathSeperator: "\\",
ShellName: "PowerShell",
Expected: "\x1b]0;MyUser@ :: PowerShell\a",
},
}
for _, tc := range cases {
settings := &Settings{
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)
env.On("getenv", "USERDOMAIN").Return("MyCompany")
env.On("getCurrentUser", nil).Return("MyUser")
env.On("getHostName", nil).Return("", fmt.Errorf("I have a bad feeling about this"))
formats := &ansiFormats{}
formats.init(tc.ShellName)
ct := &consoleTitle{