diff --git a/CHANGELOG.md b/CHANGELOG.md index cb034da0..53924e5f 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -15,6 +15,8 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/), - Add `slumber new` subcommand to generate new collection files [#376](https://github.com/LucasPickering/slumber/issues/376) - Add `default` field to profiles - When using the CLI, the `--profile` argument can be omitted to use the default profile +- Reset edited recipe values to their default using `r` + - You can [customize the key](https://slumber.lucaspickering.me/book/api/configuration/input_bindings.html) to whatever you want ### Changed diff --git a/crates/config/src/input.rs b/crates/config/src/input.rs index 9650347c..28aca629 100644 --- a/crates/config/src/input.rs +++ b/crates/config/src/input.rs @@ -144,6 +144,8 @@ pub enum Action { /// Trigger the workflow to provide a temporary override for a recipe value /// (body/param/etc.) Edit, + /// Reset temporary recipe override to its default value + Reset, /// Browse request history History, /// Start a search/filter operation diff --git a/crates/tui/src/input.rs b/crates/tui/src/input.rs index d5ec19d2..936490b4 100644 --- a/crates/tui/src/input.rs +++ b/crates/tui/src/input.rs @@ -173,6 +173,7 @@ impl Default for InputEngine { Action::Toggle => KeyCode::Char(' ').into(), Action::Cancel => KeyCode::Esc.into(), Action::Edit => KeyCode::Char('e').into(), + Action::Reset => KeyCode::Char('r').into(), Action::SelectProfileList => KeyCode::Char('p').into(), Action::SelectRecipeList => KeyCode::Char('l').into(), Action::SelectRecipe => KeyCode::Char('c').into(), diff --git a/crates/tui/src/view/component/recipe_pane/authentication.rs b/crates/tui/src/view/component/recipe_pane/authentication.rs index f1572607..2a1c353a 100644 --- a/crates/tui/src/view/component/recipe_pane/authentication.rs +++ b/crates/tui/src/view/component/recipe_pane/authentication.rs @@ -90,8 +90,11 @@ impl AuthenticationDisplay { impl EventHandler for AuthenticationDisplay { fn update(&mut self, _: &mut UpdateContext, event: Event) -> Update { - if let Some(Action::Edit) = event.action() { + let action = event.action(); + if let Some(Action::Edit) = action { self.0.open_edit_modal(); + } else if let Some(Action::Reset) = action { + self.0.reset_override(); } else if let Some(SaveAuthenticationOverride(value)) = event.local() { self.0.set_override(value); } else { @@ -249,6 +252,28 @@ impl State { } } } + + /// Reset the value template override to the default from the recipe, and + /// recompute the template preview + fn reset_override(&mut self) { + match self { + Self::Basic { + username, + password, + selected_field, + } => match selected_field.data().selected() { + BasicFields::Username => { + username.reset_override(); + } + BasicFields::Password => { + password.reset_override(); + } + }, + Self::Bearer { token } => { + token.reset_override(); + } + } + } } /// Fields in a basic auth form @@ -316,6 +341,10 @@ mod tests { }) ); + // Reset username + component.send_key(KeyCode::Char('r')).assert_empty(); + assert_eq!(component.data().inner().override_value(), None); + // Edit password component.send_key(KeyCode::Down).assert_empty(); component.send_key(KeyCode::Char('e')).assert_empty(); @@ -324,10 +353,14 @@ mod tests { assert_eq!( component.data().inner().override_value(), Some(Authentication::Basic { - username: "user1!!!".into(), + username: "user1".into(), password: Some("hunter2???".into()) }) ); + + // Reset password + component.send_key(KeyCode::Char('r')).assert_empty(); + assert_eq!(component.data().inner().override_value(), None); } #[rstest] @@ -388,6 +421,10 @@ mod tests { component.data().inner().override_value(), Some(Authentication::Bearer("i am a token!!!".into())) ); + + // Reset token + component.send_key(KeyCode::Char('r')).assert_empty(); + assert_eq!(component.data().inner().override_value(), None); } /// Basic auth fields should load persisted overrides diff --git a/crates/tui/src/view/component/recipe_pane/body.rs b/crates/tui/src/view/component/recipe_pane/body.rs index 8b9a8ed0..7be9ae9c 100644 --- a/crates/tui/src/view/component/recipe_pane/body.rs +++ b/crates/tui/src/view/component/recipe_pane/body.rs @@ -201,8 +201,11 @@ impl RawBody { impl EventHandler for RawBody { fn update(&mut self, _: &mut UpdateContext, event: Event) -> Update { - if let Some(Action::Edit) = event.action() { + let action = event.action(); + if let Some(Action::Edit) = action { self.open_editor(); + } else if let Some(Action::Reset) = action { + self.body.reset_override(); } else if let Some(SaveBodyOverride(path)) = event.local() { self.load_override(path); } else { @@ -329,6 +332,10 @@ mod tests { persisted, Some(RecipeOverrideValue::Override("goodbye!".into())) ); + + // Reset edited state + component.send_key(KeyCode::Char('r')).assert_empty(); + assert_eq!(component.data().override_value(), None); } /// Override template should be loaded from the persistence store on init diff --git a/crates/tui/src/view/component/recipe_pane/persistence.rs b/crates/tui/src/view/component/recipe_pane/persistence.rs index 3a2d0f3d..996bc82e 100644 --- a/crates/tui/src/view/component/recipe_pane/persistence.rs +++ b/crates/tui/src/view/component/recipe_pane/persistence.rs @@ -74,10 +74,10 @@ impl RecipeTemplate { Self(PersistedLazy::new( persisted_key, RecipeTemplateInner { - template: template.clone(), + original_template: template.clone(), + override_template: None, preview: TemplatePreview::new(template, content_type), content_type, - is_overridden: false, }, )) } @@ -87,8 +87,14 @@ impl RecipeTemplate { self.0.get_mut().set_override(template); } + /// Reset the template override to the default from the recipe, and + /// recompute the template preview + pub fn reset_override(&mut self) { + self.0.get_mut().reset_override(); + } + pub fn template(&self) -> &Template { - &self.0.template + self.0.template() } pub fn preview(&self) -> &TemplatePreview { @@ -100,7 +106,7 @@ impl RecipeTemplate { } pub fn is_overridden(&self) -> bool { - self.0.is_overridden + self.0.override_template.is_some() } } @@ -109,18 +115,33 @@ impl RecipeTemplate { /// [set_override](Self::set_override) when needed. #[derive(Debug)] struct RecipeTemplateInner { - template: Template, + original_template: Template, + override_template: Option