Skip to content

Commit f86dcf2

Browse files
feat: ssh session indicator
resolves #48
1 parent ed4fd89 commit f86dcf2

File tree

3 files changed

+123
-17
lines changed

3 files changed

+123
-17
lines changed

docs/docs/segment-session.md

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -24,6 +24,8 @@ Show the current user and host name.
2424
## Properties
2525

2626
- user_info_separator: `string` - text/icon to put in between the user and host name - defaults to `@`
27+
- ssh_icon: `string` - text/icon to display first when in an active SSH session - defaults
28+
to `\uF817 `
2729
- user_color: `string` [hex color code][colors] - override the foreground color of the user name
2830
- host_color: `string` [hex color code][colors] - override the foreground color of the host name
2931
- display_user: `boolean` - display the user name or not - defaults to `true`

segment_session.go

Lines changed: 21 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@ const (
2121
DisplayHost Property = "display_host"
2222
//DisplayUser hides or shows the user name
2323
DisplayUser Property = "display_user"
24+
//SSHIcon shows when in an SSH session
25+
SSHIcon Property = "ssh_icon"
2426
)
2527

2628
func (s *session) enabled() bool {
@@ -43,7 +45,11 @@ func (s *session) getFormattedText() string {
4345
if s.props.getBool(DisplayHost, true) && s.props.getBool(DisplayUser, true) {
4446
separator = s.props.getString(UserInfoSeparator, "@")
4547
}
46-
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)
48+
var ssh string
49+
if s.activeSSHSession() {
50+
ssh = s.props.getString(SSHIcon, "\uF817 ")
51+
}
52+
return fmt.Sprintf("%s<%s>%s</>%s<%s>%s</>", ssh, s.props.getColor(UserColor, s.props.foreground), username, separator, s.props.getColor(HostColor, s.props.foreground), computername)
4753
}
4854

4955
func (s *session) getComputerName() string {
@@ -68,3 +74,17 @@ func (s *session) getUserName() string {
6874
}
6975
return username
7076
}
77+
78+
func (s *session) activeSSHSession() bool {
79+
keys := []string{
80+
"SSH_CONNECTION",
81+
"SSH_CLIENT",
82+
}
83+
for _, key := range keys {
84+
content := s.env.getenv(key)
85+
if content != "" {
86+
return true
87+
}
88+
}
89+
return false
90+
}

segment_session_test.go

Lines changed: 100 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -6,13 +6,28 @@ import (
66
"github.com/stretchr/testify/assert"
77
)
88

