summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authordiogo464 <[email protected]>2025-08-08 17:52:27 +0100
committerdiogo464 <[email protected]>2025-08-08 17:52:27 +0100
commit29b3cf6098527154bd34245ee26cd1967cf8f262 (patch)
tree89050736b4fd00fdfe1b2977719e8705ae85f9c7
parentb170db9059cd9a34957945bf503a0d720f849d61 (diff)
perf: use stdout buf writer
-rw-r--r--fctdrive/src/main.rs50
1 files changed, 32 insertions, 18 deletions
diff --git a/fctdrive/src/main.rs b/fctdrive/src/main.rs
index 0f7c83c..2035680 100644
--- a/fctdrive/src/main.rs
+++ b/fctdrive/src/main.rs
@@ -849,11 +849,42 @@ fn cmd_rename(args: RenameArgs) {
849 write_log_file(&ops); 849 write_log_file(&ops);
850} 850}
851 851
852type FsNodeWriter<'a> = std::io::BufWriter<std::io::StdoutLock<'a>>;
853
852fn cmd_view(args: ViewArgs) { 854fn cmd_view(args: ViewArgs) {
855 let stdout = std::io::stdout().lock();
856 let mut writer = BufWriter::new(stdout);
853 let mut fs = Fs::default(); 857 let mut fs = Fs::default();
854 let ops = read_log_file(); 858 let ops = read_log_file();
855 ops.iter().for_each(|op| apply(&mut fs, op).unwrap()); 859 ops.iter().for_each(|op| apply(&mut fs, op).unwrap());
856 write_node(&fs, fs.root, Default::default()); 860 write_node(&mut writer, &fs, fs.root, Default::default());
861 writer.flush().unwrap();
862}
863
864fn write_node(writer: &mut FsNodeWriter, fs: &Fs, node_id: FsNodeId, path: DrivePath) {
865 let node = &fs.nodes[node_id];
866 match node.kind {
867 FsNodeKind::File => {
868 write!(
869 writer,
870 "{}\tfile\t{}\t{}\t{}",
871 path, node.lastmod, node.blob, node.author
872 )
873 .unwrap();
874 }
875 FsNodeKind::Directory => {
876 write!(
877 writer,
878 "{}\tdir\t{}\t-\t{}",
879 path, node.lastmod, node.author
880 )
881 .unwrap();
882 for (child_comp, child_id) in node.children.iter() {
883 let child_path = path.push(child_comp.clone());
884 write_node(writer, fs, *child_id, child_path);
885 }
886 }
887 }
857} 888}
858 889
859#[derive(Debug, Clone)] 890#[derive(Debug, Clone)]
@@ -954,23 +985,6 @@ fn collect_all_file_paths(root: &Path) -> Vec<PathBuf> {
954 files 985 files
955} 986}
956 987
957fn write_node(fs: &Fs, node_id: FsNodeId, path: DrivePath) {
958 let node = &fs.nodes[node_id];
959 match node.kind {
960 FsNodeKind::File => println!(
961 "{}\tfile\t{}\t{}\t{}",
962 path, node.lastmod, node.blob, node.author
963 ),
964 FsNodeKind::Directory => {
965 println!("{}\tdir\t{}\t-\t{}", path, node.lastmod, node.author);
966 for (child_comp, child_id) in node.children.iter() {
967 let child_path = path.push(child_comp.clone());
968 write_node(fs, *child_id, child_path);
969 }
970 }
971 }
972}
973
974fn read_log_file() -> Vec<Operation> { 988fn read_log_file() -> Vec<Operation> {
975 let mut operations = Vec::default(); 989 let mut operations = Vec::default();
976 if std::fs::exists("log.txt").unwrap() { 990 if std::fs::exists("log.txt").unwrap() {