fix - ordering oversight

This commit is contained in:
anon 2025-02-08 11:33:19 +01:00
parent 1a1df9a1af
commit 4938e4cf1c
6 changed files with 46 additions and 2 deletions

View File

@ -14,7 +14,7 @@
- [X] use `${VIMDIREDITOR}`
- [X] dry and moist tests
- [X] remove tempfile
- [ ] the ordering can be wrong if directories share the same prefix
- [X] path ordering that accounts for `-` (recursion; common prefix)
## Ideas
Accept multiple folders and individual files.

View File

@ -21,11 +21,26 @@ typedef struct {
bool is_mentioned;
} entry_t;
static inline
int path_cmp(const char * a, const char * b) {
while (*a == *b) {
if (!(*a)) { goto end; }
++a;
++b;
}
if (*a == '/' && *b != '\0') { return -1; }
if (*b == '/' && *a != '\0') { return 1; }
end:
return *(unsigned char *)a - *(unsigned char *)b;
}
static
int entry_cmp(const void * a, const void * b) { // For qsort()
const entry_t * const A = a;
const entry_t * const B = b;
return strcmp(A->name, B->name);
return path_cmp(A->name, B->name);
}
static kvec_t(entry_t) entries;

View File

@ -520,3 +520,32 @@ class CMDTEST_myswapdir < Cmdtest::Testcase
end
end
end
# ___ __ _ _ _
# | _ \_ _ ___ / _(_)_ ____| (_)_ _
# | _/ '_/ -_) _| \ \ / _` | | '_|
# |_| |_| \___|_| |_/_\_\__,_|_|_|
#
class CMDTEST_myswapdir < Cmdtest::Testcase
def setup
import_file "test/saver.sh", "./"
import_directory "test/myunfortunateprefixdir/", "./myprefixdir/"
end
def test_prefix_order
expected = [
"000\t./myprefixdir/prefix/",
"001\t./myprefixdir/prefix/postfix",
"002\t./myprefixdir/prefix-postfix",
"003\t./myprefixdir/prefixPostfix",
]
cmd "EDITOR=./saver.sh vimdir -n -r ./myprefixdir/" do
exit_zero
created_files ["output.txt"]
file_equal "output.txt", expected
end
end
end