summaryrefslogtreecommitdiff
path: root/pkg/gui/textstyle.go
blob: 1514b46a22cbd06e923e2428ff9433d5ed00aea7 (about) (plain)
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
package gui

import (
	"regexp"

	"github.com/isacikgoz/gitbatch/pkg/git"
	"github.com/isacikgoz/gitbatch/pkg/job"
)

var (
	maxBranchLength = 15
	maxRepositoryLength = 20

	ws = " "
	pushable = string(blue.Sprint("↖"))
	pullable = string(blue.Sprint("↘"))
	confidentArrow = string(magenta.Sprint("→"))
	unconfidentArrow = string(yellow.Sprint("→"))
	dirty = string(yellow.Sprint("✗"))
	unkown = magenta.Sprint("?")

	queuedSymbol = "•"
	workingSymbol = "•"
	successSymbol = "✔"
	failSymbol = "✗"

	modeSeperator = "▓▒"
	keyBindingSeperator = "░"

	selectionIndicator = string(green.Sprint("→")) + ws
	tab = ws + ws
)

func (gui *Gui) displayString(entity *git.RepoEntity) string {
	suffix := ""
	prefix := ""

	if entity.Branch.Pushables != "?" {
		prefix = prefix + pushable + ws + entity.Branch.Pushables + ws +
			pullable + ws + entity.Branch.Pullables + ws + confidentArrow + ws
	} else {
		prefix = prefix + unkown + ws + unconfidentArrow + ws 
	}

	branch := adjustTextLength(entity.Branch.Name, maxBranchLength)
	prefix = prefix + string(cyan.Sprint(branch))

	if !entity.Branch.Clean {
		prefix = prefix + ws + dirty + ws 
	} else {
		prefix = prefix + ws 
	}

	if entity.State == git.Queued {
		if inQueue, ty := gui.State.Queue.IsInTheQueue(entity); inQueue {
		switch mode := ty; mode {
			case job.Fetch:
				suffix = blue.Sprint(queuedSymbol)
			case job.Pull:
				suffix = magenta.Sprint(queuedSymbol)
			case job.Merge:
				suffix = cyan.Sprint(queuedSymbol)
			default:
				suffix = green.Sprint(queuedSymbol)
			}
		}
		return prefix + entity.Name + ws + suffix
	} else if entity.State == git.Working {
		return prefix + entity.Name + ws + green.Sprint(workingSymbol)
	} else if entity.State == git.Success {
		return prefix + entity.Name + ws + green.Sprint(successSymbol)
	} else if entity.State == git.Fail {
		return prefix + entity.Name + ws + red.Sprint(failSymbol)
	} else {
		return prefix + entity.Name
	}
}

func adjustTextLength(text string, maxLength int) (adjusted string) {
	if len(text) > maxLength {
		adjusted := text[:maxLength-2] + ".."
		return adjusted
	} else {
		return text
	}
}

func trimRemoteURL(url string) (urltype string, shorturl string) {
	regit := regexp.MustCompile(`.git`)
	if regit.MatchString(url[len(url)-4:]) {
		url = url[:len(url)-4]
	}
	ressh := regexp.MustCompile(`git@`)
	rehttp := regexp.MustCompile(`http://`)
	rehttps := regexp.MustCompile(`https://`)

	if ressh.MatchString(url) {
		shorturl = ressh.Split(url, 5)[1]
		urltype = "ssh"
	} else if rehttp.MatchString(url) {
		shorturl = rehttp.Split(url, 5)[1]
		urltype = "http"
	} else if rehttps.MatchString(url) {
		shorturl = rehttps.Split(url, 5)[1]
		urltype = "https"
	}
	return urltype, shorturl
}