Skip to content

Commit

Permalink
refactor: Run code formatter and enable clippy some clippy warns
Browse files Browse the repository at this point in the history
  • Loading branch information
alt-art committed Oct 7, 2023
1 parent 45ea81a commit 8250584
Show file tree
Hide file tree
Showing 4 changed files with 41 additions and 38 deletions.
1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ version = "0.1.0"
edition = "2021"
license = "MIT"
keywords = ["lyrics", "cli", "rust"]
categories = ["command-line-utilities"]
readme = "README.md"
authors = ["Ishan Joshi <[email protected]>"]
description = "A simple CLI to get lyrics of a song from Genius using a public availed API"
Expand Down
61 changes: 31 additions & 30 deletions src/cli.rs
Original file line number Diff line number Diff line change
@@ -1,53 +1,54 @@
use inquire::Text;

pub fn get_input(msg: &str, help: Option<&str>, default: Option<&str>)->String{
let input = Text::new(msg)
.with_help_message(
help.unwrap_or(""),
)
.with_default(
default.unwrap_or(""),
)
.prompt();
let input = input.unwrap();
input
pub fn get_input(msg: &str, help: Option<&str>, default: Option<&str>) -> String {
Text::new(msg)
.with_help_message(help.unwrap_or(""))
.with_default(default.unwrap_or(""))
.prompt()
.expect("Failed to get input")
}

pub fn parse_args() -> (String, String) {
let args: Vec<String> = std::env::args().collect();
let mut input = String::new();
if args.len() > 1 {
for i in 1..args.len() {
input.push_str(&args[i]);
input.push_str(" ");
for arg in args.iter().skip(1) {
input.push_str(arg);
input.push(' ');
}
(input, args[1].clone())
}
else{
let output = get_input("Get any song's lyrics: ".into(), "Enter the song name: ".into(), "Never Gonna Give You Up".into());
} else {
let output = get_input(
"Get any song's lyrics: ",
"Enter the song name: ".into(),
"Never Gonna Give You Up".into(),
);
(output, "lyrics".into())
}
}

pub fn print_help(){
bunt::println!("Please provide a song name, no need to put {$underline}quotes{/$} around it ;)");
pub fn print_help() {
bunt::println!(
"Please provide a song name, no need to put {$underline}quotes{/$} around it ;)"
);
bunt::println!("{$blue}{$underline}Usage:{/$}{/$} ");
bunt::println!("lyrics {$yellow}<Song Name>{/$}");
bunt::println!("lyrics --help / -h");
bunt::println!("lyrics --version / -v");
}

pub fn print_version(){
pub fn print_version() {
bunt::println!("lyrics {$yellow}v0.1.0{/$}");
}

pub fn splash_screen(){
bunt::println!("{$green} _ _ {/$}");
bunt::println!("{$red}| | (_) {/$}");
bunt::println!("{$blue}| | _ _ _ __ _ ___ ___ {/$}");
bunt::println!("{$yellow}| | | | | | '__| |/ __/ __|{/$}");
bunt::println!("{$yellow}| |___| |_| | | | | (__\\__ \\{/$}");
bunt::println!("{$blue}\\_____/\\__, |_| |_|\\___|___/{/$}");
bunt::println!("{$red} __/ | {/$}");
bunt::println!("{$green} |___/ {/$}Finder {$underline}v.0.1{/$}");
}
#[allow(clippy::cognitive_complexity)]
pub fn splash_screen() {
bunt::println!("{$green} _ _ {/$}");
bunt::println!("{$red}| | (_) {/$}");
bunt::println!("{$blue}| | _ _ _ __ _ ___ ___ {/$}");
bunt::println!("{$yellow}| | | | | | '__| |/ __/ __|{/$}");
bunt::println!("{$yellow}| |___| |_| | | | | (__\\__ \\{/$}");
bunt::println!("{$blue}\\_____/\\__, |_| |_|\\___|___/{/$}");
bunt::println!("{$red} __/ | {/$}");
bunt::println!("{$green} |___/ {/$}Finder {$underline}v.0.1{/$}");
}
13 changes: 7 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,6 @@
#![warn(clippy::all, clippy::pedantic, clippy::nursery, clippy::cargo)]
#![allow(clippy::multiple_crate_versions)]

use human_panic::setup_panic;

mod cli;
Expand All @@ -10,16 +13,14 @@ async fn main() {
let (input, cmd) = cli::parse_args();
if cmd == "-h" || cmd == "--help" {
cli::print_help();
}
else if cmd == "-v" || cmd == "--version" {
} else if cmd == "-v" || cmd == "--version" {
cli::print_version();
}
else {
} else {
let (url, lyrics) = web::get_lyrics(input.as_str()).await;
bunt::println!("\n--------------------------");
bunt::println!("{$yellow}{}{/$}", input);
bunt::println!("--------------------------");
println!("{}", lyrics);
println!("{lyrics}");
bunt::println!("\n{$green}Lyrics from: {/$}{}", url);
}
}
}
4 changes: 2 additions & 2 deletions src/web.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,9 +7,9 @@ pub struct Response {
}

pub async fn get_lyrics(query: &str) -> (String, String) {
let url = format!("https://lyrics.astrid.sh/api/search?q={}", query);
let url = format!("https://lyrics.astrid.sh/api/search?q={query}");
let resp = reqwest::get(&url).await.unwrap();
//use serde_json to parse the response
let json: Response = serde_json::from_str(&resp.text().await.unwrap()).unwrap();
(json.url, json.lyrics)
}
}

0 comments on commit 8250584

Please sign in to comment.