Open RaphGL opened 6 months ago
I might be willing to try and implement this myself and push a PR since I'm using the library on one of my projects. But I only have time to give it a try this weekend and I'm unfamiliar with the code base besides trying to read the library before writing this issue to see how feasible the addition would be.
Hi @RaphGL! That's an interesting proposal, even though I think this might be more difficult to do than it seems, depending on the details. I believe tilde and variables are usually not part of the actual path.
On UNIX-like systems, as far as I know, variables such as $HOME and tilde are almost always resolved by the shell, and there can be much more to it than just a simple replace. There's wordexp on POSIX systems, which probably does more or less what you are looking for.
On Windows, the story is a little different. For one, I am not entirely sure whether there's a standard behavior for tilde expansion for the home directory. I don't think CMD supports it, while PowerShell might, but I am unsure about that and would have to check it myself. Variables are usually wrapped in % characters such as %APP_DATA%, and it seems like there are multiple APIs to resolve these, one of them being ExpandEnvironmentStrings.
cwalk currently does not have any real dependencies on operating system libraries, which I would prefer not to change. So wordexp and ExpandEnvironmentStrings are probably not going to make it into cwalk. Also, if there's going to be an implementation as such, it must definitely have a clear specification of what it actually does. I am not sure yet whether it is possible to do a good and simple implementation for this
cwalk currently does not have any real dependencies on operating system libraries, which I would prefer not to change.
I wasn't thinking in adding any external dependencies as well. My thought process was just to handle basic environment variable expansions, wordexp seems to also expand other things handled by shells like patterns and globbing which seems to be out of scope for this library anyway. As for windows, I don't develop in it so I would not have the best notion for how to properly handle it, someone would have to chime in and help properly support windows.
I think only basic environment variable expansions should be supported. What I had in mind was to just parse a segment and detect if they include a~
, $VAR
or a ${VAR}
and expand it in place.
A common use case for this is passing a path to fopen since it will just literally read the path it will break paths that users expect to be valid if they're not passed to the program through a shell or something.
I am not entirely sure whether there's a standard behavior for tilde expansion for the home directory. I don't think CMD supports it, while PowerShell might, but I am unsure about that and would have to check it myself.
If memory serves me right powershell does expand tilde but cmd doesn't. Either way resolving paths wouldn't be done automatically, users would have to explicitly say they wanted it by calling cwk_path_get_resolved
, so users would have to opt into having their tildes and variables expanded.
Either way, I can't promise it would be the simplest implementation even tho it theoretically sounds very easily achievable. So I'll let you decide how to move forward :)
A simple tilde and env variable expansion sounds cool! Maybe even with flags so the user can specify what he wants.. I am just not sure yet how to handle this on windows.
A lot of times when working with paths in programs you might have stuff like
~/Documents/my_doc.pdf
or$HOME/Documents/my_doc.pdf
or some other variation. I want to propose adding: