forked from justjanne/powerline-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
segment-hostname.go
65 lines (56 loc) · 1.48 KB
/
segment-hostname.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
package main
import (
"crypto/md5"
pwl "github.com/justjanne/powerline-go/powerline"
"os"
"strconv"
"strings"
)
func getHostName(fullyQualifiedDomainName string) string {
return strings.SplitN(fullyQualifiedDomainName, ".", 2)[0]
}
func getMd5(text string) []byte {
hasher := md5.New()
hasher.Write([]byte(text))
return hasher.Sum(nil)
}
func segmentHost(p *powerline) []pwl.Segment {
var hostPrompt string
var foreground, background uint8
if *p.args.HostnameOnlyIfSSH {
if os.Getenv("SSH_CLIENT") == "" {
// It's not an ssh connection do nothing
return []pwl.Segment{}
}
}
if *p.args.ColorizeHostname {
foregroundEnv, foregroundEnvErr := strconv.ParseInt(os.Getenv("PLGO_HOSTNAMEFG"), 0, 8)
backgroundEnv, backgroundEnvErr := strconv.ParseInt(os.Getenv("PLGO_HOSTNAMEBG"), 0, 8)
if foregroundEnvErr == nil && backgroundEnvErr == nil {
foreground = uint8(foregroundEnv)
background = uint8(backgroundEnv)
} else {
hostName := getHostName(p.hostname)
hostPrompt = hostName
hash := getMd5(hostName)
background = hash[0]
foreground = p.theme.HostnameColorizedFgMap[background]
}
} else {
if *p.args.Shell == "bash" {
hostPrompt = "\\h"
} else if *p.args.Shell == "zsh" {
hostPrompt = "%m"
} else {
hostPrompt = getHostName(p.hostname)
}
foreground = p.theme.HostnameFg
background = p.theme.HostnameBg
}
return []pwl.Segment{{
Name: "host",
Content: hostPrompt,
Foreground: foreground,
Background: background,
}}
}