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
110
111
112
|
package gui
import (
"fmt"
"regexp"
"strings"
"github.com/jroimartin/gocui"
)
// open diff view for the selcted commit
func (gui *Gui) openCommitDiffView(g *gocui.Gui, v *gocui.View) error {
maxX, maxY := g.Size()
v, err := g.SetView(commitDiffViewFeature.Name, 5, 3, maxX-5, maxY-3)
if err != nil {
if err != gocui.ErrUnknownView {
return err
}
v.Title = commitDiffViewFeature.Title
v.Overwrite = true
v.Wrap = true
entity := gui.getSelectedRepository()
commit := entity.Commit
commitDetail := "Hash: " + cyan.Sprint(commit.Hash) + "\n" + "Author: " + commit.Author +
"\n" + commit.Time + "\n" + "\n" + "\t\t" + commit.Message + "\n"
fmt.Fprintln(v, commitDetail)
diff, err := entity.Diff(entity.Commit.Hash)
if err != nil {
return err
}
colorized := colorizeDiff(diff)
for _, line := range colorized {
fmt.Fprintln(v, line)
}
}
gui.updateKeyBindingsView(g, commitDiffViewFeature.Name)
if _, err := g.SetCurrentView(commitDiffViewFeature.Name); err != nil {
return err
}
return nil
}
// close the opened diff view
func (gui *Gui) closeCommitDiffView(g *gocui.Gui, v *gocui.View) error {
if err := g.DeleteView(v.Name()); err != nil {
return nil
}
if _, err := g.SetCurrentView(mainViewFeature.Name); err != nil {
return err
}
gui.updateKeyBindingsView(g, mainViewFeature.Name)
return nil
}
// cursor down acts like half-page down for faster scrolling
func (gui *Gui) commitCursorDown(g *gocui.Gui, v *gocui.View) error {
if v != nil {
ox, oy := v.Origin()
_, vy := v.Size()
// TODO: do something when it hits bottom
if err := v.SetOrigin(ox, oy+vy/2); err != nil {
return err
}
}
return nil
}
// cursor up acts like half-page up for faster scrolling
func (gui *Gui) commitCursorUp(g *gocui.Gui, v *gocui.View) error {
if v != nil {
ox, oy := v.Origin()
_, vy := v.Size()
if oy-vy/2 > 0 {
if err := v.SetOrigin(ox, oy-vy/2); err != nil {
return err
}
} else if oy-vy/2 <= 0 {
if err := v.SetOrigin(0, 0); err != nil {
return err
}
}
}
return nil
}
// colorize the plain diff text collected from system output
// the style is near to original diff command
func colorizeDiff(original string) (colorized []string) {
colorized = strings.Split(original, "\n")
re := regexp.MustCompile(`@@ .+ @@`)
for i, line := range colorized {
if len(line) > 0 {
if line[0] == '-' {
colorized[i] = red.Sprint(line)
} else if line[0] == '+' {
colorized[i] = green.Sprint(line)
} else if re.MatchString(line) {
s := re.FindString(line)
colorized[i] = cyan.Sprint(s) + line[len(s):]
} else {
continue
}
} else {
continue
}
}
return colorized
}
|