Skip to content

Commit d23bac4

Browse files
committed
Add node highlighting to Viewer::Tree
1 parent 20b4962 commit d23bac4

File tree

2 files changed

+34
-7
lines changed

2 files changed

+34
-7
lines changed

lib/Terminal/Widgets/Viewer/DirTree.rakumod

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@ class Terminal::Widgets::Viewer::DirTree
1414
#| Displayed content for a given node itself, not including children
1515
method node-content($node) {
1616
my $color = $.dir-colors.color-for($node.data.path);
17+
$color = 'inverse ' ~ $color if $node === $.current-node;
1718
span($color, $node.data.short-name)
1819
}
1920
}

lib/Terminal/Widgets/Viewer/Tree.rakumod

Lines changed: 33 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -76,8 +76,8 @@ class Terminal::Widgets::Viewer::Tree
7676
method set-root(VTree::Node:D $!root) { self!remap-root }
7777
method !remap-root() {
7878
$!display-root = DisplayParent.new(data => $!root, depth => 0, :&.sort-by);
79-
$!current-node = $!display-root;
8079
self.clear-caches;
80+
self.select-node($!display-root);
8181
}
8282

8383
# Clear caches when setting sort-by
@@ -151,7 +151,8 @@ class Terminal::Widgets::Viewer::Tree
151151

152152
#| Displayed content for a given node itself, not including children
153153
method node-content($node) {
154-
span('', $node.data.short-name)
154+
my $color = $node === $!current-node ?? 'inverse' !! '';
155+
span($color, $node.data.short-name)
155156
}
156157

157158
#| Arrow glyphs for given terminal capabilities
@@ -174,14 +175,39 @@ class Terminal::Widgets::Viewer::Tree
174175
self.flat-node-cache.first(* === $node, :k)
175176
}
176177

178+
#| Remove highlight from a node
179+
method remove-highlight($node) {
180+
my $line = self.display-node-to-line($node);
181+
return unless $line.defined;
182+
183+
my @line-spans := self.flat-line-cache[$line];
184+
my $color = @line-spans[1].color.subst('inverse ', '');
185+
@line-spans[1] = span($color, @line-spans[1].text);
186+
}
187+
188+
#| Add a highlight to a node
189+
method add-highlight($node) {
190+
self.ensure-parents-expanded($node);
191+
my $line = self.display-node-to-line($node);
192+
return unless $line.defined;
193+
194+
my @line-spans := self.flat-line-cache[$line];
195+
@line-spans[1] = span('inverse ' ~ @line-spans[1].color,
196+
@line-spans[1].text);
197+
}
198+
177199
#| Select a given node as current, expanding parents if needed and
178200
#| processing a "click" on the node
179201
method select-node($node) {
180-
$!current-node = $node;
181-
self.ensure-parents-expanded($node);
182-
$_($node) with &!process-click;
183-
184-
# XXXX: Ensure visible?
202+
if $!current-node !=== $node {
203+
self.remove-highlight($!current-node);
204+
$!current-node = $node;
205+
self.ensure-parents-expanded($node);
206+
self.add-highlight($node);
207+
self.full-refresh;
208+
$_($node) with &!process-click;
209+
# XXXX: Ensure visible?
210+
}
185211
}
186212

187213
#| Select the immediately previous node from the current one,

0 commit comments

Comments
 (0)