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

Merge pull request #525 from wtfutil/20190805-utils-into-utils

20190805 utils into utils
This commit is contained in:
Chris Cummer 2019-08-05 15:15:01 -07:00 committed by GitHub
commit 97ca61ac97
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
68 changed files with 660 additions and 575 deletions

View File

@ -3,6 +3,7 @@ package app
import ( import (
"github.com/olebedev/config" "github.com/olebedev/config"
"github.com/rivo/tview" "github.com/rivo/tview"
"github.com/wtfutil/wtf/utils"
"github.com/wtfutil/wtf/wtf" "github.com/wtfutil/wtf/wtf"
) )
@ -45,8 +46,8 @@ func (display *Display) add(widget wtf.Wtfable) {
} }
func (display *Display) build(widgets []wtf.Wtfable) *tview.Grid { func (display *Display) build(widgets []wtf.Wtfable) *tview.Grid {
cols := wtf.ToInts(display.config.UList("wtf.grid.columns")) cols := utils.ToInts(display.config.UList("wtf.grid.columns"))
rows := wtf.ToInts(display.config.UList("wtf.grid.rows")) rows := utils.ToInts(display.config.UList("wtf.grid.rows"))
display.Grid.SetColumns(cols...) display.Grid.SetColumns(cols...)
display.Grid.SetRows(rows...) display.Grid.SetRows(rows...)

View File

@ -18,7 +18,7 @@ import (
"github.com/wtfutil/wtf/app" "github.com/wtfutil/wtf/app"
"github.com/wtfutil/wtf/cfg" "github.com/wtfutil/wtf/cfg"
"github.com/wtfutil/wtf/flags" "github.com/wtfutil/wtf/flags"
"github.com/wtfutil/wtf/wtf" "github.com/wtfutil/wtf/utils"
) )
var tviewApp *tview.Application var tviewApp *tview.Application
@ -62,7 +62,7 @@ func main() {
defer profile.Start(profile.MemProfile).Stop() defer profile.Start(profile.MemProfile).Stop()
} }
wtf.Init(config.UString("wtf.openFileUtil", "open")) utils.Init(config.UString("wtf.openFileUtil", "open"))
setTerm(config) setTerm(config)

View File

