summaryrefslogtreecommitdiff
path: root/src/systems/helpdoc/helpdoc_viewer.rs
blob: 49d3059f0d15af041a21eb9bf0ea629c2972a138 (plain)
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
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
use crate::systems::helpdoc::{get_helpdoc, get_helpdoc_list};
use cli_utils::{
    display::markdown::Markdown,
    env::{helpdoc::get_helpdoc_enabled, locales::current_locales},
};
use crossterm::{
    cursor::{Hide, MoveTo, Show},
    event::{self, Event, KeyCode, KeyEvent, KeyEventKind, KeyModifiers},
    execute,
    style::{Color, Print, ResetColor, SetBackgroundColor, SetForegroundColor},
    terminal::{
        Clear, ClearType, EnterAlternateScreen, LeaveAlternateScreen, disable_raw_mode,
        enable_raw_mode,
    },
};
use rust_i18n::t;
use std::{
    collections::{BTreeMap, HashMap},
    io::{Write, stdout},
};

const TREE_WIDTH: u16 = 25;

struct HelpdocViewer {
    /// Current language
    lang: String,

    /// Document tree structure
    doc_tree: DocTree,

    /// Currently selected document path
    current_doc: String,

    /// Scroll position history
    scroll_history: HashMap<String, usize>,

    /// Current focus area
    focus: FocusArea,

    /// Currently selected node index in tree view
    tree_selection_index: usize,
}

#[derive(Debug, Clone, Copy, PartialEq)]
enum FocusArea {
    Tree,
    Content,
}

#[derive(Debug, Clone)]
struct DocTreeNode {
    /// Node name
    name: String,

    /// Full document path
    path: String,

    /// Child nodes
    children: BTreeMap<String, DocTreeNode>,

    /// Whether it is a document file
    is_document: bool,
}

#[derive(Debug, Clone)]
struct DocTree {
    /// Root node
    root: DocTreeNode,

    /// Flattened document list
    flat_docs: Vec<String>,
}

impl HelpdocViewer {
    fn new(default_doc: &str, lang: &str) -> Self {
        // Build the document tree
        let doc_tree = Self::build_doc_tree();

        // Validate if the default document exists
        let current_doc = if doc_tree.contains_doc(default_doc) {
            default_doc.to_string()
        } else {
            // If the default document does not exist, use the first document
            doc_tree.flat_docs.first().cloned().unwrap_or_default()
        };

        // Calculate the initial tree selection index
        let tree_selection_index = doc_tree
            .flat_docs
            .iter()
            .position(|doc| *doc == current_doc)
            .unwrap_or(0);

        Self {
            lang: lang.to_string(),
            doc_tree,
            current_doc,
            scroll_history: HashMap::new(),
            focus: FocusArea::Content,
            tree_selection_index,
        }
    }

    /// Build document tree
    fn build_doc_tree() -> DocTree {
        // Get all document list
        let doc_list = get_helpdoc_list();

        // Create root node
        let mut root = DocTreeNode {
            name: "helpdoc".to_string(),
            path: "".to_string(),
            children: BTreeMap::new(),
            is_document: false,
        };

        // Build tree structure for each document path
        for doc_path in doc_list {
            Self::add_doc_to_tree(&mut root, doc_path);
        }

        // Build flattened document list
        let flat_docs = Self::flatten_doc_tree(&root);

        DocTree { root, flat_docs }
    }

    /// Add document to tree
    fn add_doc_to_tree(root: &mut DocTreeNode, doc_path: &str) {
        let parts: Vec<&str> = doc_path.split('/').collect();

        // Use recursive helper function to avoid borrowing issues
        Self::add_doc_to_tree_recursive(root, &parts, 0);
    }

    /// Recursively add document to tree
    fn add_doc_to_tree_recursive(node: &mut DocTreeNode, parts: &[&str], depth: usize) {
        if depth >= parts.len() {
            return;
        }

        let part = parts[depth];
        let is_document = depth == parts.len() - 1;
        let current_path = parts[0..=depth].join("/");

        // Check if node already exists, create if not
        if !node.children.contains_key(part) {
            let new_node = DocTreeNode {
                name: part.to_string(),
                path: current_path.clone(),
                children: BTreeMap::new(),
                is_document,
            };
            node.children.insert(part.to_string(), new_node);
        }

        // Get mutable reference to child node
        if let Some(child) = node.children.get_mut(part) {
            // If this is a document node, ensure it's marked as document
            if is_document {
                child.is_document = true;
            }
            // Recursively process next part
            Self::add_doc_to_tree_recursive(child, parts, depth + 1);
        }
    }

    /// Flatten document tree
    fn flatten_doc_tree(node: &DocTreeNode) -> Vec<String> {
        let mut result = Vec::new();

        if node.is_document && !node.path.is_empty() {
            result.push(node.path.clone());
        }

        // Traverse child nodes in alphabetical order
        for child in node.children.values() {
            result.extend(Self::flatten_doc_tree(child));
        }

        result
    }

    /// Get current document content
    fn current_doc_content(&self) -> String {
        let content = get_helpdoc(&self.current_doc, &self.lang);
        if content.is_empty() {
            format!("Document '{}.{}' not found", self.current_doc, self.lang)
        } else {
            content.to_string()
        }
    }

    /// Get formatted document content
    fn formatted_doc_content(&self) -> Vec<String> {
        let content = self.current_doc_content();
        let formatted = content.markdown();
        formatted.lines().map(|s| s.to_string()).collect()
    }

    /// Truncate a string containing ANSI escape sequences, preserving ANSI sequences
    fn truncate_with_ansi(text: &str, max_width: usize) -> String {
        let mut result = String::new();
        let mut current_width = 0;
        let mut chars = text.chars().peekable();
        let mut in_ansi_escape = false;
        let mut ansi_buffer = String::new();

        while let Some(c) = chars.next() {
            if c == '\x1b' {
                ansi_buffer.push(c);
                while let Some(&next_c) = chars.peek() {
                    ansi_buffer.push(next_c);
                    chars.next();

                    if next_c.is_ascii_alphabetic() {
                        break;
                    }
                }

                result.push_str(&ansi_buffer);
                ansi_buffer.clear();
                in_ansi_escape = false;
                continue;
            }

            if in_ansi_escape {
                ansi_buffer.push(c);
                continue;
            }

            let char_width = if c.is_ascii() { 1 } else { 2 };
            if current_width + char_width > max_width {
                break;
            }

            result.push(c);
            current_width += char_width;
        }

        result
    }

    /// Run viewer
    async fn run(&mut self) -> std::io::Result<()> {
        enable_raw_mode()?;
        execute!(stdout(), EnterAlternateScreen, Hide)?;

        let mut should_exit = false;
        let mut needs_redraw = true;

        while !should_exit {
            if needs_redraw {
                self.draw()?;
                needs_redraw = false;
            }

            if event::poll(std::time::Duration::from_millis(100))?
                && let Event::Key(key) = event::read()?
            {
                match self.handle_key(key) {
                    Some(true) => should_exit = true,
                    Some(false) => needs_redraw = true,
                    None => {} // No action needed
                }
            }
        }

        execute!(stdout(), Show, LeaveAlternateScreen)?;
        disable_raw_mode()?;

        Ok(())
    }

    /// Draw interface
    fn draw(&self) -> std::io::Result<()> {
        let tree_width = TREE_WIDTH;
        let (width, height) = crossterm::terminal::size()?;

        if height <= 3 || width <= tree_width {
            return Ok(());
        }

        execute!(stdout(), Clear(ClearType::All))?;
        let content_width = width - tree_width - 1;

        // Draw title
        execute!(
            stdout(),
            MoveTo(0, 0),
            SetForegroundColor(Color::Cyan),
            Print(format!("{} - {}", t!("helpdoc_viewer.title"), self.lang)),
            ResetColor
        )?;

        // Draw separator line
        for y in 1..height {
            execute!(stdout(), MoveTo(tree_width, y), Print("│"))?;
        }

        // Draw tree view
        self.draw_tree(0, 2, tree_width, height - 4)?;

        // Draw content area
        self.draw_content(tree_width + 1, 2, content_width, height - 4)?;

        // Draw status bar
        self.draw_status_bar(height - 1, width)?;

        stdout().flush()?;
        Ok(())
    }

    /// Draw tree view
    fn draw_tree(&self, x: u16, y: u16, width: u16, height: u16) -> std::io::Result<()> {
        // Draw tree view title
        execute!(
            stdout(),
            MoveTo(x, y - 1),
            SetForegroundColor(Color::Yellow),
            Print("Documents"),
            ResetColor
        )?;

        // Recursively draw tree structure
        let mut line_counter = 0;
        let max_lines = height as usize;

        // Skip root node, start drawing from children
        for child in self.doc_tree.root.children.values() {
            line_counter = self.draw_tree_node(child, x, y, width, 0, line_counter, max_lines)?;
            if line_counter >= max_lines {
                break;
            }
        }

        Ok(())
    }

