summaryrefslogtreecommitdiff
path: root/pkg/gui/commitsview.go
blob: 6d1325cc610523d56b670c0cd46bb1ea8fff0f35 (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
package gui

import (
	"fmt"

	"github.com/isacikgoz/gitbatch/pkg/git"
	"github.com/jroimartin/gocui"
)

// updates the commitsview for given entity
func (gui *Gui) updateCommits(g *gocui.Gui, entity *git.RepoEntity) error {
	var err error
	out, err := g.View(commitViewFeature.Name)
	if err != nil {
		return err
	}
	out.Clear()

	currentindex := 0
	totalcommits := len(entity.Commits)
	for i, c := range entity.Commits {
		var body string
		if c.CommitType == git.EvenCommit {
			body = cyan.Sprint(c.Hash[:hashLength]) + " " + c.Message
		} else if c.CommitType == git.LocalCommit {
			body = blue.Sprint(c.Hash[:hashLength]) + " " + c.Message
		} else {
			body = yellow.Sprint(c.Hash[:hashLength]) + " " + c.Message
		}
		if c.Hash == entity.Commit.Hash {
			currentindex = i
			fmt.Fprintln(out, selectionIndicator+body)
			continue
		}
		fmt.Fprintln(out, tab+body)
	}
	if err = gui.smartAnchorRelativeToLine(out, currentindex, totalcommits); err != nil {
		return err
	}
	return err
}

// iteration handler for the commitsview
func (gui *Gui) nextCommit(g *gocui.Gui, v *gocui.View) error {
	var err error
	entity := gui.getSelectedRepository()
	if err = entity.NextCommit(); err != nil {
		return err
	}
	if err = gui.updateCommits(g, entity); err != nil {
		return err
	}
	return err
}

// reverse iteration handler for the commitsview
func (gui *Gui) prevCommit(g *gocui.Gui, v *gocui.View) error {
	var err error
	entity := gui.getSelectedRepository()
	if err = entity.PreviousCommit(); err != nil {
		return err
	}
	if err = gui.updateCommits(g, entity); err != nil {
		return err
	}
	return err
}