@ -5,6 +5,7 @@ import (
"time" "time"
"github.com/rivo/tview" "github.com/rivo/tview"
"github.com/wtfutil/wtf/utils"
"github.com/wtfutil/wtf/view" "github.com/wtfutil/wtf/view"
"github.com/wtfutil/wtf/wtf" "github.com/wtfutil/wtf/wtf"
) )
@ -49,7 +50,7 @@ func (widget *Widget) Refresh() {
func (widget *Widget) contentFrom(items []Item) string { func (widget *Widget) contentFrom(items []Item) string {
if len(items) == 0 { if len(items) == 0 {
return fmt.Sprintf("\n\n\n\n\n\n\n\n%s", wtf.CenterText("[grey]no one[white]", 50)) return fmt.Sprintf("\n\n\n\n\n\n\n\n%s", utils.CenterText("[grey]no one[white]", 50))
} }
str := "" str := ""

View File

@ -3,7 +3,7 @@ package clocks
import ( import (
"github.com/olebedev/config" "github.com/olebedev/config"
"github.com/wtfutil/wtf/cfg" "github.com/wtfutil/wtf/cfg"
"github.com/wtfutil/wtf/wtf" "github.com/wtfutil/wtf/utils"
) )
const defaultTitle = "Clocks" const defaultTitle = "Clocks"
@ -30,8 +30,8 @@ func NewSettingsFromYAML(name string, ymlConfig *config.Config, globalConfig *co
settings := Settings{ settings := Settings{
common: cfg.NewCommonSettingsFromModule(name, defaultTitle, ymlConfig, globalConfig), common: cfg.NewCommonSettingsFromModule(name, defaultTitle, ymlConfig, globalConfig),
dateFormat: ymlConfig.UString("dateFormat", wtf.SimpleDateFormat), dateFormat: ymlConfig.UString("dateFormat", utils.SimpleDateFormat),
timeFormat: ymlConfig.UString("timeFormat", wtf.SimpleTimeFormat), timeFormat: ymlConfig.UString("timeFormat", utils.SimpleTimeFormat),
locations: ymlConfig.UMap("locations"), locations: ymlConfig.UMap("locations"),
sort: ymlConfig.UString("sort"), sort: ymlConfig.UString("sort"),
} }

View File

@ -3,7 +3,7 @@ package cmdrunner
import ( import (
"github.com/olebedev/config" "github.com/olebedev/config"
"github.com/wtfutil/wtf/cfg" "github.com/wtfutil/wtf/cfg"
"github.com/wtfutil/wtf/wtf" "github.com/wtfutil/wtf/utils"
) )
const defaultTitle = "CmdRunner" const defaultTitle = "CmdRunner"
@ -20,7 +20,7 @@ func NewSettingsFromYAML(name string, moduleConfig *config.Config, globalConfig
settings := Settings{ settings := Settings{
common: cfg.NewCommonSettingsFromModule(name, defaultTitle, moduleConfig, globalConfig), common: cfg.NewCommonSettingsFromModule(name, defaultTitle, moduleConfig, globalConfig),
args: wtf.ToStrs(moduleConfig.UList("args")), args: utils.ToStrs(moduleConfig.UList("args")),
cmd: moduleConfig.UString("cmd"), cmd: moduleConfig.UString("cmd"),
} }

View File

@ -6,8 +6,8 @@ import (
"strings" "strings"
"github.com/rivo/tview" "github.com/rivo/tview"
"github.com/wtfutil/wtf/utils"
"github.com/wtfutil/wtf/view" "github.com/wtfutil/wtf/view"
"github.com/wtfutil/wtf/wtf"
) )
type Widget struct { type Widget struct {
@ -61,5 +61,5 @@ func (widget *Widget) String() string {
func (widget *Widget) execute() string { func (widget *Widget) execute() string {
cmd := exec.Command(widget.cmd, widget.args...) cmd := exec.Command(widget.cmd, widget.args...)
return wtf.ExecuteCommand(cmd) return utils.ExecuteCommand(cmd)
} }

View File

@ -1,7 +1,7 @@
package datadog package datadog
import ( import (
"github.com/wtfutil/wtf/wtf" "github.com/wtfutil/wtf/utils"
datadog "github.com/zorkian/go-datadog-api" datadog "github.com/zorkian/go-datadog-api"
) )
@ -12,7 +12,7 @@ func (widget *Widget) Monitors() ([]datadog.Monitor, error) {
widget.settings.applicationKey, widget.settings.applicationKey,
) )
tags := wtf.ToStrs(widget.settings.tags) tags := utils.ToStrs(widget.settings.tags)
monitors, err := client.GetMonitorsByTags(tags) monitors, err := client.GetMonitorsByTags(tags)
if err != nil { if err != nil {

View File

@ -4,8 +4,8 @@ import (
"fmt" "fmt"
"github.com/rivo/tview" "github.com/rivo/tview"
"github.com/wtfutil/wtf/utils"
"github.com/wtfutil/wtf/view" "github.com/wtfutil/wtf/view"
"github.com/wtfutil/wtf/wtf"
datadog "github.com/zorkian/go-datadog-api" datadog "github.com/zorkian/go-datadog-api"
) )
@ -84,7 +84,7 @@ func (widget *Widget) contentFrom(triggeredMonitors []datadog.Monitor) string {
*triggeredMonitor.Name, *triggeredMonitor.Name,
widget.RowColor(idx), widget.RowColor(idx),
) )
str += wtf.HighlightableHelper(widget.View, row, idx, len(*triggeredMonitor.Name)) str += utils.HighlightableHelper(widget.View, row, idx, len(*triggeredMonitor.Name))
} }
} else { } else {
str += fmt.Sprintf( str += fmt.Sprintf(
@ -101,6 +101,6 @@ func (widget *Widget) openItem() {
sel := widget.GetSelected() sel := widget.GetSelected()
if sel >= 0 && widget.monitors != nil && sel < len(widget.monitors) { if sel >= 0 && widget.monitors != nil && sel < len(widget.monitors) {
item := &widget.monitors[sel] item := &widget.monitors[sel]
wtf.OpenFile(fmt.Sprintf("https://app.datadoghq.com/monitors/%d?q=*", *item.Id)) utils.OpenFile(fmt.Sprintf("https://app.datadoghq.com/monitors/%d?q=*", *item.Id))
} }
} }

View File

@ -3,7 +3,7 @@ package feedreader
import ( import (
"github.com/olebedev/config" "github.com/olebedev/config"
"github.com/wtfutil/wtf/cfg" "github.com/wtfutil/wtf/cfg"
"github.com/wtfutil/wtf/wtf" "github.com/wtfutil/wtf/utils"
) )
const ( const (
@ -23,7 +23,7 @@ func NewSettingsFromYAML(name string, ymlConfig *config.Config, globalConfig *co
settings := &Settings{ settings := &Settings{
common: cfg.NewCommonSettingsFromModule(name, defaultTitle, ymlConfig, globalConfig), common: cfg.NewCommonSettingsFromModule(name, defaultTitle, ymlConfig, globalConfig),
feeds: wtf.ToStrs(ymlConfig.UList("feeds")), feeds: utils.ToStrs(ymlConfig.UList("feeds")),
feedLimit: ymlConfig.UInt("feedLimit", -1), feedLimit: ymlConfig.UInt("feedLimit", -1),
} }

View File

@ -7,8 +7,8 @@ import (
"github.com/mmcdole/gofeed" "github.com/mmcdole/gofeed"
"github.com/rivo/tview" "github.com/rivo/tview"
"github.com/wtfutil/wtf/utils"
"github.com/wtfutil/wtf/view" "github.com/wtfutil/wtf/view"
"github.com/wtfutil/wtf/wtf"
) )
const ( const (
@ -144,7 +144,7 @@ func (widget *Widget) contentFrom(data []*FeedItem) string {
feedItem.item.Title, feedItem.item.Title,
) )
str += wtf.HighlightableHelper(widget.View, row, idx, len(feedItem.item.Title)) str += utils.HighlightableHelper(widget.View, row, idx, len(feedItem.item.Title))
} }
return str return str
@ -169,6 +169,6 @@ func (widget *Widget) openStory() {
story := widget.stories[sel] story := widget.stories[sel]
story.viewed = true story.viewed = true
wtf.OpenFile(story.item.Link) utils.OpenFile(story.item.Link)
} }
} }

View File

@ -3,7 +3,7 @@ package gcal
import ( import (
"time" "time"
"github.com/wtfutil/wtf/wtf" "github.com/wtfutil/wtf/utils"
"google.golang.org/api/calendar/v3" "google.golang.org/api/calendar/v3"
) )
@ -100,9 +100,9 @@ func (calEvent *CalEvent) Start() time.Time {
func (calEvent *CalEvent) Timestamp() string { func (calEvent *CalEvent) Timestamp() string {
if calEvent.AllDay() { if calEvent.AllDay() {
startTime, _ := time.ParseInLocation("2006-01-02", calEvent.event.Start.Date, time.Local) startTime, _ := time.ParseInLocation("2006-01-02", calEvent.event.Start.Date, time.Local)
return startTime.Format(wtf.FriendlyDateFormat) return startTime.Format(utils.FriendlyDateFormat)
} }
startTime, _ := time.Parse(time.RFC3339, calEvent.event.Start.DateTime) startTime, _ := time.Parse(time.RFC3339, calEvent.event.Start.DateTime)
return startTime.Format(wtf.MinimumTimeFormat) return startTime.Format(utils.MinimumTimeFormat)
} }

View File

@ -6,7 +6,7 @@ import (
"strings" "strings"
"time" "time"
"github.com/wtfutil/wtf/wtf" "github.com/wtfutil/wtf/utils"
) )
func (widget *Widget) sortedEvents() ([]*CalEvent, []*CalEvent) { func (widget *Widget) sortedEvents() ([]*CalEvent, []*CalEvent) {
@ -98,7 +98,7 @@ func (widget *Widget) dayDivider(event, prevEvent *CalEvent) string {
return fmt.Sprintf("[%s::b]", return fmt.Sprintf("[%s::b]",
widget.settings.colors.day) + widget.settings.colors.day) +
event.Start().Format(wtf.FullDateFormat) + event.Start().Format(utils.FullDateFormat) +
"\n" "\n"
} }
@ -169,7 +169,7 @@ func (widget *Widget) titleColor(calEvent *CalEvent) string {
color := widget.settings.colors.title color := widget.settings.colors.title
for _, untypedArr := range widget.settings.highlights { for _, untypedArr := range widget.settings.highlights {
highlightElements := wtf.ToStrs(untypedArr.([]interface{})) highlightElements := utils.ToStrs(untypedArr.([]interface{}))
match, _ := regexp.MatchString( match, _ := regexp.MatchString(
strings.ToLower(highlightElements[0]), strings.ToLower(highlightElements[0]),

View File

@ -8,8 +8,8 @@ import (
glb "github.com/andygrunwald/go-gerrit" glb "github.com/andygrunwald/go-gerrit"
"github.com/rivo/tview" "github.com/rivo/tview"
"github.com/wtfutil/wtf/utils"
"github.com/wtfutil/wtf/view" "github.com/wtfutil/wtf/view"
"github.com/wtfutil/wtf/wtf"
) )
type Widget struct { type Widget struct {
@ -146,7 +146,7 @@ func (widget *Widget) openReview() {
} else { } else {
change = project.OutgoingReviews[sel-len(project.IncomingReviews)] change = project.OutgoingReviews[sel-len(project.IncomingReviews)]
} }
wtf.OpenFile(fmt.Sprintf("%s/%s/%d", widget.settings.domain, "#/c", change.Number)) utils.OpenFile(fmt.Sprintf("%s/%s/%d", widget.settings.domain, "#/c", change.Number))
} }
} }

View File

@ -5,7 +5,7 @@ import (
"os/exec" "os/exec"
"strings" "strings"
"github.com/wtfutil/wtf/wtf" "github.com/wtfutil/wtf/utils"
) )
type GitRepo struct { type GitRepo struct {
@ -33,7 +33,7 @@ func (repo *GitRepo) branch() string {
arg := []string{repo.gitDir(), repo.workTree(), "rev-parse", "--abbrev-ref", "HEAD"} arg := []string{repo.gitDir(), repo.workTree(), "rev-parse", "--abbrev-ref", "HEAD"}
cmd := exec.Command("git", arg...) cmd := exec.Command("git", arg...)
str := wtf.ExecuteCommand(cmd) str := utils.ExecuteCommand(cmd)
return str return str
} }
@ -42,7 +42,7 @@ func (repo *GitRepo) changedFiles() []string {
arg := []string{repo.gitDir(), repo.workTree(), "status", "--porcelain"} arg := []string{repo.gitDir(), repo.workTree(), "status", "--porcelain"}
cmd := exec.Command("git", arg...) cmd := exec.Command("git", arg...)
str := wtf.ExecuteCommand(cmd) str := utils.ExecuteCommand(cmd)
data := strings.Split(str, "\n") data := strings.Split(str, "\n")
@ -57,7 +57,7 @@ func (repo *GitRepo) commits(commitCount int, commitFormat, dateFormat string) [
arg := []string{repo.gitDir(), repo.workTree(), "log", dateStr, numStr, commitStr} arg := []string{repo.gitDir(), repo.workTree(), "log", dateStr, numStr, commitStr}
cmd := exec.Command("git", arg...) cmd := exec.Command("git", arg...)
str := wtf.ExecuteCommand(cmd) str := utils.ExecuteCommand(cmd)
data := strings.Split(str, "\n") data := strings.Split(str, "\n")
@ -67,21 +67,21 @@ func (repo *GitRepo) commits(commitCount int, commitFormat, dateFormat string) [
func (repo *GitRepo) repository() string { func (repo *GitRepo) repository() string {
arg := []string{repo.gitDir(), repo.workTree(), "rev-parse", "--show-toplevel"} arg := []string{repo.gitDir(), repo.workTree(), "rev-parse", "--show-toplevel"}
cmd := exec.Command("git", arg...) cmd := exec.Command("git", arg...)
str := wtf.ExecuteCommand(cmd) str := utils.ExecuteCommand(cmd)
return str return str
} }
func (repo *GitRepo) pull() string { func (repo *GitRepo) pull() string {
arg := []string{repo.gitDir(), repo.workTree(), "pull"} arg := []string{repo.gitDir(), repo.workTree(), "pull"}
cmd := exec.Command("git", arg...) cmd := exec.Command("git", arg...)
str := wtf.ExecuteCommand(cmd) str := utils.ExecuteCommand(cmd)
return str return str
} }
func (repo *GitRepo) checkout(branch string) string { func (repo *GitRepo) checkout(branch string) string {
arg := []string{repo.gitDir(), repo.workTree(), "checkout", branch} arg := []string{repo.gitDir(), repo.workTree(), "checkout", branch}
cmd := exec.Command("git", arg...) cmd := exec.Command("git", arg...)
str := wtf.ExecuteCommand(cmd) str := utils.ExecuteCommand(cmd)
return str return str
} }

View File

@ -8,8 +8,8 @@ import (
"github.com/gdamore/tcell" "github.com/gdamore/tcell"
"github.com/rivo/tview" "github.com/rivo/tview"
"github.com/wtfutil/wtf/utils"
"github.com/wtfutil/wtf/view" "github.com/wtfutil/wtf/view"
"github.com/wtfutil/wtf/wtf"
) )
const offscreen = -1000 const offscreen = -1000
@ -76,7 +76,7 @@ func (widget *Widget) Pull() {
} }
func (widget *Widget) Refresh() { func (widget *Widget) Refresh() {
repoPaths := wtf.ToStrs(widget.settings.repositories) repoPaths := utils.ToStrs(widget.settings.repositories)
widget.GitRepos = widget.gitRepos(repoPaths) widget.GitRepos = widget.gitRepos(repoPaths)
sort.Slice(widget.GitRepos, func(i, j int) bool { sort.Slice(widget.GitRepos, func(i, j int) bool {

View File

@ -6,7 +6,7 @@ import (
"net/http" "net/http"
ghb "github.com/google/go-github/v26/github" ghb "github.com/google/go-github/v26/github"
"github.com/wtfutil/wtf/wtf" "github.com/wtfutil/wtf/utils"
"golang.org/x/oauth2" "golang.org/x/oauth2"
) )
@ -35,7 +35,7 @@ func NewGithubRepo(name, owner, apiKey, baseURL, uploadURL string) *GithubRepo {
} }
func (repo *GithubRepo) Open() { func (repo *GithubRepo) Open() {
wtf.OpenFile(*repo.RemoteRepo.HTMLURL) utils.OpenFile(*repo.RemoteRepo.HTMLURL)
} }
// Refresh reloads the github data via the Github API // Refresh reloads the github data via the Github API

View File

@ -6,7 +6,7 @@ import (
"github.com/olebedev/config" "github.com/olebedev/config"
"github.com/wtfutil/wtf/cfg" "github.com/wtfutil/wtf/cfg"
"github.com/wtfutil/wtf/wtf" "github.com/wtfutil/wtf/utils"
) )
const defaultTitle = "GitHub" const defaultTitle = "GitHub"
@ -57,7 +57,7 @@ func parseRepositories(ymlConfig *config.Config) []string {
return result return result
} }
result = wtf.ToStrs(ymlConfig.UList("repositories")) result = utils.ToStrs(ymlConfig.UList("repositories"))
return result return result
} }

View File

@ -4,8 +4,8 @@ import (
"fmt" "fmt"
"github.com/rivo/tview" "github.com/rivo/tview"
"github.com/wtfutil/wtf/utils"
"github.com/wtfutil/wtf/view" "github.com/wtfutil/wtf/view"
"github.com/wtfutil/wtf/wtf"
) )
// A Widget represents a Gitter widget // A Widget represents a Gitter widget
@ -94,7 +94,7 @@ func (widget *Widget) contentFrom(messages []Message) string {
message.Sent.Format("Jan 02, 15:04 MST"), message.Sent.Format("Jan 02, 15:04 MST"),
) )
str += wtf.HighlightableHelper(widget.View, row, idx, len(message.Text)) str += utils.HighlightableHelper(widget.View, row, idx, len(message.Text))
} }
return str return str
@ -104,6 +104,6 @@ func (widget *Widget) openMessage() {
sel := widget.GetSelected() sel := widget.GetSelected()
if sel >= 0 && widget.messages != nil && sel < len(widget.messages) { if sel >= 0 && widget.messages != nil && sel < len(widget.messages) {
message := &widget.messages[sel] message := &widget.messages[sel]
wtf.OpenFile(message.Text) utils.OpenFile(message.Text)
} }
} }

View File

@ -19,7 +19,6 @@ import (
"strings" "strings"
"github.com/wtfutil/wtf/utils" "github.com/wtfutil/wtf/utils"
"github.com/wtfutil/wtf/wtf"
"golang.org/x/oauth2" "golang.org/x/oauth2"
"golang.org/x/oauth2/google" "golang.org/x/oauth2/google"
sheets "google.golang.org/api/sheets/v4" sheets "google.golang.org/api/sheets/v4"
@ -52,7 +51,7 @@ func (widget *Widget) Fetch() ([]*sheets.ValueRange, error) {
return nil, err return nil, err
} }
cells := wtf.ToStrs(widget.settings.cellAddresses) cells := utils.ToStrs(widget.settings.cellAddresses)
addresses := strings.Join(cells[:], ";") addresses := strings.Join(cells[:], ";")
responses := make([]*sheets.ValueRange, len(cells)) responses := make([]*sheets.ValueRange, len(cells))

View File

@ -4,8 +4,8 @@ import (
"fmt" "fmt"
"github.com/rivo/tview" "github.com/rivo/tview"
"github.com/wtfutil/wtf/utils"
"github.com/wtfutil/wtf/view" "github.com/wtfutil/wtf/view"
"github.com/wtfutil/wtf/wtf"
sheets "google.golang.org/api/sheets/v4" sheets "google.golang.org/api/sheets/v4"
) )
@ -42,7 +42,7 @@ func (widget *Widget) contentFrom(valueRanges []*sheets.ValueRange) string {
res := "" res := ""
cells := wtf.ToStrs(widget.settings.cellNames) cells := utils.ToStrs(widget.settings.cellNames)
for i := 0; i < len(valueRanges); i++ { for i := 0; i < len(valueRanges); i++ {
res += fmt.Sprintf("%s\t[%s]%s\n", cells[i], widget.settings.colors.values, valueRanges[i].Values[0][0]) res += fmt.Sprintf("%s\t[%s]%s\n", cells[i], widget.settings.colors.values, valueRanges[i].Values[0][0])
} }

View File

@ -6,8 +6,8 @@ import (
"strings" "strings"
"github.com/rivo/tview" "github.com/rivo/tview"
"github.com/wtfutil/wtf/utils"
"github.com/wtfutil/wtf/view" "github.com/wtfutil/wtf/view"
"github.com/wtfutil/wtf/wtf"
) )
type Widget struct { type Widget struct {
@ -91,7 +91,7 @@ func (widget *Widget) contentFrom(stories []Story) string {
strings.TrimPrefix(u.Host, "www."), strings.TrimPrefix(u.Host, "www."),
) )
str += wtf.HighlightableHelper(widget.View, row, idx, len(story.Title)) str += utils.HighlightableHelper(widget.View, row, idx, len(story.Title))
} }
return str return str
@ -101,7 +101,7 @@ func (widget *Widget) openStory() {
sel := widget.GetSelected() sel := widget.GetSelected()
if sel >= 0 && widget.stories != nil && sel < len(widget.stories) { if sel >= 0 && widget.stories != nil && sel < len(widget.stories) {
story := &widget.stories[sel] story := &widget.stories[sel]
wtf.OpenFile(story.URL) utils.OpenFile(story.URL)
} }
} }
@ -109,6 +109,6 @@ func (widget *Widget) openComments() {
sel := widget.GetSelected() sel := widget.GetSelected()
if sel >= 0 && widget.stories != nil && sel < len(widget.stories) { if sel >= 0 && widget.stories != nil && sel < len(widget.stories) {
story := &widget.stories[sel] story := &widget.stories[sel]
wtf.OpenFile(fmt.Sprintf("https://news.ycombinator.com/item?id=%d", story.ID)) utils.OpenFile(fmt.Sprintf("https://news.ycombinator.com/item?id=%d", story.ID))
} }
} }

