This repository has been archived by the owner on Jun 12, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathprint.go
85 lines (72 loc) · 1.91 KB
/
print.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
package main
import (
"fmt"
"strings"
"sync"
. "github.com/reujab/bronze/types"
"github.com/urfave/cli"
)
var cmdPrint = cli.Command{
Name: "print",
Usage: "Prints the prompt with the specified modules",
Description: "Example: bronze print dir:blue:black",
Action: func(ctx *cli.Context) error {
if len(ctx.Args()) == 0 {
cli.ShowCommandHelpAndExit(ctx, "print", 1)
}
cmdPrintAction(ctx.Args())
return nil
},
}
func cmdPrintAction(args []string) {
var segments []*Segment
waitgroup := new(sync.WaitGroup)
waitgroup.Add(len(args))
for _, arg := range args {
// validate argument
fields := strings.Split(arg, ":")
if len(fields) < 3 {
dief("invalid argument: %q, at least three fields expected", arg)
}
segment := &Segment{
Background: fields[1],
Foreground: fields[2],
}
segments = append(segments, segment)
go func() {
handleModule(fields[0], segment, fields[3:])
waitgroup.Done()
}()
}
// wait for all the async segments
waitgroup.Wait()
// print the prompt
first := true
lastSegment := &Segment{
Background: "black",
Foreground: "white",
}
for _, segment := range segments {
if segment.Value == "" {
continue
}
// if this isn't the first segment, before printing the next segment, separate them
if !first {
if segment.Background == lastSegment.Background {
printSegment(segment.Background, lastSegment.Foreground, thinSeparator)
} else {
// use the last background as the current foreground
printSegment(segment.Background, lastSegment.Background, separator)
}
}
first = false
printSegment(segment.Background, segment.Foreground, " "+segment.Value+" ")
lastSegment = segment
}
// print final separator
printSegment("none", lastSegment.Background, separator)
resetColors()
}
func printSegment(background, foreground, value string) {
fmt.Print(escapeBackground(background) + escapeForeground(foreground) + value)
}