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

A lot of code cleanup for Clocks and Git

This commit is contained in:
Chris Cummer 2018-04-18 16:45:50 -07:00
parent 1c72c71e81
commit 5e186323e0
7 changed files with 121 additions and 88 deletions

View File

@ -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
}

View File

@ -36,44 +36,72 @@ func (widget *Widget) Refresh() {
} }
widget.View.Clear() widget.View.Clear()
widget.display()
fmt.Fprintf(widget.View, "\n%s", widget.locations())
widget.RefreshedAt = time.Now() widget.RefreshedAt = time.Now()
} }
/* -------------------- Unexported Functions -------------------- */ /* -------------------- Unexported Functions -------------------- */
func (widget *Widget) locations() string { func (widget *Widget) colorFor(idx int) string {
timezones := Timezones(Config.UMap("wtf.mods.clocks.locations")) rowColor := Config.UString("wtf.mods.clocks.rowcolors.even", "lightblue")
if len(timezones) == 0 { if idx%2 == 0 {
return "" 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{} labels := []string{}
for label, _ := range timezones {
for label, _ := range locs {
labels = append(labels, label) labels = append(labels, label)
} }
sort.Strings(labels) sort.Strings(labels)
tzs := []string{} return labels
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")
} }

View File

@ -4,6 +4,8 @@ import (
"fmt" "fmt"
"strings" "strings"
"unicode/utf8" "unicode/utf8"
"github.com/senorprogrammer/wtf/wtf"
) )
func (widget *Widget) display() { func (widget *Widget) display() {
@ -83,3 +85,16 @@ func (widget *Widget) formatCommits(data []string) string {
func (widget *Widget) formatCommit(line string) string { func (widget *Widget) formatCommit(line string) string {
return fmt.Sprintf(" %s\n", strings.Replace(line, "\"", "", -1)) 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
}

View File

@ -1,8 +1,6 @@
package git package git
import ( import (
"fmt"
"strings"
"time" "time"
"github.com/gdamore/tcell" "github.com/gdamore/tcell"
@ -33,24 +31,13 @@ func NewWidget() *Widget {
/* -------------------- Exported Functions -------------------- */ /* -------------------- 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() { func (widget *Widget) Refresh() {
if widget.Disabled() { if widget.Disabled() {
return return
} }
repoPaths := wtf.ToStrs(Config.UList("wtf.mods.git.repositories")) repoPaths := wtf.ToStrs(Config.UList("wtf.mods.git.repositories"))
widget.Data = widget.Fetch(repoPaths) widget.Data = widget.gitRepos(repoPaths)
widget.display() widget.display()
widget.RefreshedAt = time.Now() widget.RefreshedAt = time.Now()
@ -88,6 +75,17 @@ func (widget *Widget) currentData() *GitRepo {
return widget.Data[widget.Idx] 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 { func (widget *Widget) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey {
switch event.Key() { switch event.Key() {
case tcell.KeyLeft: case tcell.KeyLeft:
@ -102,16 +100,3 @@ func (widget *Widget) keyboardIntercept(event *tcell.EventKey) *tcell.EventKey {
return event 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
}

22
github/display.go Normal file
View File

@ -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)
}

17
github/github_repo.go Normal file
View File

@ -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
}

View File

@ -31,20 +31,7 @@ func (widget *Widget) Refresh() {
return return
} }
client := NewClient() widget.display()
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.RefreshedAt = time.Now() widget.RefreshedAt = time.Now()
} }