awesome-omarchy-tui 0.5.1

A beautiful terminal UI for browsing the awesome-omarchy repository with search, navigation, and GitHub integration
Documentation
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
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
use crate::models::ThemeEntry;
use crate::models::{
    ReadmeContent, ReadmeMetadata, RepositoryEntry, SearchIndex, SearchLocation, SearchPriority,
    Section,
};
use crate::models::{Theme, ThemeColorPalette, ThemeColors};
use anyhow::{Result, anyhow};
use pulldown_cmark::{Event, Parser, Tag, TagEnd};

/// Parser for awesome-omarchy README markdown content
pub struct ReadmeParser {
    /// Common section headers found in awesome-* repositories
    known_sections: Vec<&'static str>,
    /// Patterns to exclude from parsing (e.g., TOC, badges)
    exclusion_patterns: Vec<&'static str>,
}

impl ReadmeParser {
    pub fn new() -> Self {
        Self {
            known_sections: vec![
                "Official Resources",
                "Alternative Implementations",
                "Themes",
                "Development Tools",
                "Related Projects",
                "Community Resources",
                "Documentation",
                "Tutorials",
                "Examples",
                "Libraries",
                "Plugins",
                "Extensions",
                "Integrations",
                "Testing",
                "Deployment",
                "Monitoring",
                "Security",
                "Performance",
                "Utilities",
                "Resources",
            ],
            exclusion_patterns: vec![
                "Contents",
                "Table of Contents",
                "TOC",
                "Contributing",
                "License",
                "Awesome",
                "Badge",
            ],
        }
    }

    /// Parse markdown content into structured ReadmeContent
    pub fn parse(&self, markdown_content: &str) -> Result<ReadmeContent> {
        if markdown_content.trim().is_empty() {
            return Err(anyhow!("Empty markdown content"));
        }

        let parser = Parser::new(markdown_content);
        let mut readme_content = ReadmeContent::default();
        let mut current_section: Option<Section> = None;
        let mut current_text = String::new();
        let mut is_in_header = false;
        let mut current_header_level = 0;
        let mut header_text = String::new();
        let mut link_url = String::new();
        let mut is_in_list_item = false;
        let mut current_item_text = String::new(); // Text for current list item only
        let mut metadata = ReadmeMetadata::default();

        // Extract title from first heading
        let mut title_extracted = false;

        for event in parser {
            match event {
                Event::Start(Tag::Heading { level, .. }) => {
                    is_in_header = true;
                    current_header_level = level as u32;
                    header_text.clear();
                }
                Event::End(TagEnd::Heading(_)) => {
                    if is_in_header {
                        let header = header_text.trim().to_string();

                        // Extract title from first H1
                        if !title_extracted && current_header_level == 1 {
                            metadata.title = header.clone();
                            title_extracted = true;
                        }

                        // Check if this is a section header we should parse
                        if current_header_level >= 2 && self.should_parse_section(&header) {
                            // Save previous section if exists
                            if let Some(section) = current_section.take() {
                                readme_content.sections.push(section);
                            }

                            // Start new section
                            current_section = Some(Section::new(header));
                            current_text.clear();
                        }

                        is_in_header = false;
                    }
                }
                Event::Start(Tag::Link { dest_url, .. }) => {
                    link_url = dest_url.to_string();
                }
                Event::End(TagEnd::Link) => {
                    // Link ended, continue accumulating text for description
                }
                Event::Text(text) => {
                    if is_in_header {
                        header_text.push_str(&text);
                    } else {
                        current_text.push_str(&text);
                        if is_in_list_item {
                            current_item_text.push_str(&text); // Accumulate ALL text for current item
                        }
                        if let Some(ref mut section) = current_section {
                            section.raw_content.push_str(&text);
                        }
                    }
                }
                Event::Code(code) => {
                    if is_in_header {
                        header_text.push_str(&code);
                    } else {
                        current_text.push_str(&code);
                        if is_in_list_item {
                            current_item_text.push_str(&code); // Accumulate code for current item only
                        }
                        if let Some(ref mut section) = current_section {
                            section.raw_content.push_str(&code);
                        }
                    }
                }
                Event::Start(Tag::List(_)) => {
                    if !is_in_header && !current_text.trim().is_empty() {
                        current_text.push('\n');
                    }
                }
                Event::Start(Tag::Item) => {
                    if !is_in_header {
                        current_text.push_str("• ");
                        is_in_list_item = true;
                        current_item_text.clear(); // Clear item text for new list item
                    }
                }
                Event::End(TagEnd::Item) => {
                    if !is_in_header {
                        current_text.push('\n');
                        if let Some(ref mut section) = current_section {
                            section.raw_content.push('\n');
                        }

                        // Process the accumulated text for the current item if we have a GitHub link
                        if is_in_list_item
                            && !link_url.is_empty()
                            && !current_item_text.trim().is_empty()
                            && let Some(ref mut section) = current_section
                            && self.is_github_link(&link_url)
                        {
                            let entry =
                                self.extract_repository_entry(&current_item_text, &link_url);
                            section.entries.push(entry);
                            section.entry_count += 1;
                        }

                        is_in_list_item = false;
                        current_item_text.clear();
                        link_url.clear();
                    }
                }
                Event::SoftBreak | Event::HardBreak => {
                    if !is_in_header {
                        current_text.push('\n');
                        if is_in_list_item {
                            current_item_text.push('\n');
                        }
                        if let Some(ref mut section) = current_section {
                            section.raw_content.push('\n');
                        }
                    }
                }
                _ => {}
            }
        }

        // Save last section
        if let Some(section) = current_section {
            readme_content.sections.push(section);
        }

        // Update metadata
        metadata.total_entries = readme_content.sections.iter().map(|s| s.entry_count).sum();
        readme_content.metadata = metadata;

        // Build search index
        readme_content.search_index = self.build_search_index(&readme_content.sections)?;

        // Validate we have meaningful content
        if readme_content.sections.is_empty() {
            return Err(anyhow!("No valid sections found in markdown content"));
        }

        Ok(readme_content)
    }

