Skip to content

Commit

Permalink
fix(session): support changing user
Browse files Browse the repository at this point in the history
resolves #4442
  • Loading branch information
JanDeDobbeleer committed Nov 19, 2023
1 parent 90dacce commit c7c0128
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 1 deletion.
14 changes: 13 additions & 1 deletion src/segments/session.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package segments
import (
"github.com/jandedobbeleer/oh-my-posh/src/platform"
"github.com/jandedobbeleer/oh-my-posh/src/properties"
"github.com/jandedobbeleer/oh-my-posh/src/regex"
)

type Session struct {
Expand Down Expand Up @@ -35,11 +36,22 @@ func (s *Session) activeSSHSession() bool {
"SSH_CONNECTION",
"SSH_CLIENT",
}

for _, key := range keys {
content := s.env.Getenv(key)
if content != "" {
return true
}
}
return false

if s.env.Platform() == platform.WINDOWS {
return false
}

whoAmI, err := s.env.RunCommand("who", "am", "i")
if err != nil {
return false
}

return regex.MatchString(`\(\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}\)`, whoAmI)
}
32 changes: 32 additions & 0 deletions src/segments/session_test.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
package segments

import (
"fmt"
"testing"

"github.com/jandedobbeleer/oh-my-posh/src/mock"
Expand All @@ -20,6 +21,8 @@ func TestSessionSegmentTemplate(t *testing.T) {
SSHSession bool
Root bool
Template string
WhoAmI string
Platform string
}{
{
Case: "user and computer",
Expand Down Expand Up @@ -87,6 +90,25 @@ func TestSessionSegmentTemplate(t *testing.T) {
Root: true,
Template: "{{if ne .Env.POSH_SESSION_DEFAULT_USER .UserName}}{{.UserName}}{{end}}",
},
{
Case: "user with ssh using who am i",
ExpectedString: "john on remote",
UserName: "john",
SSHSession: false,
WhoAmI: "sascha pts/1 2023-11-08 22:56 (89.246.1.1)",
ComputerName: "remote",
Template: "{{.UserName}}{{if .SSHSession}} on {{.HostName}}{{end}}",
},
{
Case: "user with ssh using who am i (windows)",
ExpectedString: "john",
UserName: "john",
SSHSession: false,
WhoAmI: "sascha pts/1 2023-11-08 22:56 (89.246.1.1)",
Platform: platform.WINDOWS,
ComputerName: "remote",
Template: "{{.UserName}}{{if .SSHSession}} on {{.HostName}}{{end}}",
},
}

for _, tc := range cases {
Expand All @@ -110,6 +132,16 @@ func TestSessionSegmentTemplate(t *testing.T) {
},
Root: tc.Root,
})

env.On("Platform").Return(tc.Platform)

var whoAmIErr error
if len(tc.WhoAmI) == 0 {
whoAmIErr = fmt.Errorf("who am i error")
}

env.On("RunCommand", "who", []string{"am", "i"}).Return(tc.WhoAmI, whoAmIErr)

session := &Session{
env: env,
props: properties.Map{},
Expand Down

0 comments on commit c7c0128

Please sign in to comment.