chore: provide a CLI flag to open log.html

On macOS, this was done by default regardless of what the user wants.
This was also not done on Windows or Linux. Instead, we now provide a
`--open-log` flag to open the log file in the default browser, and it
works on all platforms.
This commit is contained in:
Amaan Qureshi 2024-02-11 01:51:49 -05:00
parent 0afa891ffc
commit 4149ed4149
5 changed files with 27 additions and 13 deletions

View file

@ -35,22 +35,23 @@ pub struct LogSession {
path: PathBuf,
dot_process: Option<Child>,
dot_process_stdin: Option<ChildStdin>,
open_log: bool,
}
pub fn print_tree_graph(tree: &Tree, path: &str) -> Result<()> {
let session = LogSession::new(path)?;
pub fn print_tree_graph(tree: &Tree, path: &str, quiet: bool) -> Result<()> {
let session = LogSession::new(path, quiet)?;
tree.print_dot_graph(session.dot_process_stdin.as_ref().unwrap());
Ok(())
}
pub fn log_graphs(parser: &mut Parser, path: &str) -> Result<LogSession> {
let session = LogSession::new(path)?;
pub fn log_graphs(parser: &mut Parser, path: &str, open_log: bool) -> Result<LogSession> {
let session = LogSession::new(path, open_log)?;
parser.print_dot_graphs(session.dot_process_stdin.as_ref().unwrap());
Ok(session)
}
impl LogSession {
fn new(path: &str) -> Result<Self> {
fn new(path: &str, open_log: bool) -> Result<Self> {
use std::io::Write;
let mut dot_file = std::fs::File::create(path)?;
@ -71,6 +72,7 @@ impl LogSession {
path: PathBuf::from(path),
dot_process: Some(dot_process),
dot_process_stdin: Some(dot_stdin),
open_log,
})
}
}
@ -82,10 +84,8 @@ impl Drop for LogSession {
drop(self.dot_process_stdin.take().unwrap());
let output = self.dot_process.take().unwrap().wait_with_output().unwrap();
if output.status.success() {
if cfg!(target_os = "macos")
&& fs::metadata(&self.path).unwrap().len() > HTML_HEADER.len() as u64
{
Command::new("open").arg(&self.path).output().unwrap();
if self.open_log && fs::metadata(&self.path).unwrap().len() > HTML_HEADER.len() as u64 {
webbrowser::open(&self.path.to_string_lossy()).unwrap();
}
} else {
eprintln!(