Add --dot flag to parse subcommand, for printing tree as DOT graph
This commit is contained in:
parent
8389ffd2a1
commit
97fd990822
10 changed files with 106 additions and 43 deletions
|
|
@ -3,7 +3,7 @@ use std::io;
|
|||
use std::sync::atomic::{AtomicUsize, Ordering};
|
||||
use std::sync::Arc;
|
||||
use std::thread;
|
||||
use tree_sitter::Parser;
|
||||
use tree_sitter::{Parser, Tree};
|
||||
|
||||
#[cfg(unix)]
|
||||
use anyhow::{anyhow, Context};
|
||||
|
|
@ -29,39 +29,61 @@ pub fn cancel_on_stdin() -> Arc<AtomicUsize> {
|
|||
}
|
||||
result
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
pub struct LogSession();
|
||||
pub struct LogSession;
|
||||
|
||||
#[cfg(unix)]
|
||||
pub struct LogSession(PathBuf, Option<Child>, Option<ChildStdin>);
|
||||
pub struct LogSession {
|
||||
path: PathBuf,
|
||||
dot_process: Option<Child>,
|
||||
dot_process_stdin: Option<ChildStdin>,
|
||||
}
|
||||
|
||||
pub fn print_tree_graph(tree: &Tree, path: &str) -> Result<()> {
|
||||
let session = LogSession::new(path)?;
|
||||
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)?;
|
||||
parser.print_dot_graphs(session.dot_process_stdin.as_ref().unwrap());
|
||||
Ok(session)
|
||||
}
|
||||
|
||||
#[cfg(windows)]
|
||||
pub fn log_graphs(_parser: &mut Parser, _path: &str) -> Result<LogSession> {
|
||||
Ok(LogSession())
|
||||
impl LogSession {
|
||||
fn new(path: &str) -> Result<Self> {
|
||||
Ok(Self)
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
pub fn log_graphs(parser: &mut Parser, path: &str) -> Result<LogSession> {
|
||||
use std::io::Write;
|
||||
impl LogSession {
|
||||
fn new(path: &str) -> Result<Self> {
|
||||
use std::io::Write;
|
||||
|
||||
let mut dot_file = std::fs::File::create(path)?;
|
||||
dot_file.write(HTML_HEADER)?;
|
||||
let mut dot_process = Command::new("dot")
|
||||
.arg("-Tsvg")
|
||||
.stdin(Stdio::piped())
|
||||
.stdout(dot_file)
|
||||
.spawn()
|
||||
.with_context(|| "Failed to run the `dot` command. Check that graphviz is installed.")?;
|
||||
let dot_stdin = dot_process
|
||||
.stdin
|
||||
.take()
|
||||
.ok_or_else(|| anyhow!("Failed to open stdin for `dot` process."))?;
|
||||
parser.print_dot_graphs(&dot_stdin);
|
||||
Ok(LogSession(
|
||||
PathBuf::from(path),
|
||||
Some(dot_process),
|
||||
Some(dot_stdin),
|
||||
))
|
||||
let mut dot_file = std::fs::File::create(path)?;
|
||||
dot_file.write(HTML_HEADER)?;
|
||||
let mut dot_process = Command::new("dot")
|
||||
.arg("-Tsvg")
|
||||
.stdin(Stdio::piped())
|
||||
.stdout(dot_file)
|
||||
.spawn()
|
||||
.with_context(|| {
|
||||
"Failed to run the `dot` command. Check that graphviz is installed."
|
||||
})?;
|
||||
let dot_stdin = dot_process
|
||||
.stdin
|
||||
.take()
|
||||
.ok_or_else(|| anyhow!("Failed to open stdin for `dot` process."))?;
|
||||
Ok(Self {
|
||||
path: PathBuf::from(path),
|
||||
dot_process: Some(dot_process),
|
||||
dot_process_stdin: Some(dot_stdin),
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
#[cfg(unix)]
|
||||
|
|
@ -69,13 +91,13 @@ impl Drop for LogSession {
|
|||
fn drop(&mut self) {
|
||||
use std::fs;
|
||||
|
||||
drop(self.2.take().unwrap());
|
||||
let output = self.1.take().unwrap().wait_with_output().unwrap();
|
||||
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.0).unwrap().len() > HTML_HEADER.len() as u64
|
||||
&& fs::metadata(&self.path).unwrap().len() > HTML_HEADER.len() as u64
|
||||
{
|
||||
Command::new("open").arg(&self.0).output().unwrap();
|
||||
Command::new("open").arg(&self.path).output().unwrap();
|
||||
}
|
||||
} else {
|
||||
eprintln!(
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue