1use crate::utils::types::Result;
2use colored::*;
3use std::fs;
4
5pub fn execute_docs_operation(out: Option<&str>) -> Result<()> {
7 crate::say!("{}", "📚 Opening Git Editor Documentation...".cyan().bold());
8
9 let docs_html = generate_comprehensive_docs()?;
10
11 let docs_file = match out {
12 Some(path) => {
13 fs::write(path, docs_html)?;
14 std::path::PathBuf::from(path)
15 }
16 None => {
17 let mut file = tempfile::Builder::new()
19 .prefix("git-editor-docs-")
20 .suffix(".html")
21 .tempfile()?;
22 std::io::Write::write_all(&mut file, docs_html.as_bytes())?;
23 file.keep().map_err(|e| e.error)?.1
24 }
25 };
26
27 if crate::output::json_mode() {
28 crate::output::emit(&serde_json::json!({
29 "ok": true,
30 "command": "docs",
31 "path": docs_file.display().to_string(),
32 }));
33 }
34
35 match open_in_browser(&docs_file) {
37 Ok(_) => {
38 crate::say!(
39 "{}",
40 "✅ Documentation opened in your default browser!"
41 .green()
42 .bold()
43 );
44 crate::say!(
45 "{}",
46 format!("📍 File location: {}", docs_file.display()).dimmed()
47 );
48 }
49 Err(_) => {
50 crate::say!(
51 "{}",
52 "⚠️ Could not open browser automatically.".yellow().bold()
53 );
54 crate::say!(
55 "{}",
56 format!("📁 Documentation saved at: {}", docs_file.display()).cyan()
57 );
58 crate::say!(
59 "{}",
60 "💡 You can manually open this file in your browser.".dimmed()
61 );
62 }
64 }
65
66 Ok(())
67}
68
69fn open_in_browser(file_path: &std::path::Path) -> Result<()> {
70 if std::env::var("NO_BROWSER").is_ok()
72 || std::env::var("GIT_EDITOR_NO_BROWSER").is_ok()
73 || cfg!(test)
74 {
75 return Err("Browser opening disabled".into());
76 }
77
78 let file_url = format!("file://{}", file_path.display());
79 open::that(file_url)?;
80 Ok(())
81}
82
83fn generate_comprehensive_docs() -> Result<String> {
84 let template = include_str!("../docs/template.html");
86
87 let version = env!("CARGO_PKG_VERSION");
88 let current_date = chrono::Utc::now()
89 .format("%Y-%m-%d %H:%M:%S UTC")
90 .to_string();
91
92 let html = template
94 .replace("{{VERSION}}", version)
95 .replace("{{DATE}}", ¤t_date);
96
97 Ok(html)
98}
99
100#[cfg(test)]
101mod tests {
102 use super::*;
103
104 #[test]
105 fn test_generate_comprehensive_docs() {
106 let result = generate_comprehensive_docs();
107 assert!(result.is_ok());
108
109 let html = result.unwrap();
110
111 assert!(html.contains("<!DOCTYPE html>"));
113 assert!(html.contains("Git Editor - Complete Technical Documentation"));
114
115 assert!(html.contains(&format!("Git Editor v{}", env!("CARGO_PKG_VERSION"))));
117 assert!(!html.contains("{{VERSION}}"));
118 assert!(!html.contains("{{DATE}}"));
119
120 assert!(html.contains("Table of Contents"));
122 assert!(html.contains("Overview"));
123 assert!(html.contains("Installation"));
124 assert!(html.contains("Operation Modes"));
125 assert!(html.contains("Command Reference"));
126 assert!(html.contains("Usage Examples"));
127 assert!(html.contains("Advanced Features"));
128 assert!(html.contains("Development"));
129 assert!(html.contains("Troubleshooting"));
130
131 assert!(html.contains("--docs"));
133 assert!(html.contains("Open comprehensive documentation"));
134 }
135
136 #[test]
137 fn test_docs_html_contains_expected_sections() {
138 let html = generate_comprehensive_docs().unwrap();
139
140 let expected_sections = [
142 "#overview",
143 "#installation",
144 "#operation-modes",
145 "#command-reference",
146 "#examples",
147 "#advanced-features",
148 "#development",
149 "#troubleshooting",
150 ];
151
152 for section in expected_sections {
153 assert!(html.contains(section), "Missing section: {section}");
154 }
155 }
156
157 #[test]
158 fn test_docs_html_contains_operation_modes() {
159 let html = generate_comprehensive_docs().unwrap();
160
161 assert!(html.contains("Full Rewrite"));
163 assert!(html.contains("Show History"));
164 assert!(html.contains("Pick Specific"));
165 assert!(html.contains("Range Edit"));
166 assert!(html.contains("Simulation"));
167 assert!(html.contains("Documentation"));
168
169 assert!(html.contains("--show-history"));
171 assert!(html.contains("--pick-specific-commits"));
172 assert!(html.contains("--range"));
173 assert!(html.contains("--simulate"));
174 assert!(html.contains("--docs"));
175 }
176
177 #[test]
178 fn test_docs_html_contains_examples() {
179 let html = generate_comprehensive_docs().unwrap();
180
181 assert!(html.contains("git-editor --docs"));
183 assert!(html.contains("git-editor -s"));
184 assert!(html.contains("git-editor --simulate"));
185 assert!(html.contains("Opens this comprehensive documentation"));
186 }
187
188 #[test]
189 fn test_version_and_date_replacement() {
190 let html = generate_comprehensive_docs().unwrap();
191
192 let expected_version = env!("CARGO_PKG_VERSION");
194 assert!(html.contains(&format!("Git Editor v{expected_version}")));
195
196 assert!(html.contains("Generated on"));
198 assert!(html.contains("UTC"));
199
200 assert!(!html.contains("{{VERSION}}"));
202 assert!(!html.contains("{{DATE}}"));
203 }
204
205 #[test]
206 fn test_html_structure_validity() {
207 let html = generate_comprehensive_docs().unwrap();
208
209 assert!(html.starts_with("<!DOCTYPE html>"));
211 assert!(html.contains("<html lang=\"en\">"));
212 assert!(html.contains("<head>"));
213 assert!(html.contains("<body>"));
214 assert!(html.contains("</body>"));
215 assert!(html.contains("</html>"));
216
217 assert!(html.contains("<style>"));
219 assert!(html.contains(".container"));
220 assert!(html.contains(".badge"));
221
222 assert!(html.contains("<div class=\"container\">"));
224 }
225}