1
0
mirror of https://github.com/taigrr/wtf synced 2025-01-18 04:03:14 -08:00

First pass at multi-Git repos. Works; no tick marks yet

This commit is contained in:
Chris Cummer 2018-04-18 09:07:41 -07:00
parent 3175b8d9cc
commit 44ccf8e55a
7 changed files with 230 additions and 157 deletions

View File

@ -1,65 +0,0 @@
package git
import (
"fmt"
"os/exec"
"strings"
"github.com/senorprogrammer/wtf/wtf"
)
type Client struct{}
func NewClient() *Client {
return &Client{}
}
/* -------------------- Exported Functions -------------------- */
func (client *Client) Branch() string {
arg := []string{client.gitDir(), client.workTree(), "rev-parse", "--abbrev-ref", "HEAD"}
cmd := exec.Command("git", arg...)
str := wtf.ExecuteCommand(cmd)
return str
}
func (client *Client) ChangedFiles() []string {
arg := []string{client.gitDir(), client.workTree(), "status", "--porcelain"}
cmd := exec.Command("git", arg...)
str := wtf.ExecuteCommand(cmd)
data := strings.Split(str, "\n")
return data
}
func (client *Client) Commits() []string {
numStr := fmt.Sprintf("-n %d", Config.UInt("wtf.mods.git.commitCount", 10))
arg := []string{client.gitDir(), client.workTree(), "log", "--date=format:\"%b %d, %Y\"", numStr, "--pretty=format:\"[forestgreen]%h [white]%s [grey]%an on %cd[white]\""}
cmd := exec.Command("git", arg...)
str := wtf.ExecuteCommand(cmd)
data := strings.Split(str, "\n")
return data
}
func (client *Client) Repository() string {
arg := []string{client.gitDir(), client.workTree(), "rev-parse", "--show-toplevel"}
cmd := exec.Command("git", arg...)
str := wtf.ExecuteCommand(cmd)
return str
}
/* -------------------- Exported Functions -------------------- */
func (client *Client) gitDir() string {
return fmt.Sprintf("--git-dir=%s/.git", Config.UString("wtf.mods.git.repository"))
}
func (client *Client) workTree() string {
return fmt.Sprintf("--work-tree=%s", Config.UString("wtf.mods.git.repository"))
}

80
git/display.go Normal file
View File

