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

Reset edited values #386

Merged
merged 1 commit into from
Sep 17, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 2 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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

Expand Down
2 changes: 2 additions & 0 deletions crates/config/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
1 change: 1 addition & 0 deletions crates/tui/src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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(),
Expand Down
41 changes: 39 additions & 2 deletions crates/tui/src/view/component/recipe_pane/authentication.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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();
Expand All @@ -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]
Expand Down Expand Up @@ -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
Expand Down
9 changes: 8 additions & 1 deletion crates/tui/src/view/component/recipe_pane/body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand Down Expand Up @@ -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
Expand Down
46 changes: 33 additions & 13 deletions crates/tui/src/view/component/recipe_pane/persistence.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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,
},
))
}
Expand All @@ -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 {
Expand All @@ -100,7 +106,7 @@ impl RecipeTemplate {
}

pub fn is_overridden(&self) -> bool {
self.0.is_overridden
self.0.override_template.is_some()
}
}

Expand All @@ -109,29 +115,43 @@ impl RecipeTemplate {
/// [set_override](Self::set_override) when needed.
#[derive(Debug)]
struct RecipeTemplateInner {
template: Template,
original_template: Template,
override_template: Option<Template>,
preview: TemplatePreview,
/// Retain this so we can rebuild the preview with it
content_type: Option<ContentType>,
is_overridden: bool,
}

impl RecipeTemplateInner {
fn template(&self) -> &Template {
self.override_template
.as_ref()
.unwrap_or(&self.original_template)
}

fn set_override(&mut self, template: Template) {
self.template = template.clone();
self.is_overridden = true;
self.preview = TemplatePreview::new(template, self.content_type);
self.override_template = Some(template);
self.render_preview();
}

fn reset_override(&mut self) {
self.override_template = None;
self.render_preview();
}

fn render_preview(&mut self) {
self.preview =
TemplatePreview::new(self.template().clone(), self.content_type);
}
}

impl PersistedContainer for RecipeTemplateInner {
type Value = RecipeOverrideValue;

fn get_to_persist(&self) -> Self::Value {
if self.is_overridden {
RecipeOverrideValue::Override(self.template.clone())
} else {
RecipeOverrideValue::Default
match &self.override_template {
Some(template) => RecipeOverrideValue::Override(template.clone()),
None => RecipeOverrideValue::Default,
}
}

Expand Down
5 changes: 3 additions & 2 deletions crates/tui/src/view/component/recipe_pane/recipe.rs
Original file line number Diff line number Diff line change
Expand Up @@ -188,8 +188,9 @@ impl Draw for RecipeDisplay {
frame.render_widget(
Paragraph::new(Span::styled(
format!(
"Press {} to edit value",
tui_context.input_engine.binding_display(Action::Edit)
"Press {} to edit value, {} to reset",
tui_context.input_engine.binding_display(Action::Edit),
tui_context.input_engine.binding_display(Action::Reset),
),
tui_context.styles.text.hint,
))
Expand Down
15 changes: 14 additions & 1 deletion crates/tui/src/view/component/recipe_pane/table.rs
Original file line number Diff line number Diff line change
Expand Up @@ -107,11 +107,18 @@ where
RowToggleKey: PersistedKey<Value = bool>,
{
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 {
if let Some(selected_row) = self.select.data().selected() {
selected_row.open_edit_modal();
}
// Consume the event even if we have no rows, for consistency
} else if let Some(Action::Reset) = action {
if let Some(selected_row) =
self.select.data_mut().get_mut().selected_mut()
{
selected_row.value.reset_override();
}
} else if let Some(SaveRecipeTableOverride { row_index, value }) =
event.local()
{
Expand Down Expand Up @@ -449,6 +456,12 @@ mod tests {
.into_iter()
.collect(),
);

// Reset edited state
component.send_key(KeyCode::Char('r')).assert_empty();
let selected_row =
component.data().inner().select.data().selected().unwrap();
assert!(!selected_row.value.is_overridden());
}

/// Override templates should be loaded from the store on init
Expand Down
8 changes: 7 additions & 1 deletion crates/tui/src/view/state/select.rs
Original file line number Diff line number Diff line change
Expand Up @@ -217,10 +217,16 @@ impl<Item, State: SelectStateData> SelectState<Item, State> {
/// Get the currently selected item (if any)
pub fn selected(&self) -> Option<&Item> {
self.items
.get(self.state.borrow().selected()?)
.get(self.selected_index()?)
.map(|item| &item.value)
}

/// Mutable reference to the currently selected item (if any)
pub fn selected_mut(&mut self) -> Option<&mut Item> {
let index = self.selected_index()?;
self.items.get_mut(index).map(|item| &mut item.value)
}

/// Select an item by value. Context is required for callbacks. Generally
/// the given value will be the type `Item`, but it could be anything that
/// compares to `Item` (e.g. an ID type).
Expand Down
71 changes: 36 additions & 35 deletions docs/src/api/configuration/input_bindings.md
Original file line number Diff line number Diff line change
Expand Up @@ -31,41 +31,42 @@ input_bindings:

## Actions

| Action | Default Binding |
| --------------------- | --------------------------- |
| `left_click` | None |
| `right_click` | None |
| `scroll_up` | None |
| `scroll_down` | None |
| `scroll_left` | `shift left` |
| `scroll_right` | `shift right` |
| `quit` | `q` |
| `force_quit` | `ctrl c` |
| `previous_pane` | `backtab` (AKA `shift tab`) |
| `next_pane` | `tab` |
| `up` | `up` |
| `down` | `down` |
| `left` | `left` |
| `right` | `right` |
| `page_up` | `pgup` |
| `page_down` | `pgdn` |
| `home` | `home` |
| `end` | `end` |
| `submit` | `enter` |
| `toggle` | `space` |
| `cancel` | `esc` |
| `edit` | `e` |
| `history` | `h` |
| `search` | `/` |
| `reload_collection` | `f5` |
| `fullscreen` | `f` |
| `open_actions` | `x` |
| `open_help` | `?` |
| `select_profile_list` | `p` |
| `select_recipe_list` | `l` |
| `select_recipe` | `c` |
| `select_request` | `r` |
| `select_response` | `s` |
| Action | Default Binding | Description |
| --------------------- | --------------------------- | ----------------------------------------------------- |
| `left_click` | None | |
| `right_click` | None | |
| `scroll_up` | None | |
| `scroll_down` | None | |
| `scroll_left` | `shift left` | |
| `scroll_right` | `shift right` | |
| `quit` | `q` | Exit current dialog, or the entire app |
| `force_quit` | `ctrl c` | Exit the app, regardless |
| `previous_pane` | `backtab` (AKA `shift tab`) | Select previous pane in the cycle |
| `next_pane` | `tab` | |
| `up` | `up` | |
| `down` | `down` | |
| `left` | `left` | |
| `right` | `right` | |
| `page_up` | `pgup` | |
| `page_down` | `pgdn` | |
| `home` | `home` | |
| `end` | `end` | |
| `submit` | `enter` | Send a request, submit a text box, etc. |
| `toggle` | `space` | Toggle a checkbox on/off |
| `cancel` | `esc` | Cancel current dialog or request |
| `edit` | `e` | Apply a temporary override to a recipe value |
| `reset` | `r` | Reset temporary recipe override to its default |
| `history` | `h` | Open request history for a recipe |
| `search` | `/` | Open/select search for current pane |
| `reload_collection` | `f5` | Force reload collection file |
| `fullscreen` | `f` | Fullscreen current pane |
| `open_actions` | `x` | Open actions menu |
| `open_help` | `?` | Open help dialog |
| `select_profile_list` | `p` | Open Profile List dialog |
| `select_recipe_list` | `l` | Select Recipe List pane |
| `select_recipe` | `c` | Select Recipe pane |
| `select_response` | `s` | Select Request/Response pane |
| `select_request` | `r` | Select Request/Response pane (backward compatibility) |

> Note: mouse bindings are not configurable; mouse actions such as `left_click` _can_ be bound to a key combination, which cannot be unbound from the default mouse action.

Expand Down
Loading