    /// Recursively draw tree node
    #[allow(clippy::too_many_arguments)]
    fn draw_tree_node(
        &self,
        node: &DocTreeNode,
        x: u16,
        start_y: u16,
        width: u16,
        depth: usize,
        mut line_counter: usize,
        max_lines: usize,
    ) -> std::io::Result<usize> {
        if line_counter >= max_lines {
            return Ok(line_counter);
        }

        let line_y = start_y + line_counter as u16;
        line_counter += 1;

        // Build indentation and suffix
        let indent = "  ".repeat(depth);
        let suffix = if node.children.is_empty() { "" } else { "/" };

        // If this is the currently selected document, highlight it
        let is_selected = node.path == self.current_doc;

        if is_selected && self.focus == FocusArea::Tree {
            // Highlight with white background and black text
            execute!(
                stdout(),
                MoveTo(x, line_y),
                SetForegroundColor(Color::Black),
                SetBackgroundColor(Color::White),
                Print(" ".repeat(width as usize)),
                MoveTo(x, line_y),
            )?;
        } else {
            // Normal display
            execute!(
                stdout(),
                MoveTo(x, line_y),
                SetForegroundColor(Color::White),
                SetBackgroundColor(Color::Reset),
            )?;
        }

        // Display node name
        let display_text = format!("{}  {}{}", indent, node.name, suffix);
        execute!(stdout(), Print(display_text))?;
        execute!(stdout(), ResetColor, SetBackgroundColor(Color::Reset))?;

        // Recursively draw child nodes
        if !node.children.is_empty() {
            for child in node.children.values() {
                line_counter = self.draw_tree_node(
                    child,
                    x,
                    start_y,
                    width,
                    depth + 1,
                    line_counter,
                    max_lines,
                )?;
                if line_counter >= max_lines {
                    break;
                }
            }
        }

        Ok(line_counter)
    }

    /// Draw content area
    fn draw_content(&self, x: u16, y: u16, width: u16, height: u16) -> std::io::Result<()> {
        // Draw content area title
        let (fg_color, bg_color) = if self.focus == FocusArea::Content {
            (Color::Black, Color::White)
        } else {
            (Color::Yellow, Color::Reset)
        };

        execute!(
            stdout(),
            MoveTo(x, y - 1),
            SetForegroundColor(fg_color),
            SetBackgroundColor(bg_color),
            Print(format!(" Reading `{}` ", self.current_doc)),
            ResetColor
        )?;

        // Get formatted content
        let content_lines = self.formatted_doc_content();
        let scroll_pos = self
            .scroll_history
            .get(&self.current_doc)
            .copied()
            .unwrap_or(0);
        let start_line = scroll_pos.min(content_lines.len().saturating_sub(1));
        let end_line = (start_line + height as usize).min(content_lines.len());

        for (i, line) in content_lines
            .iter()
            .enumerate()
            .take(end_line)
            .skip(start_line)
        {
            let line_y = y + i as u16 - start_line as u16;
            let display_line = Self::truncate_with_ansi(line, width as usize);

            execute!(stdout(), MoveTo(x, line_y), Print(&display_line))?;
        }

        // Display scroll position indicator
        if content_lines.len() > height as usize && !content_lines.is_empty() {
            let scroll_percent = if !content_lines.is_empty() {
                (scroll_pos * 100) / content_lines.len()
            } else {
                0
            };
            execute!(
                stdout(),
                MoveTo(x + width - 5, y - 1),
                SetForegroundColor(Color::DarkGrey),
                Print(format!("{:3}%", scroll_percent)),
                ResetColor
            )?;
        }

        Ok(())
    }

    /// Draw status bar
    fn draw_status_bar(&self, y: u16, width: u16) -> std::io::Result<()> {
        // Draw status bar background
        execute!(
            stdout(),
            MoveTo(0, y),
            SetForegroundColor(Color::Black),
            Print(" ".repeat(width as usize)),
            MoveTo(0, y),
            SetForegroundColor(Color::White),
        )?;

        let status_text = match self.focus {
            FocusArea::Tree => t!("helpdoc_viewer.tree_area_hint").to_string().markdown(),
            FocusArea::Content => t!("helpdoc_viewer.content_area_hint")
                .to_string()
                .markdown(),
        }
        .to_string();

        let truncated_text = Self::truncate_with_ansi(&status_text, width as usize);
        execute!(stdout(), Print(&truncated_text))?;
        execute!(stdout(), ResetColor)?;

        Ok(())
    }

    /// Handle key input
    fn handle_key(&mut self, key: KeyEvent) -> Option<bool> {
        if key.kind != KeyEventKind::Press {
            return None;
        }

        match key.code {
            KeyCode::Char('q') | KeyCode::Esc => return Some(true),
            KeyCode::Char(' ') => self.toggle_focus(),
            KeyCode::Left => self.move_left(),
            KeyCode::Right => self.move_right(),
            KeyCode::Up => self.move_up(),
            KeyCode::Down => self.move_down(),
            KeyCode::Char('g') if key.modifiers == KeyModifiers::NONE => self.go_to_top(),
            KeyCode::Char('G') if key.modifiers == KeyModifiers::SHIFT => self.go_to_bottom(),
            KeyCode::Enter => self.select_item(),
            _ => return None,
        }
        Some(false)
    }

