Skip to content

Commit

Permalink
Accept a query parameter to start (#102)
Browse files Browse the repository at this point in the history
Fixes: #92
  • Loading branch information
joshka authored Nov 11, 2024
1 parent 2ce18f4 commit 4959637
Show file tree
Hide file tree
Showing 4 changed files with 22 additions and 7 deletions.
2 changes: 1 addition & 1 deletion src/action.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@ pub enum Action {
Suspend,
Resume,
Quit,
Init,
Init { query: Option<String> },
Refresh,
NextTab,
PreviousTab,
Expand Down
21 changes: 16 additions & 5 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -117,11 +117,16 @@ impl App {
}

/// Runs the main loop of the application, handling events and actions
pub async fn run(&mut self, mut tui: Tui, mut events: Events) -> Result<()> {
pub async fn run(
&mut self,
mut tui: Tui,
mut events: Events,
query: Option<String>,
) -> Result<()> {
// uncomment to test error handling
// panic!("test panic");
// Err(color_eyre::eyre::eyre!("Error"))?;
self.tx.send(Action::Init)?;
self.tx.send(Action::Init { query })?;

loop {
if let Some(e) = events.next().await {
Expand Down Expand Up @@ -207,7 +212,7 @@ impl App {
match action {
Action::Quit => self.quit(),
Action::KeyRefresh => self.key_refresh_tick(),
Action::Init => self.init()?,
Action::Init { ref query } => self.init(query)?,
Action::Tick => self.tick(),
Action::StoreTotalNumberOfCrates(n) => self.store_total_number_of_crates(n),
Action::ScrollUp => self.scroll_up(),
Expand Down Expand Up @@ -282,8 +287,14 @@ impl App {
self.search.update_search_table_results();
}

fn init(&mut self) -> Result<()> {
self.summary.request()?;
fn init(&mut self, query: &Option<String>) -> Result<()> {
if let Some(query) = query {
self.search.search = query.clone();
let _ = self.tx.send(Action::SwitchMode(Mode::Search));
let _ = self.tx.send(Action::SubmitSearch);
} else {
self.summary.request()?;
}
Ok(())
}

Expand Down
4 changes: 4 additions & 0 deletions src/cli.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,6 +45,10 @@ const HELP_STYLES: Styles = Styles::styled()
#[derive(Debug, Default, Parser, Serialize)]
#[command(author, version = version(), about, long_about = None, styles = HELP_STYLES)]
pub struct Cli {
/// Initial Query
#[arg(value_name = "QUERY")]
pub query: Option<String>,

/// Print default configuration
#[arg(long)]
pub print_default_config: bool,
Expand Down
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -28,7 +28,7 @@ async fn main() -> Result<()> {

let tui = tui::init()?;
let events = events::Events::new();
App::new().run(tui, events).await?;
App::new().run(tui, events, cli.query).await?;
tui::restore()?;
Ok(())
}

0 comments on commit 4959637

Please sign in to comment.