-
Notifications
You must be signed in to change notification settings - Fork 172
feat(cli): implement a tip system for CLI commands to enhance user experience #583
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
Merged
Changes from all commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,121 @@ | ||
| //! CLI tips system for providing helpful suggestions to users. | ||
| //! | ||
| //! Tips are shown after command execution to help users discover features | ||
| //! and shortcuts. | ||
|
|
||
| mod short_aliases; | ||
| mod use_vpx_or_run; | ||
|
|
||
| use clap::error::ErrorKind as ClapErrorKind; | ||
|
|
||
| use self::{short_aliases::ShortAliases, use_vpx_or_run::UseVpxOrRun}; | ||
|
|
||
| /// Execution context passed in from the CLI entry point. | ||
| pub struct TipContext { | ||
| /// CLI arguments as typed by the user, excluding the program name (`vp`). | ||
| pub raw_args: Vec<String>, | ||
| /// The exit code of the command (0 = success, non-zero = failure). | ||
| pub exit_code: i32, | ||
| /// The clap error if parsing failed. | ||
| pub clap_error: Option<clap::Error>, | ||
| } | ||
|
|
||
| impl Default for TipContext { | ||
| fn default() -> Self { | ||
| TipContext { raw_args: Vec::new(), exit_code: 0, clap_error: None } | ||
| } | ||
| } | ||
|
|
||
| impl TipContext { | ||
| /// Whether the command completed successfully. | ||
| #[expect(dead_code)] | ||
| pub fn success(&self) -> bool { | ||
| self.exit_code == 0 | ||
| } | ||
|
|
||
| #[expect(dead_code)] | ||
| pub fn is_unknown_command_error(&self) -> bool { | ||
| if let Some(err) = &self.clap_error { | ||
| matches!(err.kind(), ClapErrorKind::InvalidSubcommand) | ||
| } else { | ||
| false | ||
| } | ||
| } | ||
|
|
||
| /// Iterate positional args (skipping flags starting with `-`). | ||
| fn positionals(&self) -> impl Iterator<Item = &str> { | ||
| self.raw_args.iter().map(String::as_str).filter(|a| !a.starts_with('-')) | ||
| } | ||
|
|
||
| /// The subcommand (first positional arg, e.g., "ls", "build"). | ||
| pub fn subcommand(&self) -> Option<&str> { | ||
| self.positionals().next() | ||
| } | ||
|
|
||
| /// Whether the positional args start with the given command pattern. | ||
| /// Pattern is space-separated: "pm list" matches even if flags are interspersed. | ||
| #[expect(dead_code)] | ||
| pub fn is_subcommand(&self, pattern: &str) -> bool { | ||
| let mut positionals = self.positionals(); | ||
| pattern.split_whitespace().all(|expected| positionals.next() == Some(expected)) | ||
| } | ||
| } | ||
|
|
||
| /// A tip that can be shown to the user after command execution. | ||
| pub trait Tip { | ||
| /// Whether this tip is relevant given the current execution context. | ||
| fn matches(&self, ctx: &TipContext) -> bool; | ||
| /// The tip text shown to the user. | ||
| fn message(&self) -> &'static str; | ||
| } | ||
|
|
||
| /// Returns all registered tips. | ||
| fn all() -> &'static [&'static dyn Tip] { | ||
| &[&ShortAliases, &UseVpxOrRun] | ||
| } | ||
|
|
||
| /// Pick a random tip from those matching the current context. | ||
| /// | ||
| /// Returns `None` if: | ||
| /// - The `VITE_PLUS_CLI_TEST` env var is set (test mode) | ||
| /// - No tips match the given context | ||
| pub fn get_tip(context: &TipContext) -> Option<&'static str> { | ||
| if std::env::var_os("VITE_PLUS_CLI_TEST").is_some() { | ||
| return None; | ||
| } | ||
|
|
||
| let now = | ||
| std::time::SystemTime::now().duration_since(std::time::UNIX_EPOCH).unwrap_or_default(); | ||
|
|
||
| let all = all(); | ||
| let matching: Vec<&&dyn Tip> = all.iter().filter(|t| t.matches(context)).collect(); | ||
|
|
||
| if matching.is_empty() { | ||
| return None; | ||
| } | ||
|
|
||
| // Use subsec_nanos for random tip selection | ||
| let nanos = now.subsec_nanos() as usize; | ||
| Some(matching[nanos % matching.len()].message()) | ||
| } | ||
|
|
||
| /// Create a `TipContext` from a command string using real clap parsing. | ||
| /// | ||
| /// `command` is exactly what the user types in the terminal (e.g. `"vp list --flag"`). | ||
| /// The first arg is treated as the program name and excluded from `raw_args`, | ||
| /// matching how the real CLI uses `std::env::args()`. | ||
| #[cfg(test)] | ||
| pub fn tip_context_from_command(command: &str) -> TipContext { | ||
| // Split simulates what the OS does with command line args | ||
| let args: Vec<String> = command.split_whitespace().map(String::from).collect(); | ||
|
|
||
| let (exit_code, clap_error) = match crate::try_parse_args_from(args.iter().cloned()) { | ||
| Ok(_) => (0, None), | ||
| Err(e) => (e.exit_code(), Some(e)), | ||
| }; | ||
|
|
||
| // raw_args excludes program name (args[0]), same as real CLI: args[1..].to_vec() | ||
| let raw_args = args.get(1..).map(<[String]>::to_vec).unwrap_or_default(); | ||
|
|
||
| TipContext { raw_args, exit_code, clap_error } | ||
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,63 @@ | ||
| //! Tip suggesting short aliases for long-form commands. | ||
|
|
||
| use super::{Tip, TipContext}; | ||
|
|
||
| /// Long-form commands that have short aliases. | ||
| const LONG_FORMS: &[&str] = &["install", "remove", "uninstall", "update", "list", "link"]; | ||
|
|
||
| /// Suggest short aliases when user runs a long-form command. | ||
| pub struct ShortAliases; | ||
|
|
||
| impl Tip for ShortAliases { | ||
| fn matches(&self, ctx: &TipContext) -> bool { | ||
| ctx.subcommand().is_some_and(|cmd| LONG_FORMS.contains(&cmd)) | ||
| } | ||
|
|
||
| fn message(&self) -> &'static str { | ||
| "Available short aliases: i = install, rm = remove, un = uninstall, up = update, ls = list, ln = link" | ||
| } | ||
| } | ||
|
|
||
| #[cfg(test)] | ||
| mod tests { | ||
| use super::*; | ||
| use crate::tips::tip_context_from_command; | ||
|
|
||
| #[test] | ||
| fn matches_long_form_commands() { | ||
| for cmd in LONG_FORMS { | ||
| let ctx = tip_context_from_command(&format!("vp {cmd}")); | ||
| assert!(ShortAliases.matches(&ctx), "should match {cmd}"); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn does_not_match_short_form_commands() { | ||
| let short_forms = ["i", "rm", "un", "up", "ln"]; | ||
| for cmd in short_forms { | ||
| let ctx = tip_context_from_command(&format!("vp {cmd}")); | ||
| assert!(!ShortAliases.matches(&ctx), "should not match {cmd}"); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn does_not_match_other_commands() { | ||
| let other_commands = ["build", "test", "lint", "run", "pack"]; | ||
| for cmd in other_commands { | ||
| let ctx = tip_context_from_command(&format!("vp {cmd}")); | ||
| assert!(!ShortAliases.matches(&ctx), "should not match {cmd}"); | ||
| } | ||
| } | ||
|
|
||
| #[test] | ||
| fn install_shows_short_alias_tip() { | ||
| let ctx = tip_context_from_command("vp install"); | ||
| assert!(ShortAliases.matches(&ctx)); | ||
| } | ||
|
|
||
| #[test] | ||
| fn short_form_does_not_show_tip() { | ||
| let ctx = tip_context_from_command("vp i"); | ||
| assert!(!ShortAliases.matches(&ctx)); | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,39 @@ | ||
| //! Tip suggesting vpx or vp run for unknown commands. | ||
|
|
||
| use super::{Tip, TipContext}; | ||
|
|
||
| /// Suggest `vpx <bin>` or `vp run <script>` when an unknown command is used. | ||
| pub struct UseVpxOrRun; | ||
|
|
||
| impl Tip for UseVpxOrRun { | ||
| fn matches(&self, _ctx: &TipContext) -> bool { | ||
| // TODO: Enable when `vpx` is supported | ||
| // ctx.is_unknown_command_error() | ||
| false | ||
|
hyf0 marked this conversation as resolved.
|
||
| } | ||
|
|
||
| fn message(&self) -> &'static str { | ||
| "Run a local binary with `vpx <bin>`, or a script with `vp run <script>`" | ||
| } | ||
| } | ||
|
|
||
| // TODO: Re-enable tests when `vpx` is supported | ||
| // #[cfg(test)] | ||
| // mod tests { | ||
| // use super::*; | ||
| // use crate::tips::tip_context_from_command; | ||
| // | ||
| // #[test] | ||
| // fn matches_on_unknown_command() { | ||
| // let ctx = tip_context_from_command("vp typecheck"); | ||
| // assert!(UseVpxOrRun.matches(&ctx)); | ||
| // assert!(ctx.is_unknown_command_error()); | ||
| // } | ||
| // | ||
| // #[test] | ||
| // fn does_not_match_on_known_command() { | ||
| // let ctx = tip_context_from_command("vp build"); | ||
| // assert!(!UseVpxOrRun.matches(&ctx)); | ||
| // assert!(!ctx.is_unknown_command_error()); | ||
| // } | ||
| // } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.