-
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathhelp.rs
More file actions
498 lines (453 loc) · 15.5 KB
/
Copy pathhelp.rs
File metadata and controls
498 lines (453 loc) · 15.5 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
// SPDX-License-Identifier: MPL-2.0
//! 3-Tier Help System
//!
//! Provides comprehensive help at three levels:
//! - Tier 1: Quick reference (`help cmd` - one-liners)
//! - Tier 2: Detailed help (`help -v cmd` - full documentation)
//! - Tier 3: Man pages (`man vsh-cmd` - comprehensive reference)
//!
//! Fish-style: Shows examples, related commands, and proof references.
use anyhow::{bail, Result};
use colored::Colorize;
use crate::pager;
use crate::proof_refs;
use crate::state::OperationType;
/// Help tier level
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub enum HelpTier {
/// Quick one-liner
Quick,
/// Detailed multi-line with examples
Verbose,
/// Full man page
ManPage,
}
/// Help entry for a command
#[derive(Debug, Clone)]
pub struct HelpEntry {
/// Command name
pub name: &'static str,
/// One-line summary
pub summary: &'static str,
/// Usage pattern
pub usage: &'static str,
/// Full description
pub description: &'static str,
/// Examples
pub examples: &'static [Example],
/// Related commands
pub related: &'static [&'static str],
/// Proof reference (if reversible operation)
pub proof_operation: Option<OperationType>,
}
/// Example command usage
#[derive(Debug, Clone)]
pub struct Example {
/// Description of what the example does
pub description: &'static str,
/// The command to run
pub command: &'static str,
}
impl HelpEntry {
/// Display help at specified tier
pub fn display(&self, tier: HelpTier) -> Result<()> {
match tier {
HelpTier::Quick => self.display_quick(),
HelpTier::Verbose => self.display_verbose(),
HelpTier::ManPage => self.display_manpage(),
}
}
/// Tier 1: Quick one-liner
fn display_quick(&self) -> Result<()> {
println!("{} - {}", self.name.bright_green().bold(), self.summary);
println!("Usage: {}", self.usage.bright_cyan());
println!(
"Run {} for details",
format!("help -v {}", self.name).bright_yellow()
);
Ok(())
}
/// Tier 2: Verbose with examples
fn display_verbose(&self) -> Result<()> {
let mut output = String::new();
// Header
output.push_str(&format!(
"{}\n",
format!("=== {} ===", self.name.to_uppercase())
.bright_blue()
.bold()
));
output.push('\n');
// Summary
output.push_str(&format!("{}\n", self.summary));
output.push('\n');
// Usage
output.push_str(&format!("{}\n", "USAGE:".bright_yellow().bold()));
output.push_str(&format!(" {}\n", self.usage.bright_cyan()));
output.push('\n');
// Description
output.push_str(&format!("{}\n", "DESCRIPTION:".bright_yellow().bold()));
for line in self.description.lines() {
output.push_str(&format!(" {}\n", line));
}
output.push('\n');
// Examples
if !self.examples.is_empty() {
output.push_str(&format!("{}\n", "EXAMPLES:".bright_yellow().bold()));
for (i, example) in self.examples.iter().enumerate() {
output.push_str(&format!(" {}. {}\n", i + 1, example.description));
output.push_str(&format!(
" {}\n",
format!("$ {}", example.command).bright_cyan()
));
output.push('\n');
}
}
// Related commands
if !self.related.is_empty() {
output.push_str(&format!("{}\n", "SEE ALSO:".bright_yellow().bold()));
output.push_str(&format!(" {}\n", self.related.join(", ")));
output.push('\n');
}
// Proof reference
if let Some(op_type) = self.proof_operation {
output.push_str(&format!(
"{}\n",
"FORMAL VERIFICATION:".bright_yellow().bold()
));
let proof_ref = proof_refs::ProofReference::for_operation(op_type);
output.push_str(&format!(" {}\n", proof_ref.format_short()));
output.push_str(&format!(
" Run {} for proof details\n",
"proof".bright_cyan()
));
output.push('\n');
}
// Footer
output.push_str(&format!(
"Run {} for comprehensive documentation\n",
format!("man vsh-{}", self.name).bright_yellow()
));
// Page if output is long
pager::page(&output)?;
Ok(())
}
/// Tier 3: Full man page (formatted)
fn display_manpage(&self) -> Result<()> {
let mut output = String::new();
// Man page header
output.push_str(&format!(
"VSH-{}(1) Valence Shell Manual VSH-{}(1)\n\n",
self.name.to_uppercase(),
self.name.to_uppercase()
));
// NAME
output.push_str("NAME\n");
output.push_str(&format!(" {} - {}\n\n", self.name, self.summary));
// SYNOPSIS
output.push_str("SYNOPSIS\n");
output.push_str(&format!(" {}\n\n", self.usage));
// DESCRIPTION
output.push_str("DESCRIPTION\n");
for line in self.description.lines() {
output.push_str(&format!(" {}\n", line));
}
output.push('\n');
// EXAMPLES
if !self.examples.is_empty() {
output.push_str("EXAMPLES\n");
for example in self.examples.iter() {
output.push_str(&format!(" {}\n", example.description));
output.push_str(&format!(" $ {}\n\n", example.command));
}
}
// FORMAL VERIFICATION
if let Some(op_type) = self.proof_operation {
output.push_str("FORMAL VERIFICATION\n");
let proof_ref = proof_refs::ProofReference::for_operation(op_type);
output.push_str(" This operation is formally verified for correctness.\n\n");
output.push_str(&format!(" Theorem: {}\n", proof_ref.theorem));
output.push_str(&format!(
" Description: {}\n\n",
proof_ref.description
));
output.push_str(" Proof locations:\n");
output.push_str(&format!(" Coq: {}\n", proof_ref.coq_location));
output.push_str(&format!(" Lean 4: {}\n", proof_ref.lean_location));
output.push_str(&format!(" Agda: {}\n", proof_ref.agda_location));
output.push_str(&format!(
" Isabelle: {}\n\n",
proof_ref.isabelle_location
));
}
// SEE ALSO
if !self.related.is_empty() {
output.push_str("SEE ALSO\n");
output.push_str(&format!(
" {}\n\n",
self.related
.iter()
.map(|cmd| format!("vsh-{}(1)", cmd))
.collect::<Vec<_>>()
.join(", ")
));
}
// Footer
output.push_str(&format!(
"Valence Shell 0.14.0 {} VSH-{}(1)\n",
chrono::Utc::now().format("%Y-%m-%d"),
self.name.to_uppercase()
));
// Page the output
pager::page(&output)?;
Ok(())
}
}
/// Get help entry for a command
pub fn get_help(command: &str) -> Option<&'static HelpEntry> {
HELP_DATABASE.iter().find(|entry| entry.name == command)
}
/// Display help for a command
///
/// # Arguments
/// * `command` - Command to show help for (None = list all commands)
/// * `tier` - Help detail level
pub fn display_help(command: Option<&str>, tier: HelpTier) -> Result<()> {
match command {
Some(cmd) => {
if let Some(entry) = get_help(cmd) {
entry.display(tier)?;
} else {
bail!(
"No help available for '{}'. Run 'help' to see all commands.",
cmd
);
}
}
None => {
// List all commands
display_command_list()?;
}
}
Ok(())
}
/// Display list of all available commands
fn display_command_list() -> Result<()> {
println!("{}", "Available commands:".bright_blue().bold());
println!();
// Group by category
let categories = vec![
("Navigation", vec!["cd", "pwd"]),
("File Operations", vec!["mkdir", "rmdir", "touch", "rm"]),
("History & Undo", vec!["undo", "redo", "history"]),
("Transactions", vec!["begin", "commit", "rollback"]),
("Utilities", vec!["help", "exit"]),
];
for (category, commands) in categories {
println!("{}", category.bright_yellow());
for cmd in commands {
if let Some(entry) = get_help(cmd) {
println!(" {} - {}", cmd.bright_green(), entry.summary);
}
}
println!();
}
println!("Run {} for command details", "help <command>".bright_cyan());
println!(
"Run {} for detailed help with examples",
"help -v <command>".bright_cyan()
);
println!(
"Run {} for full manual page",
"man vsh-<command>".bright_cyan()
);
Ok(())
}
/// Help database (static)
static HELP_DATABASE: &[HelpEntry] = &[
// mkdir
HelpEntry {
name: "mkdir",
summary: "Create a directory",
usage: "mkdir <path>",
description: "Creates a new directory at the specified path.\n\
This operation is reversible via 'undo'.",
examples: &[
Example {
description: "Create a directory",
command: "mkdir project",
},
Example {
description: "Undo directory creation",
command: "mkdir test && undo",
},
],
related: &["rmdir", "undo", "touch"],
proof_operation: Some(OperationType::Mkdir),
},
// rmdir
HelpEntry {
name: "rmdir",
summary: "Remove an empty directory",
usage: "rmdir <path>",
description: "Removes an empty directory at the specified path.\n\
Directory must be empty (use 'rm -r' for non-empty directories).\n\
This operation is reversible via 'undo'.",
examples: &[
Example {
description: "Remove empty directory",
command: "rmdir old_project",
},
Example {
description: "Create and remove directory",
command: "mkdir temp && rmdir temp",
},
],
related: &["mkdir", "undo", "rm"],
proof_operation: Some(OperationType::Rmdir),
},
// touch
HelpEntry {
name: "touch",
summary: "Create an empty file",
usage: "touch <path>",
description: "Creates a new empty file at the specified path.\n\
This operation is reversible via 'undo'.",
examples: &[
Example {
description: "Create empty file",
command: "touch README.md",
},
Example {
description: "Create multiple files",
command: "touch file1.txt && touch file2.txt",
},
],
related: &["rm", "mkdir", "undo"],
proof_operation: Some(OperationType::CreateFile),
},
// rm
HelpEntry {
name: "rm",
summary: "Remove a file",
usage: "rm <path>",
description: "Removes a file at the specified path.\n\
File contents are preserved in undo history for restoration.\n\
This operation is reversible via 'undo'.\n\n\
For GDPR-compliant secure deletion, use 'obliterate'.",
examples: &[
Example {
description: "Remove file",
command: "rm old_file.txt",
},
Example {
description: "Remove and restore file",
command: "rm file.txt && undo",
},
],
related: &["touch", "rmdir", "undo", "obliterate"],
proof_operation: Some(OperationType::DeleteFile),
},
// undo
HelpEntry {
name: "undo",
summary: "Undo the last operation(s)",
usage: "undo [count]",
description: "Reverses the last N operations (default: 1).\n\
Operations are undone in reverse order.\n\
Each undo is itself recorded and can be undone with 'redo'.",
examples: &[
Example {
description: "Undo last operation",
command: "mkdir test && undo",
},
Example {
description: "Undo last 3 operations",
command: "undo 3",
},
],
related: &["redo", "history"],
proof_operation: None,
},
// redo
HelpEntry {
name: "redo",
summary: "Redo previously undone operation(s)",
usage: "redo [count]",
description: "Re-applies the last N undone operations (default: 1).\n\
Only works if operations were undone and not yet overwritten.",
examples: &[Example {
description: "Undo and redo",
command: "mkdir test && undo && redo",
}],
related: &["undo", "history"],
proof_operation: None,
},
// cd
HelpEntry {
name: "cd",
summary: "Change current directory",
usage: "cd <path>",
description: "Changes the current working directory to the specified path.\n\
Use 'cd -' to return to previous directory.",
examples: &[
Example {
description: "Change to subdirectory",
command: "cd project/src",
},
Example {
description: "Return to previous directory",
command: "cd -",
},
],
related: &["pwd", "pushd", "popd"],
proof_operation: None,
},
// help
HelpEntry {
name: "help",
summary: "Display help information",
usage: "help [command] [-v]",
description: "Shows help at three levels:\n\
- help : List all commands\n\
- help <cmd> : Quick reference\n\
- help -v <cmd> : Detailed help with examples\n\
- man vsh-<cmd> : Full manual page",
examples: &[
Example {
description: "List all commands",
command: "help",
},
Example {
description: "Quick help for mkdir",
command: "help mkdir",
},
Example {
description: "Detailed help with examples",
command: "help -v mkdir",
},
],
related: &[],
proof_operation: None,
},
];
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn test_get_help() {
assert!(get_help("mkdir").is_some());
assert!(get_help("rm").is_some());
assert!(get_help("nonexistent").is_none());
}
#[test]
fn test_help_entries_complete() {
// Verify all entries have required fields
for entry in HELP_DATABASE.iter() {
assert!(!entry.name.is_empty());
assert!(!entry.summary.is_empty());
assert!(!entry.usage.is_empty());
assert!(!entry.description.is_empty());
}
}
}