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

Some minor cleanup

This commit is contained in:
Chris Cummer 2018-04-22 23:39:33 -07:00
parent e1950bf756
commit d7bc26684a
5 changed files with 35 additions and 36 deletions

View File

@ -7,9 +7,6 @@ import (
func (widget *Widget) display() { func (widget *Widget) display() {
widget.View.Clear() widget.View.Clear()
title := fmt.Sprintf(" 📝 %s ", widget.FilePath)
widget.View.SetTitle(title)
str := "" str := ""
for idx, item := range widget.list.Items { for idx, item := range widget.list.Items {
foreColor, backColor := "white", "black" foreColor, backColor := "white", "black"

View File

@ -117,7 +117,7 @@ func (widget *Widget) load() {
confDir, _ := wtf.ConfigDir() confDir, _ := wtf.ConfigDir()
filePath := fmt.Sprintf("%s/%s", confDir, widget.FilePath) filePath := fmt.Sprintf("%s/%s", confDir, widget.FilePath)
fileData, _ := wtf.ReadYamlFile(filePath) fileData, _ := wtf.ReadFileBytes(filePath)
yaml.Unmarshal(fileData, &widget.list) yaml.Unmarshal(fileData, &widget.list)
} }

View File

@ -3,7 +3,6 @@ package wtf
import ( import (
"fmt" "fmt"
"os" "os"
"io/ioutil"
"github.com/olebedev/config" "github.com/olebedev/config"
) )
@ -73,7 +72,6 @@ func LoadConfigFile(filePath string) *config.Config {
return cfg return cfg
} }
func ReadFile(fileName string) (string, error) { func ReadFile(fileName string) (string, error) {
configDir, err := ConfigDir() configDir, err := ConfigDir()
if err != nil { if err != nil {
@ -82,10 +80,10 @@ func ReadFile(fileName string) (string, error) {
filePath := fmt.Sprintf("%s/%s", configDir, fileName) filePath := fmt.Sprintf("%s/%s", configDir, fileName)
bytes, err := ioutil.ReadFile(filePath) fileData, err := ReadFileBytes(filePath)
if err != nil { if err != nil {
return "", err return "", err
} }
return string(bytes), nil return string(fileData), nil
} }

View File

@ -40,7 +40,7 @@ func (tracker *FocusTracker) Prev() {
func (tracker *FocusTracker) blur(idx int) { func (tracker *FocusTracker) blur(idx int) {
view := tracker.Widgets[idx].TextView() view := tracker.Widgets[idx].TextView()
view.Blur() view.Blur()
view.SetBorderColor(ColorFor(Config.UString("wtf.border.normal"))) view.SetBorderColor(ColorFor(Config.UString("wtf.border.normal", "gray")))
} }
func (tracker *FocusTracker) decrement() { func (tracker *FocusTracker) decrement() {
@ -54,7 +54,7 @@ func (tracker *FocusTracker) decrement() {
func (tracker *FocusTracker) focus(idx int) { func (tracker *FocusTracker) focus(idx int) {
view := tracker.Widgets[idx].TextView() view := tracker.Widgets[idx].TextView()
tracker.App.SetFocus(view) tracker.App.SetFocus(view)
view.SetBorderColor(ColorFor(Config.UString("wtf.border.focus"))) view.SetBorderColor(ColorFor(Config.UString("wtf.border.focus", "gray")))
} }
func (tracker *FocusTracker) increment() { func (tracker *FocusTracker) increment() {

View File

@ -10,10 +10,6 @@ import (
"github.com/rivo/tview" "github.com/rivo/tview"
) )
// DateFormat defines the format we expect to receive dates from BambooHR in
const DateFormat = "2006-01-02"
const TimeFormat = "15:04"
func CenterText(str string, width int) string { func CenterText(str string, width int) string {
return fmt.Sprintf("%[1]*s", -width, fmt.Sprintf("%[1]*s", (width+len(str))/2, str)) return fmt.Sprintf("%[1]*s", -width, fmt.Sprintf("%[1]*s", (width+len(str))/2, str))
} }
@ -50,14 +46,6 @@ func Exclude(strs []string, val string) bool {
return true return true
} }
func IsToday(date time.Time) bool {
now := time.Now()
return (date.Year() == now.Year()) &&
(date.Month() == now.Month()) &&
(date.Day() == now.Day())
}
func NameFromEmail(email string) string { func NameFromEmail(email string) string {
parts := strings.Split(email, "@") parts := strings.Split(email, "@")
return strings.Title(strings.Replace(parts[0], ".", " ", -1)) return strings.Title(strings.Replace(parts[0], ".", " ", -1))
@ -73,16 +61,7 @@ func NamesFromEmails(emails []string) []string {
return names return names
} }
func PrettyDate(dateStr string) string { func ReadFileBytes(filePath string) ([]byte, error) {
newTime, _ := time.Parse(DateFormat, dateStr)
return fmt.Sprint(newTime.Format("Jan 2, 2006"))
}
func Now() time.Time {
return time.Now().Local()
}
func ReadYamlFile(filePath string) ([]byte, error) {
fileData, err := ioutil.ReadFile(filePath) fileData, err := ioutil.ReadFile(filePath)
if err != nil { if err != nil {
return []byte{}, err return []byte{}, err
@ -96,6 +75,8 @@ func RightAlignFormat(view *tview.TextView) string {
return fmt.Sprintf("%%%ds", w-1) return fmt.Sprintf("%%%ds", w-1)
} }
/* -------------------- Slice Conversion -------------------- */
func ToInts(slice []interface{}) []int { func ToInts(slice []interface{}) []int {
results := []int{} results := []int{}
for _, val := range slice { for _, val := range slice {
@ -105,10 +86,6 @@ func ToInts(slice []interface{}) []int {
return results return results
} }
func Tomorrow() time.Time {
return Now().AddDate(0, 0, 1)
}
func ToStrs(slice []interface{}) []string { func ToStrs(slice []interface{}) []string {
results := []string{} results := []string{}
for _, val := range slice { for _, val := range slice {
@ -118,6 +95,33 @@ func ToStrs(slice []interface{}) []string {
return results return results
} }
/* -------------------- Date/Time -------------------- */
// DateFormat defines the format we expect to receive dates from BambooHR in
const DateFormat = "2006-01-02"
const TimeFormat = "15:04"
func IsToday(date time.Time) bool {
now := Now()
return (date.Year() == now.Year()) &&
(date.Month() == now.Month()) &&
(date.Day() == now.Day())
}
func Now() time.Time {
return time.Now().Local()
}
func PrettyDate(dateStr string) string {
newTime, _ := time.Parse(DateFormat, dateStr)
return fmt.Sprint(newTime.Format("Jan 2, 2006"))
}
func Tomorrow() time.Time {
return Now().AddDate(0, 0, 1)
}
func UnixTime(unix int64) time.Time { func UnixTime(unix int64) time.Time {
return time.Unix(unix, 0) return time.Unix(unix, 0)
} }