Skip to content

Commit

Permalink
chore: provide a CLI flag to open log.html
Browse files Browse the repository at this point in the history
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.
  • Loading branch information
amaanq committed Feb 11, 2024
1 parent 0afa891 commit 4149ed4
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 13 deletions.
12 changes: 12 additions & 0 deletions cli/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -145,6 +145,11 @@ struct Parse {
pub edits: Option<Vec<String>>,
#[arg(long, help = "The encoding of the input files")]
pub encoding: Option<String>,
#[arg(
long,
help = "Open `log.html` in the default browser, if `--debug-graph` is supplied"
)]
pub open_log: bool,
}

#[derive(Args)]
Expand Down Expand Up @@ -191,6 +196,11 @@ struct Test {
pub wasm: bool,
#[arg(long, help = "Apply all captures to highlights")]
pub apply_all_captures: bool,
#[arg(
long,
help = "Open `log.html` in the default browser, if `--debug-graph` is supplied"
)]
pub open_log: bool,
}

#[derive(Args)]
Expand Down Expand Up @@ -480,6 +490,7 @@ fn run() -> Result<()> {
debug_graph: parse_options.debug_graph,
cancellation_flag: Some(&cancellation_flag),
encoding,
open_log: parse_options.open_log,
};

let parse_result = parse::parse_file_at_path(&mut parser, &opts)?;
Expand Down Expand Up @@ -548,6 +559,7 @@ fn run() -> Result<()> {
include: test_options.include,
exclude: test_options.exclude,
update: test_options.update,
open_log: test_options.open_log,
};

test::run_tests_at_path(&mut parser, &mut opts)?;
Expand Down
5 changes: 3 additions & 2 deletions cli/src/parse.rs
Original file line number Diff line number Diff line change
Expand Up @@ -61,6 +61,7 @@ pub struct ParseFileOptions<'a> {
pub debug_graph: bool,
pub cancellation_flag: Option<&'a AtomicUsize>,
pub encoding: Option<u32>,
pub open_log: bool,
}

#[derive(Copy, Clone)]
Expand All @@ -85,7 +86,7 @@ pub fn parse_file_at_path(parser: &mut Parser, opts: &ParseFileOptions) -> Resul

// Render an HTML graph if `--debug-graph` was passed
if opts.debug_graph {
_log_session = Some(util::log_graphs(parser, "log.html")?);
_log_session = Some(util::log_graphs(parser, "log.html", opts.open_log)?);
}
// Log to stderr if `--debug` was passed
else if opts.debug {
Expand Down Expand Up @@ -297,7 +298,7 @@ pub fn parse_file_at_path(parser: &mut Parser, opts: &ParseFileOptions) -> Resul
}

if opts.output == ParseOutput::Dot {
util::print_tree_graph(&tree, "log.html").unwrap();
util::print_tree_graph(&tree, "log.html", opts.open_log).unwrap();
}

let mut first_error = None;
Expand Down
3 changes: 2 additions & 1 deletion cli/src/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -64,14 +64,15 @@ pub struct TestOptions<'a> {
pub include: Option<Regex>,
pub exclude: Option<Regex>,
pub update: bool,
pub open_log: bool,
}

pub fn run_tests_at_path(parser: &mut Parser, opts: &mut TestOptions) -> Result<()> {
let test_entry = parse_tests(&opts.path)?;
let mut _log_session = None;

if opts.debug_graph {
_log_session = Some(util::log_graphs(parser, "log.html")?);
_log_session = Some(util::log_graphs(parser, "log.html", opts.open_log)?);
} else if opts.debug {
parser.set_logger(Some(Box::new(|log_type, message| {
if log_type == LogType::Lex {
Expand Down
2 changes: 1 addition & 1 deletion cli/src/tests/corpus_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -544,7 +544,7 @@ fn get_parser(session: &mut Option<util::LogSession>, log_filename: &str) -> Par
}
})));
} else if *LOG_GRAPH_ENABLED {
*session = Some(util::log_graphs(&mut parser, log_filename).unwrap());
*session = Some(util::log_graphs(&mut parser, log_filename, false).unwrap());
}

parser
Expand Down
18 changes: 9 additions & 9 deletions cli/src/util.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)?;
Expand All @@ -71,6 +72,7 @@ impl LogSession {
path: PathBuf::from(path),
dot_process: Some(dot_process),
dot_process_stdin: Some(dot_stdin),
open_log,
})
}
}
Expand All @@ -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!(
Expand Down

0 comments on commit 4149ed4

Please sign in to comment.