-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathprompt.go
104 lines (104 loc) · 2.49 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
package main
import (
"fmt"
"runtime"
"github.com/spf13/viper"
"path/filepath"
"strings"
"os"
"github.com/gookit/color"
)
var(
osLogo string
os string
)
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/.config")
viper.AddConfigPath("$HOME/.config/shuttle")
viper.ReadInConfig() // Find and read the config file
viper.SetDefault("prompt.icon", "$")
// detect enviorment
os = runtime.GOOS
switch os{
case "windows":
osLogo = ""
prompt(osLogo)
case "darwin":
osLogo = ""
prompt(osLogo)
case "linux":
osLogo = ""
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
use(red,blue,bgGreen,bgWhite,green) // don`t 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")
prompt = prompt + "" + cyan(icon) + " "
// print it out
fmt.Println(prompt)
}