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

feat(cache): add missing cache commands #28

Merged
merged 6 commits into from
Nov 23, 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
16 changes: 16 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@ glob-match = { version = "0.2.1" }
home = "0.5.9"
indicatif = "0.17.8"
inquire = { version = "0.7.0", features = ["editor"] }
itertools = "0.13.0"
kdl = "=4.6.0"
miette = { version = "=5.10.0", features = ["fancy"] }
reqwest = { version = "0.11.22", features = ["json"] }
Expand Down
65 changes: 2 additions & 63 deletions src/actions/prompts.rs
Original file line number Diff line number Diff line change
@@ -1,70 +1,9 @@
use std::fmt::Display;
use std::process;

use crossterm::style::Stylize;
use inquire::formatter::StringFormatter;
use inquire::ui::{Color, RenderConfig, StyleSheet, Styled};
use inquire::{Confirm, CustomType, Editor, InquireError, Select, Text};
use inquire::{Confirm, CustomType, Editor, Select, Text};

use crate::actions::State;
use crate::config::prompts::*;
use crate::config::{Number, Value};

/// Helper module holding useful functions.
mod helpers {
use super::*;

/// Returns configured theme.
pub fn theme<'r>() -> RenderConfig<'r> {
let default = RenderConfig::default();
let stylesheet = StyleSheet::default();

let prompt_prefix = Styled::new("?").with_fg(Color::LightYellow);
let answered_prefix = Styled::new("✓").with_fg(Color::LightGreen);

default
.with_prompt_prefix(prompt_prefix)
.with_answered_prompt_prefix(answered_prefix)
.with_default_value(stylesheet.with_fg(Color::DarkGrey))
}

/// Returns a formatter that shows `<empty>` if the input is empty.
pub fn empty_formatter<'s>() -> StringFormatter<'s> {
&|input| {
if input.is_empty() {
"<empty>".dark_grey().to_string()
} else {
input.to_string()
}
}
}

/// Helper method that generates `(name, hint, help)`.
pub fn messages<S>(name: S, hint: S) -> (String, String, String)
where
S: Into<String> + AsRef<str> + Display,
{
let name = name.into();
let hint = format!("{}:", &hint);
let help = format!("The answer will be mapped to: {}", &name);

(name, hint, help)
}

/// Handle interruption/cancelation events.
pub fn interrupt(err: InquireError) {
match err {
| InquireError::OperationCanceled => {
process::exit(0);
},
| InquireError::OperationInterrupted => {
println!("{}", "<interrupted>".red());
process::exit(0);
},
| _ => {},
}
}
}
use crate::utils::prompts as helpers;

impl ConfirmPrompt {
/// Execute the prompt and populate the state.
Expand Down
25 changes: 16 additions & 9 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -78,8 +78,14 @@ pub struct RepositoryArgs {
pub enum CacheCommand {
/// List cache entries.
List,
/// Remove all cache entries.
Clear,
/// Remove cache entries.
Remove {
/// List of cache entries to remove.
entries: Vec<String>,
/// Remove all cache entries.
#[arg(short, long, conflicts_with_all = ["entries", "interactive"])]
all: bool,
},
}

#[derive(Debug)]
Expand All @@ -91,6 +97,7 @@ pub struct App {
}

impl App {
#[allow(clippy::new_without_default)]
pub fn new() -> Self {
Self {
cli: Cli::parse(),
Expand Down Expand Up @@ -296,7 +303,13 @@ impl App {

match command {
| CacheCommand::List => Ok(cache.list()?),
| CacheCommand::Clear => Ok(cache.clear()?),
| CacheCommand::Remove { entries, all } => {
if all {
cache.remove_all()
} else {
cache.remove(entries)
}
},
}
}

Expand All @@ -316,9 +329,3 @@ impl App {
Ok(())
}
}

impl Default for App {
fn default() -> Self {
Self::new()
}
}
Loading
Loading