]> git.r.bdr.sh - rbdr/lyricli/blame_incremental - src/main.rs
Add macos sources
[rbdr/lyricli] / src / main.rs
... / ...
CommitLineData
1mod configuration;
2mod lyrics_engine;
3mod sources;
4
5use std::io::{Result, Error, ErrorKind::Other};
6use clap::Parser;
7
8use configuration::Configuration;
9use sources::{enable, disable, get_track, reset, list};
10use lyrics_engine::print_lyrics;
11
12#[derive(Parser, Debug)]
13#[command(version, about, long_about = None)]
14struct Arguments {
15
16 // Positional Arguments
17 /// Specify the artist.
18 artist: Option<String>,
19
20 /// Specify the artist.
21 track_name: Option<String>,
22
23 /// Show title of track if present
24 #[arg(short = 't', long)]
25 show_title: bool,
26
27 /// Lists all sources
28 #[arg(short, long)]
29 list_sources: bool,
30
31 /// Enables a source
32 #[arg(short, long, value_name = "SOURCE")]
33 enable_source: Option<String>,
34
35 #[arg(short, long, value_name = "SOURCE")]
36 disable_source: Option<String>,
37
38 #[arg(short, long, value_name = "SOURCE")]
39 reset_source: Option<String>,
40}
41
42pub struct Track {
43 pub name: String,
44 pub artist: String
45}
46
47#[tokio::main]
48async fn main() -> Result<()> {
49 let mut configuration = Configuration::new();
50 let arguments = Arguments::parse();
51
52 if arguments.list_sources {
53 let sources = list();
54 for source in sources {
55 print!("{}", source);
56 if configuration.is_enabled(&source) {
57 print!(" (enabled)");
58 }
59 println!("");
60 }
61 return Ok(());
62 }
63
64 if let Some(source_name) = arguments.enable_source {
65 if !configuration.is_enabled(&source_name) {
66 enable(&source_name)?;
67 }
68 return configuration.enable_source(&source_name);
69 }
70
71 if let Some(source_name) = arguments.disable_source {
72 if configuration.is_enabled(&source_name) {
73 disable(&source_name)?;
74 }
75 return configuration.disable_source(&source_name);
76 }
77
78 if let Some(source_name) = arguments.reset_source {
79 return reset(&source_name);
80 }
81
82 let current_track: Track;
83 if let Some(artist) = arguments.artist {
84 current_track = Track {
85 name: arguments.track_name.unwrap_or("".to_string()),
86 artist: artist
87 };
88 } else {
89 current_track = get_track()
90 .ok_or_else(|| Error::new(Other, "No Artist/Song could be found :("))?
91 }
92
93 print_lyrics(current_track, arguments.show_title).await
94}