    fn should_parse_section(&self, header: &str) -> bool {
        // Skip excluded patterns
        for pattern in &self.exclusion_patterns {
            if header.to_lowercase().contains(&pattern.to_lowercase()) {
                return false;
            }
        }

        // Include known sections
        for section in &self.known_sections {
            if header.to_lowercase().contains(&section.to_lowercase()) {
                return true;
            }
        }

        // Include sections that look like categories (contain certain keywords)
        let category_indicators = vec![
            "tools",
            "libraries",
            "resources",
            "projects",
            "extensions",
            "plugins",
            "integrations",
            "frameworks",
            "platforms",
            "services",
            "utilities",
            "apps",
            "applications",
            "implementations",
            "solutions",
        ];

        for indicator in category_indicators {
            if header.to_lowercase().contains(indicator) {
                return true;
            }
        }

        // Default to including sections (better to have too many than too few)
        true
    }

    /// Check if URL is a GitHub repository link
    fn is_github_link(&self, url: &str) -> bool {
        url.starts_with("https://github.com/") && 
            url.matches('/').count() >= 4 && // github.com/user/repo
            !url.contains("/issues") && 
            !url.contains("/wiki") && 
            !url.contains("/releases")
    }

    /// Extract repository entry from link text and URL
    fn extract_repository_entry(&self, text: &str, url: &str) -> RepositoryEntry {
        let (title, description) = self.split_title_description(text);
        let tags = self.extract_tags(&description);

        RepositoryEntry {
            title,
            url: url.to_string(),
            description,
            tags,
        }
    }

    /// Split text into title and description
    fn split_title_description(&self, text: &str) -> (String, String) {
        let text = text.trim();

        // Handle markdown list item format: "link_text - description"
        // The link_text is the repository name, description follows after " - "
        if let Some(pos) = text.find(" - ") {
            let title = text[..pos].trim().to_string();
            let description = text[pos + 3..].trim().to_string();
            return (title, description);
        }

        // Handle colon separator format: "title: description"
        if let Some(pos) = text.find(": ") {
            let title = text[..pos].trim().to_string();
            let description = text[pos + 2..].trim().to_string();
            return (title, description);
        }

        // If no separator found, use the whole text as title
        (text.to_string(), String::new())
    }

