From 5e186323e0f47336b83fd27b2e068c72d173035b Mon Sep 17 00:00:00 2001 From: Chris Cummer Date: Wed, 18 Apr 2018 16:45:50 -0700 Subject: [PATCH] A lot of code cleanup for Clocks and Git --- clocks/timezones.go | 21 ------------ clocks/widget.go | 80 +++++++++++++++++++++++++++++-------------- git/display.go | 15 ++++++++ git/widget.go | 39 +++++++-------------- github/display.go | 22 ++++++++++++ github/github_repo.go | 17 +++++++++ github/widget.go | 15 +------- 7 files changed, 121 insertions(+), 88 deletions(-) delete mode 100644 clocks/timezones.go create mode 100644 github/display.go create mode 100644 github/github_repo.go diff --git a/clocks/timezones.go b/clocks/timezones.go deleted file mode 100644 index 86f3613a..00000000 --- a/clocks/timezones.go +++ /dev/null @@ -1,21 +0,0 @@ -package clocks - -import ( - "time" -) - -func Timezones(locations map[string]interface{}) map[string]time.Time { - times := make(map[string]time.Time) - - for label, location := range locations { - tzloc, err := time.LoadLocation(location.(string)) - - if err != nil { - continue - } - - times[label] = time.Now().In(tzloc) - } - - return times -} diff --git a/clocks/widget.go b/clocks/widget.go index e7036fe2..88c008be 100644 --- a/clocks/widget.go +++ b/clocks/widget.go @@ -36,44 +36,72 @@ func (widget *Widget) Refresh() { } widget.View.Clear() - - fmt.Fprintf(widget.View, "\n%s", widget.locations()) - + widget.display() widget.RefreshedAt = time.Now() } /* -------------------- Unexported Functions -------------------- */ -func (widget *Widget) locations() string { - timezones := Timezones(Config.UMap("wtf.mods.clocks.locations")) +func (widget *Widget) colorFor(idx int) string { + rowColor := Config.UString("wtf.mods.clocks.rowcolors.even", "lightblue") - if len(timezones) == 0 { - return "" + if idx%2 == 0 { + rowColor = Config.UString("wtf.mods.clocks.rowcolors.odd", "white") } - // All this is to display the time entries in alphabetical order + return rowColor +} + +func (widget *Widget) display() { + locs := widget.locations(Config.UMap("wtf.mods.clocks.locations")) + + if len(locs) == 0 { + fmt.Fprintf(widget.View, "\n%s", " no timezone data available") + return + } + + labels := widget.sortedLabels(locs) + + tzs := []string{} + for idx, label := range labels { + zoneStr := fmt.Sprintf( + " [%s]%-12s %-10s %7s[white]", + widget.colorFor(idx), + label, + locs[label].Format(TimeFormat), + locs[label].Format(DateFormat), + ) + + tzs = append(tzs, zoneStr) + } + + fmt.Fprintf(widget.View, "\n%s", strings.Join(tzs, "\n")) +} + +func (widget *Widget) locations(locs map[string]interface{}) map[string]time.Time { + times := make(map[string]time.Time) + + for label, loc := range locs { + tzloc, err := time.LoadLocation(loc.(string)) + + if err != nil { + continue + } + + times[label] = time.Now().In(tzloc) + } + + return times +} + +func (widget *Widget) sortedLabels(locs map[string]time.Time) []string { labels := []string{} - for label, _ := range timezones { + + for label, _ := range locs { labels = append(labels, label) } sort.Strings(labels) - tzs := []string{} - for idx, label := range labels { - rowColor := Config.UString("wtf.mods.clocks.rowcolors.even", "lightblue") - if idx%2 == 0 { - rowColor = Config.UString("wtf.mods.clocks.rowcolors.odd", "white") - } - - zoneStr := fmt.Sprintf( - " [%s]%-12s %-10s %7s[white]", - rowColor, label, - timezones[label].Format(TimeFormat), - timezones[label].Format(DateFormat), - ) - tzs = append(tzs, zoneStr) - } - - return strings.Join(tzs, "\n") + return labels } diff --git a/git/display.go b/git/display.go index 2e11edd4..4a915079 100644 --- a/git/display.go +++ b/git/display.go @@ -4,6 +4,8 @@ import ( "fmt" "strings" "unicode/utf8" + + "github.com/senorprogrammer/wtf/wtf" ) func (widget *Widget) display() { @@ -83,3 +85,16 @@ func (widget *Widget) formatCommits(data []string) string { func (widget *Widget) formatCommit(line string) string { return fmt.Sprintf(" %s\n", strings.Replace(line, "\"", "", -1)) } + +func (widget *Widget) tickMarks(data []*GitRepo) string { + str := "" + + 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 +} diff --git a/git/widget.go b/git/widget.go index 55969baf..4b8cba20 100644 --- a/git/widget.go +++ b/git/widget.go @@ -1,8 +1,6 @@ package git import ( - "fmt" - "strings" "time" "github.com/gdamore/tcell" @@ -33,24 +31,13 @@ func NewWidget() *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 } repoPaths := wtf.ToStrs(Config.UList("wtf.mods.git.repositories")) - widget.Data = widget.Fetch(repoPaths) + widget.Data = widget.gitRepos(repoPaths) widget.display() widget.RefreshedAt = time.Now() @@ -88,6 +75,17 @@ func (widget *Widget) currentData() *GitRepo { return widget.Data[widget.Idx] } +func (widget *Widget) gitRepos(repoPaths []string) []*GitRepo { + repos := []*GitRepo{} + + for _, repoPath := range repoPaths { + repo := NewGitRepo(repoPath) + repos = append(repos, repo) + } + + return repos +} + func (widget *Widget) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey { switch event.Key() { case tcell.KeyLeft: @@ -102,16 +100,3 @@ func (widget *Widget) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey { return event } - -func (widget *Widget) tickMarks(data []*GitRepo) string { - str := "" - - 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 -} diff --git a/github/display.go b/github/display.go new file mode 100644 index 00000000..0b0a24a0 --- /dev/null +++ b/github/display.go @@ -0,0 +1,22 @@ +package github + +import ( + "fmt" +) + +func (widget *Widget) display() { + client := NewClient() + prs, _ := client.PullRequests(Config.UString("wtf.mods.github.owner"), Config.UString("wtf.mods.github.repo")) + + widget.View.Clear() + + widget.View.SetTitle(fmt.Sprintf(" Github: %s ", widget.title())) + + str := " [red]Open Review Requests[white]\n" + str = str + widget.prsForReview(prs, Config.UString("wtf.mods.github.username")) + str = str + "\n" + str = str + " [red]Open Pull Requests[white]\n" + str = str + widget.openPRs(prs, Config.UString("wtf.mods.github.username")) + + fmt.Fprintf(widget.View, str) +} diff --git a/github/github_repo.go b/github/github_repo.go new file mode 100644 index 00000000..f9e26963 --- /dev/null +++ b/github/github_repo.go @@ -0,0 +1,17 @@ +package github + +import () + +type GithubRepo struct { + Name string + Owner string +} + +func NewGithubRepo(owner string, name string) *GithubRepo { + repo := GithubRepo{ + Name: name, + Owner: owner, + } + + return &repo +} diff --git a/github/widget.go b/github/widget.go index a501a704..cae355e2 100644 --- a/github/widget.go +++ b/github/widget.go @@ -31,20 +31,7 @@ func (widget *Widget) Refresh() { return } - client := NewClient() - prs, _ := client.PullRequests(Config.UString("wtf.mods.github.owner"), Config.UString("wtf.mods.github.repo")) - - widget.View.SetTitle(fmt.Sprintf(" Github: %s ", widget.title())) - - str := " [red]Open Review Requests[white]\n" - str = str + widget.prsForReview(prs, Config.UString("wtf.mods.github.username")) - str = str + "\n" - str = str + " [red]Open Pull Requests[white]\n" - str = str + widget.openPRs(prs, Config.UString("wtf.mods.github.username")) - - widget.View.Clear() - fmt.Fprintf(widget.View, str) - + widget.display() widget.RefreshedAt = time.Now() }