@ -0,0 +1,80 @@
package git
import (
"fmt"
"strings"
"unicode/utf8"
)
func (widget *Widget) display() {
widget.View.Clear()
repoData := widget.currentData()
title := fmt.Sprintf("[green]%s[white]\n", repoData.Repository)
widget.View.SetTitle(fmt.Sprintf(" Git: %s ", title))
str := " [red]Branch[white]\n"
str = str + fmt.Sprintf(" %s", repoData.Branch)
str = str + "\n"
str = str + widget.formatChanges(repoData.ChangedFiles)
str = str + "\n"
str = str + widget.formatCommits(repoData.Commits)
fmt.Fprintf(widget.View, "%s", str)
}
func (widget *Widget) formatChanges(data []string) string {
str := ""
str = str + " [red]Changed Files[white]\n"
if len(data) == 1 {
str = str + " [grey]none[white]\n"
} else {
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", "[yellow]M[white]", 1)
case 'R':
line = strings.Replace(line, "R", "[purple]R[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))
}

View File

@ -1,16 +0,0 @@
package git
import ()
func Fetch() map[string][]string {
client := NewClient()
result := make(map[string][]string)
result["repo"] = []string{client.Repository()}
result["branch"] = []string{client.Branch()}
result["changes"] = client.ChangedFiles()
result["commits"] = client.Commits()
return result
}

79
git/git_repo.go Normal file
View File

@ -0,0 +1,79 @@
package git
import (
"fmt"
"os/exec"
"strings"
"github.com/senorprogrammer/wtf/wtf"
)
type GitRepo struct {
Branch string
ChangedFiles []string
Commits []string
Repository string
Path string
}
func NewGitRepo(repoPath string) *GitRepo {
repo := GitRepo{Path: repoPath}
repo.Branch = repo.branch()
repo.ChangedFiles = repo.changedFiles()
repo.Commits = repo.commits()
repo.Repository = repo.repository()
return &repo
}
/* -------------------- Unexported Functions -------------------- */
func (repo *GitRepo) branch() string {
arg := []string{repo.gitDir(), repo.workTree(), "rev-parse", "--abbrev-ref", "HEAD"}
cmd := exec.Command("git", arg...)
str := wtf.ExecuteCommand(cmd)
return str
}
func (repo *GitRepo) changedFiles() []string {
arg := []string{repo.gitDir(), repo.workTree(), "status", "--porcelain"}
cmd := exec.Command("git", arg...)
str := wtf.ExecuteCommand(cmd)
data := strings.Split(str, "\n")
return data
}
func (repo *GitRepo) commits() []string {
numStr := fmt.Sprintf("-n %d", Config.UInt("wtf.mods.git.commitCount", 10))
arg := []string{repo.gitDir(), repo.workTree(), "log", "--date=format:\"%b %d, %Y\"", numStr, "--pretty=format:\"[forestgreen]%h [white]%s [grey]%an on %cd[white]\""}
cmd := exec.Command("git", arg...)
str := wtf.ExecuteCommand(cmd)
data := strings.Split(str, "\n")
return data
}
func (repo *GitRepo) repository() string {
arg := []string{repo.gitDir(), repo.workTree(), "rev-parse", "--show-toplevel"}
cmd := exec.Command("git", arg...)
str := wtf.ExecuteCommand(cmd)
return str
}
func (repo *GitRepo) gitDir() string {
return fmt.Sprintf("--git-dir=%s/.git", repo.Path)
}
func (repo *GitRepo) workTree() string {
return fmt.Sprintf("--work-tree=%s", repo.Path)
}

View File

@ -4,8 +4,9 @@ import (
"fmt"
"strings"
"time"
"unicode/utf8"
"github.com/gdamore/tcell"
"github.com/olebedev/config"
"github.com/senorprogrammer/wtf/wtf"
)
@ -15,99 +16,95 @@ var Config *config.Config
type Widget struct {
wtf.TextWidget
Data []*GitRepo
Idx int
}
func NewWidget() *Widget {
widget := Widget{
TextWidget: wtf.NewTextWidget(" Git ", "git"),
Idx: 0,
}
widget.View.SetInputCapture(widget.keyboardIntercept)
return &widget
}
/* -------------------- Exported Functions -------------------- */
func (widget *Widget) Fetch(repoPaths []string) []*GitRepo {
data := []*GitRepo{}
for _, repoPath := range repoPaths {
repo := NewGitRepo(repoPath)
data = append(data, repo)
}
return data
}
func (widget *Widget) Refresh() {
if widget.Disabled() {
return
}
data := Fetch()
title := fmt.Sprintf("[green]%s[white]\n", data["repo"][0])
widget.View.SetTitle(fmt.Sprintf(" Git: %s ", title))
widget.View.Clear()
fmt.Fprintf(widget.View, "%s", widget.contentFrom(data))
repoPaths := wtf.ToStrs(Config.UList("wtf.mods.git.repositories"))
widget.Data = widget.Fetch(repoPaths)
widget.display()
widget.RefreshedAt = time.Now()
}
func (widget *Widget) Next() {
widget.Idx = widget.Idx + 1
if widget.Idx == len(widget.Data) {
widget.Idx = 0
}
widget.display()
}
func (widget *Widget) Prev() {
widget.Idx = widget.Idx - 1
if widget.Idx < 0 {
widget.Idx = len(widget.Data) - 1
}
widget.display()
}
/* -------------------- Unexported Functions -------------------- */
func (widget *Widget) contentFrom(data map[string][]string) string {
str := " [red]Branch[white]\n"
str = str + fmt.Sprintf(" %s", data["branch"][0])
str = str + "\n"
str = str + widget.formatChanges(data["changes"])
str = str + "\n"
str = str + widget.formatCommits(data["commits"])
return str
func (widget *Widget) currentData() *GitRepo {
return widget.Data[widget.Idx]
}
func (widget *Widget) formatChanges(data []string) string {
str := ""
str = str + " [red]Changed Files[white]\n"
if len(data) == 1 {
str = str + " [grey]none[white]\n"
} else {
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", "[yellow]M[white]", 1)
case 'R':
line = strings.Replace(line, "R", "[purple]R[white]", 1)
func (widget *Widget) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey {
switch event.Key() {
case tcell.KeyLeft:
widget.Prev()
return nil
case tcell.KeyRight:
widget.Next()
return nil
default:
line = line
return event
}
return fmt.Sprintf(" %s\n", strings.Replace(line, "\"", "", -1))
return event
}
func (widget *Widget) formatCommits(data []string) string {
func (widget *Widget) tickMarks(data []*GitRepo) string {
str := ""
str = str + " [red]Recent Commits[white]\n"
for _, line := range data {
str = str + widget.formatCommit(line)
if len(data) > 1 {
marks := strings.Repeat("*", len(data))
marks = marks[:widget.Idx] + "_" + marks[widget.Idx+1:]
str = "[lightblue]" + fmt.Sprintf(wtf.RightAlignFormat(widget.View), marks) + "[white]"
}
return str
}
func (widget *Widget) formatCommit(line string) string {
return fmt.Sprintf(" %s\n", strings.Replace(line, "\"", "", -1))
}

View File

@ -8,10 +8,10 @@ import (
"github.com/senorprogrammer/wtf/wtf"
)
func (widget *Widget) display(data []*owm.CurrentWeatherData) {
func (widget *Widget) display() {
widget.View.Clear()
cityData := widget.currentCityData(data)
cityData := widget.currentData()
if len(cityData.Weather) == 0 {
fmt.Fprintf(widget.View, "%s", " Weather data is unavailable.")
@ -20,7 +20,7 @@ func (widget *Widget) display(data []*owm.CurrentWeatherData) {
widget.View.SetTitle(widget.title(cityData))
str := widget.tickMarks(data) + "\n"
str := widget.tickMarks(widget.Data) + "\n"
str = str + widget.description(cityData) + "\n\n"
str = str + widget.temperatures(cityData) + "\n"
str = str + widget.sunInfo(cityData)
@ -59,10 +59,10 @@ func (widget *Widget) tickMarks(data []*owm.CurrentWeatherData) string {
str := ""
if len(data) > 1 {
tickMarks := strings.Repeat("*", len(data))
tickMarks = tickMarks[:widget.Idx] + "_" + tickMarks[widget.Idx+1:]
marks := strings.Repeat("*", len(data))
marks = marks[:widget.Idx] + "_" + marks[widget.Idx+1:]
str = "[lightblue]" + fmt.Sprintf(wtf.RightAlignFormat(widget.View), tickMarks) + "[white]"
str = "[lightblue]" + fmt.Sprintf(wtf.RightAlignFormat(widget.View), marks) + "[white]"
}
return str

View File

@ -62,7 +62,7 @@ func (widget *Widget) Refresh() {
widget.Data = widget.Fetch(wtf.ToInts(Config.UList("wtf.mods.weather.cityids", widget.defaultCityCodes())))
widget.display(widget.Data)
widget.display()
widget.RefreshedAt = time.Now()
}
@ -74,7 +74,7 @@ func (widget *Widget) Next() {
widget.Idx = 0
}
widget.display(widget.Data)
widget.display()
}
// Prev displays data for the previous city in the list. If the previous city is the first
@ -85,13 +85,13 @@ func (widget *Widget) Prev() {
widget.Idx = len(widget.Data) - 1
}
widget.display(widget.Data)
widget.display()
}
/* -------------------- Unexported Functions -------------------- */
func (widget *Widget) currentCityData(data []*owm.CurrentWeatherData) *owm.CurrentWeatherData {
return data[widget.Idx]
func (widget *Widget) currentData() *owm.CurrentWeatherData {
return widget.Data[widget.Idx]
}
func (widget *Widget) currentWeather(apiKey string, cityCode int) (*owm.CurrentWeatherData, error) {
@ -189,15 +189,13 @@ func (widget *Widget) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey {
switch event.Key() {
case tcell.KeyLeft:
widget.Prev()
return nil
case tcell.KeyRight:
widget.Next()
return nil
default:
return event
}
return event
}
func (widget *Widget) refreshedAt() string {
return widget.RefreshedAt.Format("15:04:05")
}