Skip to content

Commit e9c2d00

Browse files
authored
add more options to tree(): --prefix and --tarballs (#16)
1 parent c25d0fe commit e9c2d00

File tree

2 files changed

+88
-15
lines changed

2 files changed

+88
-15
lines changed

Cargo.toml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
[package]
22
name = "godot-package-manager"
3-
version = "0.6.0"
3+
version = "0.7.0"
44
edition = "2021"
55
authors = ["bendn <[email protected]>"]
66
description = "A package manager for godot"

src/main.rs

Lines changed: 87 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -54,15 +54,32 @@ Produces output like
5454
#[arg(value_enum, default_value = "utf8", long = "charset")]
5555
/// Character set to print in.
5656
charset: CharSet,
57+
58+
#[arg(value_enum, default_value = "indent", long = "prefix")]
59+
/// The prefix (indentation) of how the tree entrys are displayed
60+
prefix: PrefixType,
61+
62+
#[arg(long = "tarballs", default_value = "false")]
63+
/// To print download urls next to the package name
64+
print_tarballs: bool,
5765
},
5866
}
5967

6068
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
69+
/// Charset for the tree subcommand
6170
enum CharSet {
6271
UTF8,
6372
ASCII,
6473
}
6574

75+
#[derive(Copy, Clone, PartialEq, Eq, PartialOrd, Ord, ValueEnum)]
76+
/// Prefix type for the tree subcommand
77+
enum PrefixType {
78+
Indent,
79+
Depth,
80+
None,
81+
}
82+
6683
fn main() {
6784
#[rustfmt::skip]
6885
panic::set_hook(Box::new(|panic_info| {
@@ -92,7 +109,11 @@ fn main() {
92109
match args.action {
93110
Actions::Update => update(&mut cfg_file, true),
94111
Actions::Purge => purge(&mut cfg_file),
95-
Actions::Tree { charset } => print!("{}", tree(&cfg_file, charset)),
112+
Actions::Tree {
113+
charset,
114+
prefix,
115+
print_tarballs,
116+
} => print!("{}", tree(&mut cfg_file, charset, prefix, print_tarballs)),
96117
}
97118
let lockfile = cfg_file.lock();
98119
if args.lock_file == Path::new("-") {
@@ -173,40 +194,87 @@ fn purge(cfg: &mut ConfigFile) {
173194
}
174195
}
175196

176-
fn tree(cfg: &ConfigFile, charset: CharSet) -> String {
197+
fn tree(
198+
cfg: &mut ConfigFile,
199+
charset: CharSet,
200+
prefix: PrefixType,
201+
print_tarballs: bool,
202+
) -> String {
177203
let mut tree: String = if let Ok(s) = current_dir() {
178204
format!("{}\n", s.to_string_lossy())
179205
} else {
180206
".\n".to_string()
181207
};
182208
iter(
183-
&cfg.packages,
209+
&mut cfg.packages,
184210
"",
185211
&mut tree,
186212
match charset {
187-
CharSet::UTF8 => "├──", // believe it or not, these are different
188-
CharSet::ASCII => "|--",
213+
CharSet::UTF8 => "├──", // believe it or not, these are unlike
214+
CharSet::ASCII => "|--", // its hard to tell, with ligatures enabled
215+
// and rustfmt wants to indent like
216+
// it must not be very stabled
189217
},
190218
match charset {
191219
CharSet::UTF8 => "└──",
192220
CharSet::ASCII => "`--",
193221
},
222+
prefix,
223+
print_tarballs,
224+
0,
194225
);
195-
fn iter(packages: &Vec<Package>, prefix: &str, tree: &mut String, t: &str, l: &str) {
226+
227+
fn iter(
228+
packages: &mut Vec<Package>,
229+
prefix: &str,
230+
tree: &mut String,
231+
t: &str,
232+
l: &str,
233+
prefix_type: PrefixType,
234+
print_tarballs: bool,
235+
depth: u32,
236+
) {
196237
// the index is used to decide if the package is the last package,
197238
// so we can use a L instead of a T.
239+
let mut tmp: String;
198240
let mut index = packages.len();
199241
for p in packages {
200242
let name = p.to_string();
201243
index -= 1;
202-
tree.push_str(format!("{prefix}{} {name}\n", if index != 0 { t } else { l }).as_str());
244+
tree.push_str(
245+
match prefix_type {
246+
PrefixType::Indent => {
247+
format!("{prefix}{} {name}", if index != 0 { t } else { l })
248+
}
249+
PrefixType::Depth => format!("{depth} {name}"),
250+
PrefixType::None => format!("{name}"),
251+
}
252+
.as_str(),
253+
);
254+
if print_tarballs {
255+
tree.push(' ');
256+
tree.push_str(
257+
p.get_tarball()
258+
.expect("Should be able to get tarball")
259+
.as_str(),
260+
);
261+
}
262+
tree.push('\n');
203263
if p.has_deps() {
204264
iter(
205-
&p.dependencies,
206-
&format!("{prefix}{} ", if index != 0 { '│' } else { ' ' }),
265+
&mut p.dependencies,
266+
if prefix_type == PrefixType::Indent {
267+
tmp = format!("{prefix}{} ", if index != 0 { '│' } else { ' ' });
268+
tmp.as_str()
269+
} else {
270+
""
271+
},
207272
tree,
208273
t,
209274
l,
275+
prefix_type,
276+
print_tarballs,
277+
depth + 1,
210278
);
211279
}
212280
}
@@ -255,11 +323,16 @@ fn gpm() {
255323
purge(cfg_file);
256324
assert_eq!(test_utils::hashd("addons"), vec![] as Vec<String>);
257325
assert_eq!(
258-
tree(cfg_file, crate::CharSet::UTF8)
259-
.lines()
260-
.skip(1)
261-
.collect::<Vec<&str>>()
262-
.join("\n"),
326+
tree(
327+
cfg_file,
328+
crate::CharSet::UTF8,
329+
crate::PrefixType::Indent,
330+
false
331+
)
332+
.lines()
333+
.skip(1)
334+
.collect::<Vec<&str>>()
335+
.join("\n"),
263336
"└── @bendn/[email protected]\n └── @bendn/[email protected]"
264337
);
265338
}

0 commit comments

Comments
 (0)