Skip to content

Commit

Permalink
#22 Implement absolute plots for albums and songs
Browse files Browse the repository at this point in the history
  • Loading branch information
fsktom committed Mar 30, 2023
1 parent d5353dc commit 317a245
Show file tree
Hide file tree
Showing 5 changed files with 70 additions and 10 deletions.
2 changes: 1 addition & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -119,7 +119,7 @@ fn test(entries: &SongEntries) {
/// tests various [`plot`] functions
#[allow(dead_code)]
fn test_plot(entries: &SongEntries) {
plot::absolute::artist(entries, &types::Artist::from_str("Sabaton"));
plot::absolute::aspect(entries, &types::Artist::from_str("Sabaton"));

let coat = types::Album::from_str("Coat of Arms", "Sabaton");
plot::relative::to_all(entries, &coat);
Expand Down
13 changes: 7 additions & 6 deletions src/plot/absolute.rs
Original file line number Diff line number Diff line change
@@ -1,22 +1,23 @@
use super::{create_plot, find_dates};
use crate::display::date;
use crate::types::{Artist, SongEntries};
use crate::types::{Music, SongEntries};

/// Creates a plot of the absolute amount of plays of an [`Artist`]
/// Creates a plot of the absolute amount of plays
///
/// Opens the plot in the browser
pub fn artist(entries: &SongEntries, art: &Artist) {
pub fn aspect<Asp: Music>(entries: &SongEntries, aspect: &Asp) {
let mut times = Vec::<i64>::new();
let mut plays = Vec::<usize>::new();

let dates = find_dates(entries, art, true);
let dates = find_dates(entries, aspect, true);

let start = dates.first().unwrap();

for date in &dates {
times.push(date.timestamp());
plays.push(date::gather_plays(entries, art, start, date));
plays.push(date::gather_plays(entries, aspect, start, date));
}

create_plot(times, plays, art.name.as_str());
let title = format!("{aspect}");
create_plot(times, plays, &title);
}
4 changes: 2 additions & 2 deletions src/types.rs
Original file line number Diff line number Diff line change
Expand Up @@ -288,8 +288,8 @@ impl SongEntries {
}

/// Creates a plot of the artist
pub fn plot_artist(&self, art: &Artist) {
plot::absolute::artist(self, art);
pub fn plot<Asp: Music>(&self, aspect: &Asp) {
plot::absolute::aspect(self, aspect);
}

/// Creates a plot of the `aspect` relative to the total amount of plays
Expand Down
49 changes: 48 additions & 1 deletion src/ui.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,8 @@ fn match_input(
"print top albums" | "ptalbs" => match_print_top(entries, rl, &Aspect::Albums)?,
"print top songs" | "ptsons" => match_print_top(entries, rl, &Aspect::Songs)?,
"plot artist" | "gart" => match_plot_artist(entries, rl)?,
"plot album" | "galb" => match_plot_album(entries, rl)?,
"plot song" | "gson" => match_plot_song(entries, rl)?,
"plot artist relative" | "gartr" => match_plot_artist_relative(entries, rl)?,
"plot album relative" | "galbr" => match_plot_album_relative(entries, rl)?,
"plot song relative" | "gsonr" => match_plot_song_relative(entries, rl)?,
Expand Down Expand Up @@ -467,7 +469,52 @@ fn match_plot_artist(
let usr_input_art = rl.readline(PROMPT_MAIN)?;
let art = entries.find().artist(&usr_input_art)?;

entries.plot_artist(&art);
entries.plot(&art);
Ok(())
}

/// Used by [`match_input()`] for `plot album` command
fn match_plot_album(
entries: &SongEntries,
rl: &mut Editor<ShellHelper, FileHistory>,
) -> Result<(), Box<dyn Error>> {
// 1st prompt: artist name
println!("Artist name?");
let usr_input_art = rl.readline(PROMPT_MAIN)?;
let art = entries.find().artist(&usr_input_art)?;

// 2nd prompt: album name
println!("Album name?");
let usr_input_alb = rl.readline(PROMPT_MAIN)?;
let alb = entries.find().album(&usr_input_alb, &art.name)?;

entries.plot(&alb);
Ok(())
}

/// Used by [`match_input()`] for `plot song` command
fn match_plot_song(
entries: &SongEntries,
rl: &mut Editor<ShellHelper, FileHistory>,
) -> Result<(), Box<dyn Error>> {
// 1st prompt: artist name
println!("Artist name?");
let usr_input_art = rl.readline(PROMPT_MAIN)?;
let art = entries.find().artist(&usr_input_art)?;

// 2nd prompt: album name
println!("Album name?");
let usr_input_alb = rl.readline(PROMPT_MAIN)?;
let alb = entries.find().album(&usr_input_alb, &art.name)?;

// 3rd prompt: song name
println!("Song name?");
let usr_input_son = rl.readline(PROMPT_MAIN)?;
let son = entries
.find()
.song_from_album(&usr_input_son, &alb.name, &alb.artist.name)?;

entries.plot(&son);
Ok(())
}

Expand Down
12 changes: 12 additions & 0 deletions src/ui/help.rs
Original file line number Diff line number Diff line change
Expand Up @@ -202,6 +202,18 @@ fn plot_commands<'a>() -> Vec<[&'a str; 3]> {
"creates a plot of the absolute amount of plays of the given artist
and opens it in the web browser",
],
[
"plot album",
"galb",
"creates a plot of the absolute amount of plays of the given album
and opens it in the web browser",
],
[
"plot song",
"gson",
"creates a plot of the absolute amount of plays of the given song
and opens it in the web browser",
],
[
"plot artist relative",
"gartr",
Expand Down

0 comments on commit 317a245

Please sign in to comment.