forked from JanDeDobbeleer/oh-my-posh
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsegment_session.go
143 lines (131 loc) · 3.67 KB
/
segment_session.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
package main
import (
"fmt"
"strings"
)
type session struct {
props *properties
env environmentInfo
UserName string
DefaultUserName string
ComputerName string
SSHSession bool
Root bool
templateText string
}
const (
// UserInfoSeparator is put between the user and computer name
UserInfoSeparator Property = "user_info_separator"
// UserColor if set, is used to color the user name
UserColor Property = "user_color"
// HostColor if set, is used to color the computer name
HostColor Property = "host_color"
// DisplayHost hides or show the computer name
DisplayHost Property = "display_host"
// DisplayUser hides or shows the user name
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"
defaultUserEnvVar = "POSH_SESSION_DEFAULT_USER"
)
func (s *session) enabled() bool {
s.UserName = s.getUserName()
s.ComputerName = s.getComputerName()
s.SSHSession = s.activeSSHSession()
s.DefaultUserName = s.getDefaultUser()
segmentTemplate := s.props.getString(SegmentTemplate, "")
if segmentTemplate != "" {
s.Root = s.env.isRunningAsRoot()
template := &textTemplate{
Template: segmentTemplate,
Context: s,
Env: s.env,
}
var err error
s.templateText, err = template.render()
if err != nil {
s.templateText = err.Error()
}
return len(s.templateText) > 0
}
showDefaultUser := s.props.getBool(DisplayDefault, true)
if !showDefaultUser && s.DefaultUserName == s.UserName {
return false
}
return true
}
func (s *session) string() string {
return s.getFormattedText()
}
func (s *session) init(props *properties, env environmentInfo) {
s.props = props
s.env = env
}
func (s *session) getFormattedText() string {
if len(s.templateText) > 0 {
return s.templateText
}
separator := ""
if s.props.getBool(DisplayHost, true) && s.props.getBool(DisplayUser, true) {
separator = s.props.getString(UserInfoSeparator, "@")
}
var sshIcon string
if s.SSHSession {
sshIcon = s.props.getString(SSHIcon, "\uF817 ")
}
userColor := s.props.getColor(UserColor, s.props.foreground)
hostColor := s.props.getColor(HostColor, s.props.foreground)
if len(userColor) > 0 && len(hostColor) > 0 {
return fmt.Sprintf("%s<%s>%s</>%s<%s>%s</>", sshIcon, userColor, s.UserName, separator, hostColor, s.ComputerName)
}
if len(userColor) > 0 {
return fmt.Sprintf("%s<%s>%s</>%s%s", sshIcon, userColor, s.UserName, separator, s.ComputerName)
}
if len(hostColor) > 0 {
return fmt.Sprintf("%s%s%s<%s>%s</>", sshIcon, s.UserName, separator, hostColor, s.ComputerName)
}
return fmt.Sprintf("%s%s%s%s", sshIcon, s.UserName, separator, s.ComputerName)
}
func (s *session) getComputerName() string {
if !s.props.getBool(DisplayHost, true) {
return ""
}
computername, err := s.env.getHostName()
if err != nil {
computername = "unknown"
}
return strings.TrimSpace(computername)
}
func (s *session) getUserName() string {
if !s.props.getBool(DisplayUser, true) {
return ""
}
user := s.env.getCurrentUser()
username := strings.TrimSpace(user)
if s.env.getRuntimeGOOS() == "windows" && strings.Contains(username, "\\") {
username = strings.Split(username, "\\")[1]
}
return username
}
func (s *session) getDefaultUser() string {
user := s.env.getenv(defaultUserEnvVar)
if len(user) == 0 {
user = s.props.getString(DefaultUserName, "")
}
return user
}
func (s *session) activeSSHSession() bool {
keys := []string{
"SSH_CONNECTION",
"SSH_CLIENT",
}
for _, key := range keys {
content := s.env.getenv(key)
if content != "" {
return true
}
}
return false
}