-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprompt.go
126 lines (126 loc) · 2.99 KB
/
prompt.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
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
package main
import (
"fmt"
"runtime"
"github.com/spf13/viper"
"path/filepath"
"strings"
"os"
"github.com/gookit/color"
)
func main() {
//configure before anything
viper.AutomaticEnv()
viper.SetConfigName("shuttle")
viper.SetConfigType("yaml")
// paths to look for the config file in
viper.AddConfigPath(".")
viper.AddConfigPath("$HOME")
viper.AddConfigPath("$HOME/.shuttle")
viper.AddConfigPath("$HOME/.config")
viper.AddConfigPath("$HOME/.config/shuttle")
viper.ReadInConfig() // Find and read the config file
viper.SetDefault("prompt.icon", "$")
// detect enviorment
var os string = runtime.GOOS
switch os{
case "windows":
var osLogo string= ""
prompt(osLogo)
case "darwin":
var osLogo string= ""
prompt(osLogo)
case "linux":
var osLogo string= ""
prompt(osLogo)
}
}
func trimPath(cwd, home string) string {
var path string
if strings.HasPrefix(cwd, home) {
path = "~" + strings.TrimPrefix(cwd, home)
} else {
// If path doesn't contain $HOME, return the
// entire path as is.
path = cwd
return path
}
pathSep := os.PathSeparator
items := strings.Split(path,string(pathSep))
truncItems := []string{}
for i, item := range items {
if i == (len(items) - 1) {
truncItems = append(truncItems, item)
break
}
truncItems = append(truncItems, item[:1])
}
return filepath.Join(truncItems...)
}
func use(vals ...interface{}) {
for _, val := range vals {
_ = val
}
}
func prompt(osLogo string) {
// FG colors
red := color.FgRed.Render
green := color.FgGreen.Render
cyan := color.FgCyan.Render
white := color.FgWhite.Render
blue := color.FgBlue.Render
// BG colors
bgYellow := color.BgYellow.Render
bgRed := color.BgRed.Render
bgGreen := color.BgGreen.Render
bgCyan := color.BgCyan.Render
bgWhite := color.BgWhite.Render
bgBlue := color.BgBlue.Render
// create a symbol
use(red,blue,bgGreen,bgWhite,green) // dont complain about unused colors
osSym := bgRed(white(" "+osLogo+" "))
if(osLogo == ""){
osSym = bgBlue(white(" "+osLogo+" "))
}else if(osLogo == ""){
osSym = bgCyan(white(" "+osLogo+" "))
}
// render prompt
var prompt string
prompt = cyan("")
if(viper.Get("prompt.segments.os" == true){
prompt = prompt + osSym
}
if(viper.Get("prompt.segments.cwd") == true){
cwd , _ := os.Getwd()
homeVar := viper.Get("HOME")
prompt = prompt + bgYellow(white(" :"+""+trimPath(cwd,homeVar.(string))+" "))
}
// icon
var icon string = viper.GetString("prompt.icon")
exitCode := viper.GetBool("prompt.options.exitCode")
code := viper.Get("?")
var lastExitCode string = code.(string)
if(exitCode != false){
if(runtime.GOOS == "windows"){
if(code = "False"){
prompt = prompt + "" + red(icon) + ""
}
else {
prompt = prompt + "" + icon + " "
}
}
else{
if(code != 0){
prompt = prompt + "" + red(icon) + " "
}
else {
prompt = prompt + "" + icon + " "
}
}
}
else{
prompt = prompt + "" + icon + " "
}
// print it out
fmt.Println(prompt)
}