2021-09-28 18:53:24 -04:00
|
|
|
use anyhow::Result;
|
2020-03-25 11:26:52 -07:00
|
|
|
use std::io;
|
|
|
|
|
use std::sync::atomic::{AtomicUsize, Ordering};
|
|
|
|
|
use std::sync::Arc;
|
|
|
|
|
use std::thread;
|
|
|
|
|
use tree_sitter::Parser;
|
|
|
|
|
|
2021-09-28 19:06:05 -04:00
|
|
|
#[cfg(unix)]
|
|
|
|
|
use anyhow::{anyhow, Context};
|
2019-01-14 17:19:46 -08:00
|
|
|
#[cfg(unix)]
|
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
#[cfg(unix)]
|
2019-01-08 21:03:51 -08:00
|
|
|
use std::process::{Child, ChildStdin, Command, Stdio};
|
|
|
|
|
|
2019-01-15 19:18:33 -08:00
|
|
|
#[cfg(unix)]
|
2019-01-14 17:19:46 -08:00
|
|
|
const HTML_HEADER: &[u8] = b"<!DOCTYPE html>\n<style>svg { width: 100%; }</style>\n\n";
|
|
|
|
|
|
2020-03-25 11:26:52 -07:00
|
|
|
pub fn cancel_on_stdin() -> Arc<AtomicUsize> {
|
|
|
|
|
let result = Arc::new(AtomicUsize::new(0));
|
2020-10-22 15:55:51 -07:00
|
|
|
if atty::is(atty::Stream::Stdin) {
|
|
|
|
|
thread::spawn({
|
|
|
|
|
let flag = result.clone();
|
|
|
|
|
move || {
|
|
|
|
|
let mut line = String::new();
|
|
|
|
|
io::stdin().read_line(&mut line).unwrap();
|
|
|
|
|
flag.store(1, Ordering::Relaxed);
|
|
|
|
|
}
|
|
|
|
|
});
|
|
|
|
|
}
|
2020-03-25 11:26:52 -07:00
|
|
|
result
|
|
|
|
|
}
|
2019-01-10 15:50:54 -08:00
|
|
|
#[cfg(windows)]
|
2019-02-01 14:39:37 -08:00
|
|
|
pub struct LogSession();
|
2019-01-10 15:50:54 -08:00
|
|
|
|
2019-01-14 17:19:46 -08:00
|
|
|
#[cfg(unix)]
|
2019-02-01 14:39:37 -08:00
|
|
|
pub struct LogSession(PathBuf, Option<Child>, Option<ChildStdin>);
|
2019-01-10 15:50:54 -08:00
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
2020-07-10 10:12:46 -07:00
|
|
|
pub fn log_graphs(_parser: &mut Parser, _path: &str) -> Result<LogSession> {
|
2019-01-14 17:19:46 -08:00
|
|
|
Ok(LogSession())
|
2019-01-10 15:50:54 -08:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
#[cfg(unix)]
|
2020-07-10 10:12:46 -07:00
|
|
|
pub fn log_graphs(parser: &mut Parser, path: &str) -> Result<LogSession> {
|
2019-01-14 17:19:46 -08:00
|
|
|
use std::io::Write;
|
2019-01-08 21:03:51 -08:00
|
|
|
|
2019-01-14 17:19:46 -08:00
|
|
|
let mut dot_file = std::fs::File::create(path)?;
|
|
|
|
|
dot_file.write(HTML_HEADER)?;
|
2019-01-08 21:03:51 -08:00
|
|
|
let mut dot_process = Command::new("dot")
|
|
|
|
|
.arg("-Tsvg")
|
|
|
|
|
.stdin(Stdio::piped())
|
|
|
|
|
.stdout(dot_file)
|
|
|
|
|
.spawn()
|
2021-06-09 12:32:22 -04:00
|
|
|
.with_context(|| "Failed to run the `dot` command. Check that graphviz is installed.")?;
|
2019-01-08 21:03:51 -08:00
|
|
|
let dot_stdin = dot_process
|
|
|
|
|
.stdin
|
|
|
|
|
.take()
|
2021-06-09 12:32:22 -04:00
|
|
|
.ok_or_else(|| anyhow!("Failed to open stdin for `dot` process."))?;
|
2019-01-08 21:03:51 -08:00
|
|
|
parser.print_dot_graphs(&dot_stdin);
|
2019-01-17 17:16:04 -08:00
|
|
|
Ok(LogSession(
|
|
|
|
|
PathBuf::from(path),
|
|
|
|
|
Some(dot_process),
|
|
|
|
|
Some(dot_stdin),
|
|
|
|
|
))
|
2019-01-08 21:03:51 -08:00
|
|
|
}
|
|
|
|
|
|
2019-01-10 15:50:54 -08:00
|
|
|
#[cfg(unix)]
|
2019-01-14 17:19:46 -08:00
|
|
|
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();
|
|
|
|
|
if output.status.success() {
|
2019-01-17 17:16:04 -08:00
|
|
|
if cfg!(target_os = "macos")
|
|
|
|
|
&& fs::metadata(&self.0).unwrap().len() > HTML_HEADER.len() as u64
|
|
|
|
|
{
|
2019-01-20 16:58:49 -08:00
|
|
|
Command::new("open").arg(&self.0).output().unwrap();
|
2019-01-14 17:19:46 -08:00
|
|
|
}
|
|
|
|
|
} else {
|
2019-01-17 17:16:04 -08:00
|
|
|
eprintln!(
|
|
|
|
|
"Dot failed: {} {}",
|
|
|
|
|
String::from_utf8_lossy(&output.stdout),
|
|
|
|
|
String::from_utf8_lossy(&output.stderr)
|
|
|
|
|
);
|
2019-01-14 17:19:46 -08:00
|
|
|
}
|
2019-01-10 15:50:54 -08:00
|
|
|
}
|
2019-01-08 21:03:51 -08:00
|
|
|
}
|