4 ms·
One that's missing is treating ~ as the home directory. This appears to be a shell thing and not a POSIX API thing. For example, this doesn't work: func ma
by jrockway 2y ago
One that's missing is treating ~ as the home directory. This appears to be a shell thing and not a POSIX API thing. For example, this doesn't work:
func main() {
if _, err := os.ReadFile("~/.bashrc"); err != nil {
log.Fatal(err)
}
fmt.Println("ok")
}
Meanwhile over in shell land:
$ echo ~/~/~
/home/jrockway/~/~
The behavior is actually kind of amazing.
I mention it because while "yourprogram ~/path/to/file" always works, having a repl that asks for a filename might not work. I've seen a lot of software where this DOES work, so I think it counts as a "most TUI programs do this" thing.
- wahern 2y agoTilde expansion is part of shell word expansion: https://pubs.opengroup.org/onlinepubs/9799919799/utilities/V3_chap02.html#tag_19_06_01 https://pubs.opengroup.org/onlinepubs/9799919799/utilities/V... Tilde expansion is the first operation in word expansion: > The expansions that are performed for a given word shall be performed in the following order: 1) Tilde expansion, parameter expansion, command substitution, and arithmetic expansion shall be performed, beginning to end. 2) Field splitting shall be performed on the portions of the fields generated by step 1. 3) Pathname expansion shall be performed, unless set -f is in effect. 4) Quote removal, if performed, shall always be performed last. See https://pubs.opengroup.org/onlinepubs/9799919799/utilities/V3_chap02.html#tag_19_06 https://pubs.opengroup.org/onlinepubs/9799919799/utilities/V... To see the whole shell specification, goto the main page at https://pubs.opengroup.org/onlinepubs/9799919799/ https://pubs.opengroup.org/onlinepubs/9799919799/. Select "Shell & Utilities" from the top-left frame, then "Shell Command Language" from the bottom-left frame.
- Sharlin 2y agoThat's a good point, particularly given that .. is handled for you at the OS API level so you might be inclined to expect ~ is too on POSIX.
- jasomill 2y agoThis would massively complicate kernel path resolution on many systems. Taking Linux as an example, even in the simplest cases, ~user expansion as typically implemented — via the getpwnam(3) libc function, which in itself implies a rather unfortunate dependency — requires reading and parsing /etc/passwd, and may also require access to network directory services like LDAP.
- Sharlin 2y agoSure, I don’t mean it should be that way, just that there might be a tacit assumption.
- Calzifer 2y agoIn my opinion it should remain a shell thing. Adding tilde expansion just complicates the implementation of the tool. Now you need to know the current users home directory. And some tools might implement it incomplete so that '~' works but '~name' does not work. In POSIX shells '~name' expands to the home directory of user 'name' $ echo ~bin /bin Now your tool need a way to query home directory for any user including system users. Depending on NSS configuration this is more complicated than just reading /etc/passwd. All for the rare case that someone passes a file path starting with tilde and wants it to be expanded. IMO, when you provide a file interactively you do so through a shell and tilde expansion is handled by the shell and otherwise just provide the actual file path and do not rely on the tool doing additional expansions. PS: I mixed up TUI and CLI program a bit in my head. For an interactively used TUI program it might be beneficial to implement tilde expansion (but then it should be as complete as in Shells). A CLI program should not do magic stuff like tilde expansion.
- trealira 2y agoI remember reading some rant about how Unix sucks because of the shell's filename expansion, because programs can't tell whether you wrote a star or actually just all the files in the directory. If programs could tell the difference, then you could get the desired renaming behavior from mv like this: mv *.m *.c They suggested that this filename expansion should be in the C standard library, not the shell. Going further, as part of shell expansion, expanding the tilde could also be useful for filename expansion in the standard library.
- rascul 2y agowordexp() might be interesting. https://pubs.opengroup.org/onlinepubs/9799919799/functions/wordexp.html https://pubs.opengroup.org/onlinepubs/9799919799/functions/w...
- jrockway 2y agoOh, and I just remembered where this always burns me. It's cases like this: ./myprogram --config=~/.config/myprogram Of course, many flags parsers are aware of this, or at least accidentally aware of this, because you can write it in a form that your shell will expand. ./myprogram --config ~/.config/myprogram For some reason, my muscle memory requires me to type the =. I don't know why. It's probably a habit I picked up from a former employer's flag parsing library. I also always use --underscores_like_this instead of --hyphens-like-normal-people for the same reason. Sigh!
- jasomill 2y agoI have the same habit. The lack of tilde expansion after the equals sign has helped a bit in breaking this habit, however. Interestingly, bash does do tilde expansion, but not tab completion, after the first equal sign, and after every colon, in variable-style arguments: $ /bin/echo --foo=~ bar=~:~root --foo=~ bar=/home/jtm:/root While tab completion can be fixed in appropriate contexts using custom completion functions (e.g., [1]), AFAIK there's no way to customize tilde expansion contexts (other than the fact that tilde expanding variable-style arguments is disabled in POSIX mode). [1] https://github.com/scop/bash-completion/blob/8a8880db78e9b04fb3d66681028af701701f93f2/completions/dd#L10 https://github.com/scop/bash-completion/blob/8a8880db78e9b04...
- GuB-42 2y agoI would not do that to your own REPL unless you are really serious about it, with good documentation, escaping, etc... What if there is a file named "~"? What about "~username"? How do you escape/quote it? What if $HOME is not set, or set to something different than the actual user home directory? What about Windows? Also, a lesser known fact is that typing "~." after newline in ssh will force close the connection from the client side and there is nothing the server can do about it, so don't make it part of your workflow.
- remram 2y agoIt's even worse than that, there are others, much more likely key combinations that are interpreted by the SSH client. For example "~v" increases the verbosity of the SSH client, "~C" opens a command line, etc. Entering "~vicky/doc.txt" or "~Ricky/doc.txt" over SSH will actually only write "icky/doc.txt", and entering "~Chris/doc.txt" will have SSH print out it's internal command-line's usage instructions in the middle of the TUI. Screenshot from Vim: https://imgur.com/a/NCt0G9r https://imgur.com/a/NCt0G9r
- BenjiWiebe 2y agoThat is only after a newline, so if you typed 'cat' before the filename, for instance, it would work just fine.