Skip to content

Commit 65b72dc

Browse files
committed
Factor Volatile::Tree out of Volatile::DirTree
1 parent 4833a58 commit 65b72dc

File tree

3 files changed

+48
-24
lines changed

3 files changed

+48
-24
lines changed

META6.json

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@
5959
"Terminal::Widgets::Viewer::Log": "lib/Terminal/Widgets/Viewer/Log.rakumod",
6060
"Terminal::Widgets::Viz::SmokeChart": "lib/Terminal/Widgets/Viz/SmokeChart.rakumod",
6161
"Terminal::Widgets::Volatile::DirTree": "lib/Terminal/Widgets/Volatile/DirTree.rakumod",
62+
"Terminal::Widgets::Volatile::Tree": "lib/Terminal/Widgets/Volatile/Tree.rakumod",
6263
"Terminal::Widgets::Widget": "lib/Terminal/Widgets/Widget.rakumod"
6364
},
6465
"resources": [

lib/Terminal/Widgets/Volatile/DirTree.rakumod

Lines changed: 17 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -2,31 +2,27 @@
22

33
unit module Terminal::Widgets::Volatile::DirTree;
44

5+
use Terminal::Widgets::Volatile::Tree;
56

6-
role Node {
7-
has IO::Path:D() $.path is required;
8-
has Node $.parent;
7+
constant Tree = Terminal::Widgets::Volatile::Tree;
98

109

11-
#| Simplified gist that does not traverse parents
12-
method gist(::?CLASS:D:) {
13-
my $short-name = self.^name.subst('Terminal::Widgets::Volatile::', '');
14-
$short-name ~ ':' ~ $!path.path
15-
}
10+
role PathContainer {
11+
has IO::Path:D() $.path is required;
1612

17-
#| Find root node via parent chain, runtime is O(depth)
18-
method root(::?CLASS:D:) {
19-
my $root = self;
20-
$root .= parent while $root.parent;
21-
$root
13+
#| Simplified gist that does not traverse parents, and includes path
14+
method gist(::?CLASS:D:) {
15+
self.gist-name ~ ':' ~ $!path.path
2216
}
2317
}
2418

25-
class Dev does Node {
26-
}
19+
role Node does Tree::Node does PathContainer { }
20+
role Leaf does Tree::Leaf does PathContainer { }
21+
role Parent does Tree::Parent does PathContainer { }
2722

28-
class File does Node {
29-
}
23+
class Misc does Node { }
24+
class File does Leaf { }
25+
class Dev does Node { }
3026

3127
class SymLink does Node {
3228
has IO::Path:D() $.target is required;
@@ -38,9 +34,9 @@ class SymLink does Node {
3834
}
3935
}
4036

41-
class Dir does Node {
42-
has Node:D @!children is built;
43-
has Instant:D $.cache-time .= from-posix-nanos(0);
37+
class Dir does Parent {
38+
has Tree::Node:D @!children is built;
39+
has Instant:D $.cache-time .= from-posix-nanos(0);
4440

4541

4642
#| Lazily find (and cache) children, forcing a refresh if requested
@@ -57,12 +53,9 @@ class Dir does Node {
5753
.l ?? SymLink.new(parent => self, path => $_, target => .readlink) !!
5854
.f ?? File.new( parent => self, path => $_) !!
5955
.dev ?? Dev.new( parent => self, path => $_) !!
60-
Node.new( parent => self, path => $_) ;
56+
Misc.new( parent => self, path => $_) ;
6157
};
6258
}
6359
@!children
6460
}
6561
}
66-
67-
class Root is Dir {
68-
}
Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,30 @@
1+
# ABSTRACT: Base roles for volatile tree data structures
2+
3+
unit module Terminal::Widgets::Volatile::Tree;
4+
5+
6+
#| Basic generic tree node that knows its parent (if any)
7+
role Node {
8+
has Node $.parent;
9+
10+
#| Shortened name for gists
11+
method gist-name() {
12+
self.^name.subst('Terminal::Widgets::', '')
13+
}
14+
15+
#| Find root node via parent chain, runtime is O(depth)
16+
method root(::?CLASS:D:) {
17+
my $root = self;
18+
$root .= parent while $root.parent;
19+
$root
20+
}
21+
}
22+
23+
#| A pure leaf node, no children ever
24+
role Leaf does Node { }
25+
26+
#| A parent node, which MAY have children at any given time
27+
role Parent does Node {
28+
#| REQUIRED: Lazily find (and maybe cache) children, forcing a refresh if requested
29+
method children(::?CLASS:D: Bool:D :$refresh = False) { ... }
30+
}

0 commit comments

Comments
 (0)