From ff84099835c636e00d8812b54be335ff220af0da Mon Sep 17 00:00:00 2001 From: Benedikt Peetz Date: Tue, 2 Jun 2026 21:53:52 +0200 Subject: pkgs/lf-make-map: Special case dot-directories That makes the mappings for `~/.config` or `~/.local` more accessible. --- pkgs/by-name/lf/lf-make-map/src/mapping/map_key.rs | 12 +++-- pkgs/by-name/lf/lf-make-map/tests/base.sh | 2 +- .../lf/lf-make-map/tests/cases/dot_dirs/output.old | 13 ++++++ .../lf/lf-make-map/tests/cases/dot_dirs/test.sh | 49 ++++++++++++++++++++ .../tests/cases/dot_dirs_duplicates/output.old | 17 +++++++ .../tests/cases/dot_dirs_duplicates/test.sh | 53 ++++++++++++++++++++++ .../lf/lf-make-map/tests/cases/simple/test.sh | 8 ++-- 7 files changed, 145 insertions(+), 9 deletions(-) create mode 100644 pkgs/by-name/lf/lf-make-map/tests/cases/dot_dirs/output.old create mode 100755 pkgs/by-name/lf/lf-make-map/tests/cases/dot_dirs/test.sh create mode 100644 pkgs/by-name/lf/lf-make-map/tests/cases/dot_dirs_duplicates/output.old create mode 100755 pkgs/by-name/lf/lf-make-map/tests/cases/dot_dirs_duplicates/test.sh diff --git a/pkgs/by-name/lf/lf-make-map/src/mapping/map_key.rs b/pkgs/by-name/lf/lf-make-map/src/mapping/map_key.rs index 5fd046fb..d90eb963 100644 --- a/pkgs/by-name/lf/lf-make-map/src/mapping/map_key.rs +++ b/pkgs/by-name/lf/lf-make-map/src/mapping/map_key.rs @@ -87,7 +87,6 @@ impl MapKey { generated_chars.extend( (0..(new_resolution - self.part_path.len())) - .into_iter() .map(|_| self.part_path.chars().last().expect("This will exists")), ); @@ -110,14 +109,14 @@ impl MapKey { } pub fn display(values: &[Self]) -> String { - values.iter().map(|value| value.key.clone()).collect() + values.iter().map(|value| value.key).collect() } fn part_path_to_key(part: &str, number_of_chars: usize) -> String { fn make(pat: char, part: &str, number_of_chars: usize) -> String { let mut acc = String::new(); - if !part.split(pat).all(|part| part.len() > 0) { + if !part.split(pat).all(|part| !part.is_empty()) { panic!( "\ Can't turn this path '{}' to a mapping. @@ -152,6 +151,13 @@ This should not happen, please report the bug!", make('_', part, number_of_chars) } else if part.contains('-') && !part.starts_with('-') && !part.ends_with('-') { make('-', part, number_of_chars) + } else if part.starts_with('.') { + // HACK: Special case for directories like ~/.config ~/.local and so on. + // We just drop the starting '.' and it's easier to type. <2026-06-02> + part.chars() + .skip(1) + .take(number_of_chars) + .collect::() } else { part.chars().take(number_of_chars).collect::() }; diff --git a/pkgs/by-name/lf/lf-make-map/tests/base.sh b/pkgs/by-name/lf/lf-make-map/tests/base.sh index c7694985..d860cf84 100755 --- a/pkgs/by-name/lf/lf-make-map/tests/base.sh +++ b/pkgs/by-name/lf/lf-make-map/tests/base.sh @@ -18,7 +18,7 @@ execute_make_maps() { } fd . cases --max-depth 1 --type directory | while read -r case; do - echo "Executing '$case/test.sh'" + echo "Executing '${case}test.sh'" # shellcheck source=/dev/null LOCATION="$case/test.sh" . "$case/test.sh" diff --git a/pkgs/by-name/lf/lf-make-map/tests/cases/dot_dirs/output.old b/pkgs/by-name/lf/lf-make-map/tests/cases/dot_dirs/output.old new file mode 100644 index 00000000..49307bc6 --- /dev/null +++ b/pkgs/by-name/lf/lf-make-map/tests/cases/dot_dirs/output.old @@ -0,0 +1,13 @@ +map gc. cd "/tmp/tmp.DfcgjemfCG/.config" +map gca. cd "/tmp/tmp.DfcgjemfCG/.config/apzu" +map gcl. cd "/tmp/tmp.DfcgjemfCG/.config/lahmu" +map gct. cd "/tmp/tmp.DfcgjemfCG/.config/tiamat" +map gd. cd "/tmp/tmp.DfcgjemfCG/documents" +map gda. cd "/tmp/tmp.DfcgjemfCG/documents/apzu" +map gdl. cd "/tmp/tmp.DfcgjemfCG/documents/lahmu" +map gdt. cd "/tmp/tmp.DfcgjemfCG/documents/tiamat" +map gl. cd "/tmp/tmp.DfcgjemfCG/.local" +map gln. cd "/tmp/tmp.DfcgjemfCG/.local/nvim" +map glsh. cd "/tmp/tmp.DfcgjemfCG/.local/share" +map glst. cd "/tmp/tmp.DfcgjemfCG/.local/state" + diff --git a/pkgs/by-name/lf/lf-make-map/tests/cases/dot_dirs/test.sh b/pkgs/by-name/lf/lf-make-map/tests/cases/dot_dirs/test.sh new file mode 100755 index 00000000..d3df848c --- /dev/null +++ b/pkgs/by-name/lf/lf-make-map/tests/cases/dot_dirs/test.sh @@ -0,0 +1,49 @@ +#! /usr/bin/env sh + +# nixos-config - My current NixOS configuration +# +# Copyright (C) 2025 Benedikt Peetz +# SPDX-License-Identifier: GPL-3.0-or-later +# +# This file is part of my nixos-config. +# +# You should have received a copy of the License along with this program. +# If not, see . + +mkFile() { + t="$1" + + mkdir --parents "$(dirname "$t")" + echo "TEST_FILE" >"$t" +} + +# We need to hard code this, so that our output matches the golden sample. +base="/tmp/tmp.DfcgjemfCG" +test="$(mktemp --directory -t lf_make_temp_test_XXXXX)" + +if [ -d "$base" ]; then + rm --recursive "$base" +fi +mkdir "$base" + +cleanup() { + rm --recursive "$base" "$test" +} +trap cleanup EXIT + +mkFile "$base/.local/share/file1.txt" +mkFile "$base/.local/state/file1.txt" +mkFile "$base/.local/nvim/log.LOG" +mkFile "$base/.local/.tog/TOG.LOG" + +mkFile "$base/.config/apzu/file2.txt" +mkFile "$base/.config/tiamat/file2.txt" +mkFile "$base/.config/lahmu/file2.txt" + +mkFile "$base/documents/apzu/file2.txt" +mkFile "$base/documents/tiamat/file2.txt" +mkFile "$base/documents/lahmu/file2.txt" + +execute_make_maps --home-name "$base" --depth 100 generate "$base/.local" "$base/.config" "$base/documents" >"$test/output.new" + +diff "$test/output.new" "$(dirname "$LOCATION")/output.old" diff --git a/pkgs/by-name/lf/lf-make-map/tests/cases/dot_dirs_duplicates/output.old b/pkgs/by-name/lf/lf-make-map/tests/cases/dot_dirs_duplicates/output.old new file mode 100644 index 00000000..f7523ba3 --- /dev/null +++ b/pkgs/by-name/lf/lf-make-map/tests/cases/dot_dirs_duplicates/output.old @@ -0,0 +1,17 @@ +map gca. cd "/tmp/tmp.DfcgjemfCG/cats" +map gcaa. cd "/tmp/tmp.DfcgjemfCG/cats/apzu" +map gcal. cd "/tmp/tmp.DfcgjemfCG/cats/lahmu" +map gcat. cd "/tmp/tmp.DfcgjemfCG/cats/tiamat" +map gco. cd "/tmp/tmp.DfcgjemfCG/.config" +map gcoa. cd "/tmp/tmp.DfcgjemfCG/.config/apzu" +map gcol. cd "/tmp/tmp.DfcgjemfCG/.config/lahmu" +map gcot. cd "/tmp/tmp.DfcgjemfCG/.config/tiamat" +map gd. cd "/tmp/tmp.DfcgjemfCG/documents" +map gda. cd "/tmp/tmp.DfcgjemfCG/documents/apzu" +map gdl. cd "/tmp/tmp.DfcgjemfCG/documents/lahmu" +map gdt. cd "/tmp/tmp.DfcgjemfCG/documents/tiamat" +map gl. cd "/tmp/tmp.DfcgjemfCG/.local" +map gln. cd "/tmp/tmp.DfcgjemfCG/.local/nvim" +map glsh. cd "/tmp/tmp.DfcgjemfCG/.local/share" +map glst. cd "/tmp/tmp.DfcgjemfCG/.local/state" + diff --git a/pkgs/by-name/lf/lf-make-map/tests/cases/dot_dirs_duplicates/test.sh b/pkgs/by-name/lf/lf-make-map/tests/cases/dot_dirs_duplicates/test.sh new file mode 100755 index 00000000..33447e54 --- /dev/null +++ b/pkgs/by-name/lf/lf-make-map/tests/cases/dot_dirs_duplicates/test.sh @@ -0,0 +1,53 @@ +#! /usr/bin/env sh + +# nixos-config - My current NixOS configuration +# +# Copyright (C) 2025 Benedikt Peetz +# SPDX-License-Identifier: GPL-3.0-or-later +# +# This file is part of my nixos-config. +# +# You should have received a copy of the License along with this program. +# If not, see . + +mkFile() { + t="$1" + + mkdir --parents "$(dirname "$t")" + echo "TEST_FILE" >"$t" +} + +# We need to hard code this, so that our output matches the golden sample. +base="/tmp/tmp.DfcgjemfCG" +test="$(mktemp --directory -t lf_make_temp_test_XXXXX)" + +if [ -d "$base" ]; then + rm --recursive "$base" +fi +mkdir "$base" + +cleanup() { + rm --recursive "$base" "$test" +} +trap cleanup EXIT + +mkFile "$base/.local/share/file1.txt" +mkFile "$base/.local/state/file1.txt" +mkFile "$base/.local/nvim/log.LOG" +mkFile "$base/.local/.tog/TOG.LOG" + +mkFile "$base/.config/apzu/file2.txt" +mkFile "$base/.config/tiamat/file2.txt" +mkFile "$base/.config/lahmu/file2.txt" + +mkFile "$base/documents/apzu/file2.txt" +mkFile "$base/documents/tiamat/file2.txt" +mkFile "$base/documents/lahmu/file2.txt" + +mkFile "$base/cats/apzu/file2.txt" +mkFile "$base/cats/tiamat/file2.txt" +mkFile "$base/cats/lahmu/file2.txt" + +execute_make_maps --home-name "$base" --depth 100 generate "$base/.local" "$base/.config" "$base/documents" "$base/cats" >"$test/output.new" + +diff "$test/output.new" "$(dirname "$LOCATION")/output.old" diff --git a/pkgs/by-name/lf/lf-make-map/tests/cases/simple/test.sh b/pkgs/by-name/lf/lf-make-map/tests/cases/simple/test.sh index 22f97009..97ee0cb9 100755 --- a/pkgs/by-name/lf/lf-make-map/tests/cases/simple/test.sh +++ b/pkgs/by-name/lf/lf-make-map/tests/cases/simple/test.sh @@ -14,11 +14,9 @@ base="/tmp/tmp.DfcgjemfCG" test="$(mktemp --directory -t lf_make_temp_test_XXXXX)" -[ -d "$base" ] && { - echo "$base already exists!" - exit 1 -} - +if [ -d "$base" ]; then + rm --recursive "$base" +fi mkdir "$base" cleanup() { -- cgit v1.3.1