From a79588e9cc4b23673d9165df58955c7d62b37550 Mon Sep 17 00:00:00 2001 From: Chris Cummer Date: Sun, 1 Apr 2018 20:57:55 -0700 Subject: [PATCH] Git now displays changed files and last ten commits --- git/client.go | 17 ++++++++++---- git/git.go | 2 +- git/widget.go | 63 ++++++++++++++++++++++++++++++++++++++++++++++++++- 3 files changed, 76 insertions(+), 6 deletions(-) diff --git a/git/client.go b/git/client.go index cdfc87c2..876ea0d3 100644 --- a/git/client.go +++ b/git/client.go @@ -4,6 +4,7 @@ import ( //"fmt" "io/ioutil" "os/exec" + "strings" ) type Client struct { @@ -31,15 +32,23 @@ func (client *Client) CurrentBranch() string { } func (client *Client) ChangedFiles() []string { - files := []string{} + arg := []string{"status", "--porcelain"} + cmd := exec.Command("git", arg...) + str := executeCommand(cmd) - return files + data := strings.Split(str, "\n") + + return data } func (client *Client) Commits() []string { - files := []string{} + arg := []string{"log", "--date=format:\"%b %d, %Y\"", "-n 10", "--pretty=format:\"[forestgreen]%h [white]%s [grey]%an on %cd[white]\""} + cmd := exec.Command("git", arg...) + str := executeCommand(cmd) - return files + data := strings.Split(str, "\n") + + return data } /* -------------------- Unexported Functions -------------------- */ diff --git a/git/git.go b/git/git.go index 5c4f3ed1..dfc61c9c 100644 --- a/git/git.go +++ b/git/git.go @@ -9,7 +9,7 @@ func Fetch() map[string][]string { result["repo"] = []string{client.Repository} result["branch"] = []string{client.CurrentBranch()} - result["changed"] = client.ChangedFiles() + result["changes"] = client.ChangedFiles() result["commits"] = client.Commits() return result diff --git a/git/widget.go b/git/widget.go index ed1f6d29..2f99fd57 100644 --- a/git/widget.go +++ b/git/widget.go @@ -2,7 +2,9 @@ package git import ( "fmt" + "strings" "time" + "unicode/utf8" "github.com/rivo/tview" "github.com/senorprogrammer/wtf/wtf" @@ -48,18 +50,77 @@ func (widget *Widget) addView() { view.SetBorder(true) view.SetDynamicColors(true) view.SetTitle(widget.Name) + view.SetWrap(false) widget.View = view } func (widget *Widget) contentFrom(data map[string][]string) string { str := "\n" - str = str + fmt.Sprintf(" [green]%s[white] [grey]%s[white]\n", data["repo"][0], data["branch"][0]) + str = str + fmt.Sprintf(" [green]%s[white] [dodgerblue]%s[white]\n", data["repo"][0], data["branch"][0]) + + str = str + widget.formatChanges(data["changes"]) str = str + "\n" + str = str + widget.formatCommits(data["commits"]) return str } +func (widget *Widget) formatChanges(data []string) string { + str := "" + str = str + " [red]Changed Files[white]\n" + + for _, line := range data { + str = str + widget.formatChange(line) + } + + return str +} + +func (widget *Widget) formatChange(line string) string { + if len(line) == 0 { + return "" + } + + line = strings.TrimSpace(line) + firstChar, _ := utf8.DecodeRuneInString(line) + + // Revisit this and kill the ugly duplication + switch firstChar { + case 'A': + line = strings.Replace(line, "A", "[green]A[white]", 1) + case 'D': + line = strings.Replace(line, "D", "[red]D[white]", 1) + case 'M': + line = strings.Replace(line, "M", "[blue]M[white]", 1) + case 'R': + line = strings.Replace(line, "R", "[purple]R[white]", 1) + case '!': + line = strings.Replace(line, "!", "[yellow]![white]", 1) + case '?': + line = strings.Replace(line, "?", "[yellow]?[white]", 1) + default: + line = line + } + + return fmt.Sprintf(" %s\n", strings.Replace(line, "\"", "", -1)) +} + +func (widget *Widget) formatCommits(data []string) string { + str := "" + str = str + " [red]Recent Commits[white]\n" + + for _, line := range data { + str = str + widget.formatCommit(line) + } + + return str +} + +func (widget *Widget) formatCommit(line string) string { + return fmt.Sprintf(" %s\n", strings.Replace(line, "\"", "", -1)) +} + func (widget *Widget) refresher() { tick := time.NewTicker(time.Duration(widget.RefreshInterval) * time.Minute) quit := make(chan struct{})