Skip to content

Commit

Permalink
feat, fix: added select option, moved to cargo install, added prevent…
Browse files Browse the repository at this point in the history
…ions for usize overflow
  • Loading branch information
THEGOLDENPRO committed Feb 3, 2024
1 parent ae45dec commit 3e92b34
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 24 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -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
Expand Down
4 changes: 2 additions & 2 deletions Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -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
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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 🪟
Expand All @@ -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!
48 changes: 33 additions & 15 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand All @@ -29,7 +30,12 @@ async fn main() -> Result<(), Box<dyn Error>> {

let cmd_args: Vec<String> = 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();
Expand All @@ -41,21 +47,30 @@ async fn main() -> Result<(), Box<dyn Error>> {

let books = aghpb::search(query, category, Some(limit)).await?;

for (index, book) in books.iter().enumerate() {
println!(
"{}) {} [{}]",
index + 1,
owo_colors::OwoColorize::fg::<OrangeRed>(&book.name),
owo_colors::OwoColorize::fg::<Black>(&book.commit_author)
if select == None {
for (index, book) in books.iter().enumerate() {
println!(
"{}) {} [{}]",
index + 1,
owo_colors::OwoColorize::fg::<OrangeRed>(&book.name),
owo_colors::OwoColorize::fg::<Black>(&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::<BrightBlue>(&"Getting book..."));
let chosen_book = &books[choice.parse::<usize>().expect("Failed to parse your choice into an integer.") - 1];

let chosen_book = &books.get(
select.unwrap().parse::<usize>().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::<BrightWhite>(&"Writing image..."));
Expand All @@ -69,18 +84,19 @@ async fn main() -> Result<(), Box<dyn Error>> {
}


fn parse_query(cmd_args: Vec<String>) -> (Option<String>, Option<String>, u8) {
fn parse_query(cmd_args: Vec<String>) -> (Option<String>, Option<String>, Option<String>, u8) {
let mut query: Option<String> = None;
let mut category: Option<String> = None;

let mut limit = DEFAULT_LIMIT;
let mut select: Option<String> = 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)
}
}

Expand All @@ -91,6 +107,8 @@ fn parse_query(cmd_args: Vec<String>) -> (Option<String>, Option<String>, u8) {
limit = next_arg.parse::<u8>().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;
Expand All @@ -117,5 +135,5 @@ fn parse_query(cmd_args: Vec<String>) -> (Option<String>, Option<String>, u8) {
println!("Uhhh, enter the query properly idiot!");
}

(query, category, limit)
(query, category, select, limit)
}
2 changes: 1 addition & 1 deletion src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@ pub fn process_flags(flag: &String) -> bool {
"--last" => {
display_image(get_path(Some(TEMP_BOOK_NAME)));
true
}
},
_ => false
}
}
Expand Down

0 comments on commit 3e92b34

Please sign in to comment.