Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix fish_config theme save #9273

Merged
merged 2 commits into from
Oct 22, 2022
Merged

Fix fish_config theme save #9273

merged 2 commits into from
Oct 22, 2022

Conversation

mqudsi
Copy link
Contributor

@mqudsi mqudsi commented Oct 13, 2022

The documentation states that running fish_config theme save after fish_config theme choose [theme_name] will result in "saving" the currently chosen theme, but this does not match the actual behavior of fish_config theme save which expects a trailing argument specifying the name of the theme to select/persist.

Given that the documented way has been included in a release and that it makes more sense than calling fish_config theme save xxx when you are loading from xxx and not saving to xxx, this patch revises fish_config.fish to support the documented behavior.

When fish_config theme choose xxx is used, xxx is saved to a hidden global variable __fish_last_chosen_theme. If fish_config theme save is called without a theme name specified, then this global variable is searched for the name of the last chosen theme and that is saved/persisted as the default theme.

Closes #9088.


I'm requesting a review because I have no idea how fish_config is meant to be used and I'm going solely off what I read in the function -- I've never used it myself. I'm just conforming the code to the existing docs.

# If we are persisting a theme, either get its name from $argv[1] or
# from the last chosen theme.
if contains -- $cmd choose
set -g __fish_last_chosen_theme $argv[1]
Copy link
Member

Choose a reason for hiding this comment

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

This isn't the right way to go about this.

The current theme is just the set of the current color variables, so running fish_config theme save should just persist the current variables (to universal variables)

Copy link
Member

Choose a reason for hiding this comment

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

Basically, what you need to do is just not read any file if $argv is empty, and instead only do the saving part.

So an if set -q argv[1] on line 226 .

Copy link
Member

Choose a reason for hiding this comment

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

In essence, fish_config theme save or fish_config prompt save is the "I'm happy with this" button. It just saves the current config.

So if you do fish_config theme choose coolbeans, and you decide that it's nice, and then you do fish_config theme save it should save the coolbeans theme, but if you decide to change a color or two and then fish_config theme save it should save the theme with those alterations.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

That makes sense, thanks.

@mqudsi
Copy link
Contributor Author

mqudsi commented Oct 14, 2022

I pushed an update that addresses your concerns, @faho. (I didn't force push, but I'll squash the commits into the latter if we end up going with that direction).

Note that the latest commit includes an important caveat/note:

This does not catch color variables unknown to fish! If a theme and a
prompt agree on some variable to hold some color but it's not a color variable
known to fish, it won't be persisted!

I think we have to search for all variables that match the whitelist filter we use when loading a theme from disk and persist all of those, not just the known colors?

Basically, replace for color in $known_colors with for color in $known_colors (set | string match -r '^fish_(?:pager_)?color.*$')

@mqudsi mqudsi force-pushed the fish_theme_save branch 2 times, most recently from 7a144aa to d66c19f Compare October 14, 2022 20:05
@faho
Copy link
Member

faho commented Oct 15, 2022

Basically, replace for color in $known_colors with for color in $known_colors (set | string match -r '^fish_(?:pager_)?color.*$')

set --names, but yeah, that's probably better.

@mqudsi
Copy link
Contributor Author

mqudsi commented Oct 15, 2022

Actually, thanks for reminding me to use --names. I've made that mistake before!

I just pushed an update that does that; I think now we can comfortably use fish_config theme save to persist whatever theme was already loaded.

Separate from this pull request, I think it might be more correct for loading ("choosing") a theme to unset any pattern-matched variables that weren't overwritten in the new theme; e.g. theme A sets '$fish_color_custom' then theme B is loaded - I think loading theme B should maybe erase $fish_color_custom? Because a subsequent fish_config theme dump will include theme A's variable even after theme B has been chosen.

But I'm not sure if there are prompts that set any custom colors; we wouldn't want fish_config theme choose to erase a variable set by a prompt rather than a different theme..

The documentation states that running `fish_config theme save` after
`fish_config theme choose [theme_name]` will result in "saving" the
currently chosen theme, but this does not match the actual behavior of
`fish_config theme save` which expects a trailing argument specifying
the name of the theme to select/persist.

Given that the documented way has been included in a release and that it
makes more sense than calling `fish_config theme save xxx` when you are
*loading from* xxx and not *saving to* xxx, this patch revises
`fish_config.fish` to support the documented behavior.

When `fish_config theme save xxx` is used, xxx is loaded w/ its specified colors
saved to the according variables in the universal scope. But if `fish_config
theme save` is used without a theme's name specified, then the currently
specified (known) fish color variables are persisted from whatever scope they're
currently in (usually in the global scope from previewing a theme) to universal
variables of the same name.

This does *not* catch color variables unknown to fish! If a theme and a
prompt agree on some variable to hold some color but it's not a color variable
known to fish, it won't be persisted!

Closes fish-shell#9088.
Don't just save known color values but any values that could have been loaded
from a .theme file.

Also, refactor the theme variable name whitelist/filter in a shared "global"
variable so we never forget to update it at any of the individual use sites.
@mqudsi mqudsi merged commit 5647f78 into fish-shell:master Oct 22, 2022
faho referenced this pull request Oct 22, 2022
@github-actions github-actions bot locked as resolved and limited conversation to collaborators Oct 23, 2023
@zanchey zanchey added this to the fish 3.6.0 milestone Dec 29, 2023
Sign up for free to subscribe to this conversation on GitHub. Already have an account? Sign in.
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

fish_config theme save returns "Too few arguments"
3 participants