Skip to content

Commit

Permalink
feat: allow hiding of default user
Browse files Browse the repository at this point in the history
resolves #208
  • Loading branch information
JanDeDobbeleer committed Dec 2, 2020
1 parent 09255ae commit 5b275cc
Show file tree
Hide file tree
Showing 4 changed files with 32 additions and 3 deletions.
3 changes: 3 additions & 0 deletions docs/docs/segment-session.md
Expand Up @@ -30,5 +30,8 @@ to `\uF817 `
- host_color: `string` [color][colors] - override the foreground color of the host name
- display_user: `boolean` - display the user name or not - defaults to `true`
- display_host: `boolean` - display the host name or not - defaults to `true`
- default_user_name: `string` - name of the default user - defaults to empty
- display_default_user: `boolean` - display the segment or not when the user matches `default_user_name` - defaults
to `true`

[colors]: /docs/configure#colors
17 changes: 14 additions & 3 deletions segment_session.go
Expand Up @@ -6,8 +6,9 @@ import (
)

type session struct {
props *properties
env environmentInfo
props *properties
env environmentInfo
userName string
}

const (
Expand All @@ -23,9 +24,19 @@ const (
DisplayUser Property = "display_user"
// SSHIcon shows when in an SSH session
SSHIcon Property = "ssh_icon"
// DefaultUserName holds the default user of the platform
DefaultUserName Property = "default_user_name"
// DisplayDefaultUser hides or shows the user name when it's the user set in DefaultUserName
DisplayDefaultUser Property = "display_default_user"
)

func (s *session) enabled() bool {
s.userName = s.getUserName()
showDefaultUser := s.props.getBool(DisplayDefaultUser, true)
defaultUser := s.props.getString(DefaultUserName, "")
if !showDefaultUser && defaultUser == s.userName {
return false
}
return true
}

Expand All @@ -39,7 +50,7 @@ func (s *session) init(props *properties, env environmentInfo) {
}

func (s *session) getFormattedText() string {
username := s.getUserName()
username := s.userName
computername := s.getComputerName()
separator := ""
if s.props.getBool(DisplayHost, true) && s.props.getBool(DisplayUser, true) {
Expand Down
3 changes: 3 additions & 0 deletions segment_session_test.go
Expand Up @@ -40,6 +40,7 @@ func setupSession(args *sessionArgs) session {

func testUserInfoWriter(args *sessionArgs) string {
s := setupSession(args)
_ = s.enabled()
return s.getFormattedText()
}

Expand Down Expand Up @@ -77,6 +78,7 @@ func TestWriteOnlyUsername(t *testing.T) {
s := setupSession(args)
s.props.values[DisplayHost] = false
want := "<#fff>bill</><#fff></>"
assert.True(t, s.enabled())
got := s.getFormattedText()
assert.EqualValues(t, want, got)
}
Expand All @@ -91,6 +93,7 @@ func TestWriteOnlyHostname(t *testing.T) {
s := setupSession(args)
s.props.values[DisplayUser] = false
want := "<#fff></><#fff>surface</>"
assert.True(t, s.enabled())
got := s.getFormattedText()
assert.EqualValues(t, want, got)
}
Expand Down
12 changes: 12 additions & 0 deletions themes/schema.json
Expand Up @@ -952,6 +952,18 @@
"title": "Display Host",
"description": "Display the host name or not",
"default": true
},
"default_user_name": {
"type": "string",
"title": "Default User Name",
"description": "The name of the default user",
"default": ""
},
"display_default_user": {
"type": "boolean",
"title": "Display Default User",
"description": "Display the segment when default user or not",
"default": true
}
}
}
Expand Down

0 comments on commit 5b275cc

Please sign in to comment.