View File

@ -5,7 +5,7 @@ import (
"github.com/olebedev/config" "github.com/olebedev/config"
"github.com/wtfutil/wtf/cfg" "github.com/wtfutil/wtf/cfg"
"github.com/wtfutil/wtf/wtf" "github.com/wtfutil/wtf/utils"
) )
const ( const (
@ -34,7 +34,7 @@ func NewSettingsFromYAML(name string, ymlConfig *config.Config, globalConfig *co
common: cfg.NewCommonSettingsFromModule(name, defaultTitle, ymlConfig, globalConfig), common: cfg.NewCommonSettingsFromModule(name, defaultTitle, ymlConfig, globalConfig),
apiKey: ymlConfig.UString("apiKey", ""), apiKey: ymlConfig.UString("apiKey", ""),
accounts: wtf.ToStrs(ymlConfig.UList("accounts")), accounts: utils.ToStrs(ymlConfig.UList("accounts")),
since: ymlConfig.UString("since", ""), since: ymlConfig.UString("since", ""),
} }

View File

@ -5,8 +5,8 @@ import (
"regexp" "regexp"
"github.com/rivo/tview" "github.com/rivo/tview"
"github.com/wtfutil/wtf/utils"
"github.com/wtfutil/wtf/view" "github.com/wtfutil/wtf/view"
"github.com/wtfutil/wtf/wtf"
) )
type Widget struct { type Widget struct {
@ -83,7 +83,7 @@ func (widget *Widget) contentFrom(view *View) string {
job.Name, job.Name,
) )
str += wtf.HighlightableHelper(widget.View, row, idx, len(job.Name)) str += utils.HighlightableHelper(widget.View, row, idx, len(job.Name))
} }
} }
@ -106,6 +106,6 @@ func (widget *Widget) openJob() {
sel := widget.GetSelected() sel := widget.GetSelected()
if sel >= 0 && widget.view != nil && sel < len(widget.view.Jobs) { if sel >= 0 && widget.view != nil && sel < len(widget.view.Jobs) {
job := &widget.view.Jobs[sel] job := &widget.view.Jobs[sel]
wtf.OpenFile(job.Url) utils.OpenFile(job.Url)
} }
} }

View File

@ -4,8 +4,8 @@ import (
"fmt" "fmt"
"github.com/rivo/tview" "github.com/rivo/tview"
"github.com/wtfutil/wtf/utils"
"github.com/wtfutil/wtf/view" "github.com/wtfutil/wtf/view"
"github.com/wtfutil/wtf/wtf"
) )
type Widget struct { type Widget struct {
@ -68,7 +68,7 @@ func (widget *Widget) openItem() {
sel := widget.GetSelected() sel := widget.GetSelected()
if sel >= 0 && widget.result != nil && sel < len(widget.result.Issues) { if sel >= 0 && widget.result != nil && sel < len(widget.result.Issues) {
issue := &widget.result.Issues[sel] issue := &widget.result.Issues[sel]
wtf.OpenFile(widget.settings.domain + "/browse/" + issue.Key) utils.OpenFile(widget.settings.domain + "/browse/" + issue.Key)
} }
} }
@ -87,7 +87,7 @@ func (widget *Widget) contentFrom(searchResult *SearchResult) string {
issue.IssueFields.Summary, issue.IssueFields.Summary,
) )
str += wtf.HighlightableHelper(widget.View, row, idx, len(issue.IssueFields.Summary)) str += utils.HighlightableHelper(widget.View, row, idx, len(issue.IssueFields.Summary))
} }
return str return str

View File

@ -7,7 +7,7 @@ import (
"path" "path"
"strings" "strings"
"github.com/wtfutil/wtf/wtf" "github.com/wtfutil/wtf/utils"
) )
type MercurialRepo struct { type MercurialRepo struct {
@ -37,7 +37,7 @@ func (repo *MercurialRepo) branch() string {
arg := []string{"branch", repo.repoPath()} arg := []string{"branch", repo.repoPath()}
cmd := exec.Command("hg", arg...) cmd := exec.Command("hg", arg...)
str := wtf.ExecuteCommand(cmd) str := utils.ExecuteCommand(cmd)
return str return str
} }
@ -54,7 +54,7 @@ func (repo *MercurialRepo) changedFiles() []string {
arg := []string{"status", repo.repoPath()} arg := []string{"status", repo.repoPath()}
cmd := exec.Command("hg", arg...) cmd := exec.Command("hg", arg...)
str := wtf.ExecuteCommand(cmd) str := utils.ExecuteCommand(cmd)
data := strings.Split(str, "\n") data := strings.Split(str, "\n")
@ -68,7 +68,7 @@ func (repo *MercurialRepo) commits(commitCount int, commitFormat string) []strin
arg := []string{"log", repo.repoPath(), numStr, commitStr} arg := []string{"log", repo.repoPath(), numStr, commitStr}
cmd := exec.Command("hg", arg...) cmd := exec.Command("hg", arg...)
str := wtf.ExecuteCommand(cmd) str := utils.ExecuteCommand(cmd)
data := strings.Split(str, "\n") data := strings.Split(str, "\n")
@ -78,14 +78,14 @@ func (repo *MercurialRepo) commits(commitCount int, commitFormat string) []strin
func (repo *MercurialRepo) pull() string { func (repo *MercurialRepo) pull() string {
arg := []string{"pull", repo.repoPath()} arg := []string{"pull", repo.repoPath()}
cmd := exec.Command("hg", arg...) cmd := exec.Command("hg", arg...)
str := wtf.ExecuteCommand(cmd) str := utils.ExecuteCommand(cmd)
return str return str
} }
func (repo *MercurialRepo) checkout(branch string) string { func (repo *MercurialRepo) checkout(branch string) string {
arg := []string{"checkout", repo.repoPath(), branch} arg := []string{"checkout", repo.repoPath(), branch}
cmd := exec.Command("hg", arg...) cmd := exec.Command("hg", arg...)
str := wtf.ExecuteCommand(cmd) str := utils.ExecuteCommand(cmd)
return str return str
} }

View File

@ -3,8 +3,8 @@ package mercurial
import ( import (
"github.com/gdamore/tcell" "github.com/gdamore/tcell"
"github.com/rivo/tview" "github.com/rivo/tview"
"github.com/wtfutil/wtf/utils"
"github.com/wtfutil/wtf/view" "github.com/wtfutil/wtf/view"
"github.com/wtfutil/wtf/wtf"
) )
const offscreen = -1000 const offscreen = -1000
@ -73,7 +73,7 @@ func (widget *Widget) Pull() {
} }
func (widget *Widget) Refresh() { func (widget *Widget) Refresh() {
repoPaths := wtf.ToStrs(widget.settings.repositories) repoPaths := utils.ToStrs(widget.settings.repositories)
widget.Data = widget.mercurialRepos(repoPaths) widget.Data = widget.mercurialRepos(repoPaths)

View File

@ -9,8 +9,8 @@ import (
"time" "time"
"github.com/rivo/tview" "github.com/rivo/tview"
"github.com/wtfutil/wtf/utils"
"github.com/wtfutil/wtf/view" "github.com/wtfutil/wtf/view"
"github.com/wtfutil/wtf/wtf"
) )
// A Widget represents an NBA Score widget // A Widget represents an NBA Score widget
@ -79,7 +79,7 @@ func (widget *Widget) nbascore() string {
result := map[string]interface{}{} result := map[string]interface{}{}
json.Unmarshal(contents, &result) json.Unmarshal(contents, &result)
allGame := "" // store result in allgame allGame := "" // store result in allgame
allGame += (" " + "[red]" + (cur.Format(wtf.FriendlyDateFormat) + "\n\n") + "[white]") allGame += (" " + "[red]" + (cur.Format(utils.FriendlyDateFormat) + "\n\n") + "[white]")
for _, game := range result["games"].([]interface{}) { for _, game := range result["games"].([]interface{}) {
vTeam, hTeam, vScore, hScore := "", "", "", "" vTeam, hTeam, vScore, hScore := "", "", "", ""
quarter := 0. quarter := 0.

View File

@ -4,6 +4,7 @@ import (
"fmt" "fmt"
"github.com/rivo/tview" "github.com/rivo/tview"
"github.com/wtfutil/wtf/utils"
"github.com/wtfutil/wtf/view" "github.com/wtfutil/wtf/view"
"github.com/wtfutil/wtf/wtf" "github.com/wtfutil/wtf/wtf"
nr "github.com/yfronto/newrelic" nr "github.com/yfronto/newrelic"
@ -63,7 +64,7 @@ func (widget *Widget) contentFrom(deploys []nr.ApplicationDeployment) string {
revisions := []string{} revisions := []string{}
for _, deploy := range deploys { for _, deploy := range deploys {
if (deploy.Revision != "") && wtf.Exclude(revisions, deploy.Revision) { if (deploy.Revision != "") && utils.DoesNotInclude(revisions, deploy.Revision) {
lineColor := "white" lineColor := "white"
if wtf.IsToday(deploy.Timestamp) { if wtf.IsToday(deploy.Timestamp) {
lineColor = "lightblue" lineColor = "lightblue"
@ -79,7 +80,7 @@ func (widget *Widget) contentFrom(deploys []nr.ApplicationDeployment) string {
deploy.Revision[0:revLen], deploy.Revision[0:revLen],
lineColor, lineColor,
deploy.Timestamp.Format("Jan 02 15:04 MST"), deploy.Timestamp.Format("Jan 02 15:04 MST"),
wtf.NameFromEmail(deploy.User), utils.NameFromEmail(deploy.User),
) )
revisions = append(revisions, deploy.Revision) revisions = append(revisions, deploy.Revision)

View File

@ -5,8 +5,8 @@ import (
"strings" "strings"
"github.com/rivo/tview" "github.com/rivo/tview"
"github.com/wtfutil/wtf/utils"
"github.com/wtfutil/wtf/view" "github.com/wtfutil/wtf/view"
"github.com/wtfutil/wtf/wtf"
) )
type Widget struct { type Widget struct {
@ -59,7 +59,7 @@ func (widget *Widget) contentFrom(onCallResponses []*OnCallResponse) string {
if len(data.OnCallData.Recipients) == 0 { if len(data.OnCallData.Recipients) == 0 {
msg = " [gray]no one[white]\n\n" msg = " [gray]no one[white]\n\n"
} else { } else {
msg = fmt.Sprintf(" %s\n\n", strings.Join(wtf.NamesFromEmails(data.OnCallData.Recipients), ", ")) msg = fmt.Sprintf(" %s\n\n", strings.Join(utils.NamesFromEmails(data.OnCallData.Recipients), ", "))
} }
str += widget.cleanScheduleName(data.OnCallData.Parent.Name) str += widget.cleanScheduleName(data.OnCallData.Parent.Name)

View File

@ -6,8 +6,8 @@ import (
"github.com/PagerDuty/go-pagerduty" "github.com/PagerDuty/go-pagerduty"
"github.com/rivo/tview" "github.com/rivo/tview"
"github.com/wtfutil/wtf/utils"
"github.com/wtfutil/wtf/view" "github.com/wtfutil/wtf/view"
"github.com/wtfutil/wtf/wtf"
) )
type Widget struct { type Widget struct {
@ -40,7 +40,7 @@ func (widget *Widget) Refresh() {
} }
if widget.settings.showSchedules { if widget.settings.showSchedules {
scheduleIDs := wtf.ToStrs(widget.settings.scheduleIDs) scheduleIDs := utils.ToStrs(widget.settings.scheduleIDs)
onCalls, err1 = GetOnCalls(widget.settings.apiKey, scheduleIDs) onCalls, err1 = GetOnCalls(widget.settings.apiKey, scheduleIDs)
} }

View File

@ -9,7 +9,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"github.com/wtfutil/wtf/wtf" "github.com/wtfutil/wtf/utils"
) )
const TimeRegExp = "^(?:\\d|[01]\\d|2[0-3]):[0-5]\\d" const TimeRegExp = "^(?:\\d|[01]\\d|2[0-3]):[0-5]\\d"
@ -47,7 +47,7 @@ func (battery *Battery) String() string {
func (battery *Battery) execute() string { func (battery *Battery) execute() string {
cmd := exec.Command(battery.cmd, battery.args...) cmd := exec.Command(battery.cmd, battery.args...)
return wtf.ExecuteCommand(cmd) return utils.ExecuteCommand(cmd)
} }
func (battery *Battery) parse(data string) string { func (battery *Battery) parse(data string) string {

View File

@ -8,7 +8,7 @@ import (
"strconv" "strconv"
"strings" "strings"
"github.com/wtfutil/wtf/wtf" "github.com/wtfutil/wtf/utils"
) )
var batteryState string var batteryState string
@ -41,7 +41,7 @@ func (battery *Battery) String() string {
func (battery *Battery) execute() string { func (battery *Battery) execute() string {
cmd := exec.Command("upower", "-e") cmd := exec.Command("upower", "-e")
lines := strings.Split(wtf.ExecuteCommand(cmd), "\n") lines := strings.Split(utils.ExecuteCommand(cmd), "\n")
var target string var target string
for _, l := range lines { for _, l := range lines {
if strings.Contains(l, "/battery") { if strings.Contains(l, "/battery") {
@ -50,7 +50,7 @@ func (battery *Battery) execute() string {
} }
} }
cmd = exec.Command("upower", "-i", target) cmd = exec.Command("upower", "-i", target)
return wtf.ExecuteCommand(cmd) return utils.ExecuteCommand(cmd)
} }
func (battery *Battery) parse(data string) string { func (battery *Battery) parse(data string) string {

View File

@ -7,7 +7,7 @@ import (
"regexp" "regexp"
"strings" "strings"
"github.com/wtfutil/wtf/wtf" "github.com/wtfutil/wtf/utils"
) )
const SingleQuotesRegExp = "'(.*)'" const SingleQuotesRegExp = "'(.*)'"
@ -16,7 +16,7 @@ const SingleQuotesRegExp = "'(.*)'"
// "AC Power" or "Battery Power" // "AC Power" or "Battery Power"
func powerSource() string { func powerSource() string {
cmd := exec.Command("pmset", []string{"-g", "ps"}...) cmd := exec.Command("pmset", []string{"-g", "ps"}...)
result := wtf.ExecuteCommand(cmd) result := utils.ExecuteCommand(cmd)
r, _ := regexp.Compile(SingleQuotesRegExp) r, _ := regexp.Compile(SingleQuotesRegExp)

View File

@ -4,8 +4,8 @@ import (
"fmt" "fmt"
"github.com/rivo/tview" "github.com/rivo/tview"
"github.com/wtfutil/wtf/utils"
"github.com/wtfutil/wtf/view" "github.com/wtfutil/wtf/view"
"github.com/wtfutil/wtf/wtf"
) )
// A Widget represents a Rollbar widget // A Widget represents a Rollbar widget
@ -91,7 +91,7 @@ func (widget *Widget) contentFrom(result *Result) string {
"blue", "blue",
item.Environment, item.Environment,
) )
str += wtf.HighlightableHelper(widget.View, row, idx, len(item.Title)) str += utils.HighlightableHelper(widget.View, row, idx, len(item.Title))
} }
return str return str
@ -124,7 +124,7 @@ func (widget *Widget) openBuild() {
if widget.GetSelected() >= 0 && widget.items != nil && widget.GetSelected() < len(widget.items.Items) { if widget.GetSelected() >= 0 && widget.items != nil && widget.GetSelected() < len(widget.items.Items) {
item := &widget.items.Items[widget.GetSelected()] item := &widget.items.Items[widget.GetSelected()]
wtf.OpenFile( utils.OpenFile(
fmt.Sprintf( fmt.Sprintf(
"https://rollbar.com/%s/%s/%s/%d", "https://rollbar.com/%s/%s/%s/%d",
widget.settings.projectOwner, widget.settings.projectOwner,

View File

@ -5,7 +5,7 @@ import (
"runtime" "runtime"
"strings" "strings"
"github.com/wtfutil/wtf/wtf" "github.com/wtfutil/wtf/utils"
) )
/* -------------------- Exported Functions -------------------- */ /* -------------------- Exported Functions -------------------- */
@ -28,7 +28,7 @@ func DnsServers() []string {
func dnsLinux() []string { func dnsLinux() []string {
// This may be very Ubuntu specific // This may be very Ubuntu specific
cmd := exec.Command("nmcli", "device", "show") cmd := exec.Command("nmcli", "device", "show")
out := wtf.ExecuteCommand(cmd) out := utils.ExecuteCommand(cmd)
lines := strings.Split(out, "\n") lines := strings.Split(out, "\n")
@ -46,7 +46,7 @@ func dnsLinux() []string {
func dnsMacOS() []string { func dnsMacOS() []string {
cmdString := `scutil --dns | head -n 7 | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'` cmdString := `scutil --dns | head -n 7 | grep -o '[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}\.[0-9]\{1,3\}'`
cmd := exec.Command("sh", "-c", cmdString) cmd := exec.Command("sh", "-c", cmdString)
out := wtf.ExecuteCommand(cmd) out := utils.ExecuteCommand(cmd)
lines := strings.Split(out, "\n") lines := strings.Split(out, "\n")
@ -61,5 +61,5 @@ func dnsWindows() []string {
cmd := exec.Command("powershell.exe", "-NoProfile", "Get-DnsClientServerAddress | Select-Object ExpandProperty ServerAddresses") cmd := exec.Command("powershell.exe", "-NoProfile", "Get-DnsClientServerAddress | Select-Object ExpandProperty ServerAddresses")
return []string{wtf.ExecuteCommand(cmd)} return []string{utils.ExecuteCommand(cmd)}
} }

View File

@ -7,7 +7,7 @@ import (
"runtime" "runtime"
"strings" "strings"
"github.com/wtfutil/wtf/wtf" "github.com/wtfutil/wtf/utils"
) )
const osxFirewallCmd = "/usr/libexec/ApplicationFirewall/socketfilterfw" const osxFirewallCmd = "/usr/libexec/ApplicationFirewall/socketfilterfw"
@ -66,7 +66,7 @@ func firewallStateLinux() string { // might be very Ubuntu specific
func firewallStateMacOS() string { func firewallStateMacOS() string {
cmd := exec.Command(osxFirewallCmd, "--getglobalstate") cmd := exec.Command(osxFirewallCmd, "--getglobalstate")
str := wtf.ExecuteCommand(cmd) str := utils.ExecuteCommand(cmd)
return statusLabel(str) return statusLabel(str)
} }
@ -80,7 +80,7 @@ func firewallStateWindows() string {
cmd := exec.Command("powershell.exe", "-NoProfile", cmd := exec.Command("powershell.exe", "-NoProfile",
"-Command", "& { ((Get-NetFirewallProfile | select name,enabled) | where { $_.Enabled -eq $True } | measure ).Count }") "-Command", "& { ((Get-NetFirewallProfile | select name,enabled) | where { $_.Enabled -eq $True } | measure ).Count }")
fwStat := wtf.ExecuteCommand(cmd) fwStat := utils.ExecuteCommand(cmd)
fwStat = strings.TrimSpace(fwStat) // Always sanitize PowerShell output: "3\r\n" fwStat = strings.TrimSpace(fwStat) // Always sanitize PowerShell output: "3\r\n"
//fmt.Printf("%d %q\n", len(fwStat), fwStat) //fmt.Printf("%d %q\n", len(fwStat), fwStat)
@ -107,7 +107,7 @@ func firewallStealthStateLinux() string {
func firewallStealthStateMacOS() string { func firewallStealthStateMacOS() string {
cmd := exec.Command(osxFirewallCmd, "--getstealthmode") cmd := exec.Command(osxFirewallCmd, "--getstealthmode")
str := wtf.ExecuteCommand(cmd) str := utils.ExecuteCommand(cmd)
return statusLabel(str) return statusLabel(str)
} }
@ -116,7 +116,6 @@ func firewallStealthStateWindows() string {
return "[white]N/A[white]" return "[white]N/A[white]"
} }
func statusLabel(str string) string { func statusLabel(str string) string {
label := "off" label := "off"

View File

@ -7,7 +7,7 @@ import (
"runtime" "runtime"
"strings" "strings"
"github.com/wtfutil/wtf/wtf" "github.com/wtfutil/wtf/utils"
) )
/* -------------------- Exported Functions -------------------- */ /* -------------------- Exported Functions -------------------- */
@ -51,7 +51,7 @@ func cleanUsers(users []string) []string {
func loggedInUsersLinux() []string { func loggedInUsersLinux() []string {
cmd := exec.Command("who", "-us") cmd := exec.Command("who", "-us")
users := wtf.ExecuteCommand(cmd) users := utils.ExecuteCommand(cmd)
cleaned := []string{} cleaned := []string{}
@ -77,7 +77,7 @@ func loggedInUsersLinux() []string {
func loggedInUsersMacOs() []string { func loggedInUsersMacOs() []string {
cmd := exec.Command("dscl", []string{".", "-list", "/Users"}...) cmd := exec.Command("dscl", []string{".", "-list", "/Users"}...)
users := wtf.ExecuteCommand(cmd) users := utils.ExecuteCommand(cmd)
return cleanUsers(strings.Split(users, "\n")) return cleanUsers(strings.Split(users, "\n"))
} }
@ -95,6 +95,6 @@ func loggedInUsersWindows() []string {
cmd := exec.Command("powershell.exe", "-NoProfile", "-Command", "& { [System.Security.Principal.WindowsIdentity]::GetCurrent().Name }") cmd := exec.Command("powershell.exe", "-NoProfile", "-Command", "& { [System.Security.Principal.WindowsIdentity]::GetCurrent().Name }")
// ToDo: Make list for multi-user systems // ToDo: Make list for multi-user systems
users := wtf.ExecuteCommand(cmd) users := utils.ExecuteCommand(cmd)
return cleanUsers(strings.Split(users, "\n")) return cleanUsers(strings.Split(users, "\n"))
} }

View File

@ -5,7 +5,7 @@ import (
"runtime" "runtime"
"strings" "strings"
"github.com/wtfutil/wtf/wtf" "github.com/wtfutil/wtf/utils"
) )
// https://github.com/yelinaung/wifi-name/blob/master/wifi-name.go // https://github.com/yelinaung/wifi-name/blob/master/wifi-name.go
@ -44,9 +44,9 @@ func WifiName() string {
func wifiEncryptionLinux() string { func wifiEncryptionLinux() string {
cmd := exec.Command("nmcli", "-t", "-f", "in-use,security", "dev", "wifi") cmd := exec.Command("nmcli", "-t", "-f", "in-use,security", "dev", "wifi")
out := wtf.ExecuteCommand(cmd) out := utils.ExecuteCommand(cmd)
name := wtf.FindMatch(`\*:(.+)`, out) name := utils.FindMatch(`\*:(.+)`, out)
if len(name) > 0 { if len(name) > 0 {
return name[0][1] return name[0][1]
@ -56,19 +56,19 @@ func wifiEncryptionLinux() string {
} }
func wifiEncryptionMacOS() string { func wifiEncryptionMacOS() string {
name := wtf.FindMatch(`s*auth: (.+)s*`, wifiInfo()) name := utils.FindMatch(`s*auth: (.+)s*`, wifiInfo())
return matchStr(name) return matchStr(name)
} }
func wifiInfo() string { func wifiInfo() string {
cmd := exec.Command(osxWifiCmd, osxWifiArg) cmd := exec.Command(osxWifiCmd, osxWifiArg)
return wtf.ExecuteCommand(cmd) return utils.ExecuteCommand(cmd)
} }
func wifiNameLinux() string { func wifiNameLinux() string {
cmd := exec.Command("nmcli", "-t", "-f", "in-use,ssid", "dev", "wifi") cmd := exec.Command("nmcli", "-t", "-f", "in-use,ssid", "dev", "wifi")
out := wtf.ExecuteCommand(cmd) out := utils.ExecuteCommand(cmd)
name := wtf.FindMatch(`\*:(.+)`, out) name := utils.FindMatch(`\*:(.+)`, out)
if len(name) > 0 { if len(name) > 0 {
return name[0][1] return name[0][1]
} }
@ -76,7 +76,7 @@ func wifiNameLinux() string {
} }
func wifiNameMacOS() string { func wifiNameMacOS() string {
name := wtf.FindMatch(`s*SSID: (.+)s*`, wifiInfo()) name := utils.FindMatch(`s*SSID: (.+)s*`, wifiInfo())
return matchStr(name) return matchStr(name)
} }

View File

@ -5,8 +5,8 @@ import (
"github.com/rivo/tview" "github.com/rivo/tview"
"github.com/sticreations/spotigopher/spotigopher" "github.com/sticreations/spotigopher/spotigopher"
"github.com/wtfutil/wtf/utils"
"github.com/wtfutil/wtf/view" "github.com/wtfutil/wtf/view"
"github.com/wtfutil/wtf/wtf"
) )
// A Widget represents a Spotify widget // A Widget represents a Spotify widget
@ -70,9 +70,9 @@ func (w *Widget) render() {
} }
func (w *Widget) createOutput() string { func (w *Widget) createOutput() string {
output := wtf.CenterText(fmt.Sprintf("[green]Now %v [white]\n", w.Info.Status), w.CommonSettings().Width) output := utils.CenterText(fmt.Sprintf("[green]Now %v [white]\n", w.Info.Status), w.CommonSettings().Width)
output += wtf.CenterText(fmt.Sprintf("[green]Title:[white] %v\n ", w.Info.Title), w.CommonSettings().Width) output += utils.CenterText(fmt.Sprintf("[green]Title:[white] %v\n ", w.Info.Title), w.CommonSettings().Width)
output += wtf.CenterText(fmt.Sprintf("[green]Artist:[white] %v\n", w.Info.Artist), w.CommonSettings().Width) output += utils.CenterText(fmt.Sprintf("[green]Artist:[white] %v\n", w.Info.Artist), w.CommonSettings().Width)
output += wtf.CenterText(fmt.Sprintf("[green]%v:[white] %v\n", w.Info.TrackNumber, w.Info.Album), w.CommonSettings().Width) output += utils.CenterText(fmt.Sprintf("[green]%v:[white] %v\n", w.Info.TrackNumber, w.Info.Album), w.CommonSettings().Width)
return output return output
} }

View File

@ -7,8 +7,8 @@ import (
"os" "os"
"github.com/rivo/tview" "github.com/rivo/tview"
"github.com/wtfutil/wtf/utils"
"github.com/wtfutil/wtf/view" "github.com/wtfutil/wtf/view"
"github.com/wtfutil/wtf/wtf"
"github.com/zmb3/spotify" "github.com/zmb3/spotify"
) )
@ -111,7 +111,7 @@ func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *
// The only solution would be to include quotes in the command, which is why I do here, but it doesn't work. // The only solution would be to include quotes in the command, which is why I do here, but it doesn't work.
// //
// If inconvenient, I'll remove this option and save the URL in a file or some other method. // If inconvenient, I'll remove this option and save the URL in a file or some other method.
wtf.OpenFile(`"` + authURL + `"`) utils.OpenFile(`"` + authURL + `"`)
widget.settings.common.RefreshInterval = 5 widget.settings.common.RefreshInterval = 5
@ -167,14 +167,14 @@ func (widget *Widget) HelpText() string {
} }
func (w *Widget) createOutput() string { func (w *Widget) createOutput() string {
output := wtf.CenterText(fmt.Sprintf("[green]Now %v [white]\n", w.Info.Status), w.CommonSettings().Width) output := utils.CenterText(fmt.Sprintf("[green]Now %v [white]\n", w.Info.Status), w.CommonSettings().Width)
output += wtf.CenterText(fmt.Sprintf("[green]Title:[white] %v\n", w.Info.Title), w.CommonSettings().Width) output += utils.CenterText(fmt.Sprintf("[green]Title:[white] %v\n", w.Info.Title), w.CommonSettings().Width)
output += wtf.CenterText(fmt.Sprintf("[green]Artist:[white] %v\n", w.Info.Artists), w.CommonSettings().Width) output += utils.CenterText(fmt.Sprintf("[green]Artist:[white] %v\n", w.Info.Artists), w.CommonSettings().Width)
output += wtf.CenterText(fmt.Sprintf("[green]Album:[white] %v\n", w.Info.Album), w.CommonSettings().Width) output += utils.CenterText(fmt.Sprintf("[green]Album:[white] %v\n", w.Info.Album), w.CommonSettings().Width)
if w.playerState.ShuffleState { if w.playerState.ShuffleState {
output += wtf.CenterText(fmt.Sprintf("[green]Shuffle:[white] on\n"), w.CommonSettings().Width) output += utils.CenterText(fmt.Sprintf("[green]Shuffle:[white] on\n"), w.CommonSettings().Width)
} else { } else {
output += wtf.CenterText(fmt.Sprintf("[green]Shuffle:[white] off\n"), w.CommonSettings().Width) output += utils.CenterText(fmt.Sprintf("[green]Shuffle:[white] off\n"), w.CommonSettings().Width)
} }
return output return output
} }

View File

@ -7,7 +7,7 @@ import (
"runtime" "runtime"
"strings" "strings"
"github.com/wtfutil/wtf/wtf" "github.com/wtfutil/wtf/utils"
) )
type SystemInfo struct { type SystemInfo struct {
@ -32,7 +32,7 @@ func NewSystemInfo() *SystemInfo {
cmd = exec.Command("sw_vers", arg...) cmd = exec.Command("sw_vers", arg...)
} }
raw := wtf.ExecuteCommand(cmd) raw := utils.ExecuteCommand(cmd)
for _, row := range strings.Split(raw, "\n") { for _, row := range strings.Split(raw, "\n") {
parts := strings.Split(row, ":") parts := strings.Split(row, ":")

View File

@ -5,8 +5,8 @@ import (
"time" "time"
"github.com/rivo/tview" "github.com/rivo/tview"
"github.com/wtfutil/wtf/utils"
"github.com/wtfutil/wtf/view" "github.com/wtfutil/wtf/view"
"github.com/wtfutil/wtf/wtf"
) )
type Widget struct { type Widget struct {
@ -50,7 +50,7 @@ func (widget *Widget) Refresh() {
} }
func (widget *Widget) prettyDate() string { func (widget *Widget) prettyDate() string {
str, err := time.Parse(wtf.TimestampFormat, widget.Date) str, err := time.Parse(utils.TimestampFormat, widget.Date)
if err != nil { if err != nil {
return err.Error() return err.Error()

View File

@ -2,7 +2,7 @@ package textfile
import ( import (
"github.com/gdamore/tcell" "github.com/gdamore/tcell"
"github.com/wtfutil/wtf/wtf" "github.com/wtfutil/wtf/utils"
) )
func (widget *Widget) initializeKeyboardControls() { func (widget *Widget) initializeKeyboardControls() {
@ -18,5 +18,5 @@ func (widget *Widget) initializeKeyboardControls() {
func (widget *Widget) openFile() { func (widget *Widget) openFile() {
src := widget.CurrentSource() src := widget.CurrentSource()
wtf.OpenFile(src) utils.OpenFile(src)
} }

View File

@ -5,7 +5,7 @@ import (
"github.com/rivo/tview" "github.com/rivo/tview"
"github.com/wtfutil/wtf/checklist" "github.com/wtfutil/wtf/checklist"
"github.com/wtfutil/wtf/wtf" "github.com/wtfutil/wtf/utils"
) )
func (widget *Widget) display() { func (widget *Widget) display() {
@ -54,5 +54,5 @@ func (widget *Widget) formattedItemLine(idx int, item *checklist.ChecklistItem,
tview.Escape(item.Text), tview.Escape(item.Text),
) )
return wtf.HighlightableHelper(widget.View, row, idx, len(item.Text)) return utils.HighlightableHelper(widget.View, row, idx, len(item.Text))
} }

View File

@ -5,7 +5,7 @@ import (
"github.com/gdamore/tcell" "github.com/gdamore/tcell"
"github.com/wtfutil/wtf/cfg" "github.com/wtfutil/wtf/cfg"
"github.com/wtfutil/wtf/wtf" "github.com/wtfutil/wtf/utils"
) )
func (widget *Widget) initializeKeyboardControls() { func (widget *Widget) initializeKeyboardControls() {
@ -50,7 +50,7 @@ func (widget *Widget) displayPrev() {
func (widget *Widget) openFile() { func (widget *Widget) openFile() {
confDir, _ := cfg.WtfConfigDir() confDir, _ := cfg.WtfConfigDir()
wtf.OpenFile(fmt.Sprintf("%s/%s", confDir, widget.filePath)) utils.OpenFile(fmt.Sprintf("%s/%s", confDir, widget.filePath))
} }
func (widget *Widget) promoteSelected() { func (widget *Widget) promoteSelected() {

View File

@ -8,6 +8,7 @@ import (
"github.com/rivo/tview" "github.com/rivo/tview"
"github.com/wtfutil/wtf/cfg" "github.com/wtfutil/wtf/cfg"
"github.com/wtfutil/wtf/checklist" "github.com/wtfutil/wtf/checklist"
"github.com/wtfutil/wtf/utils"
"github.com/wtfutil/wtf/view" "github.com/wtfutil/wtf/view"
"github.com/wtfutil/wtf/wtf" "github.com/wtfutil/wtf/wtf"
"gopkg.in/yaml.v2" "gopkg.in/yaml.v2"
@ -108,7 +109,7 @@ func (widget *Widget) load() {
confDir, _ := cfg.WtfConfigDir() confDir, _ := cfg.WtfConfigDir()
filePath := fmt.Sprintf("%s/%s", confDir, widget.filePath) filePath := fmt.Sprintf("%s/%s", confDir, widget.filePath)
fileData, _ := wtf.ReadFileBytes(filePath) fileData, _ := utils.ReadFileBytes(filePath)
yaml.Unmarshal(fileData, &widget.list) yaml.Unmarshal(fileData, &widget.list)

View File

@ -4,7 +4,7 @@ import (
"fmt" "fmt"
"github.com/rivo/tview" "github.com/rivo/tview"
"github.com/wtfutil/wtf/wtf" "github.com/wtfutil/wtf/utils"
) )
func (widget *Widget) display() { func (widget *Widget) display() {
@ -25,7 +25,7 @@ func (widget *Widget) display() {
widget.RowColor(idx), widget.RowColor(idx),
) )
str += wtf.HighlightableHelper(widget.View, row, idx, len(item.Content)) str += utils.HighlightableHelper(widget.View, row, idx, len(item.Content))
} }
widget.ScrollableWidget.Redraw(title, str, false) widget.ScrollableWidget.Redraw(title, str, false)

View File

@ -6,7 +6,7 @@ import (
"github.com/hekmon/transmissionrpc" "github.com/hekmon/transmissionrpc"
"github.com/rivo/tview" "github.com/rivo/tview"
"github.com/wtfutil/wtf/wtf" "github.com/wtfutil/wtf/utils"
) )
func (widget *Widget) contentFrom(data []*transmissionrpc.Torrent) string { func (widget *Widget) contentFrom(data []*transmissionrpc.Torrent) string {
@ -24,7 +24,7 @@ func (widget *Widget) contentFrom(data []*transmissionrpc.Torrent) string {
tview.Escape(widget.prettyTorrentName(torrName)), tview.Escape(widget.prettyTorrentName(torrName)),
) )
str += wtf.HighlightableHelper(widget.View, row, idx, len(torrName)) str += utils.HighlightableHelper(widget.View, row, idx, len(torrName))
} }
return str return str

View File

@ -5,8 +5,8 @@ import (
"strings" "strings"
"github.com/rivo/tview" "github.com/rivo/tview"
"github.com/wtfutil/wtf/utils"
"github.com/wtfutil/wtf/view" "github.com/wtfutil/wtf/view"
"github.com/wtfutil/wtf/wtf"
) )
type Widget struct { type Widget struct {
@ -78,7 +78,7 @@ func (widget *Widget) contentFrom(builds *Builds) string {
strings.Split(build.Commit.Message, "\n")[0], strings.Split(build.Commit.Message, "\n")[0],
build.CreatedBy.Login, build.CreatedBy.Login,
) )
str += wtf.HighlightableHelper(widget.View, row, idx, len(build.Branch.Name)) str += utils.HighlightableHelper(widget.View, row, idx, len(build.Branch.Name))
} }
return str return str
@ -110,6 +110,6 @@ func (widget *Widget) openBuild() {
if sel >= 0 && widget.builds != nil && sel < len(widget.builds.Builds) { if sel >= 0 && widget.builds != nil && sel < len(widget.builds.Builds) {
build := &widget.builds.Builds[sel] build := &widget.builds.Builds[sel]
travisHost := TRAVIS_HOSTS[widget.settings.pro] travisHost := TRAVIS_HOSTS[widget.settings.pro]
wtf.OpenFile(fmt.Sprintf("https://%s/%s/%s/%d", travisHost, build.Repository.Slug, "builds", build.ID)) utils.OpenFile(fmt.Sprintf("https://%s/%s/%s/%d", travisHost, build.Repository.Slug, "builds", build.ID))
} }
} }

View File

@ -2,7 +2,7 @@ package twitter
import ( import (
"github.com/gdamore/tcell" "github.com/gdamore/tcell"
"github.com/wtfutil/wtf/wtf" "github.com/wtfutil/wtf/utils"
) )
func (widget *Widget) initializeKeyboardControls() { func (widget *Widget) initializeKeyboardControls() {
@ -19,5 +19,5 @@ func (widget *Widget) initializeKeyboardControls() {
func (widget *Widget) openFile() { func (widget *Widget) openFile() {
src := widget.currentSourceURI() src := widget.currentSourceURI()
wtf.OpenFile(src) utils.OpenFile(src)
} }

View File

@ -7,8 +7,8 @@ import (
"github.com/dustin/go-humanize" "github.com/dustin/go-humanize"
"github.com/rivo/tview" "github.com/rivo/tview"
"github.com/wtfutil/wtf/utils"
"github.com/wtfutil/wtf/view" "github.com/wtfutil/wtf/view"
"github.com/wtfutil/wtf/wtf"
) )
type Widget struct { type Widget struct {
@ -68,7 +68,7 @@ func (widget *Widget) display() {
title := fmt.Sprintf("Twitter - [green]@%s[white]", widget.CurrentSource()) title := fmt.Sprintf("Twitter - [green]@%s[white]", widget.CurrentSource())
if len(tweets) == 0 { if len(tweets) == 0 {
str := fmt.Sprintf("\n\n\n%s", wtf.CenterText("[lightblue]No Tweets[white]", 50)) str := fmt.Sprintf("\n\n\n%s", utils.CenterText("[lightblue]No Tweets[white]", 50))
widget.Redraw(title, str, true) widget.Redraw(title, str, true)
return return
} }

View File

@ -3,8 +3,8 @@ package weather
import ( import (
owm "github.com/briandowns/openweathermap" owm "github.com/briandowns/openweathermap"
"github.com/rivo/tview" "github.com/rivo/tview"
"github.com/wtfutil/wtf/utils"
"github.com/wtfutil/wtf/view" "github.com/wtfutil/wtf/view"
"github.com/wtfutil/wtf/wtf"
) )
// Widget is the container for weather data. // Widget is the container for weather data.
@ -63,7 +63,7 @@ func (widget *Widget) Fetch(cityIDs []int) []*owm.CurrentWeatherData {
// widget's view for rendering // widget's view for rendering
func (widget *Widget) Refresh() { func (widget *Widget) Refresh() {
if widget.apiKeyValid() { if widget.apiKeyValid() {
widget.Data = widget.Fetch(wtf.ToInts(widget.settings.cityIDs)) widget.Data = widget.Fetch(utils.ToInts(widget.settings.cityIDs))
} }
widget.display() widget.display()

View File

@ -5,8 +5,8 @@ import (
"log" "log"
"github.com/rivo/tview" "github.com/rivo/tview"
"github.com/wtfutil/wtf/utils"
"github.com/wtfutil/wtf/view" "github.com/wtfutil/wtf/view"
"github.com/wtfutil/wtf/wtf"
) )
// A Widget represents a Zendesk widget // A Widget represents a Zendesk widget
@ -99,6 +99,6 @@ func (widget *Widget) openTicket() {
if sel >= 0 && widget.result != nil && sel < len(widget.result.Tickets) { if sel >= 0 && widget.result != nil && sel < len(widget.result.Tickets) {
issue := &widget.result.Tickets[sel] issue := &widget.result.Tickets[sel]
ticketURL := fmt.Sprintf("https://%s.zendesk.com/agent/tickets/%d", widget.settings.subdomain, issue.Id) ticketURL := fmt.Sprintf("https://%s.zendesk.com/agent/tickets/%d", widget.settings.subdomain, issue.Id)
wtf.OpenFile(ticketURL) utils.OpenFile(ticketURL)
} }
} }

47
utils/conversions.go Normal file
View File

@ -0,0 +1,47 @@
package utils
import (
"strconv"
)
/* -------------------- Map Conversion -------------------- */
// MapToStrs takes a map of interfaces and returns a map of strings
func MapToStrs(aMap map[string]interface{}) map[string]string {
results := make(map[string]string)
for key, val := range aMap {
results[key] = val.(string)
}
return results
}
/* -------------------- Slice Conversion -------------------- */
// ToInts takes a slice of interfaces and returns a slice of ints
func ToInts(slice []interface{}) []int {
results := []int{}
for _, val := range slice {
results = append(results, val.(int))
}
return results
}
// ToStrs takes a slice of interfaces and returns a slice of strings
func ToStrs(slice []interface{}) []string {
results := []string{}
for _, val := range slice {
switch val.(type) {
case int:
results = append(results, strconv.Itoa(val.(int)))
case string:
results = append(results, val.(string))
}
}
return results
}

44
utils/conversions_test.go Normal file
View File

@ -0,0 +1,44 @@
package utils
import (
"testing"
. "github.com/stretchr/testify/assert"
)
func Test_MapToStrs(t *testing.T) {
expected := map[string]string{
"a": "a",
"b": "b",
"c": "c",
}
source := make(map[string]interface{})
for _, val := range expected {
source[val] = val
}
Equal(t, expected, MapToStrs(source))
}
func Test_ToInts(t *testing.T) {
expected := []int{1, 2, 3}
source := make([]interface{}, len(expected))
for idx, val := range expected {
source[idx] = val
}
Equal(t, expected, ToInts(source))
}
func Test_ToStrs(t *testing.T) {
expected := []string{"cat", "dog", "rat"}
source := make([]interface{}, len(expected))
for idx, val := range expected {
source[idx] = val
}
Equal(t, expected, ToStrs(source))
}

35
utils/email_addresses.go Normal file
View File

@ -0,0 +1,35 @@
package utils
import (
"strings"
)
// NameFromEmail takes an email address and returns the part that comes before the @ symbol
//
// Example:
//
// NameFromEmail("test_user@example.com")
// > "Test_user"
//
func NameFromEmail(email string) string {
parts := strings.Split(email, "@")
return strings.Title(strings.Replace(parts[0], ".", " ", -1))
}
// NamesFromEmails takes a slice of email addresses and returns a slice of the parts that
// come before the @ symbol
//
// Example:
//
// NamesFromEmail("test_user@example.com", "other_user@example.com")
// > []string{"Test_user", "Other_user"}
//
func NamesFromEmails(emails []string) []string {
names := []string{}
for _, email := range emails {
names = append(names, NameFromEmail(email))
}
return names
}

View File

@ -0,0 +1,22 @@
package utils
import (
"testing"
. "github.com/stretchr/testify/assert"
)
func Test_NameFromEmail(t *testing.T) {
Equal(t, "", NameFromEmail(""))
Equal(t, "Chris Cummer", NameFromEmail("chris.cummer@me.com"))
}
func Test_NamesFromEmails(t *testing.T) {
var result []string
result = NamesFromEmails([]string{})
Equal(t, []string{}, result)
result = NamesFromEmails([]string{"chris.cummer@me.com", "chriscummer@me.com"})
Equal(t, []string{"Chris Cummer", "Chriscummer"}, result)
}

View File

@ -10,47 +10,7 @@ import (
"github.com/wtfutil/wtf/cfg" "github.com/wtfutil/wtf/cfg"
) )
func lowercaseTitle(title string) string { /* -------------------- Exported Functions -------------------- */
if title == "" {
return ""
}
r, n := utf8.DecodeRuneInString(title)
return string(unicode.ToLower(r)) + title[n:]
}
var (
openColorRegex = regexp.MustCompile(`\[.*?\]`)
)
func StripColorTags(input string) string {
return openColorRegex.ReplaceAllString(input, "")
}
func helpFromValue(field reflect.StructField) string {
result := ""
optional, err := strconv.ParseBool(field.Tag.Get("optional"))
if err != nil {
optional = false
}
help := field.Tag.Get("help")
if optional {
help = "Optional " + help
}
values := field.Tag.Get("values")
if help != "" {
result += "\n\n " + lowercaseTitle(field.Name)
result += "\n " + help
if values != "" {
result += "\n Values: " + values
}
}
return result
}
func HelpFromInterface(item interface{}) string { func HelpFromInterface(item interface{}) string {
result := "" result := ""
@ -78,3 +38,45 @@ func HelpFromInterface(item interface{}) string {
return result return result
} }
// StripColorTags removes tcell color tags from a given string
func StripColorTags(input string) string {
openColorRegex := regexp.MustCompile(`\[.*?\]`)
return openColorRegex.ReplaceAllString(input, "")
}
/* -------------------- Unexported Functions -------------------- */
func helpFromValue(field reflect.StructField) string {
result := ""
optional, err := strconv.ParseBool(field.Tag.Get("optional"))
if err != nil {
optional = false
}
help := field.Tag.Get("help")
if optional {
help = "Optional " + help
}
values := field.Tag.Get("values")
if help != "" {
result += "\n\n " + lowercaseTitle(field.Name)
result += "\n " + help
if values != "" {
result += "\n Values: " + values
}
}
return result
}
func lowercaseTitle(title string) string {
if title == "" {
return ""
}
r, n := utf8.DecodeRuneInString(title)
return string(unicode.ToLower(r)) + title[n:]
}

View File

@ -10,21 +10,7 @@ import (
"path/filepath" "path/filepath"
) )
// Dir returns the home directory for the executing user. // ExpandHomeDir expands the path to include the home directory if the path
// An error is returned if a home directory cannot be detected.
func Home() (string, error) {
currentUser, err := user.Current()
if err != nil {
return "", err
}
if currentUser.HomeDir == "" {
return "", errors.New("cannot find user-specific home dir")
}
return currentUser.HomeDir, nil
}
// Expand expands the path to include the home directory if the path
// is prefixed with `~`. If it isn't prefixed with `~`, the path is // is prefixed with `~`. If it isn't prefixed with `~`, the path is
// returned as-is. // returned as-is.
func ExpandHomeDir(path string) (string, error) { func ExpandHomeDir(path string) (string, error) {
@ -47,3 +33,18 @@ func ExpandHomeDir(path string) (string, error) {
return filepath.Join(dir, path[1:]), nil return filepath.Join(dir, path[1:]), nil
} }
// Home returns the home directory for the executing user.
// An error is returned if a home directory cannot be detected.
func Home() (string, error) {
currentUser, err := user.Current()
if err != nil {
return "", err
}
if currentUser.HomeDir == "" {
return "", errors.New("cannot find user-specific home dir")
}
return currentUser.HomeDir, nil
}

9
utils/init.go Normal file
View File

@ -0,0 +1,9 @@
package utils
// OpenFileUtil defines the system utility to use to open files
var OpenFileUtil = "open"
// Init initializes global settings in the wtf package
func Init(openFileUtil string) {
OpenFileUtil = openFileUtil
}

12
utils/init_test.go Normal file
View File

@ -0,0 +1,12 @@
package utils
import (
"testing"
. "github.com/stretchr/testify/assert"
)
func Test_Init(t *testing.T) {
Init("cats")
Equal(t, OpenFileUtil, "cats")
}

48
utils/text.go Normal file
View File

@ -0,0 +1,48 @@
package utils
import (
"fmt"
"strings"
"github.com/rivo/tview"
)
// CenterText takes a string and a width and pads the left and right of the string with
// empty spaces to ensure that the string is in the middle of the returned value
//
// Example:
//
// x := CenterText("cat", 11)
// > " cat "
//
func CenterText(str string, width int) string {
if width < 0 {
width = 0
}
return fmt.Sprintf("%[1]*s", -width, fmt.Sprintf("%[1]*s", (width+len(str))/2, str))
}
// HighlightableHelper pads the given text with blank spaces to the width of the view
// containing it. This is helpful for extending row highlighting across the entire width
// of the view
func HighlightableHelper(view *tview.TextView, input string, idx, offset int) string {
fmtStr := fmt.Sprintf(`["%d"][""]`, idx)
_, _, w, _ := view.GetInnerRect()
fmtStr += input
fmtStr += RowPadding(offset, w+1)
fmtStr += `[""]` + "\n"
return fmtStr
}
// RowPadding returns a padding for a row to make it the full width of the containing widget.
// Useful for ensurig row highlighting spans the full width (I suspect tcell has a better
// way to do this, but I haven't yet found it)
func RowPadding(offset int, max int) string {
padSize := max - offset
if padSize < 0 {
padSize = 0
}
return strings.Repeat(" ", padSize)
}

24
utils/text_test.go Normal file
View File

@ -0,0 +1,24 @@
package utils
import (
"testing"
. "github.com/stretchr/testify/assert"
)
func Test_CenterText(t *testing.T) {
Equal(t, "cat", CenterText("cat", -9))
Equal(t, "cat", CenterText("cat", 0))
Equal(t, " cat ", CenterText("cat", 9))
}
func Test_HighlightableHelper(t *testing.T) {
}
func Test_RowPadding(t *testing.T) {
Equal(t, "", RowPadding(0, 0))
Equal(t, "", RowPadding(5, 2))
Equal(t, " ", RowPadding(1, 2))
Equal(t, " ", RowPadding(0, 5))
}

106
utils/utils.go Normal file
View File

@ -0,0 +1,106 @@
package utils
import (
"fmt"
"io/ioutil"
"os/exec"
"regexp"
"runtime"
"strings"
)
const (
SimpleDateFormat = "Jan 2"
SimpleTimeFormat = "15:04 MST"
MinimumTimeFormat = "15:04"
FullDateFormat = "Monday, Jan 2"
FriendlyDateFormat = "Mon, Jan 2"
FriendlyDateTimeFormat = "Mon, Jan 2, 15:04"
TimestampFormat = "2006-01-02T15:04:05-0700"
)
// DoesNotInclude takes a slice of strings and a target string and returns
// TRUE if the slice does not include the target, FALSE if it does
//
// Example:
//
// x := DoesNotInclude([]string{"cat", "dog", "rat"}, "dog")
// > false
//
// x := DoesNotInclude([]string{"cat", "dog", "rat"}, "pig")
// > true
//
func DoesNotInclude(strs []string, val string) bool {
for _, str := range strs {
if val == str {
return false
}
}
return true
}
// ExecuteCommand executes an external command on the local machine as the current user
func ExecuteCommand(cmd *exec.Cmd) string {
if cmd == nil {
return ""
}
stdout, err := cmd.StdoutPipe()
if err != nil {
return fmt.Sprintf("%v\n", err)
}
if err := cmd.Start(); err != nil {
return fmt.Sprintf("%v\n", err)
}
var str string
if b, err := ioutil.ReadAll(stdout); err == nil {
str += string(b)
}
err = cmd.Wait()
if err != nil {
return fmt.Sprintf("%v\n", err)
}
return str
}
// FindMatch takes a regex pattern and a string of data and returns back all the matches
// in that string
func FindMatch(pattern string, data string) [][]string {
r := regexp.MustCompile(pattern)
return r.FindAllStringSubmatch(data, -1)
}
// OpenFile opens the file defined in `path` via the operating system
func OpenFile(path string) {
if (strings.HasPrefix(path, "http://")) || (strings.HasPrefix(path, "https://")) {
switch runtime.GOOS {
case "linux":
exec.Command("xdg-open", path).Start()
case "windows":
exec.Command("rundll32", "url.dll,FileProtocolHandler", path).Start()
case "darwin":
exec.Command("open", path).Start()
default:
}
} else {
filePath, _ := ExpandHomeDir(path)
cmd := exec.Command(OpenFileUtil, filePath)
ExecuteCommand(cmd)
}
}
// ReadFileBytes reads the contents of a file and returns those contents as a slice of bytes
func ReadFileBytes(filePath string) ([]byte, error) {
fileData, err := ioutil.ReadFile(filePath)
if err != nil {
return []byte{}, err
}
return fileData, nil
}

75
utils/utils_test.go Normal file
View File

@ -0,0 +1,75 @@
package utils
import (
"os/exec"
"reflect"
"testing"
. "github.com/stretchr/testify/assert"
)
func Test_DoesNotInclude(t *testing.T) {
Equal(t, true, DoesNotInclude([]string{"cat", "dog", "rat"}, "bat"))
Equal(t, false, DoesNotInclude([]string{"cat", "dog", "rat"}, "dog"))
}
func Test_ExecuteCommand(t *testing.T) {
tests := []struct {
name string
cmd *exec.Cmd
expected string
}{
{
name: "with nil command",
cmd: nil,
expected: "",
},
{
name: "with defined command",
cmd: exec.Command("echo", "cats"),
expected: "cats\n",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual := ExecuteCommand(tt.cmd)
if tt.expected != actual {
t.Errorf("\nexpected: %s\n got: %s", tt.expected, actual)
}
})
}
}
func Test_FindMatch(t *testing.T) {
var result [][]string
expected := [][]string([][]string{[]string{"SSID: 7E5B5C", "7E5B5C"}})
result = FindMatch(`s*SSID: (.+)s*`, "SSID: 7E5B5C")
Equal(t, expected, result)
}
func Test_ReadFileBytes(t *testing.T) {
tests := []struct {
name string
file string
expected []byte
}{
{
name: "with non-existant file",
file: "/tmp/junk-daa6bf613f4c.md",
expected: []byte{},
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual, _ := ReadFileBytes(tt.file)
if reflect.DeepEqual(tt.expected, actual) == false {
t.Errorf("\nexpected: %q\n got: %q", tt.expected, actual)
}
})
}
}

View File

@ -2,7 +2,7 @@ package view
import ( import (
"github.com/wtfutil/wtf/cfg" "github.com/wtfutil/wtf/cfg"
"github.com/wtfutil/wtf/wtf" "github.com/wtfutil/wtf/utils"
) )
// MultiSourceWidget is a widget that supports displaying data from multiple sources // MultiSourceWidget is a widget that supports displaying data from multiple sources
@ -90,7 +90,7 @@ func (widget *MultiSourceWidget) loadSources() {
single := widget.moduleConfig.Config.UString(widget.singular, "") single := widget.moduleConfig.Config.UString(widget.singular, "")
multiple := widget.moduleConfig.Config.UList(widget.plural, empty) multiple := widget.moduleConfig.Config.UList(widget.plural, empty)
asStrs := wtf.ToStrs(multiple) asStrs := utils.ToStrs(multiple)
if single != "" { if single != "" {
asStrs = append(asStrs, single) asStrs = append(asStrs, single)

View File

@ -1,223 +0,0 @@
package wtf
import (
"fmt"
"io/ioutil"
"os/exec"
"regexp"
"runtime"
"strconv"
"strings"
"github.com/rivo/tview"
"github.com/wtfutil/wtf/utils"
)
const (
SimpleDateFormat = "Jan 2"
SimpleTimeFormat = "15:04 MST"
MinimumTimeFormat = "15:04"
FullDateFormat = "Monday, Jan 2"
FriendlyDateFormat = "Mon, Jan 2"
FriendlyDateTimeFormat = "Mon, Jan 2, 15:04"
TimestampFormat = "2006-01-02T15:04:05-0700"
)
var OpenFileUtil = "open"
// Init initializes global settings in the wtf package
func Init(openFileUtil string) {
OpenFileUtil = openFileUtil
}
// CenterText takes a string and a width and pads the left and right of the string with
// empty spaces to ensure that the string is in the middle of the returned value
//
// Example:
//
// x := CenterText("cat", 11)
// > " cat "
//
func CenterText(str string, width int) string {
if width < 0 {
width = 0
}
return fmt.Sprintf("%[1]*s", -width, fmt.Sprintf("%[1]*s", (width+len(str))/2, str))
}
// ExecuteCommand executes an external command on the local machine as the current user
func ExecuteCommand(cmd *exec.Cmd) string {
if cmd == nil {
return ""
}
stdout, err := cmd.StdoutPipe()
if err != nil {
return fmt.Sprintf("%v\n", err)
}
if err := cmd.Start(); err != nil {
return fmt.Sprintf("%v\n", err)
}
var str string
if b, err := ioutil.ReadAll(stdout); err == nil {
str += string(b)
}
err = cmd.Wait()
if err != nil {
return fmt.Sprintf("%v\n", err)
}
return str
}
// Exclude takes a slice of strings and a target string and returns the contents of the original
// slice of strings without the target string in it
//
// Example:
//
// x := Exclude([]string{"cat", "dog", "rat"}, "dog")
// > []string{"cat", "rat"}
//
func Exclude(strs []string, val string) bool {
for _, str := range strs {
if val == str {
return false
}
}
return true
}
// FindMatch takes a regex pattern and a string of data and returns back all the matches
// in that string
func FindMatch(pattern string, data string) [][]string {
r := regexp.MustCompile(pattern)
return r.FindAllStringSubmatch(data, -1)
}
// NameFromEmail takes an email address and returns the part that comes before the @ symbol
//
// Example:
//
// NameFromEmail("test_user@example.com")
// > "Test_user"
//
func NameFromEmail(email string) string {
parts := strings.Split(email, "@")
return strings.Title(strings.Replace(parts[0], ".", " ", -1))
}
// NamesFromEmails takes a slice of email addresses and returns a slice of the parts that
// come before the @ symbol
//
// Example:
//
// NamesFromEmail("test_user@example.com", "other_user@example.com")
// > []string{"Test_user", "Other_user"}
//
func NamesFromEmails(emails []string) []string {
names := []string{}
for _, email := range emails {
names = append(names, NameFromEmail(email))
}
return names
}
// OpenFile opens the file defined in `path` via the operating system
func OpenFile(path string) {
if (strings.HasPrefix(path, "http://")) || (strings.HasPrefix(path, "https://")) {
switch runtime.GOOS {
case "linux":
exec.Command("xdg-open", path).Start()
case "windows":
exec.Command("rundll32", "url.dll,FileProtocolHandler", path).Start()
case "darwin":
exec.Command("open", path).Start()
default:
}
} else {
filePath, _ := utils.ExpandHomeDir(path)
cmd := exec.Command(OpenFileUtil, filePath)
ExecuteCommand(cmd)
}
}
// PadRow returns a padding for a row to make it the full width of the containing widget.
// Useful for ensurig row highlighting spans the full width (I suspect tcell has a better
// way to do this, but I haven't yet found it)
func PadRow(offset int, max int) string {
padSize := max - offset
if padSize < 0 {
padSize = 0
}
return strings.Repeat(" ", padSize)
}
// ReadFileBytes reads the contents of a file and returns those contents as a slice of bytes
func ReadFileBytes(filePath string) ([]byte, error) {
fileData, err := ioutil.ReadFile(filePath)
if err != nil {
return []byte{}, err
}
return fileData, nil
}
/* -------------------- Map Conversion -------------------- */
// MapToStrs takes a map of interfaces and returns a map of strings
func MapToStrs(aMap map[string]interface{}) map[string]string {
results := make(map[string]string)
for key, val := range aMap {
results[key] = val.(string)
}
return results
}
/* -------------------- Slice Conversion -------------------- */
// ToInts takes a slice of interfaces and returns a slice of ints
func ToInts(slice []interface{}) []int {
results := []int{}
for _, val := range slice {
results = append(results, val.(int))
}
return results
}
// ToStrs takes a slice of interfaces and returns a slice of strings
func ToStrs(slice []interface{}) []string {
results := []string{}
for _, val := range slice {
switch val.(type) {
case int:
results = append(results, strconv.Itoa(val.(int)))
case string:
results = append(results, val.(string))
}
}
return results
}
func HighlightableHelper(view *tview.TextView, input string, idx, offset int) string {
fmtStr := fmt.Sprintf(`["%d"][""]`, idx)
_, _, w, _ := view.GetInnerRect()
fmtStr += input
fmtStr += PadRow(offset, w+1)
fmtStr += `[""]` + "\n"
return fmtStr
}

View File

@ -1,119 +0,0 @@
package wtf
import (
"os/exec"
"testing"
. "github.com/stretchr/testify/assert"
)
func Test_Init(t *testing.T) {
Init("cats")
Equal(t, OpenFileUtil, "cats")
}
func Test_CenterText(t *testing.T) {
Equal(t, "cat", CenterText("cat", -9))
Equal(t, "cat", CenterText("cat", 0))
Equal(t, " cat ", CenterText("cat", 9))
}
func Test_ExecuteCommand(t *testing.T) {
tests := []struct {
name string
cmd *exec.Cmd
expected string
}{
{
name: "with nil command",
cmd: nil,
expected: "",
},
{
name: "with defined command",
cmd: exec.Command("echo", "cats"),
expected: "cats\n",
},
}
for _, tt := range tests {
t.Run(tt.name, func(t *testing.T) {
actual := ExecuteCommand(tt.cmd)
if tt.expected != actual {
t.Errorf("\nexpected: %s\n got: %s", tt.expected, actual)
}
})
}
}
func Test_FindMatch(t *testing.T) {
var result [][]string
expected := [][]string([][]string{[]string{"SSID: 7E5B5C", "7E5B5C"}})
result = FindMatch(`s*SSID: (.+)s*`, "SSID: 7E5B5C")
Equal(t, expected, result)
}
func Test_ExcludeWhenTrue(t *testing.T) {
Equal(t, true, Exclude([]string{"cat", "dog", "rat"}, "bat"))
Equal(t, false, Exclude([]string{"cat", "dog", "rat"}, "dog"))
}
func Test_NameFromEmail(t *testing.T) {
Equal(t, "", NameFromEmail(""))
Equal(t, "Chris Cummer", NameFromEmail("chris.cummer@me.com"))
}
func Test_NamesFromEmails(t *testing.T) {
var result []string
result = NamesFromEmails([]string{})
Equal(t, []string{}, result)
result = NamesFromEmails([]string{"chris.cummer@me.com", "chriscummer@me.com"})
Equal(t, []string{"Chris Cummer", "Chriscummer"}, result)
}
func Test_PadRow(t *testing.T) {
Equal(t, "", PadRow(0, 0))
Equal(t, "", PadRow(5, 2))
Equal(t, " ", PadRow(1, 2))
}
func Test_MapToStrs(t *testing.T) {
expected := map[string]string{
"a": "a",
"b": "b",
"c": "c",
}
source := make(map[string]interface{})
for _, val := range expected {
source[val] = val
}
Equal(t, expected, MapToStrs(source))
}
func Test_ToInts(t *testing.T) {
expected := []int{1, 2, 3}
source := make([]interface{}, len(expected))
for idx, val := range expected {
source[idx] = val
}
Equal(t, expected, ToInts(source))
}
func Test_ToStrs(t *testing.T) {
expected := []string{"cat", "dog", "rat"}
source := make([]interface{}, len(expected))
for idx, val := range expected {
source[idx] = val
}
Equal(t, expected, ToStrs(source))
}