Commit Graph

15725 Commits

Author SHA1 Message Date
Fabian Boehm
4612343d6e
Merge pull request #8958 from faho/builtin-path
This adds a path builtin to deal with paths.

It offers the following subcommands:

    filter to go through a list of paths and only print the ones that pass some filter - exist, are a directory, have read permission, ...
    is as a shortcut for filter -q to only return true if one of the paths passed the filter
    basename, dirname and extension to print certain parts of the path
    change-extension to change the extension to a different one (as a string operation)
    normalize and resolve to canonicalize the paths in various flavors
    sort to sort paths, also only using the basename or dirname as a key

The definition of "extension" here was carefully considered and should line up with how extensions are actually used - ~/.bashrc doesn't have an extension, but ~/.conf.d does (".d").

These subcommands all compose well - they can read from arguments or stdin (like string), they can use null-delimited input or output (input is autodetected - if a NULL happens in the first PATH_MAX bytes it switches automatically).

It is both a failglob exception (so like set if a glob passed to it fails it just doesn't get any arguments for it instead of triggering an error), and passes output to command substitution buffers explicitly split (like string split0) so newlines are easy to handle.
2022-05-29 20:15:03 +02:00
Fabian Homborg
67b0860fe7 Rename sort --invert to sort --reverse/-r
To match sort(1).
2022-05-29 17:53:03 +02:00
Fabian Homborg
c5aa796d91 Invert takes no argument 2022-05-29 17:48:40 +02:00
Fabian Homborg
c6bffe7ceb Clarify comment for resolve 2022-05-29 17:48:40 +02:00
Fabian Homborg
1d4d238577 Rename func to keyfunc 2022-05-29 17:48:40 +02:00
Fabian Homborg
c87d063211 Update docs 2022-05-29 17:48:40 +02:00
Fabian Homborg
5d96f5d00b Update completions 2022-05-29 17:48:40 +02:00
Fabian Homborg
8e38ee884f Undo "+=" thing
oh no this made no sense given that it was *prepending* to `rest`.
2022-05-29 17:48:40 +02:00
Fabian Homborg
00949fccda Rename --what to --key
More sorty, less generic.
2022-05-29 17:48:40 +02:00
Fabian Homborg
3991af9ed6 Use += instead of temporaries
clang-tidy explains this is better. I hate C++.
2022-05-29 17:48:40 +02:00
Fabian Homborg
633fd5000e Remove useless c_str 2022-05-29 17:48:40 +02:00
Fabian Homborg
b9bd0ce3a3 Use path_apply_working_directory
Using getcwd is naughty here because we want to separate these things
in future.
2022-05-29 17:48:40 +02:00
Fabian Homborg
e87ad48f9b Test and document symlink loop 2022-05-29 17:48:40 +02:00
Fabian Homborg
b6ebf15c75 Refer to asci 0x00 as "NUL"
it is the american standard code for information, after all
2022-05-29 17:48:40 +02:00
Fabian Homborg
e5858522e3 Document ./- more. 2022-05-29 17:48:12 +02:00
Fabian Homborg
e088c974dd Fix path filter --invert
This would still remove non-existent paths, which isn't a strict
inversion and contradicts the docs.

Currently, to only allow paths that exist but don't pass a type check,
you'd have to filter twice:

path filter -Z foo bar | path filter -vfz

If a shortcut for this becomes necessary we can add it later.
2022-05-29 17:48:12 +02:00
Fabian Homborg
a9034610e1 Fix --invert long form 2022-05-29 17:48:12 +02:00
Fabian Homborg
bc3d3de30a Also prepend "./" for filter if a filename starts with "-"
This is now added to the two commands that definitely deal with
relative paths.

It doesn't work for e.g. `path basename`, because after removing the
dirname prepending a "./" doesn't refer to the same file, and the
basename is also expected to not contain any slashes.
2022-05-29 17:48:12 +02:00
Fabian Homborg
c88f648cdf Add sort --unique 2022-05-29 17:48:12 +02:00
Fabian Homborg
54778f65f8 Some sort docs 2022-05-29 17:48:12 +02:00
Fabian Homborg
4fec045073 sort: Use a stable sort
This allows e.g. sorting first by dirname and then by basename.
2022-05-29 17:48:12 +02:00
Fabian Homborg
640bd7b183 extension: Print empty entry if there is no extension
Because we now count the extension including the ".", we print an
empty entry.

This makes e.g.

```fish
set -l base (path change-extension '' $somefile)
set -l ext (path extension $somefile)
echo $base$ext
```

reconstruct the filename, and makes it easier to deal with files with
no extension.
2022-05-29 17:48:12 +02:00
Fabian Homborg
5cce6d01ad resolve: Normalize
This means "../" components are cancelled out even after non-existent
paths or files.

(the alternative is to error out, but being able to say `path resolve
/path/to/file/../../` over `path resolve (path dirname
/path/to/file)/../../` seems worth it?)
2022-05-29 17:48:11 +02:00
Fabian Homborg
dfded633c6 Fix woption 2022-05-29 17:48:11 +02:00
Fabian Homborg
b961afed49 normalize: Add "./" if a path starts with a "-" 2022-05-29 17:48:11 +02:00
Fabian Homborg
bb3700997c Correct docs for normalize/resolve
Resolve absolutizes, normalize doesn't
2022-05-29 17:48:11 +02:00
Fabian Homborg
9fdfad1d45 WIP Add path sort
This sorts paths by basename, dirname or full path - in future
possibly size or age.

It takes --invert to invert the sort and "--what=basename|dirname|..."
to specify what to sort

This can be used to implement better conf.d sorting, with something
like

```fish
set -l sourcelist
for file in (path sort --what=basename $__fish_config_dir/conf.d/*.fish $__fish_sysconf_dir/conf.d/*.fish $vendor_confdirs/*.fish)
```

which will iterate over the files by their basename. Then we keep a
list of their basenames to skip over anything that was already
sourced, like before.
2022-05-29 17:48:11 +02:00
Fabian Homborg
e429f76e9f append_with_separation: Default to wanting a newline
The recent change to skip the newline for `string` changed this, and
it also hit builtin path (which is in development separately, so it's
not like it broke master).

Let's pick a good default here.
2022-05-29 17:48:11 +02:00
Fabian Homborg
83a993a28e Remove references to match/expand in the docs 2022-05-29 17:48:11 +02:00
Fabian Homborg
55c34cbb7c Use physical $PWD
Yeah, the macOS tests fail because it's started in /private/var... with a
$PWD of /var.... So resolve canonicalizes the path, which makes it no
longer match $PWD.

Simply use pwd -P
2022-05-29 17:48:11 +02:00
Fabian Homborg
23a5e53247 tests: Print $PWD if resolving fails
Seems to be a macOS issue
2022-05-29 17:48:11 +02:00
Fabian Homborg
d13ba046b0 resolve: Use the new real path
This failed for

/bin/foo/bar

if /bin is a symlink to /usr/bin and foo doesn't exist.

It returned /bin/foo/bar instead of the correct /usr/bin/foo/bar.
2022-05-29 17:48:11 +02:00
Fabian Homborg
80e04a1e86 Rename real to resolve also in completions 2022-05-29 17:48:11 +02:00
Fabian Homborg
2b8bb5bd7f path: Rename "real" to "resolve" 2022-05-29 17:48:11 +02:00
Fabian Homborg
5844164feb document real change 2022-05-29 17:48:11 +02:00
Fabian Homborg
479fde27d7 path: Make path real "work" with nonexistent paths
This just goes back until it finds an existent path, resolves that,
and adds the normalized rest on top.

So if you try

/bin/foo/bar////../baz

and /bin exists as a symlink to /usr/bin, it would resolve that, and
normalize the rest, giving

/usr/bin/foo/baz

(note: We might want to add this to realpath as well?)
2022-05-29 17:48:11 +02:00
Fabian Homborg
4fced3ef5a Remove sticky filter
This isn't super useful, and having a caveat in the docs that it might
cause the entire filter to fail is awkward.

So just remove it.
2022-05-29 17:48:11 +02:00
Fabian Homborg
972ed61266 path: Docs work 2022-05-29 17:48:11 +02:00
Fabian Homborg
1c1e643218 WIP path: Make extensions start at the "."
This includes the "." in what `path extension` prints.

This allows distinguishing between an empty extension (just `.`) and a
non-existent extension (no `.` at all).
2022-05-29 17:48:11 +02:00
Fabian Homborg
17a8dd8f62 Move path to src/builtins 2022-05-29 17:48:11 +02:00
Fabian Homborg
37fd508a59 Path is also a failglob exception 2022-05-29 17:48:11 +02:00
Fabian Homborg
d991096cb4 Add some more links in the docs 2022-05-29 17:48:11 +02:00
Fabian Homborg
de0a64a016 Update tests for change-extension's status 2022-05-29 17:48:11 +02:00
Fabian Homborg
5c28473183 Update completions 2022-05-29 17:48:11 +02:00
Fabian Homborg
ce7281905d Switch strip-extension to change-extension
This allows replacing the extension, e.g.

    > path change-extension mp4 foo.wmv
    foo.mp4
2022-05-29 17:48:11 +02:00
Fabian Homborg
00ed0bfb5d Rename base/dir to basename/dirname
"dir" sounds like it asks "is it a directory".
2022-05-29 17:48:11 +02:00
Fabian Homborg
268a9d8db3 Prevent some copies 2022-05-29 17:48:11 +02:00
Fabian Homborg
fbfad686aa Another pass over the docs 2022-05-29 17:48:11 +02:00
Fabian Homborg
9f174d3a62 Moar on the docs 2022-05-29 17:48:11 +02:00
Fabian Homborg
d0e8eb1700 docs: Replace the general options recantation with "GENERAL_OPTIONS"
I'm not sure if this is the actual proper syntax to describe this, but
it sure is a heck of a lot more readable.
2022-05-29 17:48:11 +02:00