From d7f988f220a91a233eba97de89ffa549c968c63a Mon Sep 17 00:00:00 2001 From: Kevin Kuehler Date: Thu, 10 Oct 2019 15:27:10 -0700 Subject: 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 --- worker/types/thread.go | 40 ++++++++++++++++++++++++++++++++++++++++ 1 file changed, 40 insertions(+) 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) + } + } + +} -- cgit v1.2.3