diff --git a/docs/docs/configuration.md b/docs/docs/configuration.md index 272e054ebe25..417121b3b438 100644 --- a/docs/docs/configuration.md +++ b/docs/docs/configuration.md @@ -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 } ``` diff --git a/src/console_title.go b/src/console_title.go index da18836331ef..8e7a1e3b9362 100644 --- a/src/console_title.go +++ b/src/console_title.go @@ -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{} diff --git a/src/console_title_test.go b/src/console_title_test.go index 852580b72fbc..f9f159cb768f 100644 --- a/src/console_title_test.go +++ b/src/console_title_test.go @@ -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,70 @@ 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 { + 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("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 { @@ -49,6 +115,8 @@ 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("", fmt.Errorf("I have a bad feeling about this")) formats := &ansiFormats{} formats.init(tc.ShellName) ct := &consoleTitle{