diff options
-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) + } + } + +} |