bacon icon indicating copy to clipboard operation
bacon copied to clipboard

Shortcut for `cargo fmt`

Open kpcyrd opened this issue 1 year ago • 12 comments

hello! I've switched from cargo watch to bacon recently, and noticed my workflow of:

ctrl+z, cargo fmt, fg

is not possible with bacon. I was wondering if maybe this feature is common enough to add it as a bacon operation?

I'd also get if this is not a good fit. Let me know what you think!

kpcyrd avatar Nov 19 '24 13:11 kpcyrd

I would say bacon doesn't have any business changing files. I would either wire up a custom trigger (git ls-files | entr -c rustfmt /_) or use an editor trigger. Most LSP enabled editors have formatting triggers built in and those play nicer with any potential multi-file writes than filesystem event triggered modifications.

alerque avatar Nov 19 '24 13:11 alerque

As @alerque said, fmt should probably not be done in bacon. Personally I do it with a shortcut in my editor and, depending on the project, with scripts or git hooks.

But I may be missing something. What's that fg ?

Canop avatar Nov 19 '24 13:11 Canop

It’s the opposite of ctrl+z in POSIX shells: After pausing the current foreground process (ctrl+z), you can resume it as foreground (fg) or background (bg) task. The above sequence pauses cargo watch to run cargo fmt from the interactive shell, then returns to the previous state.

Supporting that workflow would mean to change the SIGTSTP handler to switch back to normal terminal mode (same as when hitting q) and accept the pause rather than ignoring it (and switch again to bacon display when getting SIGCONT).

Ltrlg avatar Nov 19 '24 14:11 Ltrlg

But why do you want to stop bacon ? You don't have to stop it just because you do a cargo fmt. Is it because you have only one terminal on screen and you want it to launch cargo fmt ? The watch/exec algorithm of bacon has no problem with dozens of files being modified at about the same time.

Canop avatar Nov 19 '24 14:11 Canop

I have occasionally just added a format job to bacon and trigger it with f. This is useful for when there are non-editor edits to files (e.g. git merge operations / github suggestions / sed replacements).

But really, set your editor to auto-format on save.

joshka avatar Nov 21 '24 01:11 joshka

I spent the past few months fighting the "auto-format on save" setting because in the projects I edit, Rust is the only one with formatting properly setup. In case somebody else has the same problem, I concluded instead of disabling it for each other file type I should use:

    "[rust]" {
        "editor.formatOnSave": true
    }

kpcyrd avatar Mar 10 '25 10:03 kpcyrd

I use stable rust normally except I want line wrapping for comment which is only available on nightly and I use bacon to run cargo fmt with the following job that I put int prefs.toml so it's available for all my projects.

[jobs.fmt]
command = ["cargo", "+nightly", "fmt"]

and I just run it in a separate terminal. Not sure if this helps the OP.

PS. My editors (vscode rn) does do fmt on save but it uses the same version of rust which is stable and doesn't give the comment line wrapping that I want.

c-git avatar Mar 10 '25 15:03 c-git

I'm adding this job in bacon's bacon.toml

Canop avatar Mar 10 '25 15:03 Canop

Not everyone will have the nightly compiler installed. If I were adding it in general I would leave out the nightly.

c-git avatar Mar 10 '25 16:03 c-git

It's not for the default bacon.toml, but just for the one of bacon, used when developing bacon. Bacon contributors have to install nightly before they can submit a PR.

Canop avatar Mar 10 '25 16:03 Canop

Oh ok cool, I follow what you mean now.

c-git avatar Mar 10 '25 16:03 c-git

fmt (with stable) would be a nice job to add as a default, but it's also possible to add this to the user level bacon config, so that's not a huge deal.

joshka avatar Mar 10 '25 22:03 joshka