Skip to content

Commit a6ce11e

Browse files
committed
Save plots in folder and fix opening them on Windows
plotly/plotly.rs#132 (comment)
1 parent 0314514 commit a6ce11e

File tree

2 files changed

+54
-1
lines changed

2 files changed

+54
-1
lines changed

Diff for: .gitignore

+3
Original file line numberDiff line numberDiff line change
@@ -10,3 +10,6 @@
1010

1111
# mock files with unique endsong data?
1212
ex*.txt
13+
14+
# folder with plots generated at runtime
15+
/plots/

Diff for: src/plot/absolute.rs

+51-1
Original file line numberDiff line numberDiff line change
@@ -31,8 +31,58 @@ pub fn artist(entries: &SongEntries, art: &Artist) {
3131
let layout = Layout::new().title(format!("<b>{art}</b>").as_str().into());
3232
plot.set_layout(layout);
3333

34+
// creates plots/ folder
35+
std::fs::create_dir_all("plots").unwrap();
36+
3437
// opens the plot in the browser
35-
plot.show();
38+
match std::env::consts::OS {
39+
// see https://github.com/igiagkiozis/plotly/issues/132#issuecomment-1488920563
40+
"windows" => {
41+
let path = format!(
42+
"{}\\plots\\{}.html",
43+
std::env::current_dir().unwrap().display(),
44+
&art.name
45+
);
46+
plot.write_html(path.as_str());
47+
std::process::Command::new("explorer")
48+
.arg(&path)
49+
.output()
50+
.unwrap();
51+
}
52+
"macos" => {
53+
let path = format!(
54+
"{}/plots/{}.html",
55+
std::env::current_dir().unwrap().display(),
56+
&art.name
57+
);
58+
plot.write_html(path.as_str());
59+
std::process::Command::new("open")
60+
.arg(&path)
61+
.output()
62+
.unwrap();
63+
}
64+
_ => {
65+
let path = format!(
66+
"{}/plots/{}.html",
67+
std::env::current_dir().unwrap().display(),
68+
&art.name
69+
);
70+
plot.write_html(path.as_str());
71+
72+
// https://doc.rust-lang.org/book/ch12-05-working-with-environment-variables.html
73+
match std::env::var("BROWSER") {
74+
Ok(browser) => {
75+
std::process::Command::new(&browser)
76+
.arg(&path)
77+
.output()
78+
.unwrap();
79+
}
80+
Err(_) => {
81+
eprintln!("Your BROWSER environmental variable is not set!");
82+
}
83+
}
84+
}
85+
};
3686
}
3787

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

0 commit comments

Comments
 (0)