1package main
2
3import (
4	"strings"
5
6	"github.com/xyproto/vt100"
7)
8
9var gitRebasePrefixes = []string{"p", "pick", "f", "fixup", "r", "reword", "d", "drop", "e", "edit", "s", "squash", "x", "exec", "b", "break", "l", "label", "t", "reset", "m", "merge"}
10
11// nextGitRebaseKeywords takes the first word and increase it to the next git rebase keyword
12func nextGitRebaseKeyword(line string) string {
13	var (
14		cycle1 = filterS(gitRebasePrefixes, func(s string) bool { return len(s) > 1 })
15		cycle2 = filterS(gitRebasePrefixes, func(s string) bool { return len(s) == 1 })
16		first  = strings.Fields(line)[0]
17		next   = ""
18	)
19	// Check if the word is in cycle1, then set "next" to the next one in the cycle
20	for i, w := range cycle1 {
21		if first == w {
22			if i+1 < len(cycle1) {
23				next = cycle1[i+1]
24				break
25			} else {
26				next = cycle1[0]
27			}
28		}
29	}
30	if next == "" {
31		// Check if the word is in cycle2, then set "next" to the next one in the cycle
32		for i, w := range cycle2 {
33			if first == w {
34				if i+1 < len(cycle2) {
35					next = cycle2[i+1]
36					break
37				} else {
38					next = cycle2[0]
39				}
40			}
41		}
42	}
43	if next == "" {
44		// Return the line as it is, no git rebase keyword found
45		return line
46	}
47	// Return the line with the keyword replaced with the next one in cycle1 or cycle2
48	return strings.Replace(line, first, next, 1)
49}
50
51func (e *Editor) gitHighlight(line string) string {
52	var coloredString string
53	if strings.HasPrefix(line, "#") {
54		filenameColor := vt100.Red
55		renameColor := vt100.Magenta
56		if strings.HasPrefix(line, "# On branch ") {
57			coloredString = vt100.DarkGray.Get(line[:12]) + vt100.LightCyan.Get(line[12:])
58		} else if strings.HasPrefix(line, "# Your branch is up to date with '") && strings.Count(line, "'") == 2 {
59			parts := strings.SplitN(line, "'", 3)
60			coloredString = vt100.DarkGray.Get(parts[0]+"'") + vt100.LightGreen.Get(parts[1]) + vt100.DarkGray.Get("'"+parts[2])
61		} else if line == "# Changes to be committed:" {
62			coloredString = vt100.DarkGray.Get("# ") + vt100.LightBlue.Get("Changes to be committed:")
63		} else if line == "# Changes not staged for commit:" {
64			coloredString = vt100.DarkGray.Get("# ") + vt100.LightBlue.Get("Changes not staged for commit:")
65		} else if line == "# Untracked files:" {
66			coloredString = vt100.DarkGray.Get("# ") + vt100.LightBlue.Get("Untracked files:")
67		} else if strings.Contains(line, "new file:") {
68			parts := strings.SplitN(line[1:], ":", 2)
69			coloredString = vt100.DarkGray.Get("#") + vt100.LightYellow.Get(parts[0]) + vt100.DarkGray.Get(":") + filenameColor.Get(parts[1])
70		} else if strings.Contains(line, "modified:") {
71			parts := strings.SplitN(line[1:], ":", 2)
72			coloredString = vt100.DarkGray.Get("#") + vt100.LightYellow.Get(parts[0]) + vt100.DarkGray.Get(":") + filenameColor.Get(parts[1])
73		} else if strings.Contains(line, "deleted:") {
74			parts := strings.SplitN(line[1:], ":", 2)
75			coloredString = vt100.DarkGray.Get("#") + vt100.LightYellow.Get(parts[0]) + vt100.DarkGray.Get(":") + filenameColor.Get(parts[1])
76		} else if strings.Contains(line, "renamed:") {
77			parts := strings.SplitN(line[1:], ":", 2)
78			coloredString = vt100.DarkGray.Get("#") + vt100.LightYellow.Get(parts[0]) + vt100.DarkGray.Get(":")
79			if strings.Contains(parts[1], "->") {
80				filenames := strings.SplitN(parts[1], "->", 2)
81				coloredString += renameColor.Get(filenames[0]) + vt100.White.Get("->") + renameColor.Get(filenames[1])
82			} else {
83				coloredString += filenameColor.Get(parts[1])
84			}
85		} else if fields := strings.Fields(line); strings.HasPrefix(line, "# Rebase ") && len(fields) >= 5 && strings.Contains(fields[2], "..") {
86			textColor := vt100.LightGray
87			commitRange := strings.SplitN(fields[2], "..", 2)
88			coloredString = vt100.DarkGray.Get("# ") + textColor.Get(fields[1]) + " " + vt100.LightBlue.Get(commitRange[0]) + textColor.Get("..") + vt100.LightBlue.Get(commitRange[1]) + " " + textColor.Get(fields[3]) + " " + vt100.LightBlue.Get(fields[4]) + " " + textColor.Get(strings.Join(fields[5:], " "))
89		} else {
90			coloredString = vt100.DarkGray.Get(line)
91		}
92	} else if fields := strings.Fields(line); len(fields) >= 3 && hasAnyPrefixWord(line, []string{"p", "pick", "r", "reword", "e", "edit", "s", "squash", "f", "fixup", "x", "exec", "b", "break", "d", "drop", "l", "label", "t", "reset", "m", "merge"}) {
93		coloredString = vt100.Red.Get(fields[0]) + " " + vt100.LightBlue.Get(fields[1]) + " " + vt100.LightGray.Get(strings.Join(fields[2:], " "))
94	} else {
95		coloredString = e.Git.Get(line)
96	}
97	return coloredString
98}
99