From cdc8ad49fd1f9482dcb75cf501bb5a1cba43b9cb Mon Sep 17 00:00:00 2001 From: Soispha Peetz Date: Sun, 29 Nov 2026 23:13:50 +0100 Subject: pkgs/lf-make-map: Only print the trie to the depth that we can show Otherwise, the whole display is kinda pointless, because you can no longer see the starting parts. --- pkgs/by-name/lf/lf-make-map/flake.nix | 2 +- .../lf/lf-make-map/src/mapping/interactive.rs | 145 ++++++++++++++++++++- 2 files changed, 143 insertions(+), 4 deletions(-) diff --git a/pkgs/by-name/lf/lf-make-map/flake.nix b/pkgs/by-name/lf/lf-make-map/flake.nix index 743103b6..91698d02 100644 --- a/pkgs/by-name/lf/lf-make-map/flake.nix +++ b/pkgs/by-name/lf/lf-make-map/flake.nix @@ -16,7 +16,7 @@ system = "x86_64-linux"; sources = import ../../../../unflake.nix; - pkgs = sources.nixpkgs.legacyPackages."${system}"; + pkgs = sources.nixpkgs-unstable.legacyPackages."${system}"; in { devShells."${system}".default = pkgs.mkShell { packages = [ diff --git a/pkgs/by-name/lf/lf-make-map/src/mapping/interactive.rs b/pkgs/by-name/lf/lf-make-map/src/mapping/interactive.rs index b8ac27d2..7f6aa89c 100644 --- a/pkgs/by-name/lf/lf-make-map/src/mapping/interactive.rs +++ b/pkgs/by-name/lf/lf-make-map/src/mapping/interactive.rs @@ -8,7 +8,7 @@ // You should have received a copy of the License along with this program. // If not, see . -use std::{io::stderr, path::PathBuf}; +use std::{cmp, collections::HashMap, fmt::Write, io::stderr, path::PathBuf, slice}; use anyhow::Result; use crossterm::{ @@ -21,17 +21,156 @@ use crossterm::{ style::Print, terminal::{self, Clear, ClearType, EnterAlternateScreen, LeaveAlternateScreen}, }; +use keymaps::map_tree::{Node, NodeValue, Trie}; -use crate::mapping::map_key::MapKey; +use crate::mapping::{MapChild, map_key::MapKey}; use super::MappingsTrie; +// macro_rules! dbg { +// ($name:expr) => {{ +// use std::io::Write; +// +// let mut opts = std::fs::File::options(); +// opts.create(true).append(true); +// +// let mut file = opts.open("./debug").unwrap(); +// let _ = writeln!(file, "{} = {:#?}", stringify!($name), $name); +// +// $name +// }}; +// } + enum Status { Done(PathBuf), Stop, } impl MappingsTrie { + fn render_trie(node: &Node) -> String { + #[expect(clippy::too_many_arguments)] + fn write_node( + f: &mut String, + node: &Node, + indention: &str, + location: &[MapKey], + is_last: bool, + is_root: bool, + current_depth: usize, + max_depth: u16, + ) -> std::fmt::Result { + let node_value = match &node.value() { + None => match location.last() { + Some(last) => format!("<{} ..>", last.part_path), + None => "".to_owned(), + }, + Some(value) => value.to_string(), + }; + + let new_idention = indention.to_string() + + if is_root { + "" + } else if is_last { + " " + } else { + "│ " + }; + + let bullet = if is_last { + String::from("└── ") + } else { + String::from("├── ") + }; + + if is_root { + writeln!(f, ": {node_value}")?; + } else { + writeln!( + f, + "{indention}{bullet}\x1b[1;33m{location}\x1b[0m: {node_value}", + location = location.iter().map(ToString::to_string).collect::(), + )?; + }; + + match &node.value() { + None if current_depth <= (max_depth as usize) => { + let mut children_vec: Vec<_> = node.children().unwrap().iter().collect(); + children_vec.sort_by_key(|(a, _)| *a); + + for (counter, (key, child)) in (1..).zip(children_vec.iter()) { + let mut new_location = location.to_vec(); + new_location.push((*key).to_owned()); + + write_node( + f, + child, + &new_idention, + &new_location, + counter == children_vec.len(), + false, + current_depth + 1, + max_depth, + )?; + } + } + None | Some(_) => { + // Do nothing and stop the recursion + } + } + + Ok(()) + } + + let max_depth = { + fn collect_nodes_to( + node: &Node, + target_depth: u16, + current_depth: u16, + current_children: usize, + ) -> usize { + if target_depth == current_depth { + return current_children + 1; + } + + match node.children() { + Some(children) => { + 1 + children + .values() + .map(|child| { + collect_nodes_to(child, target_depth, current_depth + 1, 0) + }) + .sum::() + } + None => current_children + 1, + } + } + + let (_, terminal_lines) = crossterm::terminal::size().unwrap_or((u16::MAX, u16::MAX)); + + let mut found_depth = 0; + let mut previous_children_number = None; + for depth in 0.. { + let number_of_children = collect_nodes_to(node, depth, 0, 0); + + if number_of_children >= (terminal_lines as usize) { + break; + } else if previous_children_number == Some(number_of_children) { + // We reached the end, increasing the depth does not mean more children. + break; + } else { + previous_children_number = Some(number_of_children); + found_depth = depth; + } + } + + found_depth - 1 + }; + + let mut f = String::new(); + write_node(&mut f, node, "", &[], false, true, 0, max_depth).unwrap(); + f + } + pub fn interactive_start(&self, home_path: PathBuf) -> Result<()> { terminal::enable_raw_mode()?; execute!( @@ -97,7 +236,7 @@ impl MappingsTrie { { terminal::disable_raw_mode()?; - let string = trie.to_string(); + let string = Self::render_trie(trie); execute!( stderr(), MoveUp(last_length as u16), -- cgit v1.3.1