diff --git a/.all-contributorsrc b/.all-contributorsrc index 43bf7e57..f0232bd6 100644 --- a/.all-contributorsrc +++ b/.all-contributorsrc @@ -210,6 +210,13 @@ "name": "Amr Tamimi", "avatar_url": "https://avatars3.githubusercontent.com/u/21756?v=4", "profile": "https://tamimi.se", + "contributions": [] + }, + { + "login": "jdsingh", + "name": "Jagdeep Singh", + "avatar_url": "https://avatars3.githubusercontent.com/u/3717137?v=4", + "profile": "https://jagdeep.me", "contributions": [ ] } diff --git a/README.md b/README.md index 1147ee25..cf71889f 100644 --- a/README.md +++ b/README.md @@ -55,7 +55,7 @@ Thanks goes to these wonderful people: | [
baustinanki](https://github.com/baustinanki)
| [
lucus lee](https://github.com/lixin9311)
| [
Mike Lloyd](https://github.com/mxplusb)
| [
Sergio Rubio](http://rubiojr.rbel.co)
| [
Farhad Farahi](https://github.com/FarhadF)
| [
Lasantha Kularatne](http://lasantha.blogspot.com/)
| [
Mark Old](https://github.com/dlom)
| | [
flw](http://flw.tools/)
| [
David Barda](https://github.com/davebarda)
| [
Geoff Lee](https://github.com/matrinox)
| [
George Opritescu](http://international.github.io)
| [
Grazfather](https://twitter.com/Grazfather)
| [
Michael Cordell](http://www.mikecordell.com/)
| [
Patrick José Pereira](http://patrick.ibexcps.com)
| | [
sherod taylor](https://github.com/sherodtaylor)
| [
Andrew Scott](http://cogentia.io)
| [
Anand Sudhir Prayaga](https://github.com/anandsudhir)
| [
Lassi Piironen](https://github.com/lsipii)
| [
BlackWebWolf](https://github.com/BlackWebWolf)
| [
andrewzolotukhin](https://github.com/andrewzolotukhin)
| [
Leon Stigter](https://retgits.github.io)
| -| [
Amr Tamimi](https://tamimi.se)
| +| [
Amr Tamimi](https://tamimi.se)
| [
Jagdeep Singh](https://jagdeep.me)
| This project follows the [all-contributors](https://github.com/kentcdodds/all-contributors) specification. Contributions of any kind welcome! diff --git a/cfg/config_files.go b/cfg/config_files.go index 5d41bfcd..21eb1a67 100644 --- a/cfg/config_files.go +++ b/cfg/config_files.go @@ -133,22 +133,6 @@ func LoadConfigFile(filePath string) *config.Config { return cfg } -func ReadConfigFile(fileName string) (string, error) { - configDir, err := ConfigDir() - if err != nil { - return "", err - } - - filePath := fmt.Sprintf("%s/%s", configDir, fileName) - - fileData, err := wtf.ReadFileBytes(filePath) - if err != nil { - return "", err - } - - return string(fileData), nil -} - const simpleConfig = `wtf: colors: border: diff --git a/wtf/colors.go b/wtf/colors.go index b430910c..406e67be 100644 --- a/wtf/colors.go +++ b/wtf/colors.go @@ -4,7 +4,7 @@ import ( "github.com/gdamore/tcell" ) -var Colors = map[string]tcell.Color{ +var colors = map[string]tcell.Color{ "aliceblue": tcell.ColorAliceBlue, "antiquewhite": tcell.ColorAntiqueWhite, "aqua": tcell.ColorAqua, @@ -148,8 +148,8 @@ var Colors = map[string]tcell.Color{ } func ColorFor(label string) tcell.Color { - if _, ok := Colors[label]; ok { - return Colors[label] + if _, ok := colors[label]; ok { + return colors[label] } else { return tcell.ColorGreen } diff --git a/wtf/datetime.go b/wtf/datetime.go new file mode 100644 index 00000000..b2d1f2bd --- /dev/null +++ b/wtf/datetime.go @@ -0,0 +1,35 @@ +package wtf + +import ( + "fmt" + "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 { + return time.Unix(unix, 0) +} diff --git a/wtf/utils.go b/wtf/utils.go index c65a925b..1bb6ec9e 100644 --- a/wtf/utils.go +++ b/wtf/utils.go @@ -6,7 +6,6 @@ import ( "os/exec" "regexp" "strings" - "time" "github.com/rivo/tview" ) @@ -18,6 +17,10 @@ const FriendlyDateTimeFormat = "Mon, Jan 2, 15:04" const TimestampFormat = "2006-01-02T15:04:05-0700" 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)) } @@ -151,34 +154,3 @@ func ToStrs(slice []interface{}) []string { 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 { - return time.Unix(unix, 0) -} diff --git a/wtf_tests/colors_test.go b/wtf_tests/colors_test.go new file mode 100644 index 00000000..a52da11b --- /dev/null +++ b/wtf_tests/colors_test.go @@ -0,0 +1,14 @@ +package wtf_tests + +import ( + "testing" + + "github.com/gdamore/tcell" + . "github.com/senorprogrammer/wtf/wtf" + . "github.com/stretchr/testify/assert" +) + +func TestColorFor(t *testing.T) { + Equal(t, tcell.ColorRed, ColorFor("red")) + Equal(t, tcell.ColorGreen, ColorFor("cat")) +} diff --git a/wtf_tests/datetime_test.go b/wtf_tests/datetime_test.go new file mode 100644 index 00000000..8415d345 --- /dev/null +++ b/wtf_tests/datetime_test.go @@ -0,0 +1,21 @@ +package wtf_tests + +import ( + "testing" + "time" + + . "github.com/senorprogrammer/wtf/wtf" + . "github.com/stretchr/testify/assert" +) + +func TestIsToday(t *testing.T) { + Equal(t, true, IsToday(time.Now().Local())) + Equal(t, false, IsToday(time.Now().AddDate(0, 0, -1))) + Equal(t, false, IsToday(time.Now().AddDate(0, 0, +1))) +} + +/* -------------------- PrettyDate() -------------------- */ + +func TestPrettyDate(t *testing.T) { + Equal(t, "Oct 21, 1999", PrettyDate("1999-10-21")) +} diff --git a/wtf_tests/utils_test.go b/wtf_tests/utils_test.go index a069301c..d78425cc 100644 --- a/wtf_tests/utils_test.go +++ b/wtf_tests/utils_test.go @@ -3,62 +3,60 @@ package wtf_tests import ( "testing" - "github.com/go-test/deep" . "github.com/senorprogrammer/wtf/wtf" + . "github.com/stretchr/testify/assert" ) +/* -------------------- CenterText() -------------------- */ + +func TestCenterText(t *testing.T) { + Equal(t, "cat", CenterText("cat", -9)) + Equal(t, "cat", CenterText("cat", 0)) + Equal(t, " cat ", CenterText("cat", 9)) +} + +/* -------------------- FindMatch() -------------------- */ + +func TestFindMatch(t *testing.T) { + var result [][]string + + expected := [][]string([][]string{[]string{"SSID: 7E5B5C", "7E5B5C"}}) + result = FindMatch(`s*SSID: (.+)s*`, "SSID: 7E5B5C") + Equal(t, expected, result) +} + /* -------------------- Exclude() -------------------- */ func TestExcludeWhenTrue(t *testing.T) { - if Exclude([]string{"cat", "dog", "rat"}, "bat") != true { - t.Fatalf("Expected true but got false") - } -} - -func TestExcludeWhenFalse(t *testing.T) { - if Exclude([]string{"cat", "dog", "rat"}, "dog") != false { - t.Fatalf("Expected false but got true") - } + Equal(t, true, Exclude([]string{"cat", "dog", "rat"}, "bat")) + Equal(t, false, Exclude([]string{"cat", "dog", "rat"}, "dog")) } /* -------------------- NameFromEmail() -------------------- */ -func TestNameFromEmailWhenEmpty(t *testing.T) { - expected := "" - actual := NameFromEmail("") - - if expected != actual { - t.Fatalf("Expected %s but got %s", expected, actual) - } -} - -func TestNameFromEmailWithEmail(t *testing.T) { - expected := "Chris Cummer" - actual := NameFromEmail("chris.cummer@me.com") - - if expected != actual { - t.Fatalf("Expected %s but got %s", expected, actual) - } +func TestNameFromEmail(t *testing.T) { + Equal(t, "", NameFromEmail("")) + Equal(t, "Chris Cummer", NameFromEmail("chris.cummer@me.com")) } /* -------------------- NamesFromEmails() -------------------- */ -func TestNamesFromEmailsWhenEmpty(t *testing.T) { - expected := []string{} - actual := NamesFromEmails([]string{}) +func TestNamesFromEmails(t *testing.T) { + var result []string - if diff := deep.Equal(expected, actual); diff != nil { - t.Fatalf("Expected %s but got %s", expected, actual) - } + 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 TestNamesFromEmailsWithEmails(t *testing.T) { - expected := []string{"Chris Cummer", "Chriscummer"} - actual := NamesFromEmails([]string{"chris.cummer@me.com", "chriscummer@me.com"}) +/* -------------------- PadRow() -------------------- */ - if diff := deep.Equal(expected, actual); diff != nil { - t.Fatalf("Expected %s but got %s", expected, actual) - } +func TestPadRow(t *testing.T) { + Equal(t, "", PadRow(0, 0)) + Equal(t, "", PadRow(5, 2)) + Equal(t, " ", PadRow(1, 2)) } /* -------------------- ToInts() -------------------- */ @@ -71,11 +69,7 @@ func TestToInts(t *testing.T) { source[idx] = val } - actual := ToInts(source) - - if diff := deep.Equal(expected, actual); diff != nil { - t.Fatalf("Expected %v but got %v", expected, actual) - } + Equal(t, expected, ToInts(source)) } /* -------------------- ToStrs() -------------------- */ @@ -88,20 +82,5 @@ func TestToStrs(t *testing.T) { source[idx] = val } - actual := ToStrs(source) - - if diff := deep.Equal(expected, actual); diff != nil { - t.Fatalf("Expected %s but got %s", expected, actual) - } -} - -/* -------------------- PrettyDate() -------------------- */ - -func TestPrettyDate(t *testing.T) { - expected := "Oct 21, 1999" - actual := PrettyDate("1999-10-21") - - if expected != actual { - t.Fatalf("Expected %s but got %s", expected, actual) - } + Equal(t, expected, ToStrs(source)) }