File: segment-hg.go

package info (click to toggle)
powerline-go 1.25-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 612 kB
  • sloc: sh: 30; makefile: 4
file content (73 lines) | stat: -rw-r--r-- 1,507 bytes parent folder | download | duplicates (2)
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
package main

import (
	"fmt"
	"os/exec"
	"strings"

	pwl "github.com/justjanne/powerline-go/powerline"
)

func getHgStatus() (bool, bool, bool) {
	hasModifiedFiles := false
	hasUntrackedFiles := false
	hasMissingFiles := false

	out, err := exec.Command("hg", "status").Output()
	if err == nil {
		output := strings.Split(string(out), "\n")
		for _, line := range output {
			if line != "" {
				if line[0] == '?' {
					hasUntrackedFiles = true
				} else if line[0] == '!' {
					hasMissingFiles = true
				} else {
					hasModifiedFiles = true
				}
			}
		}
	}
	return hasModifiedFiles, hasUntrackedFiles, hasMissingFiles
}

func segmentHg(p *powerline) []pwl.Segment {
	out, _ := exec.Command("hg", "branch").Output()
	output := strings.SplitN(string(out), "\n", 2)
	if !(len(output) > 0 && output[0] != "") {
		return []pwl.Segment{}
	}
	branch := output[0]
	hasModifiedFiles, hasUntrackedFiles, hasMissingFiles := getHgStatus()

	var foreground, background uint8
	var content string
	if hasModifiedFiles || hasUntrackedFiles || hasMissingFiles {
		foreground = p.theme.RepoDirtyFg
		background = p.theme.RepoDirtyBg

		extra := ""

		if hasUntrackedFiles {
			extra += "+"
		}

		if hasMissingFiles {
			extra += "!"
		}

		content = fmt.Sprintf("%s %s", branch, extra)
	} else {
		foreground = p.theme.RepoCleanFg
		background = p.theme.RepoCleanBg

		content = branch
	}

	return []pwl.Segment{{
		Name:       "hg",
		Content:    content,
		Foreground: foreground,
		Background: background,
	}}
}