    /// Extract tags from description text
    fn extract_tags(&self, description: &str) -> Vec<String> {
        let mut tags = Vec::new();
        let description_lower = description.to_lowercase();

        // Common tag patterns in awesome lists (removed generic "go" to prevent spurious matches)
        let tag_indicators = vec![
            ("rust", "rust"),
            ("python", "python"),
            ("javascript", "javascript"),
            ("typescript", "typescript"),
            ("golang", "go"), // More specific pattern for Go language
            ("java", "java"),
            ("c++", "cpp"),
            ("cli", "command-line"),
            ("web", "web"),
            ("api", "api"),
            ("tool", "tool"),
            ("library", "library"),
            ("framework", "framework"),
            ("plugin", "plugin"),
            ("extension", "extension"),
        ];

        for (pattern, tag) in tag_indicators {
            if description_lower.contains(pattern) {
                tags.push(tag.to_string());
            }
        }

        tags
    }

    /// Extract theme entries from README content
    pub fn extract_themes_from_readme(&self, readme: &ReadmeContent) -> Result<Vec<ThemeEntry>> {
        let mut theme_entries = Vec::new();

        // Find the "Themes" section
        for section in &readme.sections {
            if section.title.to_lowercase().contains("theme") {
                // Extract theme entries from this section
                for entry in &section.entries {
                    // Ensure it's a GitHub repository
                    if self.is_github_link(&entry.url) {
                        theme_entries.push(ThemeEntry {
                            name: entry.title.clone(),
                            url: entry.url.clone(),
                            description: entry.description.clone(),
                        });
                    }
                }
                break; // Found themes section, stop looking
            }
        }

        if theme_entries.is_empty() {
            return Err(anyhow!("No theme entries found in README"));
        }

        Ok(theme_entries)
    }

    /// Build search index from parsed sections
    fn build_search_index(&self, sections: &[Section]) -> Result<SearchIndex> {
        let mut search_index = SearchIndex::default();

        for (section_idx, section) in sections.iter().enumerate() {
            // Index section title (but not prioritized for repository search)
            // self.index_text(&section.title, section_idx, None, SearchPriority::RawContent, None, &mut search_index);

            // Index entries with priorities
            for (entry_idx, entry) in section.entries.iter().enumerate() {
                // Priority 1: Repository names
                self.index_text(
                    &entry.title,
                    section_idx,
                    Some(entry_idx),
                    SearchPriority::RepositoryName,
                    Some(&entry.url),
                    &mut search_index,
                );

                // Priority 2: Repository descriptions
                if !entry.description.is_empty() {
                    self.index_text(
                        &entry.description,
                        section_idx,
                        Some(entry_idx),
                        SearchPriority::Description,
                        Some(&entry.url),
                        &mut search_index,
                    );
                }

                // Don't index tags or raw content for prioritized search
            }

            // Don't index raw content for prioritized search
            // self.index_text(&section.raw_content, section_idx, None, SearchPriority::RawContent, None, &mut search_index);
        }

        Ok(search_index)
    }

    /// Add text to search index with location information
    fn index_text(
        &self,
        text: &str,
        section_idx: usize,
        entry_idx: Option<usize>,
        priority: SearchPriority,
        github_url: Option<&str>,
        search_index: &mut SearchIndex,
    ) {
        let words = text
            .split_whitespace()
            .filter(|word| word.len() > 2) // Skip very short words
            .map(|word| {
                // Clean word of punctuation
                word.chars()
                    .filter(|c| c.is_alphanumeric() || c.is_whitespace())
                    .collect::<String>()
                    .trim()
                    .to_lowercase()
            })
            .filter(|word| !word.is_empty() && word.len() > 2);

        for word in words {
            let location = SearchLocation {
                section_index: section_idx,
                entry_index: entry_idx,
                line_content: text.to_string(),
                start_pos: 0, // Could be enhanced to track actual positions
                end_pos: text.len(),
                search_priority: priority.clone(),
                github_url: github_url.map(|s| s.to_string()),
            };

            search_index.add_term(word, location);
        }
    }
}

impl Default for ReadmeParser {
    fn default() -> Self {
        Self::new()
    }
}

/// Parser for theme files (alacritty.toml)
pub struct ThemeParser;

impl ThemeParser {
    pub fn new() -> Self {
        Self
    }

