-
-
Notifications
You must be signed in to change notification settings - Fork 2.6k
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
feat(commands): allows cycling option values at runtime #4411
Conversation
Looks similar to #4085 |
You are right, with the addition that cycle allows us to cycle settings that are not booleans. Let me know the best way to move forward. |
Maybe I could remove the logic that flips booleans, so boolean settings are handled with toggle. String settings are handled with cycle. |
Ah sorry, I forgot about this PR and merged #4085 first. Can you rebase and update the I also think it would be interesting if it allowed you to cycle between options without having to specify possible values. |
Thanks for this, I am really hoping it will have the ability to toggle line numbers:
Is that possible yet? |
I thought about it when independently working on a toggle command. With enumerated values we have enough information to cycle, but the way things are currently implemented rely on the toml encoding, which doesn't have this information anymore. I think this is especially important to avoid boolean blindness and avoid nudging implementors using booleans when 2-valued enums would be better. Another reason why it would be interesting to do that is that the underlying mechanism could also be used to provide completion for setting values as well, not just keys. If there's no active work on this, i can give it a try. |
@archseer you want to update the toggle implementation to what I propose in the cycle one? |
2fd171a
to
1c1c846
Compare
36c1003
to
25c8583
Compare
Updated toggle to support cycling of options 👍 |
c703836
to
cc6e5ab
Compare
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
great that you are picking this back up!
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
LGTM this makes :toggle
much more useful 👍
@alevinval Thanks, great contribution! I am fiddling around trying to answer my own question but can't get anywhere, could you quickly let me know if #4411 (comment) is possible? |
To keep it short: no, the current implementation is quite limited in supporting cycling of configuration values that have a string value out of several possibilities. What you are suggesting would require adding/removing elements from the vector. I could look into extending the implementation to deal with arrays, such that if you do |
Looking back into this... I've just realised that I wrote some poor code here, I did not realise that I could pattern match on It does two things:
Based on that clean-up PR, if we were to do something like that: diff --git a/helix-term/src/commands/typed.rs b/helix-term/src/commands/typed.rs
index 824abbf4..656e4aa3 100644
--- a/helix-term/src/commands/typed.rs
+++ b/helix-term/src/commands/typed.rs
@@ -1,3 +1,4 @@
+use std::cmp::min;
use std::fmt::Write;
use std::ops::Deref;
@@ -1824,7 +1825,22 @@ fn toggle_option(
.to_string(),
)
}
- Value::Null | Value::Object(_) | Value::Array(_) | Value::Number(_) => {
+ Value::Array(ref mut arr) => {
+ ensure!(
+ args.len() == 3,
+ "Bad arguments. For array configurations use: `:toggle key val index`",
+ );
+
+ let value = Value::String(args[1].to_string());
+ let index: usize = min(arr.len(), args[2].parse()?);
+ if arr.contains(&value) {
+ arr.retain(|e| *e != value)
+ } else {
+ arr.insert(index, value)
+ }
+ Value::Array(arr.to_owned())
+ }
+ Value::Null | Value::Object(_) | Value::Number(_) => {
anyhow::bail!("Configuration {key} does not support toggle yet")
}
};
Screen.Recording.2023-06-06.at.18.26.33.movSupporting toggling on You also get issues with repeated values, as the gutters layout contains a couple of spacer entries, so depending on the order of running the commands you end up with a mess. All in all, I think this is very tricky and not sure is worth pursuing. Let me know thoughts on this. |
@alevinval With great joy I realized that a toggleable zen mode is almost within reach, but this does not work :(
Would it be easy to fix? Would be a massive feature! |
I did not implement |
Cheers, using this for now :)
|
Is there reason you can't just use parse like in set-option for everything that's not a string or a bool: https://github.com/helix-editor/helix/blob/master/helix-term/src/commands/typed.rs#L1802 |
Ah you're genius! Here's the PR to add support to toggle numbers: #7877 |
This patch updates the
:toggle
command to supports cycling of values at runtime. When the config is a boolean, it flips the value, otherwise cycles the argument strings. See the demo below: