forked from justjanne/powerline-go
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathsegment-hostname.go
49 lines (42 loc) · 996 Bytes
/
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
package main
import (
"crypto/md5"
"fmt"
"os"
"strings"
)
func getHostName() string {
fullyQualifiedDomainName, _ := os.Hostname()
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) {
var hostPrompt string
var foreground, background uint8
if *p.args.ColorizeHostname {
hostName := getHostName()
hostPrompt = fmt.Sprintf(" %s ", 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 = fmt.Sprintf(" %s ", getHostName())
}
foreground = p.theme.HostnameFg
background = p.theme.HostnameBg
}
p.appendSegment("host", segment{
content: hostPrompt,
foreground: foreground,
background: background,
})
}