    /// Parse alacritty.toml theme file
    pub fn parse_alacritty_theme(&self, theme_name: &str, toml_content: &str) -> Result<Theme> {
        use serde_json::Value;

        // Parse TOML content
        let toml_value: toml::Value =
            toml::from_str(toml_content).map_err(|e| anyhow!("Failed to parse TOML: {}", e))?;

        // Convert to JSON for easier navigation
        let json_str = serde_json::to_string(&toml_value)?;
        let json: Value = serde_json::from_str(&json_str)?;

        // Extract colors from the TOML structure
        let colors = self.extract_colors_from_json(&json)?;

        Ok(Theme {
            name: self.format_theme_name(theme_name),
            description: format!("Theme colors from {theme_name}"),
            source_url: format!(
                "https://github.com/basecamp/omarchy/tree/main/themes/{theme_name}"
            ),
            colors,
        })
    }

    /// Parse alacritty.yml theme file
    pub fn parse_alacritty_yaml(&self, theme_name: &str, yaml_content: &str) -> Result<Theme> {
        use serde_json::Value;
        use serde_yaml;

        // Parse YAML content
        let yaml_value: serde_yaml::Value = serde_yaml::from_str(yaml_content)
            .map_err(|e| anyhow!("Failed to parse YAML: {}", e))?;

        // Convert to JSON for easier navigation
        let json_str = serde_json::to_string(&yaml_value)?;
        let json: Value = serde_json::from_str(&json_str)?;

        // Extract colors from the YAML structure
        let colors = self.extract_colors_from_json(&json)?;

        Ok(Theme {
            name: self.format_theme_name(theme_name),
            description: format!("Theme colors from {theme_name}"),
            source_url: format!("Custom theme: {theme_name}"),
            colors,
        })
    }

    fn extract_colors_from_json(&self, json: &serde_json::Value) -> Result<ThemeColors> {
        // Try to find colors in common TOML structures
        let colors = if let Some(colors) = json.get("colors") {
            colors
        } else if let Some(theme) = json.get("theme") {
            if let Some(colors) = theme.get("colors") {
                colors
            } else {
                theme
            }
        } else {
            json
        };

        // Extract background and foreground
        let background = self
            .extract_color(colors, &["primary", "background"])
            .or_else(|| self.extract_color(colors, &["background"]))
            .unwrap_or_else(|| "#1a1b26".to_string());

        let foreground = self
            .extract_color(colors, &["primary", "foreground"])
            .or_else(|| self.extract_color(colors, &["foreground"]))
            .unwrap_or_else(|| "#a9b1d6".to_string());

        // Extract normal colors
        let normal = self
            .extract_color_palette(colors, "normal")
            .unwrap_or_else(|| self.create_default_palette());

        // Extract bright colors
        let bright = self
            .extract_color_palette(colors, "bright")
            .unwrap_or_else(|| self.create_default_bright_palette());

        Ok(ThemeColors {
            background,
            foreground,
            normal,
            bright,
        })
    }

    fn extract_color(&self, colors: &serde_json::Value, path: &[&str]) -> Option<String> {
        let mut current = colors;
        for key in path {
            current = current.get(key)?;
        }

        // Handle different color formats
        match current {
            serde_json::Value::String(s) => Some(self.normalize_color(s)),
            serde_json::Value::Number(n) => {
                // Convert number to hex color
                n.as_u64().map(|num| format!("#{num:06x}"))
            }
            _ => None,
        }
    }

    fn extract_color_palette(
        &self,
        colors: &serde_json::Value,
        palette_name: &str,
    ) -> Option<ThemeColorPalette> {
        let palette = colors.get(palette_name)?;

        Some(ThemeColorPalette {
            black: self
                .extract_color(palette, &["black"])
                .unwrap_or_else(|| "#000000".to_string()),
            red: self
                .extract_color(palette, &["red"])
                .unwrap_or_else(|| "#ff0000".to_string()),
            green: self
                .extract_color(palette, &["green"])
                .unwrap_or_else(|| "#00ff00".to_string()),
            yellow: self
                .extract_color(palette, &["yellow"])
                .unwrap_or_else(|| "#ffff00".to_string()),
            blue: self
                .extract_color(palette, &["blue"])
                .unwrap_or_else(|| "#0000ff".to_string()),
            magenta: self
                .extract_color(palette, &["magenta"])
                .unwrap_or_else(|| "#ff00ff".to_string()),
            cyan: self
                .extract_color(palette, &["cyan"])
                .unwrap_or_else(|| "#00ffff".to_string()),
            white: self
                .extract_color(palette, &["white"])
                .unwrap_or_else(|| "#ffffff".to_string()),
        })
    }

