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

WTF-400 Move settings-related functions from util.go into common_settings.go

This commit is contained in:
Chris Cummer 2019-04-21 21:19:28 -07:00
parent bd2c6e5c9f
commit 88fdf9702b
20 changed files with 105 additions and 99 deletions

View File

@ -1,6 +1,9 @@
package cfg
import (
"fmt"
"strings"
"github.com/olebedev/config"
)
@ -10,10 +13,16 @@ type Colors struct {
BorderFocused string
BorderNormal string
Checked string
Foreground string
HighlightFore string
HighlightBack string
Text string
Title string
Rows struct {
Even string
Odd string
}
}
type Module struct {
@ -29,8 +38,14 @@ type Position struct {
}
type Sigils struct {
CheckedIcon string
UncheckedIcon string
Checkbox struct {
Checked string
Unchecked string
}
Paging struct {
Normal string
Selected string
}
}
type Common struct {
@ -53,11 +68,12 @@ func NewCommonSettingsFromYAML(name, configKey string, ymlConfig *config.Config)
common := Common{
Colors: Colors{
Background: ymlConfig.UString(modulePath+".colors.background", ymlConfig.UString(colorsPath+".background", "black")),
Background: ymlConfig.UString(modulePath+".background", ymlConfig.UString(colorsPath+".background", "black")),
BorderFocusable: ymlConfig.UString(colorsPath+".border.focusable", "red"),
BorderFocused: ymlConfig.UString(colorsPath+".border.focused", "orange"),
BorderNormal: ymlConfig.UString(colorsPath+".border.normal", "gray"),
Checked: ymlConfig.UString(colorsPath+".checked", "white"),
Foreground: ymlConfig.UString(modulePath+".foreground", ymlConfig.UString(colorsPath+".foreground", "white")),
HighlightFore: ymlConfig.UString(colorsPath+".highlight.fore", "black"),
HighlightBack: ymlConfig.UString(colorsPath+".highlight.back", "green"),
Text: ymlConfig.UString(modulePath+".colors.text", ymlConfig.UString(colorsPath+".text", "white")),
@ -76,16 +92,54 @@ func NewCommonSettingsFromYAML(name, configKey string, ymlConfig *config.Config)
Width: ymlConfig.UInt(positionPath + ".width"),
},
Sigils: Sigils{
CheckedIcon: ymlConfig.UString(sigilsPath+".checkedIcon", "x"),
UncheckedIcon: ymlConfig.UString(sigilsPath+".uncheckedIcon", " "),
},
Enabled: ymlConfig.UBool(modulePath+".enabled", false),
FocusChar: ymlConfig.UInt(modulePath+".focusChar", -1),
RefreshInterval: ymlConfig.UInt(modulePath+".refreshInterval", 300),
Title: ymlConfig.UString(modulePath+".title", name),
}
common.Colors.Rows.Even = ymlConfig.UString(modulePath+".colors.rows.even", ymlConfig.UString(colorsPath+".rows.even", "white"))
common.Colors.Rows.Odd = ymlConfig.UString(modulePath+".colors.rows.even", ymlConfig.UString(colorsPath+".rows.odd", "lightblue"))
common.Sigils.Checkbox.Checked = ymlConfig.UString(sigilsPath+".Checkbox.Checked", "x")
common.Sigils.Checkbox.Unchecked = ymlConfig.UString(sigilsPath+".Checkbox.Unchecked", " ")
common.Sigils.Paging.Normal = ymlConfig.UString(sigilsPath+".Paging.Normal", ymlConfig.UString("wtf.paging.pageSigil", "*"))
common.Sigils.Paging.Selected = ymlConfig.UString(sigilsPath+".Paging.Select", ymlConfig.UString("wtf.paging.selectedSigil", "_"))
return &common
}
func (common *Common) DefaultFocussedRowColor() string {
return fmt.Sprintf("%s:%s", common.Colors.HighlightFore, common.Colors.HighlightBack)
}
func (common *Common) DefaultRowColor() string {
return fmt.Sprintf("%s:%s", common.Colors.Foreground, common.Colors.Background)
}
func (common *Common) RowColor(idx int) string {
if idx%2 == 0 {
return common.Colors.Rows.Even
}
return common.Colors.Rows.Odd
}
func (common *Common) RightAlignFormat(width int) string {
return fmt.Sprintf("%%%ds", width-1)
}
func (common *Common) SigilStr(len, pos int, width int) string {
sigils := ""
if len > 1 {
sigils = strings.Repeat(common.Sigils.Paging.Normal, pos)
sigils = sigils + common.Sigils.Paging.Selected
sigils = sigils + strings.Repeat(common.Sigils.Paging.Normal, len-1-pos)
sigils = "[lightblue]" + fmt.Sprintf(common.RightAlignFormat(width), sigils) + "[white]"
}
return sigils
}

View File

@ -23,7 +23,7 @@ type Widget struct {
func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *Widget {
widget := Widget{
HelpfulWidget: wtf.NewHelpfulWidget(app, pages, HelpText),
TextWidget: wtf.NewTextWidget(app, "{{(Title .Name)}}", "{{(Lower .Name)}}", true),
TextWidget: wtf.NewTextWidget(app, settings.common, false),
settings: settings,
}

View File

@ -2,8 +2,6 @@ package gerrit
import (
"fmt"
"github.com/wtfutil/wtf/wtf"
)
func (widget *Widget) display() {
@ -16,7 +14,8 @@ func (widget *Widget) display() {
widget.View.SetTitle(widget.ContextualTitle(fmt.Sprintf("%s- %s", widget.Name(), widget.title(project))))
str := wtf.SigilStr(len(widget.GerritProjects), widget.Idx, widget.View) + "\n"
_, _, width, _ := widget.View.GetRect()
str := widget.settings.common.SigilStr(len(widget.GerritProjects), widget.Idx, width) + "\n"
str = str + " [red]Stats[white]\n"
str = str + widget.displayStats(project)
str = str + "\n"
@ -64,11 +63,12 @@ func (widget *Widget) displayStats(project *GerritProject) string {
return str
}
func (widget *Widget) rowColor(index int) string {
if widget.View.HasFocus() && (index == widget.selected) {
return wtf.DefaultFocussedRowColor()
func (widget *Widget) rowColor(idx int) string {
if widget.View.HasFocus() && (idx == widget.selected) {
widget.settings.common.DefaultFocussedRowColor()
}
return wtf.RowColor("gerrit", index)
return widget.settings.common.RowColor(idx)
}
func (widget *Widget) title(project *GerritProject) string {

View File

@ -4,8 +4,6 @@ import (
"fmt"
"strings"
"unicode/utf8"
"github.com/wtfutil/wtf/wtf"
)
func (widget *Widget) display() {
@ -18,7 +16,8 @@ func (widget *Widget) display() {
title := fmt.Sprintf("%s - [green]%s[white]", widget.Name(), repoData.Repository)
widget.View.SetTitle(widget.ContextualTitle(title))
str := wtf.SigilStr(len(widget.GitRepos), widget.Idx, widget.View) + "\n"
_, _, width, _ := widget.View.GetRect()
str := widget.settings.common.SigilStr(len(widget.GitRepos), widget.Idx, width) + "\n"
str = str + " [red]Branch[white]\n"
str = str + fmt.Sprintf(" %s", repoData.Branch)
str = str + "\n"

View File

@ -4,7 +4,6 @@ import (
"fmt"
"github.com/google/go-github/github"
"github.com/wtfutil/wtf/wtf"
)
func (widget *Widget) display() {
@ -16,7 +15,8 @@ func (widget *Widget) display() {
widget.View.SetTitle(widget.ContextualTitle(fmt.Sprintf("%s - %s", widget.Name(), widget.title(repo))))
str := wtf.SigilStr(len(widget.GithubRepos), widget.Idx, widget.View) + "\n"
_, _, width, _ := widget.View.GetRect()
str := widget.settings.common.SigilStr(len(widget.GithubRepos), widget.Idx, width) + "\n"
str = str + " [red]Stats[white]\n"
str = str + widget.displayStats(repo)
str = str + "\n"

View File

@ -2,8 +2,6 @@ package gitlab
import (
"fmt"
"github.com/wtfutil/wtf/wtf"
)
func (widget *Widget) display() {
@ -16,7 +14,8 @@ func (widget *Widget) display() {
widget.View.SetTitle(fmt.Sprintf("%s- %s", widget.Name(), widget.title(project)))
str := wtf.SigilStr(len(widget.GitlabProjects), widget.Idx, widget.View) + "\n"
_, _, width, _ := widget.View.GetRect()
str := widget.settings.common.SigilStr(len(widget.GitlabProjects), widget.Idx, width) + "\n"
str = str + " [red]Stats[white]\n"
str = str + widget.displayStats(project)
str = str + "\n"

View File

@ -116,10 +116,10 @@ func (widget *Widget) contentFrom(messages []Message) string {
func (widget *Widget) rowColor(idx int) string {
if widget.View.HasFocus() && (idx == widget.selected) {
return wtf.DefaultFocussedRowColor()
widget.settings.common.DefaultFocussedRowColor()
}
return wtf.RowColor("gitter", idx)
return widget.settings.common.RowColor(idx)
}
func (widget *Widget) next() {

View File

@ -123,10 +123,10 @@ func (widget *Widget) contentFrom(stories []Story) string {
func (widget *Widget) rowColor(idx int) string {
if widget.View.HasFocus() && (idx == widget.selected) {
return wtf.DefaultFocussedRowColor()
widget.settings.common.DefaultFocussedRowColor()
}
return wtf.RowColor("hackernews", idx)
return widget.settings.common.RowColor(idx)
}
func (widget *Widget) next() {

View File

@ -112,10 +112,10 @@ func (widget *Widget) contentFrom(view *View) string {
func (widget *Widget) rowColor(idx int) string {
if widget.View.HasFocus() && (idx == widget.selected) {
return wtf.DefaultFocussedRowColor()
widget.settings.common.DefaultFocussedRowColor()
}
return wtf.DefaultRowColor()
return widget.settings.common.RowColor(idx)
}
func (widget *Widget) jobColor(job *Job) string {

View File

@ -138,14 +138,10 @@ func (widget *Widget) contentFrom(searchResult *SearchResult) string {
func (widget *Widget) rowColor(idx int) string {
if widget.View.HasFocus() && (idx == widget.selected) {
return wtf.DefaultFocussedRowColor()
widget.settings.common.DefaultFocussedRowColor()
}
if idx%2 == 0 {
return widget.settings.colors.rows.even
}
return widget.settings.colors.rows.odd
return widget.settings.common.RowColor(idx)
}
func (widget *Widget) issueTypeColor(issue *Issue) string {

View File

@ -4,8 +4,6 @@ import (
"fmt"
"strings"
"unicode/utf8"
"github.com/wtfutil/wtf/wtf"
)
func (widget *Widget) display() {
@ -18,7 +16,8 @@ func (widget *Widget) display() {
title := fmt.Sprintf("%s - [green]%s[white]", widget.Name(), repoData.Repository)
widget.View.SetTitle(widget.ContextualTitle(title))
str := wtf.SigilStr(len(widget.Data), widget.Idx, widget.View) + "\n"
_, _, width, _ := widget.View.GetRect()
str := widget.settings.common.SigilStr(len(widget.Data), widget.Idx, width) + "\n"
str = str + " [red]Branch:Bookmark[white]\n"
str = str + fmt.Sprintf(" %s:%s\n", repoData.Branch, repoData.Bookmark)
str = str + "\n"

View File

@ -110,9 +110,10 @@ func (widget *Widget) contentFrom(result *Result) string {
func (widget *Widget) rowColor(idx int) string {
if widget.View.HasFocus() && (idx == widget.selected) {
return wtf.DefaultFocussedRowColor()
widget.settings.common.DefaultFocussedRowColor()
}
return "white"
return widget.settings.common.RowColor(idx)
}
func statusColor(item *Item) string {

View File

@ -76,7 +76,8 @@ func (widget *Widget) display() {
title := fmt.Sprintf("[green]%s[white]", widget.CurrentSource())
title = widget.ContextualTitle(title)
text := wtf.SigilStr(len(widget.Sources), widget.Idx, widget.View) + "\n"
_, _, width, _ := widget.View.GetRect()
text := widget.settings.common.SigilStr(len(widget.Sources), widget.Idx, width) + "\n"
if widget.settings.format {
text = text + widget.formattedText()

View File

@ -14,8 +14,8 @@ const checkWidth = 4
func (widget *Widget) display() {
str := ""
newList := checklist.NewChecklist(
widget.settings.common.Sigils.CheckedIcon,
widget.settings.common.Sigils.UncheckedIcon,
widget.settings.common.Sigils.Checkbox.Checked,
widget.settings.common.Sigils.Checkbox.Unchecked,
)
offset := 0

View File

@ -54,7 +54,7 @@ func NewWidget(app *tview.Application, pages *tview.Pages, settings *Settings) *
app: app,
settings: settings,
filePath: settings.filePath,
list: checklist.NewChecklist(settings.common.Sigils.CheckedIcon, settings.common.Sigils.UncheckedIcon),
list: checklist.NewChecklist(settings.common.Sigils.Checkbox.Checked, settings.common.Sigils.Checkbox.Unchecked),
pages: pages,
}
@ -234,8 +234,8 @@ func (widget *Widget) persist() {
// items have the correct checked/unchecked icon per the user's preferences
func (widget *Widget) setItemChecks() {
for _, item := range widget.list.Items {
item.CheckedIcon = widget.settings.common.CheckedIcon
item.UncheckedIcon = widget.settings.common.UncheckedIcon
item.CheckedIcon = widget.settings.common.Checkbox.Checked
item.UncheckedIcon = widget.settings.common.Checkbox.Unchecked
}
}

View File

@ -19,7 +19,8 @@ func (widget *Widget) display() {
title := fmt.Sprintf("[green]%s[white]", proj.Project.Name)
widget.View.SetTitle(widget.ContextualTitle(title))
str := wtf.SigilStr(len(widget.projects), widget.idx, widget.View) + "\n"
_, _, width, _ := widget.View.GetRect()
str := widget.settings.common.SigilStr(len(widget.projects), widget.idx, width) + "\n"
maxLen := proj.LongestLine()

View File

@ -102,9 +102,10 @@ func (widget *Widget) contentFrom(builds *Builds) string {
func (widget *Widget) rowColor(idx int) string {
if widget.View.HasFocus() && (idx == widget.selected) {
return wtf.DefaultFocussedRowColor()
widget.settings.common.DefaultFocussedRowColor()
}
return "White"
return widget.settings.common.RowColor(idx)
}
func buildColor(build *Build) string {

View File

@ -79,7 +79,8 @@ func (widget *Widget) display() {
return
}
str := wtf.SigilStr(len(widget.Sources), widget.Idx, widget.View) + "\n"
_, _, width, _ := widget.View.GetRect()
str := widget.settings.common.SigilStr(len(widget.Sources), widget.Idx, width) + "\n"
for _, tweet := range tweets {
str = str + widget.format(tweet)
}

View File

@ -27,7 +27,8 @@ func (widget *Widget) display() {
widget.View.SetTitle(widget.title(cityData))
content := wtf.SigilStr(len(widget.Data), widget.Idx, widget.View) + "\n"
_, _, width, _ := widget.View.GetRect()
content := widget.settings.common.SigilStr(len(widget.Data), widget.Idx, width) + "\n"
content = content + widget.description(cityData) + "\n\n"
content = content + widget.temperatures(cityData) + "\n"
content = content + widget.sunInfo(cityData)

View File

@ -7,8 +7,6 @@ import (
"regexp"
"runtime"
"strings"
"github.com/rivo/tview"
)
const SimpleDateFormat = "Jan 2"
@ -27,20 +25,6 @@ func CenterText(str string, width int) string {
return fmt.Sprintf("%[1]*s", -width, fmt.Sprintf("%[1]*s", (width+len(str))/2, str))
}
func DefaultFocussedRowColor() string {
foreColor := Config.UString("wtf.colors.highlight.fore", "black")
backColor := Config.UString("wtf.colors.highlight.back", "orange")
return fmt.Sprintf("%s:%s", foreColor, backColor)
}
func DefaultRowColor() string {
foreColor := Config.UString("wtf.colors.foreground", "white")
backColor := Config.UString("wtf.colors.background", "black")
return fmt.Sprintf("%s:%s", foreColor, backColor)
}
func ExecuteCommand(cmd *exec.Cmd) string {
stdout, err := cmd.StdoutPipe()
if err != nil {
@ -134,37 +118,6 @@ func ReadFileBytes(filePath string) ([]byte, error) {
return fileData, nil
}
func RightAlignFormat(view *tview.TextView) string {
_, _, w, _ := view.GetInnerRect()
return fmt.Sprintf("%%%ds", w-1)
}
func RowColor(module string, idx int) string {
evenKey := fmt.Sprintf("wtf.mods.%s.colors.rows.even", module)
oddKey := fmt.Sprintf("wtf.mods.%s.colors.rows.odd", module)
if idx%2 == 0 {
return Config.UString(evenKey, "white")
}
return Config.UString(oddKey, "lightblue")
}
func SigilStr(len, pos int, view *tview.TextView) string {
sigils := ""
if len > 1 {
sigils = strings.Repeat(Config.UString("wtf.paging.pageSigil", "*"), pos)
sigils = sigils + Config.UString("wtf.paging.selectedSigil", "_")
sigils = sigils + strings.Repeat(Config.UString("wtf.paging.pageSigil", "*"), len-1-pos)
sigils = "[lightblue]" + fmt.Sprintf(RightAlignFormat(view), sigils) + "[white]"
}
return sigils
}
/* -------------------- Map Conversion -------------------- */
func MapToStrs(aMap map[string]interface{}) map[string]string {