-
Notifications
You must be signed in to change notification settings - Fork 0
/
powershell.go
109 lines (91 loc) · 2.64 KB
/
powershell.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
package main
import (
"fmt"
"github.com/docopt/docopt.go"
)
type Characters struct {
separator string
separator_thin string
ln string
branch string
padlock string
}
const (
color_template string = "\\[\\e%s\\]"
reset string = "\\[\\e[0m\\]"
)
func Color(prefix int, code int) string {
return fmt.Sprintf(color_template, fmt.Sprintf("[%v;5;%vm", prefix, code))
}
func FgColor(code int) string {
return Color(38, code)
}
func BgColor(code int) string {
return Color(48, code)
}
func PrintSegment(segment *Segment, next *Segment) {
var sepColor string
if next == nil {
sepColor = reset + FgColor(segment.separatorFg)
} else {
sepColor = FgColor(segment.separatorFg) + BgColor(next.bg)
}
if segment.content[0] != ' ' {
segment.content = " " + segment.content
}
if segment.content[len(segment.content) - 1] != ' ' {
segment.content += " "
}
fmt.Print(FgColor(segment.fg), BgColor(segment.bg), segment.content, sepColor, segment.separator)
}
// Global variables
var chars Characters
var colorScheme ColorScheme
var line []*Segment
func main() {
usage := `Go Powershell
Usage: powershell [options] <segment>...
Options:
-h, --help Show this screen.
--version Show the version.
-c TYPE, --characters=TYPE The type of line characters (powerline, compatible or flat) [default: powerline]
Segments:
username
hostname
path
git
prompt
`
arguments, _ := docopt.Parse(usage, nil, true, "Go Powershell 0.1", true)
switch arguments["--characters"] {
case "flat":
chars = Characters{"", "", "", "", ""}
case "compatible":
chars = Characters{"\u25B6", "\u25B7", "", "", ""}
case "powerline":
chars = Characters{"\uE0B0", "\uE0B1", "\uE0A1", "\uE0A0", "\uE0A2"}
default:
chars = Characters{"\uE0B0", "\uE0B1", "\uE0A1", "\uE0A0", "\uE0A2"}
}
// Init colors and set used ones
colors := InitColors()
colorScheme = colors["default"]
// Execute them and save them in an array before printing
segments := InitSegments()
line = make([]*Segment, 0, len(arguments["<segment>"].([]string)))
for _, seg := range arguments["<segment>"].([]string) {
if chosen, exists := segments[seg]; exists {
chosen()
}
}
// Print all chosen segments
for i, seg := range line {
if i < len(line) - 1 {
PrintSegment(seg, line[i + 1])
} else {
PrintSegment(seg, nil)
}
}
// Reset the cmdline before the user prompt
fmt.Print(reset)
}