    fn normalize_color(&self, color: &str) -> String {
        let color = color.trim();

        // Ensure color starts with #
        if color.starts_with('#') {
            color.to_string()
        } else if color.len() == 6 && color.chars().all(|c| c.is_ascii_hexdigit()) {
            format!("#{color}")
        } else {
            // Try to parse as hex without #
            color.to_string()
        }
    }

    fn format_theme_name(&self, name: &str) -> String {
        name.split('-')
            .map(|word| {
                let mut chars = word.chars();
                match chars.next() {
                    None => String::new(),
                    Some(first) => {
                        first.to_uppercase().collect::<String>() + &chars.collect::<String>()
                    }
                }
            })
            .collect::<Vec<_>>()
            .join(" ")
    }

    fn create_default_palette(&self) -> ThemeColorPalette {
        ThemeColorPalette {
            black: "#32344a".to_string(),
            red: "#f7768e".to_string(),
            green: "#9ece6a".to_string(),
            yellow: "#e0af68".to_string(),
            blue: "#7aa2f7".to_string(),
            magenta: "#ad8ee6".to_string(),
            cyan: "#449dab".to_string(),
            white: "#787c99".to_string(),
        }
    }

    fn create_default_bright_palette(&self) -> ThemeColorPalette {
        ThemeColorPalette {
            black: "#444b6a".to_string(),
            red: "#ff7a93".to_string(),
            green: "#b9f27c".to_string(),
            yellow: "#ff9e64".to_string(),
            blue: "#7da6ff".to_string(),
            magenta: "#bb9af7".to_string(),
            cyan: "#0db9d7".to_string(),
            white: "#acb0d0".to_string(),
        }
    }
}

impl Default for ThemeParser {
    fn default() -> Self {
        Self::new()
    }
}

#[cfg(test)]
mod tests {
    use super::*;

    #[test]
    fn test_parse_empty_content() {
        let parser = ReadmeParser::new();
        let result = parser.parse("");
        assert!(result.is_err());
    }

    #[test]
    fn test_parse_basic_structure() {
        let parser = ReadmeParser::new();
        let markdown = r#"
# Awesome Omarchy

## Official Resources

- [Official Website](https://github.com/omarchy/awesome) - The main project website
- [Documentation](https://github.com/omarchy/docs) - Comprehensive documentation

## Development Tools

- [CLI Tool](https://github.com/omarchy/cli) - Command line interface for Omarchy
"#;

        let result = parser.parse(markdown).unwrap();
        assert_eq!(result.metadata.title, "Awesome Omarchy");
        assert_eq!(result.sections.len(), 2);
        assert_eq!(result.sections[0].title, "Official Resources");
        assert_eq!(result.sections[0].entries.len(), 2);
        assert_eq!(result.sections[1].title, "Development Tools");
        assert_eq!(result.sections[1].entries.len(), 1);
    }

    #[test]
    fn test_extract_repository_entry() {
        let parser = ReadmeParser::new();
        let text = "Awesome Tool - A comprehensive tool for doing awesome things";
        let url = "https://github.com/user/awesome-tool";

        let entry = parser.extract_repository_entry(text, url);
        assert_eq!(entry.title, "Awesome Tool");
        assert_eq!(
            entry.description,
            "A comprehensive tool for doing awesome things"
        );
        assert_eq!(entry.url, url);
    }

    #[test]
    fn test_search_functionality() {
        let parser = ReadmeParser::new();
        let markdown = r#"
# Test Awesome List

## Tools

- [Rust CLI](https://github.com/user/rust-cli) - A command line tool written in Rust
- [Python Script](https://github.com/user/python-script) - A useful Python script

## Libraries

- [Web Framework](https://github.com/user/web-framework) - Fast web framework
"#;

        let result = parser.parse(markdown).unwrap();
        let search_results = result.search_index.search("rust");
        assert!(!search_results.is_empty());

        let search_results = result.search_index.search("web");
        assert!(!search_results.is_empty());
    }
}