Skip to content

Commit

Permalink
Refactoring main a bit
Browse files Browse the repository at this point in the history
  • Loading branch information
DMcP89 committed Dec 20, 2024
1 parent deb8718 commit 83d064a
Showing 1 changed file with 18 additions and 22 deletions.
40 changes: 18 additions & 22 deletions main.go
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,18 @@ func NewTabTextView(next *TabTextView) *TabTextView {
}
}

func GetTextForView(f func(string) (string, error), envVar string, missingEnvErrorMessage string) string {
if token, ok := os.LookupEnv(envVar); ok {
result, err := f(token)
if err != nil {
return err.Error()
}
return result
} else {
return missingEnvErrorMessage
}
}

func main() {
app := tview.NewApplication()
changeFunc := func() { app.Draw() }
Expand Down Expand Up @@ -93,33 +105,17 @@ func main() {
}()

go func() {
var result string
var err error
if token, ok := os.LookupEnv("TODOIST_TOKEN"); ok {
result, err = apis.GetTodaysTasks(token)
text := GetTextForView(apis.GetTodaysTasks, "TODOIST_TOKEN", "")
if text != "" {
tasksView.SetText(text)
} else {
if todoFile, ok := os.LookupEnv("TODO_FILE"); ok {
result, err = local.GetLocalTasks(todoFile)
} else {
tasksView.SetText("Please set either the TODOIST_TOKEN or TODO_FILE environment variable")
}
tasksView.SetText(GetTextForView(local.GetLocalTasks, "TODO_FILE", "Please set either the TODOIST_TOKEN or TODO_FILE environment variable"))
}
if err != nil {
tasksView.SetText(err.Error())
}
tasksView.SetText(result)
}()

go func() {
if POSTAL_CODE, ok := os.LookupEnv("TINYCARE_POSTAL_CODE"); ok {
result, err := apis.GetWeather(POSTAL_CODE)
if err != nil {
weatherView.SetText(err.Error())
}
weatherView.SetText(result)
} else {
weatherView.SetText("Please set TINYCARE_POSTAL_CODE environment variable")
}
text := GetTextForView(apis.GetWeather, "TINYCARE_POSTAL_CODE", "Please set TINYCARE_POSTAL_CODE environment variable")
weatherView.SetText(text)
}()

go func() {
Expand Down

0 comments on commit 83d064a

Please sign in to comment.