|
1 | | -use clap::{Parser, ValueEnum}; |
2 | 1 | use parallel_disk_usage::man_page::render_man_page; |
3 | | -use std::{ |
4 | | - fs, |
5 | | - process::{Command, ExitCode}, |
6 | | -}; |
7 | 2 |
|
8 | | -const LINE_LENGTH: &str = "120"; |
9 | | - |
10 | | -/// Manage generated man pages. |
11 | | -#[derive(Debug, Parser)] |
12 | | -struct Args { |
13 | | - /// Action to take. |
14 | | - #[clap(value_enum)] |
15 | | - action: Action, |
16 | | - /// Type of file to target. |
17 | | - #[clap(value_enum)] |
18 | | - kind: Kind, |
19 | | - /// Number of the man page. |
20 | | - #[clap(value_enum)] |
21 | | - page: Page, |
22 | | -} |
23 | | - |
24 | | -#[derive(Debug, Clone, ValueEnum)] |
25 | | -enum Action { |
26 | | - /// Check whether the man page is up-to-date. |
27 | | - Check, |
28 | | - /// Generate the man page. |
29 | | - Generate, |
30 | | -} |
31 | | - |
32 | | -#[derive(Debug, Clone, ValueEnum)] |
33 | | -enum Kind { |
34 | | - /// Check or generate the roff file (`pdu.N`) from `Args`. |
35 | | - Roff, |
36 | | - /// Check or generate the man file (`pdu.N.man`) from the generated roff file (`pdu.N`). |
37 | | - Man, |
38 | | -} |
39 | | - |
40 | | -#[derive(Debug, Clone, ValueEnum)] |
41 | | -enum Page { |
42 | | - #[clap(name = "1")] |
43 | | - One, |
44 | | -} |
45 | | - |
46 | | -impl Page { |
47 | | - fn number(&self) -> u8 { |
48 | | - match self { |
49 | | - Page::One => 1, |
50 | | - } |
51 | | - } |
52 | | -} |
53 | | - |
54 | | -fn roff_path(page_num: u8) -> String { |
55 | | - format!("exports/pdu.{page_num}") |
56 | | -} |
57 | | - |
58 | | -fn man_path(page_num: u8) -> String { |
59 | | - format!("exports/pdu.{page_num}.man") |
60 | | -} |
61 | | - |
62 | | -fn render_man_output(page_num: u8) -> Result<String, String> { |
63 | | - let roff_file = roff_path(page_num); |
64 | | - let output = Command::new("groff") |
65 | | - .args(["-man", "-Tutf8", "-P-cbou"]) |
66 | | - .arg(format!("-rLL={LINE_LENGTH}n")) |
67 | | - .arg(format!("./{roff_file}")) |
68 | | - .output() |
69 | | - .map_err(|error| format!("failed to run groff: {error}"))?; |
70 | | - if !output.status.success() { |
71 | | - let stderr = String::from_utf8_lossy(&output.stderr); |
72 | | - return Err(format!("groff failed: {stderr}")); |
73 | | - } |
74 | | - let content = String::from_utf8(output.stdout) |
75 | | - .map_err(|error| format!("groff output is not UTF-8: {error}"))?; |
76 | | - Ok(normalize_text(&strip_formatting(&content))) |
77 | | -} |
78 | | - |
79 | | -/// Strips terminal formatting from grotty output. |
80 | | -/// |
81 | | -/// Handles two styles grotty may use: |
82 | | -/// - **SGR mode** (default): ANSI escape sequences like `\x1b[1m` (bold), `\x1b[0m` (reset). |
83 | | -/// - **Legacy mode** (`-c`): Backspace overstrikes like `X\x08X` (bold), `_\x08X` (underline). |
84 | | -fn strip_formatting(text: &str) -> String { |
85 | | - let chars: Vec<char> = text.chars().collect(); |
86 | | - let mut result = String::with_capacity(text.len()); |
87 | | - let mut index = 0; |
88 | | - while index < chars.len() { |
89 | | - if chars[index] == '\x1b' && index + 1 < chars.len() && chars[index + 1] == '[' { |
90 | | - // Skip ANSI escape: ESC [ ... m |
91 | | - index += 2; |
92 | | - while index < chars.len() && chars[index] != 'm' { |
93 | | - index += 1; |
94 | | - } |
95 | | - if index < chars.len() { |
96 | | - index += 1; // skip the 'm' |
97 | | - } |
98 | | - } else if index + 1 < chars.len() && chars[index + 1] == '\x08' { |
99 | | - // Skip backspace overstrike: char + BS |
100 | | - index += 2; |
101 | | - } else { |
102 | | - result.push(chars[index]); |
103 | | - index += 1; |
104 | | - } |
105 | | - } |
106 | | - result |
107 | | -} |
108 | | - |
109 | | -/// Strips trailing whitespace per line, trims trailing blank lines, |
110 | | -/// and ensures the output ends with exactly one newline. |
111 | | -fn normalize_text(text: &str) -> String { |
112 | | - let mut result: String = text |
113 | | - .lines() |
114 | | - .map(str::trim_end) |
115 | | - .collect::<Vec<_>>() |
116 | | - .join("\n"); |
117 | | - let trimmed_len = result.trim_end().len(); |
118 | | - result.truncate(trimmed_len); |
119 | | - result.push('\n'); |
120 | | - result |
121 | | -} |
122 | | - |
123 | | -fn write_file(path: &str, content: &str) -> ExitCode { |
124 | | - match fs::write(path, content) { |
125 | | - Ok(()) => ExitCode::SUCCESS, |
126 | | - Err(error) => { |
127 | | - eprintln!("error writing {path}: {error}"); |
128 | | - ExitCode::FAILURE |
129 | | - } |
130 | | - } |
131 | | -} |
132 | | - |
133 | | -fn check_file(path: &str, expected: &str) -> ExitCode { |
134 | | - match fs::read_to_string(path) { |
135 | | - Ok(actual) if actual == expected => ExitCode::SUCCESS, |
136 | | - Ok(_) => { |
137 | | - eprintln!("{path} is outdated, run ./generate-completions.sh to update it"); |
138 | | - ExitCode::FAILURE |
139 | | - } |
140 | | - Err(error) => { |
141 | | - eprintln!("error reading {path}: {error}"); |
142 | | - ExitCode::FAILURE |
143 | | - } |
144 | | - } |
145 | | -} |
146 | | - |
147 | | -fn main() -> ExitCode { |
148 | | - let args = Args::parse(); |
149 | | - let page_num = args.page.number(); |
150 | | - match (args.action, args.kind) { |
151 | | - (Action::Generate, Kind::Roff) => write_file(&roff_path(page_num), &render_man_page()), |
152 | | - (Action::Generate, Kind::Man) => match render_man_output(page_num) { |
153 | | - Ok(content) => write_file(&man_path(page_num), &content), |
154 | | - Err(error) => { |
155 | | - eprintln!("error: {error}"); |
156 | | - ExitCode::FAILURE |
157 | | - } |
158 | | - }, |
159 | | - (Action::Check, Kind::Roff) => check_file(&roff_path(page_num), &render_man_page()), |
160 | | - (Action::Check, Kind::Man) => match render_man_output(page_num) { |
161 | | - Ok(expected) => check_file(&man_path(page_num), &expected), |
162 | | - Err(error) => { |
163 | | - eprintln!("error: {error}"); |
164 | | - ExitCode::FAILURE |
165 | | - } |
166 | | - }, |
167 | | - } |
| 3 | +fn main() { |
| 4 | + print!("{}", render_man_page()); |
168 | 5 | } |
0 commit comments