feat: session segment updates

only display UserInfoSeparator if both username and hostname are dislayed
cleanHostName only if it ends with garbage instead of contains.
  fixes .localdomain having just .local stripped
add .localdomain to garbage
This commit is contained in:
nwykes 2020-10-07 09:14:26 -06:00 committed by Jan De Dobbeleer
parent 529fc75e9b
commit dabc9d4f20
3 changed files with 33 additions and 3 deletions

View file

@ -138,9 +138,12 @@ func cleanHostName(hostName string) string {
garbage := []string{
".lan",
".local",
".localdomain",
}
for _, g := range garbage {
hostName = strings.Replace(hostName, g, "", 1)
if strings.HasSuffix(hostName, g) {
hostName = strings.Replace(hostName, g, "", 1)
}
}
return hostName
}

View file

@ -39,7 +39,11 @@ func (s *session) init(props *properties, env environmentInfo) {
func (s *session) getFormattedText() string {
username := s.getUserName()
computername := s.getComputerName()
return fmt.Sprintf("<%s>%s</>%s<%s>%s</>", s.props.getColor(UserColor, s.props.foreground), username, s.props.getString(UserInfoSeparator, "@"), s.props.getColor(HostColor, s.props.foreground), computername)
separator := ""
if s.props.getBool(DisplayHost, true) && s.props.getBool(DisplayUser, true) {
separator = s.props.getString(UserInfoSeparator, "@")
}
return fmt.Sprintf("<%s>%s</>%s<%s>%s</>", s.props.getColor(UserColor, s.props.foreground), username, separator, s.props.getColor(HostColor, s.props.foreground), computername)
}
func (s *session) getComputerName() string {

View file

@ -7,7 +7,7 @@ import (
"github.com/stretchr/testify/assert"
)
func testUserInfoWriter(userInfoSeparator string, username string, hostname string, goos string) string {
func setupSession(userInfoSeparator string, username string, hostname string, goos string) session {
env := new(MockedEnvironment)
user := user.User{
Username: username,
@ -24,6 +24,11 @@ func testUserInfoWriter(userInfoSeparator string, username string, hostname stri
env: env,
props: props,
}
return s
}
func testUserInfoWriter(userInfoSeparator string, username string, hostname string, goos string) string {
s := setupSession(userInfoSeparator, username, hostname, goos)
return s.getFormattedText()
}
@ -38,3 +43,21 @@ func TestWriteUserInfoWindowsIncludingHostname(t *testing.T) {
got := testUserInfoWriter("@", "surface\\bill", "surface", "windows")
assert.EqualValues(t, want, got)
}
func TestWriteOnlyUsername(t *testing.T) {
s := setupSession("@", "surface\\bill", "surface", "windows")
s.props.values[DisplayHost] = false
want := "<#fff>bill</><#fff></>"
got := s.getFormattedText()
assert.EqualValues(t, want, got)
}
func TestWriteOnlyHostname(t *testing.T) {
s := setupSession("@", "surface\\bill", "surface", "windows")
s.props.values[DisplayUser] = false
want := "<#fff></><#fff>surface</>"
got := s.getFormattedText()
assert.EqualValues(t, want, got)
}