mirror of
https://github.com/JanDeDobbeleer/oh-my-posh.git
synced 2025-03-05 20:49:04 -08:00
refactor: session segment template properties
This commit is contained in:
parent
9aa813b7af
commit
0d87942fe3
|
@ -34,4 +34,10 @@ to `\uF817 `
|
||||||
- display_default_user: `boolean` - display the segment or not when the user matches `default_user_name` - defaults
|
- display_default_user: `boolean` - display the segment or not when the user matches `default_user_name` - defaults
|
||||||
to `true`
|
to `true`
|
||||||
|
|
||||||
|
## Template Properties
|
||||||
|
|
||||||
|
- `.UserName`: `string` - the current user's name
|
||||||
|
- `.ComputerName`: `string` - the current computer's name
|
||||||
|
- `.SSHSession`: `boolean` - active SSH session or not
|
||||||
|
|
||||||
[colors]: /docs/configure#colors
|
[colors]: /docs/configure#colors
|
||||||
|
|
|
@ -6,9 +6,11 @@ import (
|
||||||
)
|
)
|
||||||
|
|
||||||
type session struct {
|
type session struct {
|
||||||
props *properties
|
props *properties
|
||||||
env environmentInfo
|
env environmentInfo
|
||||||
userName string
|
UserName string
|
||||||
|
ComputerName string
|
||||||
|
SSHSession bool
|
||||||
}
|
}
|
||||||
|
|
||||||
const (
|
const (
|
||||||
|
@ -31,10 +33,10 @@ const (
|
||||||
)
|
)
|
||||||
|
|
||||||
func (s *session) enabled() bool {
|
func (s *session) enabled() bool {
|
||||||
s.userName = s.getUserName()
|
s.UserName = s.getUserName()
|
||||||
showDefaultUser := s.props.getBool(DisplayDefaultUser, true)
|
showDefaultUser := s.props.getBool(DisplayDefaultUser, true)
|
||||||
defaultUser := s.props.getString(DefaultUserName, "")
|
defaultUser := s.props.getString(DefaultUserName, "")
|
||||||
if !showDefaultUser && defaultUser == s.userName {
|
if !showDefaultUser && defaultUser == s.UserName {
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
return true
|
return true
|
||||||
|
@ -50,19 +52,18 @@ func (s *session) init(props *properties, env environmentInfo) {
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *session) getFormattedText() string {
|
func (s *session) getFormattedText() string {
|
||||||
username := s.userName
|
s.ComputerName = s.getComputerName()
|
||||||
computername := s.getComputerName()
|
|
||||||
separator := ""
|
separator := ""
|
||||||
if s.props.getBool(DisplayHost, true) && s.props.getBool(DisplayUser, true) {
|
if s.props.getBool(DisplayHost, true) && s.props.getBool(DisplayUser, true) {
|
||||||
separator = s.props.getString(UserInfoSeparator, "@")
|
separator = s.props.getString(UserInfoSeparator, "@")
|
||||||
}
|
}
|
||||||
var ssh string
|
var sshIcon string
|
||||||
if s.activeSSHSession() {
|
if s.activeSSHSession() {
|
||||||
ssh = s.props.getString(SSHIcon, "\uF817 ")
|
sshIcon = s.props.getString(SSHIcon, "\uF817 ")
|
||||||
}
|
}
|
||||||
userColor := s.props.getColor(UserColor, s.props.foreground)
|
userColor := s.props.getColor(UserColor, s.props.foreground)
|
||||||
hostColor := s.props.getColor(HostColor, 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)
|
return fmt.Sprintf("%s<%s>%s</>%s<%s>%s</>", sshIcon, userColor, s.UserName, separator, hostColor, s.ComputerName)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *session) getComputerName() string {
|
func (s *session) getComputerName() string {
|
||||||
|
@ -96,6 +97,7 @@ func (s *session) activeSSHSession() bool {
|
||||||
for _, key := range keys {
|
for _, key := range keys {
|
||||||
content := s.env.getenv(key)
|
content := s.env.getenv(key)
|
||||||
if content != "" {
|
if content != "" {
|
||||||
|
s.SSHSession = true
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
Loading…
Reference in a new issue