Skip to content

Commit

Permalink
feat: add the User and Host properties to the console title template
Browse files Browse the repository at this point in the history
  • Loading branch information
beppler authored and JanDeDobbeleer committed Feb 6, 2021
1 parent 55fb043 commit 3729dee
Show file tree
Hide file tree
Showing 3 changed files with 76 additions and 0 deletions.
3 changes: 3 additions & 0 deletions docs/docs/configuration.md
Expand Up @@ -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.
Expand All @@ -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
}
```
Expand Down
5 changes: 5 additions & 0 deletions src/console_title.go
Expand Up @@ -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{}
Expand Down
68 changes: 68 additions & 0 deletions src/console_title_test.go
@@ -1,6 +1,7 @@
package main

import (
"fmt"
"testing"

"github.com/stretchr/testify/assert"
Expand All @@ -11,6 +12,7 @@ func TestGetConsoleTitle(t *testing.T) {
Style ConsoleTitleStyle
Template string
Root bool
User string
Cwd string
PathSeperator string
ShellName string
Expand All @@ -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 {
Expand All @@ -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{
Expand Down

0 comments on commit 3729dee

Please sign in to comment.