-
-
Notifications
You must be signed in to change notification settings - Fork 2.2k
/
console_title.go
79 lines (68 loc) · 1.81 KB
/
console_title.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
package main
import (
"fmt"
"strings"
)
type consoleTitle struct {
env environmentInfo
config *Config
formats *ansiFormats
}
// ConsoleTitleStyle defines how to show the title in the console window
type ConsoleTitleStyle string
const (
// FolderName show the current folder name
FolderName ConsoleTitleStyle = "folder"
// FullPath show the current path
FullPath ConsoleTitleStyle = "path"
// Template allows a more powerful custom string
Template ConsoleTitleStyle = "template"
templateEnvRegex = `\.Env\.(?P<ENV>[^ \.}]*)`
)
func (t *consoleTitle) getConsoleTitle() string {
var title string
switch t.config.ConsoleTitleStyle {
case FullPath:
title = t.getPwd()
case Template:
title = t.getTemplateText()
case FolderName:
fallthrough
default:
title = base(t.getPwd(), t.env)
}
return fmt.Sprintf(t.formats.title, title)
}
func (t *consoleTitle) getTemplateText() string {
context := make(map[string]interface{})
context["Root"] = t.env.isRunningAsRoot()
context["Path"] = t.getPwd()
context["Folder"] = base(t.getPwd(), t.env)
context["Shell"] = t.env.getShellName()
context["User"] = t.env.getCurrentUser()
context["Host"] = ""
if host, err := t.env.getHostName(); err == nil {
context["Host"] = host
}
// load environment variables into the map
envVars := map[string]string{}
matches := findAllNamedRegexMatch(templateEnvRegex, t.config.ConsoleTitleTemplate)
for _, match := range matches {
envVars[match["ENV"]] = t.env.getenv(match["ENV"])
}
context["Env"] = envVars
template := &textTemplate{
Template: t.config.ConsoleTitleTemplate,
Context: context,
}
text, err := template.render()
if err != nil {
return err.Error()
}
return text
}
func (t *consoleTitle) getPwd() string {
pwd := t.env.getcwd()
pwd = strings.Replace(pwd, t.env.homeDir(), "~", 1)
return pwd
}