From 3e92b345ba6b22c7c6bfc970d41ed506985ec42b Mon Sep 17 00:00:00 2001 From: Goldy <66202304+THEGOLDENPRO@users.noreply.github.com> Date: Sat, 3 Feb 2024 23:13:48 +0000 Subject: [PATCH] feat, fix: added select option, moved to cargo install, added preventions for usize overflow --- Cargo.toml | 2 +- Makefile | 4 ++-- README.md | 10 +++++----- src/main.rs | 48 +++++++++++++++++++++++++++++++++--------------- src/utils.rs | 2 +- 5 files changed, 42 insertions(+), 24 deletions(-) diff --git a/Cargo.toml b/Cargo.toml index 4b4e9e2..53c5219 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "aghpb-cli" -version = "1.2.0" +version = "1.2.3" edition = "2021" # See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html diff --git a/Makefile b/Makefile index 79f0afc..dd1086a 100644 --- a/Makefile +++ b/Makefile @@ -2,10 +2,10 @@ build: cargo build --release install: - cp ./target/release/aghpb-cli ~/.local/bin + cargo install --path . uninstall: - rm ~/.local/bin/aghpb-cli + cargo uninstall clean: cargo clean \ No newline at end of file diff --git a/README.md b/README.md index 4a93631..49f7dd7 100644 --- a/README.md +++ b/README.md @@ -26,15 +26,15 @@ Check out the help command for more: ``aghpb-cli --help`` I don't plan on releasing to any package managers any time soon so for now you have two options, install from source (like a real man) or use my already published binary. > [!Warning] -> On Linux if the ``aghpb-cli`` command doesn't work you may need to add the ``~/.local/bin`` directory to your path. You can do so by adding ``export PATH=$PATH:~/.local/bin`` to your ``.bashrc`` if you're using that. +> On Linux if the ``aghpb-cli`` command doesn't work you may need to add the ``~/.cargo/bin`` directory to your path if you . You can do so by adding ``export PATH=$PATH:~/.cargo/bin`` to your ``.bashrc`` or an equivalent. ### Install the binary 🧑‍💻 -Prerequisites: **[``wget``]()** +Prerequisites: **[``wget``](https://www.gnu.org/software/wget/)** #### Linux 🐧 ```sh -wget https://github.com/THEGOLDENPRO/aghpb-cli/releases/latest/download/aghpb-cli && mv ./aghpb-cli ~/.local/bin -chmod +x ~/.local/bin/aghpb-cli +wget https://github.com/THEGOLDENPRO/aghpb-cli/releases/latest/download/aghpb-cli && sudo mv ./aghpb-cli /usr/local/bin +sudo chmod +x /usr/local/bin/aghpb-cli ``` #### Windows 🪟 @@ -58,4 +58,4 @@ make install # install to bin #### Windows 🪟 *too lazy to add the instructions, someone do it for me* 😴 -like fr! +like fr! \ No newline at end of file diff --git a/src/main.rs b/src/main.rs index ec8003b..6c553a3 100644 --- a/src/main.rs +++ b/src/main.rs @@ -13,6 +13,7 @@ const HELP_MSG: &str = " USAGE: aghpb-cli [options] {query} Options: + -s or -select: Pre-selects search options for you. -l or -limit: Changes the amount of results returned by the API. Default is 15. -c or -category: The book category to filter the search by. @@ -29,7 +30,12 @@ async fn main() -> Result<(), Box> { let cmd_args: Vec = env::args().collect(); - let (query, category, limit) = parse_query((&cmd_args[1..]).to_vec()); + let ( + query, + category, + mut select, + limit + ) = parse_query((&cmd_args[1..]).to_vec()); if !query.is_none() { let query = query.unwrap(); @@ -41,21 +47,30 @@ async fn main() -> Result<(), Box> { let books = aghpb::search(query, category, Some(limit)).await?; - for (index, book) in books.iter().enumerate() { - println!( - "{}) {} [{}]", - index + 1, - owo_colors::OwoColorize::fg::(&book.name), - owo_colors::OwoColorize::fg::(&book.commit_author) + if select == None { + for (index, book) in books.iter().enumerate() { + println!( + "{}) {} [{}]", + index + 1, + owo_colors::OwoColorize::fg::(&book.name), + owo_colors::OwoColorize::fg::(&book.commit_author) + ); + } + + select = Some( + input( + format!("\nSelect your book ({} - {}): ", 1, books.len())).expect( + "We failed to grab your input!" + ) ); } - let choice = input( - format!("\nSelect your book ({} - {}): ", 1, books.len()) - ).expect("We failed to grab your input!"); - println!("{}", owo_colors::OwoColorize::fg::(&"Getting book...")); - let chosen_book = &books[choice.parse::().expect("Failed to parse your choice into an integer.") - 1]; + + let chosen_book = &books.get( + select.unwrap().parse::().expect("Failed to parse your choice into an integer.").checked_sub(1).unwrap_or(0) + ).unwrap(); + let chosen_book = chosen_book.get_book().await.expect("Failed to get book's image!"); println!("{}", owo_colors::OwoColorize::fg::(&"Writing image...")); @@ -69,18 +84,19 @@ async fn main() -> Result<(), Box> { } -fn parse_query(cmd_args: Vec) -> (Option, Option, u8) { +fn parse_query(cmd_args: Vec) -> (Option, Option, Option, u8) { let mut query: Option = None; let mut category: Option = None; let mut limit = DEFAULT_LIMIT; + let mut select: Option = None; let mut args = cmd_args.iter(); while let Some(arg) = args.next() { if arg.starts_with("--"){ if process_flags(arg) { - return (None, None, limit) + return (None, None, select, limit) } } @@ -91,6 +107,8 @@ fn parse_query(cmd_args: Vec) -> (Option, Option, u8) { limit = next_arg.parse::().expect("Failed to parse limit!"); } else if arg == "-c" || arg == "-cat" || arg == "-category" { category = Some(next_arg.to_string()); + } else if arg == "-s" || arg == "-select" { + select = Some(next_arg.to_string()); } continue; @@ -117,5 +135,5 @@ fn parse_query(cmd_args: Vec) -> (Option, Option, u8) { println!("Uhhh, enter the query properly idiot!"); } - (query, category, limit) + (query, category, select, limit) } \ No newline at end of file diff --git a/src/utils.rs b/src/utils.rs index 05c5997..5e61c29 100644 --- a/src/utils.rs +++ b/src/utils.rs @@ -39,7 +39,7 @@ pub fn process_flags(flag: &String) -> bool { "--last" => { display_image(get_path(Some(TEMP_BOOK_NAME))); true - } + }, _ => false } }