about summary refs log tree commit diff stats
path: root/sys/nixpkgs/pkgs/lf-make-map/src/main.rs
diff options
context:
space:
mode:
Diffstat (limited to '')
-rw-r--r--sys/nixpkgs/pkgs/lf-make-map/src/main.rs128
1 files changed, 97 insertions, 31 deletions
diff --git a/sys/nixpkgs/pkgs/lf-make-map/src/main.rs b/sys/nixpkgs/pkgs/lf-make-map/src/main.rs
index 362b28db..ccda1f14 100644
--- a/sys/nixpkgs/pkgs/lf-make-map/src/main.rs
+++ b/sys/nixpkgs/pkgs/lf-make-map/src/main.rs
@@ -1,10 +1,14 @@
-use anyhow::Context;
+use std::path::{Path, PathBuf};
+
+use anyhow::{Context, Result};
 use clap::Parser;
 use cli::Args;
-use log::trace;
+use log::{debug, trace};
 use mapping::map_tree::MappingTree;
 use walkdir::{DirEntry, WalkDir};
 
+use crate::mapping::MapKey;
+
 mod cli;
 mod mapping;
 
@@ -23,39 +27,64 @@ fn main() -> anyhow::Result<()> {
     let mut mappings = MappingTree::new();
 
     for dir in args.relevant_directories {
-        for dir2 in WalkDir::new(&dir)
-            .max_depth(args.depth)
-            .into_iter()
-            .filter_entry(|e| is_dir(e))
-        {
-            let directory =
-                dir2.with_context(|| format!("Failed to read dir ('{}')", &dir.display()))?;
-
-            let path = directory
-                .path()
-                .strip_prefix(&args.home_name)
-                .with_context(|| {
-                    format!(
-                        "'{}' is not under the specified home path ('{}')!",
-                        directory.path().display(),
-                        args.home_name.display()
-                    )
-                })?;
+        trace!("Processing '{}'..", dir.display());
+        let path = strip_path(&dir, &args.home_name)?;
 
-            mappings
-                .include(path.to_str().with_context(|| {
-                    format!(
-                        "\
-Can't derive a keymapping from path: '{}' \
-because it can't be turned to a string
-",
-                        path.display()
+        mappings
+            .include(path_to_str(path)?)
+            .with_context(|| format!("Failed to include path: '{}'", path.display()))?;
+    }
+
+    let home = path_to_str(&args.home_name)?.to_owned();
+
+    let mut current_depth = 1;
+    while current_depth != args.depth {
+        for (key, value) in mappings.iter() {
+            trace!(
+                "Adding to child ('{}' -> '{}')",
+                MapKey::display(&key),
+                value
+            );
+
+            let mut local_mappings = MappingTree::new();
+            for dir in WalkDir::new(extend(&home, &value)?)
+                .min_depth(1)
+                .max_depth(1)
+                .into_iter()
+                .filter_entry(|e| is_dir(e) && !is_hidden(e))
+            {
+                let directory = dir
+                    .with_context(|| format!("Failed to read dir ('{}')", home.clone() + &value))?;
+                let path_to_strip = &PathBuf::from(extend(&home, &value)?);
+                let path = strip_path(&directory.path(), &path_to_strip)?;
+                trace!(
+                    "Including: '{}' (after stripping '{}' from '{}' -> '{}' + '/' + '{}')",
+                    path.display(),
+                    directory.path().display(),
+                    path_to_strip.display(),
+                    home,
+                    value
+                );
+
+                let gen_key = MapKey::new_ones_from_path(path_to_str(path)?, 1);
+                local_mappings
+                    .insert(
+                        &gen_key,
+                        path_to_str(strip_path(&directory.path(), &PathBuf::from(&home))?)?,
                     )
-                })?)
-                .with_context(|| format!("Failed to include path: '{}'", path.display()))?;
+                    .with_context(|| format!("Failed to include path: '{}'", path.display()))?;
+            }
+
+            trace!("{}", local_mappings);
 
-            trace!("Processed '{}'..", directory.path().display());
+            trace!(
+                "'{}' -> '{:#?}'",
+                MapKey::display(&key),
+                local_mappings.root_node()
+            );
+            mappings.interleave(&key, local_mappings.root_node().to_owned())?;
         }
+        current_depth += 1;
     }
 
     println!("{}", mappings);
@@ -63,10 +92,47 @@ because it can't be turned to a string
     Ok(())
 }
 
+fn extend(base: &str, value: &str) -> Result<String> {
+    let base_path = PathBuf::from(base);
+    let value_path = PathBuf::from(value);
+
+    Ok(path_to_str(&base_path.join(&value_path))?.to_owned())
+}
+
+fn is_hidden(entry: &DirEntry) -> bool {
+    entry
+        .file_name()
+        .to_str()
+        .map(|s| s.starts_with("."))
+        .unwrap_or(false)
+}
+
 fn is_dir(entry: &DirEntry) -> bool {
     entry.file_type().is_dir()
 }
 
+fn strip_path<'a>(path: &'a Path, to_strip: &Path) -> Result<&'a Path> {
+    path.strip_prefix(&to_strip).with_context(|| {
+        format!(
+            "'{}' is not under the specified home path ('{}')!",
+            path.display(),
+            to_strip.display()
+        )
+    })
+}
+
+fn path_to_str(path: &Path) -> Result<&str> {
+    path.to_str().with_context(|| {
+        format!(
+            "\
+Can't derive a keymapping from path: '{}' \
+because it can't be turned to a string
+",
+            path.display()
+        )
+    })
+}
+
 // fn gen_lf_mappings(home_name: PathBuf, char_num: usize, rel_dirs: Vec<PathBuf>) {
 //     let mut mappings_vec = vec![];
 //     let mut index_counter = 0;