-
Notifications
You must be signed in to change notification settings - Fork 162
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
add cwd aware hinter #647
add cwd aware hinter #647
Conversation
Codecov Report
@@ Coverage Diff @@
## main #647 +/- ##
==========================================
- Coverage 49.76% 49.17% -0.60%
==========================================
Files 41 42 +1
Lines 7827 7921 +94
==========================================
Hits 3895 3895
- Misses 3932 4026 +94
|
I tried this out and it works - and I love the experience. This was the only thing I missed from fish. Help with writing tests would be appreciated |
Nice! It would be good to have an example that demonstrates its use in the examples folder. Can you add one of those please? It would also be ideal to have a test or two in the cwd_aware.rs file itself so we can ensure this feature isn't accidentally broken. |
I tried running the existing one ( // Create a reedline object with in-line hint support.
// cargo run --example cwd_aware_hinter
//
// Fish-style cwd history based hinting
// assuming history ["abc", "ade"]
// pressing "a" hints to abc.
// Up/Down or Ctrl p/n, to select next/previous match
use nu_ansi_term::{Color, Style};
use reedline::{CwdAwareHinter, DefaultPrompt, Reedline, Signal};
use std::io;
fn main() -> io::Result<()> {
let mut line_editor = Reedline::create().with_hinter(Box::new(
CwdAwareHinter::default().with_style(Style::new().italic().fg(Color::LightGray)),
));
let prompt = DefaultPrompt::default();
loop {
let sig = line_editor.read_line(&prompt)?;
match sig {
Signal::Success(buffer) => {
println!("We processed: {buffer}");
}
Signal::CtrlD | Signal::CtrlC => {
println!("\nAborted!");
break Ok(());
}
}
}
} |
I think so, but you'll need to add some items to the history in order to get hints to work. |
right - how do I change the cwd inside the repl though? One idea is to alternate between original cwd and |
I'm not sure exactly. The |
I tried https://paste.sr.ht/~p00f/5b683a638a097cf58f8ea4cdd7da17bf16a7e7c9 but it doesn't save the extra attributes:
perfect, i'll change that function to add a few more entries in |
- guard CwdAwareHinter with feature flag - remove references to fish from DefaultHinter as fish is cwd aware - add example
How does the example look? I tested the example and it works as intended. |
I think I need help writing tests as I'm not familiar with the code too much and there are no tests for the existing |
What is the behavior when you try to use it with |
On the nushell side I just use |
https://paste.sr.ht/~p00f/5752fc99ba31990710f08c2a85de897a5b15bf54#cwd.diff-L48 @sholderbach this is my nushell patch |
Note that this causes no undesirable behaviour - if you change the history format you don't get hints from the other history file anyway |
My caution is driven by the fact that we have a substantial number of non-nushell users of reedline. And since we introduced the new history search API (#401) we had a few panics based on the capabilities or assumptions what info is accessible (e.g. #480). I would like to avoid a situation where you would encounter runtime panics when the Things should be either abundantly clear from documentation, failure should happen either at the earliest point possible or handled gracefully.
This would bring every user of |
I see - there's no way to find out the type of imo I should just document that it should always be used with
If they do, I'll add a config option. At least for now, I think everyone will love this - fish has this by default without a config option, nobody complains. |
Personally, I pass on this behavior. I'll play with it and test it out, but I don't think I'll be using it. Let's make it configurable. I think it's great to have it, and there's no doubt that some people will love it. However, I've worked on nushell long enough to know that you will never please everyone. On a separate note, maybe I'll change my nick from "fdncred" to "everyone" then we can list the features that 'everyone' loves. 🤣 |
of course, I haven't created a nushell PR yet.
Please do, I hate when people ping |
src/hinter/cwd_aware.rs
Outdated
use nu_ansi_term::{Color, Style}; | ||
|
||
/// A hinter that uses the completions or the history to show a hint to the user | ||
/// NOTE: Only use this with `SqliteBackedHistory`! |
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.
@sholderbach How does this look?
Briefly, I don't think just documenting it is a good approach for the public API. With a Panicking patch
diff --git a/examples/cwd_aware_hinter.rs b/examples/cwd_aware_hinter.rs
index 17aa7dd..35f9c2f 100644
--- a/examples/cwd_aware_hinter.rs
+++ b/examples/cwd_aware_hinter.rs
@@ -49,16 +49,14 @@ fn main() -> io::Result<()> {
#[allow(deprecated)]
let home_dir = std::env::home_dir().unwrap();
- let history = create_filled_example_history(
- &home_dir.to_string_lossy().to_string(),
- &orig_dir.to_string_lossy().to_string(),
- );
+ let history = reedline::FileBackedHistory::new(100);
+
let mut line_editor = Reedline::create()
.with_hinter(Box::new(
CwdAwareHinter::default().with_style(Style::new().italic().fg(Color::Yellow)),
))
- .with_history(history);
+ .with_history(Box::new(history));
let prompt = DefaultPrompt::default(); Either we replace the current fallback/local error handlingAs check at construction timeYou are right that trait objects are limited on its own, but for "RTTI we have at home" the cost of adding a method that advertises a particular capability doesn't sound too bad if it is only checked in the constructors. P.S.: just to disagree with @fdncred I really like this feature as a default and would be ok with saving ourselves another config option (just strawmanning it in for API design purposes) |
Sorry, it took me a while to realize how "just don't use it" sounds. I went with the first option as the second one requires more thought. Thanks for the help! |
re: config option, we can introduce this without an option on |
@sholderbach i fixed the warning causing the CI failure, can you re-run it? |
Thanks for implementing the fallback!
Agree, your nushell patch looked already pretty good. |
sorry, I think this one should pass |
should I squash? |
No need for manual squashing, we happily use the Github button for that. Thanks for improving the quality of the hints. Looking forward to using them in Nushell |
How do I do the nushell PR? By uncommenting https://github.com/nushell/nushell/blob/d0dc6986dd14e7ea5443f3dc0eb5b101c5de5efc/Cargo.toml#L167 ? |
Yup uncommenting that line and running |
Yeah - I was wondering if it was acceptable to commit into main, then I read the diff - it was uncommented in |
towards nushell/nushell#8883