|
| 1 | +# ABSTRACT: A viewer/browser for a Volatile::Tree |
| 2 | + |
| 3 | +use Terminal::Widgets::Widget; |
| 4 | +use Terminal::Widgets::Scrollable; |
| 5 | +use Terminal::Widgets::Focusable; |
| 6 | +use Terminal::Widgets::Volatile::Tree; |
| 7 | + |
| 8 | +constant VTree = Terminal::Widgets::Volatile::Tree; |
| 9 | + |
| 10 | + |
| 11 | +my role DisplayNode { |
| 12 | + has DisplayNode $.parent; |
| 13 | + has VTree::Node $.data is required; |
| 14 | + has UInt:D $.depth is required; |
| 15 | + |
| 16 | + # REQUIRED: Total number of entries in this node and any visible children |
| 17 | + method branch-size(--> UInt:D) { ... } |
| 18 | +} |
| 19 | + |
| 20 | +my class DisplayLeaf does DisplayNode { |
| 21 | + method branch-size(--> 1) { } |
| 22 | +} |
| 23 | + |
| 24 | +my class DisplayParent does DisplayNode { |
| 25 | + has DisplayNode:D @.children; |
| 26 | + has Bool:D $.expanded = False; |
| 27 | + |
| 28 | + method refresh-children() { |
| 29 | + my $depth = $!depth + 1; |
| 30 | + @!children = $.data.children(:refresh).sort(*.short-name).map: { |
| 31 | + $_ ~~ VTree::Parent |
| 32 | + ?? DisplayParent.new(parent => self, data => $_, :$depth) |
| 33 | + !! DisplayLeaf.new( parent => self, data => $_, :$depth) |
| 34 | + } |
| 35 | + } |
| 36 | + |
| 37 | + method toggle-expanded() { self.set-expanded(!$!expanded) } |
| 38 | + |
| 39 | + method set-expanded($!expanded) { |
| 40 | + if $!expanded { |
| 41 | + self.refresh-children; |
| 42 | + } |
| 43 | + else { |
| 44 | + @!children = Empty; |
| 45 | + } |
| 46 | + } |
| 47 | + |
| 48 | + method branch-size(--> UInt:D) { |
| 49 | + $!expanded ?? 1 + @!children.map(*.branch-size).sum |
| 50 | + !! 1 |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | + |
| 55 | +class Terminal::Widgets::Viewer::Tree |
| 56 | + is Terminal::Widgets::Widget |
| 57 | + does Terminal::Widgets::Scrollable |
| 58 | + does Terminal::Widgets::Focusable { |
| 59 | + has VTree::Node $.root; |
| 60 | + has DisplayParent $.display-root is built(False); |
| 61 | + |
| 62 | + # Keep root and display-root in sync |
| 63 | + method set-root(VTree::Node:D $!root) { self!remap-root } |
| 64 | + method !remap-root() { |
| 65 | + $!display-root = DisplayParent.new(data => $!root, depth => 0); |
| 66 | + } |
| 67 | + |
| 68 | + method draw-content() { |
| 69 | + my @lines = self.node-lines($!display-root); |
| 70 | + .note for @lines; |
| 71 | + } |
| 72 | + |
| 73 | + #| Displayable lines for a given node |
| 74 | + method node-lines($node) { |
| 75 | + my $is-parent = $node ~~ DisplayParent; |
| 76 | + my $first-line = self.prefix-string($node) |
| 77 | + ~ self.node-content($node); |
| 78 | + |
| 79 | + $is-parent ?? ($first-line, |
| 80 | + $node.children.map({ self.node-lines($_).Slip })).flat |
| 81 | + !! ($first-line, ) |
| 82 | + } |
| 83 | + |
| 84 | + #| Prefix for first line of a given node |
| 85 | + method prefix-string($node) { |
| 86 | + ' ' x $node.depth |
| 87 | + ~ ($node ~~ DisplayParent ?? self.arrows()[+$node.expanded] !! ' ') |
| 88 | + ~ ' ' |
| 89 | + } |
| 90 | + |
| 91 | + #| Displayed content for a given node itself, not including children |
| 92 | + method node-content($node) { |
| 93 | + $node.data.short-name |
| 94 | + } |
| 95 | + |
| 96 | + #| Arrow glyphs for given terminal capabilities |
| 97 | + method arrows($caps = self.terminal.caps) { |
| 98 | + my constant %arrows = |
| 99 | + ASCII => « > v », |
| 100 | + MES2 => « > ∨ », |
| 101 | + Uni7 => « ⮞ ⮟ »; |
| 102 | + |
| 103 | + $caps.best-symbol-choice(%arrows) |
| 104 | + } |
| 105 | +} |
0 commit comments