cargo_plot/core/path_view/
list.rs1use super::node::FileNode;
2use crate::core::file_stats::weight::{self, WeightConfig};
3use crate::core::path_matcher::SortStrategy;
4use crate::theme::for_path_list::get_icon_for_path;
5use colored::Colorize;
6pub struct PathList {
8 items: Vec<FileNode>,
9}
10
11impl PathList {
12 pub fn build(
14 paths_strings: &[String],
15 base_dir: &str,
16 sort_strategy: SortStrategy,
17 weight_cfg: &WeightConfig,
18 no_emoji: bool,
19 ) -> Self {
20 let mut items: Vec<FileNode> = paths_strings
22 .iter()
23 .map(|p_str| {
24 let absolute = std::path::Path::new(base_dir).join(p_str);
25 let is_dir = p_str.ends_with('/');
26 let weight_bytes =
27 crate::core::file_stats::weight::get_path_weight(&absolute, true);
28 let weight_str = weight::format_weight(weight_bytes, is_dir, weight_cfg);
29
30 FileNode {
31 name: p_str.clone(),
32 path: absolute,
33 is_dir,
34 icon: if no_emoji {
35 String::new()
36 } else {
37 get_icon_for_path(p_str).to_string()
38 },
39 weight_str,
40 weight_bytes,
41 children: vec![], }
43 })
44 .collect();
45
46 FileNode::sort_slice(&mut items, sort_strategy);
47
48 Self { items }
49 }
50
51 pub fn render_cli(&self, _is_match: bool) -> String {
53 let mut out = String::new();
54 for item in &self.items {
57 let line = format!(
58 "{} {} {}\n",
59 item.weight_str.truecolor(120, 120, 120),
60 item.icon,
61 if item.is_dir {
62 item.name.yellow()
63 } else {
64 item.name.white()
65 }
66 );
67 out.push_str(&line);
68 }
69 out
70 }
71
72 #[must_use]
73 pub fn render_txt(&self) -> String {
74 let mut out = String::new();
75 for item in &self.items {
76 let line = format!("{} {} {}\n", item.weight_str, item.icon, item.name);
78 out.push_str(&line);
79 }
80 out
81 }
82}