Skip to content

Commit 7522f88

Browse files
committed
Add ptree display support for non-ModVec FileTrees
1 parent 4da4a3a commit 7522f88

2 files changed

Lines changed: 53 additions & 10 deletions

File tree

core/src/file_tree/display.rs

Lines changed: 46 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515

1616
//! Utilities for displaying the contents of a [`FileTree`].
1717
18+
use std::borrow::Cow;
1819
use std::io;
1920

2021
use nary_tree::NodeId;
@@ -23,15 +24,43 @@ use super::{FileTree, ModVec, TreeNodeKind};
2324
use crate::instance::Instance;
2425

2526
/// Structure to display [`FileTree`]s using [`ptree`].
26-
#[derive(Clone)]
27+
#[derive(Copy, Clone)]
2728
pub struct FileTreeDisplay<'a> {
29+
tree: &'a FileTree,
30+
current_node: NodeId,
31+
}
32+
33+
impl ptree::TreeItem for FileTreeDisplay<'_> {
34+
type Child = Self;
35+
36+
fn write_self<W: io::Write>(&self, f: &mut W, style: &ptree::Style) -> io::Result<()> {
37+
let node = self.tree.get(self.current_node).expect("node exists");
38+
match &node.data().kind {
39+
TreeNodeKind::Dir => write!(f, "📁 {}", style.paint(&node.data().name)),
40+
TreeNodeKind::File(()) => write!(f, "📄 {}", style.paint(&node.data().name)),
41+
}
42+
}
43+
44+
fn children(&self) -> Cow<'_, [Self::Child]> {
45+
let node = self.tree.get(self.current_node).expect("node exists");
46+
let children: Vec<_> = node
47+
.children()
48+
.map(|node| Self { tree: self.tree, current_node: node.node_id() })
49+
.collect();
50+
Cow::Owned(children)
51+
}
52+
}
53+
54+
/// Structure to display [`FileTree<ModVec>`]s using [`ptree`].
55+
#[derive(Copy, Clone)]
56+
pub struct ModVecFileTreeDisplay<'a> {
2857
tree: &'a FileTree<ModVec>,
2958
instance: &'a dyn Instance,
3059
current_node: NodeId,
3160
kind: FileTreeDisplayKind,
3261
}
3362

34-
/// Specifies what files are displayed by [`FileTreeDisplay`].
63+
/// Specifies what files are displayed by [`ModVecFileTreeDisplay`].
3564
#[derive(Copy, Clone, Debug, PartialEq, Eq)]
3665
pub enum FileTreeDisplayKind {
3766
/// Show all files.
@@ -40,7 +69,7 @@ pub enum FileTreeDisplayKind {
4069
Conflicts,
4170
}
4271

43-
impl<'a> FileTreeDisplay<'a> {
72+
impl<'a> ModVecFileTreeDisplay<'a> {
4473
#[must_use]
4574
pub fn new(tree: &'a FileTree<ModVec>, instance: &'a dyn Instance, kind: FileTreeDisplayKind) -> Self {
4675
Self {
@@ -52,7 +81,17 @@ impl<'a> FileTreeDisplay<'a> {
5281
}
5382
}
5483

55-
impl ptree::TreeItem for FileTreeDisplay<'_> {
84+
impl<'a> FileTreeDisplay<'a> {
85+
#[must_use]
86+
pub fn new(tree: &'a FileTree) -> Self {
87+
Self {
88+
tree,
89+
current_node: tree.root_id().expect("has root node"),
90+
}
91+
}
92+
}
93+
94+
impl ptree::TreeItem for ModVecFileTreeDisplay<'_> {
5695
type Child = Self;
5796

5897
fn write_self<W: io::Write>(&self, f: &mut W, style: &ptree::Style) -> io::Result<()> {
@@ -73,7 +112,7 @@ impl ptree::TreeItem for FileTreeDisplay<'_> {
73112
}
74113
}
75114

76-
fn children(&self) -> std::borrow::Cow<'_, [Self::Child]> {
115+
fn children(&self) -> Cow<'_, [Self::Child]> {
77116
let node = self.tree.get(self.current_node).expect("node exists");
78117
let children: Vec<_> = node
79118
.children()
@@ -89,13 +128,13 @@ impl ptree::TreeItem for FileTreeDisplay<'_> {
89128
TreeNodeKind::File(providing_mods) => providing_mods.len() > 1,
90129
}
91130
})
92-
.map(|node| FileTreeDisplay {
131+
.map(|node| Self {
93132
tree: self.tree,
94133
instance: self.instance,
95134
current_node: node.node_id(),
96135
kind: self.kind,
97136
})
98137
.collect();
99-
std::borrow::Cow::Owned(children)
138+
Cow::Owned(children)
100139
}
101140
}

deploy/src/main.rs

Lines changed: 7 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@ use anyhow::Context;
2828
use clap::Parser;
2929
use signal_hook::consts::SIGINT;
3030

31-
use mmm_core::file_tree::display::{FileTreeDisplay, FileTreeDisplayKind};
31+
use mmm_core::file_tree::display::{FileTreeDisplayKind, ModVecFileTreeDisplay};
3232
use mmm_core::file_tree::{FileTreeBuilder, new_tree};
3333

3434
use crate::instance::DeployInstance;
@@ -61,8 +61,12 @@ fn main() -> anyhow::Result<()> {
6161
FileTreeBuilder::new()
6262
.iter_mods(&mut tree, &mods)
6363
.context("failed to build tree of mod files")?;
64-
ptree::print_tree(&FileTreeDisplay::new(&tree, &mods, FileTreeDisplayKind::Conflicts))
65-
.context("failed to display file tree")?;
64+
ptree::print_tree(&ModVecFileTreeDisplay::new(
65+
&tree,
66+
&mods,
67+
FileTreeDisplayKind::Conflicts,
68+
))
69+
.context("failed to display file tree")?;
6670

6771
if matches!(mount_method, MountMethod::UserNamespace) {
6872
namespace::enter_namespace().context("failed to enter user namespace")?;

0 commit comments

Comments
 (0)