| 107 | } |
| 108 | |
| 109 | fn write_and_open_html( |
| 110 | html: &str, |
| 111 | output: Option<&std::path::Path>, |
| 112 | open_browser: impl FnOnce(&str) -> std::io::Result<()>, |
| 113 | ) -> CliResult<std::path::PathBuf> { |
| 114 | use std::io::Write; |
| 115 | |
| 116 | let path = match output { |
| 117 | Some(path) => { |
| 118 | std::fs::write(path, html)?; |
| 119 | path.to_path_buf() |
| 120 | } |
| 121 | None => { |
| 122 | // Keep the file after the process exits so asynchronous browsers |
| 123 | // can load it. Unique names avoid replacing another open graph. |
| 124 | let path = std::env::temp_dir() |
| 125 | .join(format!("atomic-intent-graph-{}.html", uuid::Uuid::new_v4())); |
| 126 | let mut file = std::fs::OpenOptions::new() |
| 127 | .write(true) |
| 128 | .create_new(true) |
| 129 | .open(&path)?; |
| 130 | file.write_all(html.as_bytes())?; |
| 131 | path |
| 132 | } |
| 133 | }; |
| 134 | let path = std::fs::canonicalize(path)?; |
| 135 | let url = url::Url::from_file_path(&path).map_err(|_| CliError::InvalidArgument { |
| 136 | message: format!("cannot convert HTML path to a file URL: {}", path.display()), |
| 137 | })?; |
| 138 | println!("Wrote intent graph to {}", path.display()); |
| 139 | if let Err(error) = open_browser(url.as_str()) { |
| 140 | eprintln!("Could not open a browser: {error}. Open {url} manually."); |
| 141 | } |
| 142 | Ok(path) |
| 143 | } |
| 144 | |
| 145 | fn annotate(graph: &Graph, nodes: &mut [atomic_core::KgNode]) { |
| 146 | for node in nodes { |