From e41aeecf9b44efe964831283a0c5a25f665b53c6 Mon Sep 17 00:00:00 2001 From: Ilan Cosman Date: Thu, 9 Dec 2021 22:56:17 -0800 Subject: [PATCH] Speed up pwd truncation algorithm --- functions/_tide_pwd.fish | 7 +++---- tests/_tide_item_pwd.test.fish | 5 +++++ 2 files changed, 8 insertions(+), 4 deletions(-) diff --git a/functions/_tide_pwd.fish b/functions/_tide_pwd.fish index 4cb0cb4..9191b04 100644 --- a/functions/_tide_pwd.fish +++ b/functions/_tide_pwd.fish @@ -27,12 +27,11 @@ function _tide_pwd end set split_output[$i] $_tide_color_anchors$dir_section$_tide_reset_to_color_dirs else if test $pwd_length -gt $dist_btwn_sides - string sub --length 1 -- $dir_section | read -l trunc - trunc_len=1 while test $trunc != $dir_section -a (count $parent_dir/$trunc*/) != 1 + trunc_len=1 while string match -qr "(?.{$trunc_len})" $dir_section && + test (count $parent_dir/$trunc*/) != 1 math $trunc_len+1 | read trunc_len - string sub --length $trunc_len -- $dir_section | read trunc end - if test $trunc != $dir_section + if test -n "$trunc" set split_output[$i] $_tide_color_truncated_dirs$trunc$_tide_reset_to_color_dirs string join / $split_output | string length --visible | read -g pwd_length end diff --git a/tests/_tide_item_pwd.test.fish b/tests/_tide_item_pwd.test.fish index 8f8cb65..ab11da7 100644 --- a/tests/_tide_item_pwd.test.fish +++ b/tests/_tide_item_pwd.test.fish @@ -49,6 +49,11 @@ mkdir -p $tmpdir/alfa/bratwurst _pwd "$longDir" # CHECK: ~/a/brav/c/d/e/f/golf/hotel/india/juliett/kilo/lima/mike/november/oscar/papa rm -r $tmpdir/alfa/bratwurst +# Truncate to unique +mkdir -p $tmpdir/alfa/bravohello +_pwd "$longDir" # CHECK: ~/a/bravo/c/d/e/f/golf/hotel/india/juliett/kilo/lima/mike/november/oscar/papa +rm -r $tmpdir/alfa/bravohello + # Markers mkdir -p $tmpdir/alfa/.git _pwd "$longDir" # CHECK: ~/alfa/b/c/d/e/f/golf/hotel/india/juliett/kilo/lima/mike/november/oscar/papa