X-Git-Url: https://git.giacomolli.uk/onechat/blobdiff_plain/6e20c4df4a0eac7f993d7783e82eb4c1ca6d8c70..442c68e1d384a7da0d9ab039704bf763a1c3ac35:/tree.go?ds=inline diff --git a/tree.go b/tree.go index ee3ff68..30a40ae 100644 --- a/tree.go +++ b/tree.go @@ -1,12 +1,14 @@ package main import ( + "cmp" "context" "encoding/json" "fmt" "math/bits" "os" "path/filepath" + "slices" "time" "unicode/utf8" ) @@ -147,6 +149,20 @@ func (t *Tree) Node(level, index int) (Node, bool) { return node, built } +// Nodes returns the built nodes, ordered by level and then by index. +func (t *Tree) Nodes() []Node { + nodes := make([]Node, 0, len(t.nodes)) + for _, node := range t.nodes { + nodes = append(nodes, node) + } + + slices.SortFunc(nodes, func(a, b Node) int { + return cmp.Or(cmp.Compare(a.Level, b.Level), cmp.Compare(a.Index, b.Index)) + }) + + return nodes +} + // leafSource returns the text that the leaf of message holds or summarizes. func leafSource(message Message) string { return message.Kind + ": " + message.Text