    /// Toggle focus area
    fn toggle_focus(&mut self) {
        self.focus = match self.focus {
            FocusArea::Tree => FocusArea::Content,
            FocusArea::Content => FocusArea::Tree,
        };
    }

    /// Move left
    fn move_left(&mut self) {
        if self.focus == FocusArea::Content {
            self.focus = FocusArea::Tree;
        }
    }

    /// Move right
    fn move_right(&mut self) {
        if self.focus == FocusArea::Tree {
            self.focus = FocusArea::Content;
        }
    }

    /// Move up
    fn move_up(&mut self) {
        match self.focus {
            FocusArea::Tree => self.previous_doc(),
            FocusArea::Content => self.scroll_up(),
        }
    }

    /// Move down
    fn move_down(&mut self) {
        match self.focus {
            FocusArea::Tree => self.next_doc(),
            FocusArea::Content => self.scroll_down(),
        }
    }

    /// Scroll to top
    fn go_to_top(&mut self) {
        match self.focus {
            FocusArea::Content => {
                self.scroll_history.insert(self.current_doc.clone(), 0);
            }
            FocusArea::Tree => {
                // Select first document
                self.tree_selection_index = 0;
                if let Some(first_doc) = self.doc_tree.flat_docs.first() {
                    self.current_doc = first_doc.clone();
                }
            }
        }
    }

    /// Scroll to bottom
    fn go_to_bottom(&mut self) {
        match self.focus {
            FocusArea::Content => {
                let content_lines = self.formatted_doc_content();
                if content_lines.len() > 10 {
                    self.scroll_history
                        .insert(self.current_doc.clone(), content_lines.len() - 10);
                }
            }
            FocusArea::Tree => {
                // Select last document
                self.tree_selection_index = self.doc_tree.flat_docs.len().saturating_sub(1);
                if let Some(last_doc) = self.doc_tree.flat_docs.last() {
                    self.current_doc = last_doc.clone();
                }
            }
        }
    }

    /// Select current item
    fn select_item(&mut self) {
        if self.focus == FocusArea::Tree {
            // Update current document to the one selected in tree view
            if let Some(doc) = self.doc_tree.flat_docs.get(self.tree_selection_index) {
                self.current_doc = doc.clone();
            }
            // Switch focus to content area
            self.focus = FocusArea::Content;
        }
    }

    /// Previous document
    fn previous_doc(&mut self) {
        if self.tree_selection_index > 0 {
            self.tree_selection_index -= 1;
            if let Some(doc) = self.doc_tree.flat_docs.get(self.tree_selection_index) {
                self.current_doc = doc.clone();
                // Reset scroll position
                self.scroll_history.remove(&self.current_doc);
            }
        }
    }

    /// Next document
    fn next_doc(&mut self) {
        if self.tree_selection_index + 1 < self.doc_tree.flat_docs.len() {
            self.tree_selection_index += 1;
            if let Some(doc) = self.doc_tree.flat_docs.get(self.tree_selection_index) {
                self.current_doc = doc.clone();
                // Reset scroll position
                self.scroll_history.remove(&self.current_doc);
            }
        }
    }

    /// Scroll up
    fn scroll_up(&mut self) {
        let current_scroll = self
            .scroll_history
            .get(&self.current_doc)
            .copied()
            .unwrap_or(0);
        if current_scroll > 0 {
            self.scroll_history
                .insert(self.current_doc.clone(), current_scroll - 1);
        }
    }

    /// Scroll down
    fn scroll_down(&mut self) {
        let content_lines = self.formatted_doc_content();
        let current_scroll = self
            .scroll_history
            .get(&self.current_doc)
            .copied()
            .unwrap_or(0);
        if current_scroll + 1 < content_lines.len() {
            self.scroll_history
                .insert(self.current_doc.clone(), current_scroll + 1);
        }
    }
}

impl DocTree {
    /// Check if document exists
    fn contains_doc(&self, doc_path: &str) -> bool {
        self.flat_docs.contains(&doc_path.to_string())
    }
}

/// Display help document viewer
pub async fn display_with_lang(default_focus_doc: &str, lang: &str) {
    if get_helpdoc_enabled() {
        let mut viewer = HelpdocViewer::new(default_focus_doc, lang);

        if let Err(e) = viewer.run().await {
            eprintln!("Error running helpdoc viewer: {}", e);
        }
    } else {
        let content = get_helpdoc(default_focus_doc, lang).markdown();
        println!("{}", content)
    }
}

/// Display help document viewer
pub async fn display(default_focus_doc: &str) {
    display_with_lang(default_focus_doc, current_locales().as_str()).await;
}