Skip to content

Builtin indent and key reader - #10876

Merged
faho merged 2 commits into
fish-shell:masterfrom
faho:builtin-indent-and-key-reader
Jan 10, 2025
Merged

faho merged 2 commits into
fish-shell:masterfrom
faho:builtin-indent-and-key-reader

Conversation

@faho

@faho faho commented Nov 27, 2024 •

Copy link
Copy Markdown
Member

Description

This makes it so fish_key_reader and fish_indent are available as builtins. That makes it easier to distribute fish as a single file (especially when paired with #10367).

However, to allow calling them from the outside, the main fish binary now checks its argv[0] - if that uses a filename "fish_indent" or "fish_key_reader", it will immediately become the respective program and exit. This is like how busybox can be ls or rm or a shell.

TODOs:

This is pretty rough and WIP. A quick attempt to see how well it works.

  • Changes to fish usage are reflected in user documentation/manpages.
  • Tests have been added for regressions fixed
  • User-visible changes noted in CHANGELOG.rst
  • These two use direct {e,}printf! and so aren't redirectable - [ ] Cleanup

And the big picture:

  • Do we want this?
  • Do we want to ship links by default?
  • How?
  • Do we want to encourage calling fish -c fish_indent instead? Deprecate the links?
  • Is fish_key_reader a big enough thing that it deserves a link? Or is it only ever called inside of fish?
  • Do we want other builtins to be available via direct call?

@faho faho added this to the fish-future milestone Nov 27, 2024
@faho
faho force-pushed the builtin-indent-and-key-reader branch from 0c1885c to 0305f47 Compare November 27, 2024 20:12
@zanchey

zanchey commented Nov 28, 2024

Copy link
Copy Markdown
Member

cargo install already installs all three binaries, right? So this would be more to allow us to publish a single binary as a download? I'm fine with it, it makes the fish binary slightly larger, the magic of LTO (I think) means fish_key_reader stays the same size at about 25% of the main binary. CMake can be taught to install as a symlink, which saves on disk space. I don't think it will reduce package sizes because they should be compressed anyway.

@faho

faho commented Nov 28, 2024 •

Copy link
Copy Markdown
Member Author

cargo install already installs all three binaries, right?

That's right, and this doesn't change it.

We could remove one or both of them in future if we go through with this. fish_indent is probably more useful outside of fish given that it may be called by editors or CI scripts and such.

I doubt fish_key_reader sees much use (it's too tied to fish's bind notation for it).

So this would be more to allow us to publish a single binary as a download?

That's the big thing. We could have a fish.xz, and you would decompress and put it wherever you want.

Installing fish on a machine would be curl https://fishshell.com/current/fish.xz; xz -d fish.xz; ./fish --install; ./fish. On a remote machine you'd have scp fish user@host.

(I think we may still want a tarball because of permissions, I'm unsure)

Uninstallation is rm -r /path/to/fish ~/.local/share/fish/install.

Plus we could guarantee that fish_indent is available inside of fish - there's no way to break the installation by removing it or forgetting to install it.

E.g. see funced, where we call __fish_indent because we need to figure out where fish_indent is:

echo -n $cmd | __fish_indent --only-unindent | read -lz cmd

That can be entirely removed with this because it's just always available.

@faho faho mentioned this pull request Nov 29, 2024
4 of 8 tasks
@ridiculousfish

Copy link
Copy Markdown
Member

This is a good idea; I like this direction.

Comment thread src/builtins/fish_indent.rs Outdated
let long_opts: &[WOption] = &[
wopt(L!("debug"), ArgType::RequiredArgument, 'd'),
wopt(L!("debug-output"), ArgType::RequiredArgument, 'o'),
wopt(L!("debug-stack-frames"), ArgType::RequiredArgument, 'D'),

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I am inclined to remove these debug options - they've never been very useful and are awkward from inside fish. The third is also unused and has been for years now.

I would also move "--version" to the throwing_main because it is weird to have a builtin write its version?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: fish_indent --version is called from doc_src/conf.py to determine the version for the docs.

This should probably be passed in from the build script via an environment variable, like it already is in #10367.

Comment thread src/builtins/fish_indent.rs Resolved
Comment thread src/builtins/fish_indent.rs Outdated
'\x03' => output_type = OutputType::PygmentsCsv,
'c' => output_type = OutputType::Check,
'd' => {
activate_flog_categories_by_pattern(w.woptarg.unwrap());

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This currently allows activating debug options globally by running fish_indent.

I'm not sure we currently have anything else to do that?

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I've removed these - we would have to do some work to make them not affect the running fish session, and they don't really apply to the fish_indent itself.

If you want to debug how some script parses, you can use fish --no-execute --debug ....

wopt(L!("write"), ArgType::NoArgument, 'w'),
wopt(L!("html"), ArgType::NoArgument, '\x01'),
wopt(L!("ansi"), ArgType::NoArgument, '\x02'),
wopt(L!("pygments"), ArgType::NoArgument, '\x03'),

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is used by our docs - we highlight the code samples by shelling out to fish_indent.

That would have to be adjusted to call fish -c 'fish_indent $argv' args if we decide to drop the second binary.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Note: Making it fish -c would have to change the build order, but then it's extremely weird to have fish_indent, docs, fish in that order to begin with, and I'm not sure it helps anyone.

(we can't currently build the html docs without the corresponding fish_indent for lexing, which is a bit of a downside for #10367)

@faho

faho commented Dec 3, 2024

Copy link
Copy Markdown
Member Author

Also: This gives us the opportunity to rename fish_indent.

We could call it fishfmt, given that it does a bunch more than indentation, and for consistency with cargo fmt and gofmt.

Not saying we have to, but I wouldn't be opposed.

@krobelus

krobelus commented Dec 4, 2024

Copy link
Copy Markdown
Contributor

Sounds good.

Renaming would be great (also underscores are odd, I'd rather use "-" or no separator).

fish_key_reader could go into bind --which-keys eventually, not sure.

Comment thread src/builtins/fish_indent.rs Resolved
// We need to set the shell-modes for ICRNL,
// in fish-proper this is done once a command is run.
unsafe { libc::tcsetattr(0, TCSANOW, &*shell_modes()) };
terminal_protocol_hacks();

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

these 3 statements should not be necessary when running as builtin

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hmm except for TCSANOW which we need to keep it seems. Should try to clean this up eventually

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

The important bit here is the shell_modes, which we unset while running a builtin, apparently?

Tbh I'd want to leave this for now, it seems harmless either way.

Comment thread src/builtins/fish_key_reader.rs Resolved
Comment thread src/builtins/shared.rs Resolved
@faho

faho commented Dec 4, 2024 •

Copy link
Copy Markdown
Member Author

Renaming would be great (also underscores are odd, I'd rather use "-" or no separator).

If we did rename, I would want it to be more substantial change than fish-indent, especially given that we use underscores everywhere - fish_prompt, not fish-prompt.

Like I said, my favorite is fishfmt.

@faho
faho force-pushed the builtin-indent-and-key-reader branch from bd71cfc to cb530e3 Compare December 6, 2024 21:31
@faho

faho commented Dec 7, 2024

Copy link
Copy Markdown
Member Author

An alternative is to replace fish_indent with an option to the main fish binary - fish --format.

This would make it easier to call from the outside if you don't have symlinks (e.g. if you installed via cargo install).

However, there are some issues with that:

  • We would have to add the other fish_indent options like --check, --write, --ansi, --html there too
  • How do you call it from inside fish? Do we still add a builtin?

The issue with renaming is that we would have to keep the old name around longer in some way, even from the outside.

So we'd leave a fish_indent binary and/or symlink in place, but what gets editor plugins to move to the newer version then?

Overall, thinking more about it I don't think it's worth it.

Those are the questions I would like to have answers to before merging this, because that's the easiest time to do so.

@faho
faho marked this pull request as ready for review December 7, 2024 12:54
@faho faho mentioned this pull request Dec 27, 2024
1 task done
@zanchey

zanchey commented Jan 4, 2025

Copy link
Copy Markdown
Member

Disk is cheap and compression is good (there's essentially no difference in size between a .xz with one or two copies of the same binary, if they're stripped), I'd not bother with a symlink for now. Maybe make a note in the README for distributors who want to symlink instead.

I also don't think there's much to be gained from a rename.

@faho
faho force-pushed the builtin-indent-and-key-reader branch 2 times, most recently from 261531a to 1560c80 Compare January 10, 2025 09:08
faho added 2 commits January 10, 2025 10:12
This brings us closer to making fish available as a single file.
@faho
faho force-pushed the builtin-indent-and-key-reader branch from 1560c80 to a9926fb Compare January 10, 2025 09:22
@faho
faho merged commit c7358d1 into fish-shell:master Jan 10, 2025
@faho
faho deleted the builtin-indent-and-key-reader branch January 10, 2025 13:12
@faho faho removed this from the fish-future milestone Jan 10, 2025
@faho faho added this to the fish 4.1 milestone Jan 10, 2025
@faho

faho commented Jan 10, 2025

Copy link
Copy Markdown
Member Author

I've decided to merge this now so we can get some experience in.

There are improvements to be made, but it's already useful.

@krobelus

Copy link
Copy Markdown
Contributor

echo asdf | fish_indent hangs while /bin/echo asdf | fish_indent works.
Maybe we're not reading from stdin the way a builtin should.

@github-actions github-actions Bot locked as resolved and limited conversation to collaborators Jan 12, 2026
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.

Projects

None yet

Development

Successfully merging this pull request may close these issues.

4 participants