1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
mod args;
pub use args::*;
use {
crate::*,
anyhow::*,
argh,
std::{
fs::File,
io::{self, Write},
path::PathBuf,
},
};
pub fn run() -> Result<()> {
let args: Args = argh::from_env();
debug!("args: {:#?}", &args);
if args.version {
println!("csv2svg {}", env!("CARGO_PKG_VERSION"));
return Ok(());
}
let raw_tbl = RawTbl::read(io::stdin())?;
let tbl = Tbl::from_raw(raw_tbl)?;
debug!("tbl dim: {:?}", tbl.dim());
let graph = Graph::new(tbl);
let svg = graph.build_svg();
if is_output_piped() {
let mut w = io::stdout();
match args.format {
Some(Format::Html) => {
html::write_embedded(&mut w, &svg)?;
}
_ => {
svg::write(&mut w, &svg)?;
}
}
w.write_all(b"\n")?;
} else {
let (mut w, path) = temp_file()?;
match args.format {
Some(Format::Svg) => {
svg::write(&mut w, &svg)?;
}
_ => {
html::write_embedded(&mut w, &svg)?;
}
}
open::that(path)?;
}
Ok(())
}
fn is_output_piped() -> bool {
unsafe {
libc::isatty(libc::STDOUT_FILENO) == 0
}
}
pub fn temp_file() -> io::Result<(File, PathBuf)> {
tempfile::Builder::new()
.prefix("csv2svg-")
.suffix(".html")
.rand_bytes(12)
.tempfile()?
.keep()
.map_err(|_| io::Error::new(
io::ErrorKind::Other,
"temp file can't be kept",
))
}