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

Start crates-tui with initial query. #92

Closed
TornaxO7 opened this issue Aug 12, 2024 · 1 comment · Fixed by #102
Closed

Start crates-tui with initial query. #92

TornaxO7 opened this issue Aug 12, 2024 · 1 comment · Fixed by #102

Comments

@TornaxO7
Copy link
Contributor

Hi! I often just want to search for a package quickly. So something like crates-tui --query ratatui should show up the TUI but also start the query immediately so that I'm getting the search result instantly.

I've tried to dig into the code with the following diff:

diff --git a/src/app.rs b/src/app.rs
index 99acd42..bc26892 100644
--- a/src/app.rs
+++ b/src/app.rs
@@ -117,12 +117,22 @@ 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,
+        init_query: Option<String>,
+    ) -> Result<()> {
         // uncomment to test error handling
         // panic!("test panic");
         // Err(color_eyre::eyre::eyre!("Error"))?;
         self.tx.send(Action::Init)?;
 
+        if let Some(query) = init_query {
+            self.handle_action(Action::NextTab)?;
+            self.search.submit_query(query);
+        }
+
         loop {
             if let Some(e) = events.next().await {
                 self.handle_event(e)?.map(|action| self.tx.send(action));
@@ -229,7 +239,7 @@ impl App {
             Action::PreviousTab => self.goto_previous_tab(),
             Action::SwitchMode(mode) => self.switch_mode(mode),
             Action::SwitchToLastMode => self.switch_to_last_mode(),
-            Action::SubmitSearch => self.search.submit_query(),
+            Action::SubmitSearch => self.search.submit_query(self.search.input.value().into()),
             Action::ToggleShowCrateInfo => self.search.toggle_show_crate_info(),
             Action::UpdateCurrentSelectionCrateInfo => self.update_current_selection_crate_info(),
             Action::UpdateSearchTableResults => self.search.update_search_table_results(),
diff --git a/src/cli.rs b/src/cli.rs
index e89b886..eb9f68a 100644
--- a/src/cli.rs
+++ b/src/cli.rs
@@ -60,6 +60,9 @@ pub struct Cli {
     #[arg(long, value_name = "DIR")]
     pub data_dir: Option<PathBuf>,
 
+    #[arg(long, value_name = "QUERY")]
+    pub query: Option<String>,
+
     /// The log level to use.
     ///
     /// Valid values are: error, warn, info, debug, trace, off. The default is
diff --git a/src/main.rs b/src/main.rs
index 1143b46..ddfc043 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -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(())
 }
diff --git a/src/widgets/search_page.rs b/src/widgets/search_page.rs
index ddd5aec..2acd0e2 100644
--- a/src/widgets/search_page.rs
+++ b/src/widgets/search_page.rs
@@ -242,10 +242,10 @@ impl SearchPage {
         self.last_task_details_handle.clear()
     }
 
-    pub fn submit_query(&mut self) {
+    pub fn submit_query(&mut self, query: String) {
         self.clear_all_previous_task_details_handles();
         self.filter.clear();
-        self.search = self.input.value().into();
+        self.search = query;
         let _ = self.tx.send(Action::SwitchMode(Mode::PickerHideCrateInfo));
     }

but sadly no success with that (run it with cargo run -- --query ratatui). May I ask what I'm doing wrong here or could you please implement this feature?

pikaThink

joshka added a commit that referenced this issue Sep 2, 2024
@joshka
Copy link
Member

joshka commented Sep 2, 2024

Take a look at #102

joshka added a commit that referenced this issue Nov 11, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants