]> git.r.bdr.sh - rbdr/lyricli/blame_incremental - src/main.rs
Add manpage
[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 result = run().await;
50
51 if cfg!(debug_assertions) {
52 result
53 } else {
54 match result {
55 Ok(_) => Ok(()),
56 Err(e) => {
57 eprintln!("Error: {}", e);
58 std::process::exit(1);
59 }
60 }
61 }
62}
63
64async fn run() -> Result<()> {
65 let mut configuration = Configuration::new();
66 let arguments = Arguments::parse();
67
68 if arguments.list_sources {
69 let sources = list();
70 for source in sources {
71 print!("{}", source);
72 if configuration.is_enabled(&source) {
73 print!(" (enabled)");
74 }
75 println!("");
76 }
77 return Ok(());
78 }
79
80 if let Some(source_name) = arguments.enable_source {
81 if !configuration.is_enabled(&source_name) {
82 enable(&source_name)?;
83 }
84 return configuration.enable_source(&source_name);
85 }
86
87 if let Some(source_name) = arguments.disable_source {
88 if configuration.is_enabled(&source_name) {
89 disable(&source_name)?;
90 }
91 return configuration.disable_source(&source_name);
92 }
93
94 if let Some(source_name) = arguments.reset_source {
95 return reset(&source_name);
96 }
97
98 let current_track: Track;
99 if let Some(artist) = arguments.artist {
100 current_track = Track {
101 name: arguments.track_name.unwrap_or("".to_string()),
102 artist: artist
103 };
104 } else {
105 current_track = get_track()
106 .ok_or_else(|| Error::new(Other, "No Artist/Song could be found :("))?
107 }
108
109 print_lyrics(current_track, arguments.show_title).await
110}