From 2ba309fe36f8c2d265bffeb3ae9f93a041b5a075 Mon Sep 17 00:00:00 2001 From: Brian Buller Date: Thu, 29 Apr 2021 10:11:51 -0500 Subject: [PATCH] Add support for i3status-rust --- app_state.go | 4 ++++ helpers.go | 15 +++++++++++++++ task_ops.go | 29 +++++++++++++++++++++++++++++ 3 files changed, 48 insertions(+) diff --git a/app_state.go b/app_state.go index 8f0d929..8d6be13 100644 --- a/app_state.go +++ b/app_state.go @@ -167,6 +167,10 @@ func (a *AppState) initialize() { } a.ValidOperations = make(map[string][]string) a.OpFuncs = make(map[string]func([]string) int) + a.addOperation("i3status", + []string{"i3status - Output json appropriate for an i3status-rust block"}, + a.opI3Status, + ) a.addOperation("ls", []string{"ls - List Tasks"}, a.opListTasks, diff --git a/helpers.go b/helpers.go index 5000c71..fa459ff 100644 --- a/helpers.go +++ b/helpers.go @@ -3,7 +3,10 @@ package main import ( "strconv" "strings" + "time" "unicode" + + todotxt "git.bullercodeworks.com/brian/go-todotxt" ) func itoa(val int) string { @@ -76,3 +79,15 @@ func sliceIsValidTags(v []string) bool { } return true } + +func copyTaskList(t todotxt.TaskList) todotxt.TaskList { + re := todotxt.NewTaskList() + for _, v := range t { + re.AddTask(&v) + } + return *re +} + +func dateWithinNextDay(d time.Time) bool { + return !d.IsZero() && d.Before(time.Now().Add(time.Hour*24)) +} diff --git a/task_ops.go b/task_ops.go index c6fdf1c..6e29762 100644 --- a/task_ops.go +++ b/task_ops.go @@ -8,6 +8,35 @@ import ( todotxt "git.bullercodeworks.com/brian/go-todotxt" ) +func (a *AppState) opI3Status(args []string) int { + var filterString string + state := "Idle" + if len(args) > 0 { + filterString = strings.Join(args, " ") + a.TaskList = a.getFilteredList(filterString) + } + total := len(*a.TaskList) + var incomplete int + for _, v := range *a.TaskList { + if !v.HasCompletedDate() { + incomplete++ + if dateWithinNextDay(v.DueDate) { + state = "Warning" + } + } + } + if state != "Warning" { + warning := a.config.Get("i3status_warning") + if warning != "" { + if len(*a.getFilteredList(warning)) > 0 { + state = "Warning" + } + } + } + fmt.Printf("{\"icon\":\"tasks\",\"state\":\"%s\", \"text\": \"%s: %d/%d\"}", state, filterString, incomplete, total) + return 0 +} + func (a *AppState) opListTasks(args []string) int { var lastIdx int var filterString string