9-
func setupSession(userInfoSeparator string, username string, hostname string, goos string) session {
9+
type sessionArgs struct {
10+
userInfoSeparator string
11+
username string
12+
hostname string
13+
goos string
14+
connection string
15+
client string
16+
sshIcon string
17+
}
18+
19+
func setupSession(args *sessionArgs) session {
1020
env := new(MockedEnvironment)
11-
env.On("getCurrentUser", nil).Return(username)
12-
env.On("getHostName", nil).Return(hostname, nil)
13-
env.On("getRuntimeGOOS", nil).Return(goos)
21+
env.On("getCurrentUser", nil).Return(args.username)
22+
env.On("getHostName", nil).Return(args.hostname, nil)
23+
env.On("getRuntimeGOOS", nil).Return(args.goos)
24+
env.On("getenv", "SSH_CONNECTION").Return(args.connection)
25+
env.On("getenv", "SSH_CLIENT").Return(args.client)
1426
props := &properties{
15-
values: map[Property]interface{}{UserInfoSeparator: userInfoSeparator},
27+
values: map[Property]interface{}{
28+
UserInfoSeparator: args.userInfoSeparator,
29+
SSHIcon: args.sshIcon,
30+
},
1631
foreground: "#fff",
1732
background: "#000",
1833
}
@@ -23,44 +38,113 @@ func setupSession(userInfoSeparator string, username string, hostname string, go
2338
return s
2439
}
2540

26-
func testUserInfoWriter(userInfoSeparator string, username string, hostname string, goos string) string {
27-
s := setupSession(userInfoSeparator, username, hostname, goos)
41+
func testUserInfoWriter(args *sessionArgs) string {
42+
s := setupSession(args)
2843
return s.getFormattedText()
2944
}
3045

3146
func TestWriteUserInfo(t *testing.T) {
3247
want := "<#fff>bill</>@<#fff>surface</>"
33-
got := testUserInfoWriter("@", "bill", "surface", "windows")
48+
args := &sessionArgs{
49+
userInfoSeparator: "@",
50+
username: "bill",
51+
hostname: "surface",
52+
goos: "windows",
53+
}
54+
got := testUserInfoWriter(args)
3455
assert.EqualValues(t, want, got)
3556
}
3657

3758
func TestWriteUserInfoWindowsIncludingHostname(t *testing.T) {
3859
want := "<#fff>bill</>@<#fff>surface</>"
39-
got := testUserInfoWriter("@", "surface\\bill", "surface", "windows")
60+
args := &sessionArgs{
61+
userInfoSeparator: "@",
62+
username: "surface\\bill",
63+
hostname: "surface",
64+
goos: "windows",
65+
}
66+
got := testUserInfoWriter(args)
4067
assert.EqualValues(t, want, got)
4168
}
4269

4370
func TestWriteOnlyUsername(t *testing.T) {
44-
s := setupSession("@", "surface\\bill", "surface", "windows")
71+
args := &sessionArgs{
72+
userInfoSeparator: "@",
73+
username: "surface\\bill",
74+
hostname: "surface",
75+
goos: "windows",
76+
}
77+
s := setupSession(args)
4578
s.props.values[DisplayHost] = false
46-
4779
want := "<#fff>bill</><#fff></>"
4880
got := s.getFormattedText()
4981
assert.EqualValues(t, want, got)
5082
}
5183

5284
func TestWriteOnlyHostname(t *testing.T) {
53-
s := setupSession("@", "surface\\bill", "surface", "windows")
85+
args := &sessionArgs{
86+
userInfoSeparator: "@",
87+
username: "surface\\bill",
88+
hostname: "surface",
89+
goos: "windows",
90+
}
91+
s := setupSession(args)
5492
s.props.values[DisplayUser] = false
55-
5693
want := "<#fff></><#fff>surface</>"
5794
got := s.getFormattedText()
5895
assert.EqualValues(t, want, got)
5996
}
6097

61-
func TestSession(t *testing.T) {
98+
func TestWriteActiveSSHSession(t *testing.T) {
99+
want := "ssh <#fff>bill</>@<#fff>surface</>"
100+
args := &sessionArgs{
101+
userInfoSeparator: "@",
102+
username: "bill",
103+
hostname: "surface",
104+
goos: "windows",
105+
sshIcon: "ssh ",
106+
connection: "1.1.1.1",
107+
}
108+
got := testUserInfoWriter(args)
109+
assert.EqualValues(t, want, got)
110+
}
111+
112+
func TestActiveSSHSessionInactive(t *testing.T) {
113+
env := new(MockedEnvironment)
114+
env.On("getenv", "SSH_CONNECTION").Return("")
115+
env.On("getenv", "SSH_CLIENT").Return("")
116+
s := &session{
117+
env: env,
118+
}
119+
assert.False(t, s.activeSSHSession())
120+
}
121+
122+
func TestActiveSSHSessionActiveConnection(t *testing.T) {
123+
env := new(MockedEnvironment)
124+
env.On("getenv", "SSH_CONNECTION").Return("1.1.1.1")
125+
env.On("getenv", "SSH_CLIENT").Return("")
126+
s := &session{
127+
env: env,
128+
}
129+
assert.True(t, s.activeSSHSession())
130+
}
131+
132+
func TestActiveSSHSessionActiveClient(t *testing.T) {
133+
env := new(MockedEnvironment)
134+
env.On("getenv", "SSH_CONNECTION").Return("")
135+
env.On("getenv", "SSH_CLIENT").Return("1.1.1.1")
136+
s := &session{
137+
env: env,
138+
}
139+
assert.True(t, s.activeSSHSession())
140+
}
141+
142+
func TestActiveSSHSessionActiveBoth(t *testing.T) {
143+
env := new(MockedEnvironment)
144+
env.On("getenv", "SSH_CONNECTION").Return("2.2.2.2")
145+
env.On("getenv", "SSH_CLIENT").Return("1.1.1.1")
62146
s := &session{
63-
env: &environment{},
147+
env: env,
64148
}
65-
assert.NotEmpty(t, s.getUserName())
149+
assert.True(t, s.activeSSHSession())
66150
}

0 commit comments

Comments
 (0)