aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorKevin Kuehler <keur@xcf.berkeley.edu>2019-10-10 15:27:10 -0700
committerBen Burwell <ben@benburwell.com>2019-10-12 20:57:57 -0400
commitd7f988f220a91a233eba97de89ffa549c968c63a (patch)
tree3f94284b8affeb30d8a9509c13b10b9759174f7e
parent9e47953ebe3bafdfc4d3af4ed26dc7ca6c2053fd (diff)
worker/types/thread: Add FormatThread function
FormatThread performs dfs on a thread tree. For every node in the tree, a callback function is called with a thread and a format string for that thread. The format string is to be used when displaying the full thread tree. Signed-off-by: Kevin Kuehler <keur@xcf.berkeley.edu>
-rw-r--r--worker/types/thread.go40
1 files changed, 40 insertions, 0 deletions
diff --git a/worker/types/thread.go b/worker/types/thread.go
index 265438f..a51a5c4 100644
--- a/worker/types/thread.go
+++ b/worker/types/thread.go
@@ -4,3 +4,43 @@ type Thread struct {
Uid uint32
Children []*Thread
}
+
+func (t *Thread) FormatThread(cb func(*Thread, []rune) bool) {
+ cb(t, []rune{})
+ walkThreads(t.Children, []rune{}, cb)
+}
+
+func walkThreads(threads []*Thread, threadFmt []rune,
+ cb func(*Thread, []rune) bool) {
+ if threads == nil {
+ return
+ }
+
+ var (
+ indent []rune = []rune{'\u0020', '\u0020'}
+ indentConnected []rune = []rune{'\u2502', '\u0020'}
+ arrow []rune = []rune{'\u251c', '\u2500', '\u003e'}
+ arrowConnected []rune = []rune{'\u2514', '\u2500', '\u003e'}
+
+ threadPrefix []rune = append(threadFmt, arrow...)
+ threadPrefixConnected []rune = append(threadFmt, arrowConnected...)
+ nextThread []rune = append(threadFmt, indent...)
+ nextThreadConnected []rune = append(threadFmt, indentConnected...)
+ )
+
+ for i := len(threads) - 1; i >= 0; i-- {
+ t := threads[i]
+ if i > 0 && len(threads) > 1 {
+ if cb(t, threadPrefix) {
+ return
+ }
+ walkThreads(t.Children, nextThreadConnected, cb)
+ } else {
+ if cb(t, threadPrefixConnected) {
+ return
+ }
+ walkThreads(t.Children, nextThread, cb)
+ }
+ }
+
+}