summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordiogo464 <[email protected]>2025-08-12 15:50:42 +0100
committerdiogo464 <[email protected]>2025-08-12 15:50:42 +0100
commit0f5c849cc33431d22b1fdc87f1070f8769bfdf00 (patch)
treed9ecaffc2c2a9273b28abf6b68de976c1f149895
parent50b468d6e90bb606474bccdbd732a068da91cc8f (diff)
cli: added type filter to ls command
-rw-r--r--src/main.rs54
1 files changed, 46 insertions, 8 deletions
diff --git a/src/main.rs b/src/main.rs
index 3e8cd95..9e12cd1 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -471,12 +471,35 @@ impl From<&str> for FsError {
471 } 471 }
472} 472}
473 473
474#[derive(Debug)]
475pub struct InvalidFsNodeKind;
476
477impl Display for InvalidFsNodeKind {
478 fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
479 f.write_str("invalid filesystem node type, must be one of 'directory' or 'file'")
480 }
481}
482
483impl std::error::Error for InvalidFsNodeKind {}
484
474#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)] 485#[derive(Debug, Clone, Copy, PartialEq, Eq, Hash)]
475pub enum FsNodeKind { 486pub enum FsNodeKind {
476 File, 487 File,
477 Directory, 488 Directory,
478} 489}
479 490
491impl FromStr for FsNodeKind {
492 type Err = InvalidFsNodeKind;
493
494 fn from_str(s: &str) -> Result<Self, Self::Err> {
495 match s {
496 "file" => Ok(Self::File),
497 "dir" | "directory" => Ok(Self::Directory),
498 _ => Err(InvalidFsNodeKind),
499 }
500 }
501}
502
480pub struct FsNode { 503pub struct FsNode {
481 pub kind: FsNodeKind, 504 pub kind: FsNodeKind,
482 pub author: String, 505 pub author: String,
@@ -803,6 +826,9 @@ struct LsArgs {
803 #[clap(long)] 826 #[clap(long)]
804 relative: bool, 827 relative: bool,
805 828
829 #[clap(short = 't', long = "type")]
830 type_: Option<FsNodeKind>,
831
806 path: Option<DrivePath>, 832 path: Option<DrivePath>,
807} 833}
808 834
@@ -982,6 +1008,7 @@ fn cmd_ls(args: LsArgs) {
982 }, 1008 },
983 args.recursive, 1009 args.recursive,
984 true, 1010 true,
1011 args.type_,
985 ); 1012 );
986 } 1013 }
987 writer.flush().unwrap(); 1014 writer.flush().unwrap();
@@ -995,19 +1022,22 @@ fn write_node(
995 path: DrivePath, 1022 path: DrivePath,
996 recursive: bool, 1023 recursive: bool,
997 recurse: bool, 1024 recurse: bool,
1025 filter_type: Option<FsNodeKind>,
998) { 1026) {
999 let node = &fs.nodes[node_id]; 1027 let node = &fs.nodes[node_id];
1000 match node.kind { 1028 match node.kind {
1001 FsNodeKind::File => { 1029 FsNodeKind::File => {
1002 writeln!( 1030 if filter_type == None || filter_type == Some(FsNodeKind::File) {
1003 writer, 1031 writeln!(
1004 "{}\tfile\t{}\t{}\t{}\t{}", 1032 writer,
1005 path, node.lastmod, node.blob, node.size, node.author 1033 "{}\tfile\t{}\t{}\t{}\t{}",
1006 ) 1034 path, node.lastmod, node.blob, node.size, node.author
1007 .unwrap(); 1035 )
1036 .unwrap();
1037 }
1008 } 1038 }
1009 FsNodeKind::Directory => { 1039 FsNodeKind::Directory => {
1010 if !recurse { 1040 if !recurse && (filter_type == None || filter_type == Some(FsNodeKind::Directory)) {
1011 writeln!( 1041 writeln!(
1012 writer, 1042 writer,
1013 "{}\tdir\t{}\t-\t-\t{}", 1043 "{}\tdir\t{}\t-\t-\t{}",
@@ -1018,7 +1048,15 @@ fn write_node(
1018 if recursive || recurse { 1048 if recursive || recurse {
1019 for (child_comp, child_id) in node.children.iter() { 1049 for (child_comp, child_id) in node.children.iter() {
1020 let child_path = path.push(child_comp.clone()); 1050 let child_path = path.push(child_comp.clone());
1021 write_node(writer, fs, *child_id, child_path, recursive, false); 1051 write_node(
1052 writer,
1053 fs,
1054 *child_id,
1055 child_path,
1056 recursive,
1057 false,
1058 filter_type,
1059 );
1022 } 1060 }
1023 } 1061 }
1024 } 1062 }