Skip to content

Commit

Permalink
Save plots in folder and fix opening them on Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
fsktom committed Mar 29, 2023
1 parent 0314514 commit a6ce11e
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 1 deletion.
3 changes: 3 additions & 0 deletions .gitignore
Original file line number Diff line number Diff line change
Expand Up @@ -10,3 +10,6 @@

# mock files with unique endsong data?
ex*.txt

# folder with plots generated at runtime
/plots/
52 changes: 51 additions & 1 deletion src/plot/absolute.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,8 +31,58 @@ pub fn artist(entries: &SongEntries, art: &Artist) {
let layout = Layout::new().title(format!("<b>{art}</b>").as_str().into());
plot.set_layout(layout);

// creates plots/ folder
std::fs::create_dir_all("plots").unwrap();

// opens the plot in the browser
plot.show();
match std::env::consts::OS {
// see https://github.com/igiagkiozis/plotly/issues/132#issuecomment-1488920563
"windows" => {
let path = format!(
"{}\\plots\\{}.html",
std::env::current_dir().unwrap().display(),
&art.name
);
plot.write_html(path.as_str());
std::process::Command::new("explorer")
.arg(&path)
.output()
.unwrap();
}
"macos" => {
let path = format!(
"{}/plots/{}.html",
std::env::current_dir().unwrap().display(),
&art.name
);
plot.write_html(path.as_str());
std::process::Command::new("open")
.arg(&path)
.output()
.unwrap();
}
_ => {
let path = format!(
"{}/plots/{}.html",
std::env::current_dir().unwrap().display(),
&art.name
);
plot.write_html(path.as_str());

// https://doc.rust-lang.org/book/ch12-05-working-with-environment-variables.html
match std::env::var("BROWSER") {
Ok(browser) => {
std::process::Command::new(&browser)
.arg(&path)
.output()
.unwrap();
}
Err(_) => {
eprintln!("Your BROWSER environmental variable is not set!");
}
}
}
};
}

/// Used by [`artist()`] to get the dates of all of its occurrence
Expand Down

0 comments on commit a6ce11e

Please sign in to comment.