diff options
author | Kevin Kuehler <keur@xcf.berkeley.edu> | 2019-10-28 12:07:03 -0700 |
---|---|---|
committer | Ben Burwell <ben@benburwell.com> | 2019-10-29 11:07:36 -0400 |
commit | d1414f13c929df81133d7c3cf9192c4e368162bc (patch) | |
tree | de4b13bf7cce9fc3f1444ebbb3ecdf4f0c203e96 | |
parent | 75ee7826e16a63f296a04774cf49fb855f70f69c (diff) |
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.go | 40 |
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) + } + } + +} |