Skip to content

Commit

Permalink
change "[x]" to "[*]" when current view date != node's completion date (
Browse files Browse the repository at this point in the history
  • Loading branch information
climech authored and Jai Singh committed Apr 8, 2021
1 parent e59f344 commit b59280a
Show file tree
Hide file tree
Showing 2 changed files with 45 additions and 2 deletions.
36 changes: 35 additions & 1 deletion multitree/node.go
Original file line number Diff line number Diff line change
@@ -1,6 +1,9 @@
package multitree

import "fmt"
import (
"fmt"
"time"
)

type Node struct {
ID int64
Expand Down Expand Up @@ -46,6 +49,28 @@ func (n *Node) IsCompleted() bool {
return n.Completed != nil
}

// IsCompletedOnDate returns true if n was completed on date given as a string
// in the format "YYYY-MM-DD". The start of day is determined by offset, e.g. if
// offset is 4, the day starts at 4 A.M.
func (n *Node) IsCompletedOnDate(date string, offset int) bool {
t := n.TimeCompleted()

if !t.IsZero() {
start, err := time.Parse("2006-01-02", date)
if err != nil {
panic(err)
}
start = start.Local().Add(time.Duration(offset) * time.Hour)
end := start.Add(24 * time.Hour)

if t.Equal(start) || (t.After(start) && t.Before(end)) {
return true
}
}

return false
}

func (n *Node) IsInProgress() bool {
if n.IsCompleted() {
return false
Expand Down Expand Up @@ -73,6 +98,15 @@ func (n *Node) IsDateNode() bool {
return false
}

// TimeCompleted returns the task completion time as local time.Time.
func (n *Node) TimeCompleted() time.Time {
var t time.Time
if n.Completed != nil {
t = time.Unix(*n.Completed, 0)
}
return t
}

func (n *Node) Children() []*Node {
return n.children
}
Expand Down
11 changes: 10 additions & 1 deletion multitree/repr.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,6 +71,7 @@ const (
func (n *Node) StringTree() string {
var sb strings.Builder
var traverse func(*Node, []bool)
viewRoot := n.Tree().Roots()[0]

// The stack holds a boolean value for each of the node's indent levels. If
// the value is true, there are more siblings to come on that level, and the
Expand Down Expand Up @@ -104,7 +105,15 @@ func (n *Node) StringTree() string {
for _, i := range indents {
sb.WriteString(i)
}
sb.WriteString(n.String())

// Change "[x]" to "[*]" when current view date != node's completion date.
// TODO: make start of day configurable.
nodeStr := n.String()
if viewRoot.IsDateNode() && !n.IsCompletedOnDate(viewRoot.Name, 4) {
nodeStr = strings.Replace(nodeStr, "[x]", "[*]", 1)
}

sb.WriteString(nodeStr)
sb.WriteString("\n")

if len(n.children) != 0 {
Expand Down

0 comments on commit b59280a

Please sign in to comment.