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

Support MIME map for commands.default_query #446

Merged
merged 3 commits into from
Jan 4, 2025
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
5 changes: 3 additions & 2 deletions crates/config/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -143,7 +143,8 @@ pub struct CommandsConfig {
/// If empty, commands will be parsed with shell-words and run natievly
pub shell: Vec<String>,
/// Default query command for responses
pub query_default: Option<String>,
#[serde(default)]
pub default_query: MimeMap<String>,
}

impl Default for CommandsConfig {
Expand All @@ -159,7 +160,7 @@ impl Default for CommandsConfig {

Self {
shell: default_shell.iter().map(|s| s.to_string()).collect(),
query_default: None,
default_query: MimeMap::default(),
}
}
}
Expand Down
10 changes: 5 additions & 5 deletions crates/tui/src/view/component/queryable_body.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,7 +43,7 @@ pub struct QueryableBody {
/// Default query to use when none is present. We have to store this so we
/// can apply it when an empty query is loaded from persistence. Generally
/// this will come from the config but it's parameterized for testing
query_default: Option<String>,
default_query: Option<String>,
/// Track status of the current query command
query_state: QueryState,
/// Where the user enters their body query
Expand Down Expand Up @@ -81,7 +81,7 @@ impl QueryableBody {
emitter_id: EmitterId::new(),
response,
query_focused: false,
query_default: default_query,
default_query,
last_executed_command: None,
query_state: QueryState::None,
query_text_box: query_text_box.into(),
Expand Down Expand Up @@ -279,7 +279,7 @@ impl PersistedContainer for QueryableBody {
// for this recipe). It's possible the user really wants an empty box
// and this is annoying, but I think it'll be more good than bad.
if text_box.text().is_empty() {
if let Some(query) = self.query_default.clone() {
if let Some(query) = self.default_query.clone() {
self.query_text_box.data_mut().set_text(query);
}
}
Expand Down Expand Up @@ -558,7 +558,7 @@ mod tests {
/// Test that the user's configured query default is applied on a fresh load
#[rstest]
#[tokio::test]
async fn test_query_default_initial(
async fn test_default_query_initial(
harness: TestHarness,
terminal: TestTerminal,
response: Arc<ResponseRecord>,
Expand Down Expand Up @@ -586,7 +586,7 @@ mod tests {
/// persisted value, but it's an empty string
#[rstest]
#[tokio::test]
async fn test_query_default_persisted(
async fn test_default_query_persisted(
harness: TestHarness,
terminal: TestTerminal,
response: Arc<ResponseRecord>,
Expand Down
10 changes: 6 additions & 4 deletions crates/tui/src/view/component/response_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,12 +38,14 @@ pub struct ResponseBodyView {

impl ResponseBodyView {
pub fn new(recipe_id: RecipeId, response: Arc<ResponseRecord>) -> Self {
// Select default query based on content type
let config = &TuiContext::get().config.commands;
let default_query = response
.mime()
.and_then(|mime| config.default_query.get(&mime).cloned());
let body = PersistedLazy::new(
ResponseQueryPersistedKey(recipe_id),
QueryableBody::new(
Arc::clone(&response),
TuiContext::get().config.commands.query_default.clone(),
),
QueryableBody::new(Arc::clone(&response), default_query),
)
.into();
Self {
Expand Down
1 change: 1 addition & 0 deletions docs/src/SUMMARY.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,7 @@

- [slumber collections](./cli/collections.md)
- [slumber generate](./cli/generate.md)
- [slumber history](./cli/history.md)
- [slumber import](./cli/import.md)
- [slumber new](./cli/new.md)
- [slumber request](./cli/request.md)
Expand Down
4 changes: 2 additions & 2 deletions docs/src/api/configuration/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -42,9 +42,9 @@ The following fields are available in `config.yml`:

Shell used to execute commands within the TUI. Use `[]` for no shell (commands will be parsed and executed directly). [More info](../../user_guide/tui/filter_query.md)

### `commands.query_default`
### `commands.default_query`

**Type:** `string`
**Type:** `string` or `mapping[Mime, string]` (see [MIME Maps](./mime.md))

**Default:** `""`

Expand Down
12 changes: 12 additions & 0 deletions docs/src/cli/history.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
# `slumber history`

View your Slumber request history.

## Examples

```sh
slumber history list login # List all requests for the "login" recipe
slumber history list login -p dev # List all requests for "login" under the "dev" profile
slumber history get login # Get the most recent request/response for "login"
slumber history get 548ba3e7-3b96-4695-9856-236626ea0495 # Get a particular request/response by ID (IDs can be retrieved from the `list` subcommand)
```
10 changes: 10 additions & 0 deletions docs/src/user_guide/tui/filter_query.md
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,16 @@ _Example of using pipes in a query command_

Keep in mind that your queries are being executed as shell commands on your system. You should avoid running any commands that interact with the file system, such as using `>` or `<` to pipe to/from files. TODO add more about side-effect commands once implemented

## Default command

You can set the default command to query with via the [`commands.default_query`](../../api/configuration/index.md#commandsdefault_query) config field. This accepts either a single string to set it for all content types, or a [MIME map](../../api/configuration/mime.md) to set different defaults based on the response content type. For example, to default to `jq` for all JSON responses:

```yaml
commands:
default_query:
json: jq
```

## Which shell does Slumber use?

By default, Slumber executes your command via `sh -c` on Unix and `cmd /S /C` on Windows. You can customize this via the [`commands.shell` configuration field](../../api/configuration/index.md#commandsshell). For example, to use `fish` instead of `sh`:
Expand Down
Loading