Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Tree and list command enhancements #33

Open
wants to merge 1 commit into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
75 changes: 57 additions & 18 deletions cmd/grit/cmds.go
Original file line number Diff line number Diff line change
Expand Up @@ -60,37 +60,70 @@ func cmdAdd(cmd *cli.Cmd) {
}

func cmdTree(cmd *cli.Cmd) {
cmd.Spec = "[NODE]"
today := time.Now().Format("2006-01-02")
cmd.Spec = "[-i] [-C] [NODE]"
var (
selector = cmd.StringArg("NODE", today, "node selector")
idsort = cmd.BoolOpt("i id-sort", false, "sort by id instead of name")
nochecked = cmd.BoolOpt("C no-checked", false, "filter out checked nodes")
selector = cmd.StringArg("NODE", "", "node selector")
)
cmd.Action = func() {
a, err := app.New()
if err != nil {
die(err)
}
defer a.Close()
var nodes []*multitree.Node

node, err := a.GetGraph(*selector)
if err != nil {
die(capitalize(err.Error()))
}
if node == nil {
die("Node does not exist")
if *selector == "" {
// List all dates by default.
dnodes, err := a.GetDateNodes()
if err != nil {
die(err)
}
for _, d := range dnodes {
// Get as part of multitree for accurate status.
n, err := a.GetGraph(d.ID)
if err != nil {
die(capitalize(err.Error()))
}
if n == nil {
continue
}
nodes = append(nodes, n)
}
} else {
n, err := a.GetGraph(*selector)
if err != nil {
die(capitalize(err.Error()))
}
if n == nil {
die("Node does not exist")
}
nodes = append(nodes, n)
}

node.TraverseDescendants(func(current *multitree.Node, _ func()) {
multitree.SortNodesByName(current.Children())
})
fmt.Print(node.StringTree())
for _, node := range nodes {
if *nochecked && !node.IsCompleted() {
fmt.Print(node.Name)
}
node.TraverseDescendants(func(current *multitree.Node, _ func()) {
if *idsort {
multitree.SortNodesByID(current.Children())
} else {
multitree.SortNodesByName(current.Children())
}
})
fmt.Print(node.StringTree(*nochecked))
}
}
}

func cmdList(cmd *cli.Cmd) {
cmd.Spec = "[NODE]"
cmd.Spec = "[-i] [-C] [NODE]"
var (
selector = cmd.StringArg("NODE", "", "node selector")
idsort = cmd.BoolOpt("i id-sort", false, "sort by id instead of name")
nochecked = cmd.BoolOpt("C no-checked", false, "filter out checked nodes")
selector = cmd.StringArg("NODE", "", "node selector")
)
cmd.Action = func() {
a, err := app.New()
Expand Down Expand Up @@ -128,9 +161,15 @@ func cmdList(cmd *cli.Cmd) {
nodes = node.Children()
}

multitree.SortNodesByName(nodes)
if *idsort {
multitree.SortNodesByID(nodes)
} else {
multitree.SortNodesByName(nodes)
}
for _, n := range nodes {
fmt.Println(n)
if !*nochecked || !n.IsCompleted() {
fmt.Println(n)
}
}
}
}
Expand Down Expand Up @@ -422,7 +461,7 @@ func cmdImport(cmd *cli.Cmd) {
if g, err := a.GetGraph(id); err != nil {
errs = append(errs, err)
} else {
fmt.Print(g.StringTree())
fmt.Print(g.StringTree(false))
treesTotal++
nodesTotal += len(g.Tree().All())
}
Expand Down
6 changes: 5 additions & 1 deletion multitree/repr.go
Original file line number Diff line number Diff line change
Expand Up @@ -68,7 +68,7 @@ const (
// ├──[ ] Clean up the kitchen (239)
// └──[ ] ...
//
func (n *Node) StringTree() string {
func (n *Node) StringTree(skipCompleted bool) string {
var sb strings.Builder
var traverse func(*Node, []bool)
viewRoot := n.Tree().Roots()[0]
Expand All @@ -80,6 +80,10 @@ func (n *Node) StringTree() string {
traverse = func(n *Node, stack []bool) {
var indents []string

if skipCompleted && n.IsCompleted() {
return
}

if len(stack) != 0 {
// Previous levels -- extend or leave blank.
for _, v := range stack[:len(stack)-1] {
Expand Down