@@ -54,15 +54,32 @@ Produces output like
54
54
#[ arg( value_enum, default_value = "utf8" , long = "charset" ) ]
55
55
/// Character set to print in.
56
56
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 ,
57
65
} ,
58
66
}
59
67
60
68
#[ derive( Copy , Clone , PartialEq , Eq , PartialOrd , Ord , ValueEnum ) ]
69
+ /// Charset for the tree subcommand
61
70
enum CharSet {
62
71
UTF8 ,
63
72
ASCII ,
64
73
}
65
74
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
+
66
83
fn main ( ) {
67
84
#[ rustfmt:: skip]
68
85
panic:: set_hook ( Box :: new ( |panic_info| {
@@ -92,7 +109,11 @@ fn main() {
92
109
match args. action {
93
110
Actions :: Update => update ( & mut cfg_file, true ) ,
94
111
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) ) ,
96
117
}
97
118
let lockfile = cfg_file. lock ( ) ;
98
119
if args. lock_file == Path :: new ( "-" ) {
@@ -173,40 +194,87 @@ fn purge(cfg: &mut ConfigFile) {
173
194
}
174
195
}
175
196
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 {
177
203
let mut tree: String = if let Ok ( s) = current_dir ( ) {
178
204
format ! ( "{}\n " , s. to_string_lossy( ) )
179
205
} else {
180
206
".\n " . to_string ( )
181
207
} ;
182
208
iter (
183
- & cfg. packages ,
209
+ & mut cfg. packages ,
184
210
"" ,
185
211
& mut tree,
186
212
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
189
217
} ,
190
218
match charset {
191
219
CharSet :: UTF8 => "└──" ,
192
220
CharSet :: ASCII => "`--" ,
193
221
} ,
222
+ prefix,
223
+ print_tarballs,
224
+ 0 ,
194
225
) ;
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
+ ) {
196
237
// the index is used to decide if the package is the last package,
197
238
// so we can use a L instead of a T.
239
+ let mut tmp: String ;
198
240
let mut index = packages. len ( ) ;
199
241
for p in packages {
200
242
let name = p. to_string ( ) ;
201
243
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' ) ;
203
263
if p. has_deps ( ) {
204
264
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
+ } ,
207
272
tree,
208
273
t,
209
274
l,
275
+ prefix_type,
276
+ print_tarballs,
277
+ depth + 1 ,
210
278
) ;
211
279
}
212
280
}
@@ -255,11 +323,16 @@ fn gpm() {
255
323
purge ( cfg_file) ;
256
324
assert_eq ! ( test_utils:: hashd( "addons" ) , vec![ ] as Vec <String >) ;
257
325
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 " ) ,
263
336
264
337
) ;
265
338
}
0 commit comments