oh-my-posh/segment_session.go

93 lines
2.2 KiB
Go
Raw Normal View History

2019-03-13 04:14:30 -07:00
package main
import (
"fmt"
"strings"
)
type session struct {
props *properties
env environmentInfo
}
const (
// UserInfoSeparator is put between the user and computer name
2019-03-13 04:14:30 -07:00
UserInfoSeparator Property = "user_info_separator"
// UserColor if set, is used to color the user name
2019-03-13 04:14:30 -07:00
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
2019-03-13 04:14:30 -07:00
DisplayUser Property = "display_user"
// SSHIcon shows when in an SSH session
SSHIcon Property = "ssh_icon"
2019-03-13 04:14:30 -07:00
)
func (s *session) enabled() bool {
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 {
username := s.getUserName()
computername := s.getComputerName()
separator := ""
if s.props.getBool(DisplayHost, true) && s.props.getBool(DisplayUser, true) {
separator = s.props.getString(UserInfoSeparator, "@")
}
var ssh string
if s.activeSSHSession() {
ssh = s.props.getString(SSHIcon, "\uF817 ")
}
userColor := s.props.getColor(UserColor, s.props.foreground)
hostColor := s.props.getColor(HostColor, s.props.foreground)
return fmt.Sprintf("%s<%s>%s</>%s<%s>%s</>", ssh, userColor, username, separator, hostColor, computername)
2019-03-13 04:14:30 -07:00
}
func (s *session) getComputerName() string {
if !s.props.getBool(DisplayHost, true) {
2019-03-13 04:14:30 -07:00
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)
2019-03-13 04:14:30 -07:00
if s.env.getRuntimeGOOS() == "windows" && strings.Contains(username, "\\") {
username = strings.Split(username, "\\")[1]
}
return username
}
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
}