diff --git a/git/client.go b/git/client.go deleted file mode 100644 index ce9121c5..00000000 --- a/git/client.go +++ /dev/null @@ -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")) -} diff --git a/git/display.go b/git/display.go new file mode 100644 index 00000000..8315e478 --- /dev/null +++ b/git/display.go @@ -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)) +} diff --git a/git/git.go b/git/git.go deleted file mode 100644 index 78162541..00000000 --- a/git/git.go +++ /dev/null @@ -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 -} diff --git a/git/git_repo.go b/git/git_repo.go new file mode 100644 index 00000000..7f82e4bd --- /dev/null +++ b/git/git_repo.go @@ -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) +} diff --git a/git/widget.go b/git/widget.go index 973da71d..52bc6d08 100644 --- a/git/widget.go +++ b/git/widget.go @@ -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)) -} diff --git a/weather/display.go b/weather/display.go index 99a60713..71db5667 100644 --- a/weather/display.go +++ b/weather/display.go @@ -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 diff --git a/weather/widget.go b/weather/widget.go index d76660ac..bf7f55c9 100644 --- a/weather/widget.go +++ b/weather/widget.go @@ -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") -}