flerp 0.4.0

CLI tool that does XYZ
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
use crate::app_structs::AppState;
use ratatui::{
    layout::{Alignment, Constraint, Direction, Layout, Rect},
    style::{Color, Modifier, Style},
    text::{Line, Span},
    widgets::{Block, Borders, Clear, Gauge, List, ListItem, Paragraph, Tabs, Wrap},
    Frame,
};

pub fn ui(f: &mut Frame, state: &mut AppState) {
    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .margin(1)
        .constraints([
            Constraint::Length(3), // Header
            Constraint::Length(3), // Tabs
            Constraint::Min(10),   // Content
            Constraint::Length(3), // Footer
        ])
        .split(f.area());

    // Header
    let header = Paragraph::new(vec![
        Line::from(vec![
            Span::styled("", Style::default().fg(Color::Rgb(220, 180, 255))), // Pastel Purple
            Span::styled(
                "Flerp Text Analysis TUI",
                Style::default()
                    .fg(Color::Rgb(170, 200, 255)) // Pastel Blue
                    .add_modifier(Modifier::BOLD),
            ),
        ]),
        Line::from(vec![
            Span::styled("📂 File: ", Style::default().fg(Color::Rgb(150, 150, 150))), // Medium Gray
            Span::styled(
                &state.file_name,
                Style::default().fg(Color::Rgb(200, 220, 255)),
            ), // Light Pastel Blue
        ]),
    ])
    .block(
        Block::default()
            .borders(Borders::ALL)
            .border_style(Style::default().fg(Color::Rgb(170, 200, 255))) // Pastel Blue
            .title(Span::styled(
                " ⚜️ Flerp ⚜️ ", // Changed icon for "elite" feel
                Style::default()
                    .fg(Color::Rgb(158, 210, 243)) // Pastel Pink
                    .add_modifier(Modifier::BOLD),
            )),
    )
    .alignment(Alignment::Center);
    f.render_widget(header, chunks[0]);

    // Tabs
    let tab_titles = vec!["📊 Overview", "🔑 Keywords", "🔎 Search", "📜 Content"];
    let tabs = Tabs::new(tab_titles)
        .block(
            Block::default()
                .borders(Borders::ALL)
                .border_style(Style::default().fg(Color::Rgb(180, 220, 200))) // Pastel Mint
                .title(Span::styled(
                    "🚀 Navigation 🚀",
                    Style::default()
                        .fg(Color::Rgb(180, 220, 200)) // Pastel Mint
                        .add_modifier(Modifier::BOLD),
                )),
        )
        .style(
            Style::default()
                .fg(Color::Rgb(210, 210, 210))
                .bg(Color::Rgb(70, 70, 90)),
        ) // Light Gray on Dark Pastel Purple/Blue
        .highlight_style(
            Style::default()
                .fg(Color::Rgb(255, 220, 180)) // Pastel Peach
                .bg(Color::Rgb(100, 120, 170)) // Medium Pastel Blue
                .add_modifier(Modifier::BOLD),
        )
        .select(state.current_tab)
        .divider(Span::styled(
            " | ",
            Style::default().fg(Color::Rgb(130, 130, 150)),
        )); // Lighter divider
    f.render_widget(tabs, chunks[1]);

    // Content area
    match state.current_tab {
        0 => render_overview(f, chunks[2], state),
        1 => render_keywords(f, chunks[2], state),
        2 => render_search(f, chunks[2], state),
        3 => render_content(f, chunks[2], state),
        _ => {}
    }

    // Footer
    let footer_text = if state.search_mode {
        "▶️ Press Enter to confirm search,  Esc to cancel ◀️"
    } else {
        "🚪 'q' to quit | ⇆ Tab to switch | 🔍 '/' to search | Aa 'c' for case sensitivity"
    };

    let footer = Paragraph::new(footer_text)
        .block(
            Block::default()
                .borders(Borders::ALL)
                .border_style(Style::default().fg(Color::Rgb(255, 200, 220))) // Light Pastel Pink
                .title(Span::styled(
                    "💡 Help 💡",
                    Style::default()
                        .fg(Color::Rgb(255, 200, 220)) // Light Pastel Pink
                        .add_modifier(Modifier::BOLD),
                )),
        )
        .style(Style::default().fg(Color::Rgb(240, 240, 180))) // Pastel Yellow
        .alignment(Alignment::Center);
    f.render_widget(footer, chunks[3]);

    // Search input overlay
    if state.search_mode {
        render_search_input(f, state);
    }
}

fn render_overview(f: &mut Frame, area: Rect, state: &AppState) {
    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(8), // Stats
            Constraint::Min(5),    // Visual stats
        ])
        .split(area);

    // Statistics
    let stats_text = vec![
        Line::from(vec![
            Span::styled("📏 Lines: ", Style::default().fg(Color::Rgb(180, 220, 255))), // Light Pastel Blue
            Span::styled(
                state.structural_analysis.lines.to_string(),
                Style::default().fg(Color::Rgb(255, 230, 200)), // Light Pastel Peach
            ),
        ]),
        Line::from(vec![
            Span::styled("📝 Words: ", Style::default().fg(Color::Rgb(180, 220, 255))), // Light Pastel Blue
            Span::styled(
                state.structural_analysis.words.to_string(),
                Style::default().fg(Color::Rgb(255, 230, 200)), // Light Pastel Peach
            ),
        ]),
        Line::from(vec![
            Span::styled(
                "🔤 Characters: ",
                Style::default().fg(Color::Rgb(180, 220, 255)),
            ), // Light Pastel Blue
            Span::styled(
                state.structural_analysis.characters.to_string(),
                Style::default().fg(Color::Rgb(255, 230, 200)), // Light Pastel Peach
            ),
        ]),
        Line::from(vec![
            Span::styled(
                "📑 Stanzas: ",
                Style::default().fg(Color::Rgb(180, 220, 255)),
            ), // Light Pastel Blue
            Span::styled(
                state.structural_analysis.stanzas.to_string(),
                Style::default().fg(Color::Rgb(255, 230, 200)), // Light Pastel Peach
            ),
        ]),
        Line::from(""),
        Line::from(vec![
            Span::styled(
                "Aa Case Sensitive: ",
                Style::default().fg(Color::Rgb(170, 170, 170)),
            ), // Medium Gray
            Span::styled(
                if state.case_sensitive {
                    "ON ✔️"
                } else {
                    "OFF ❌"
                },
                Style::default().fg(if state.case_sensitive {
                    Color::Rgb(180, 255, 180) // Pastel Green
                } else {
                    Color::Rgb(255, 180, 180) // Pastel Red
                }),
            ),
        ]),
    ];

    let stats = Paragraph::new(stats_text)
        .block(
            Block::default()
                .borders(Borders::ALL)
                .border_style(Style::default().fg(Color::Rgb(180, 255, 180))) // Pastel Green
                .title(Span::styled(
                    "📊 File Statistics 📊",
                    Style::default()
                        .fg(Color::Rgb(180, 255, 180)) // Pastel Green
                        .add_modifier(Modifier::BOLD),
                )),
        )
        .wrap(Wrap { trim: true });
    f.render_widget(stats, chunks[0]);

    // Visual progress bars
    let max_val = state
        .structural_analysis
        .words
        .max(state.structural_analysis.lines)
        .max(100) as f64;

    let words_ratio = (state.structural_analysis.words as f64 / max_val).min(1.0);
    let lines_ratio = (state.structural_analysis.lines as f64 / max_val).min(1.0);

    let progress_chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([Constraint::Length(3), Constraint::Length(3)])
        .split(chunks[1]);

    let words_gauge = Gauge::default()
        .block(
            Block::default()
                .borders(Borders::ALL)
                .title("Words Progress")
                .border_style(Style::default().fg(Color::Rgb(176, 220, 160))), // Pastel Pink
        )
        .gauge_style(
            Style::default()
                .fg(Color::Rgb(176, 220, 160)) // Pastel Pink
                .bg(Color::Rgb(80, 80, 100)) // Darker Pastel Purple/Blue
                .add_modifier(Modifier::ITALIC),
        )
        .ratio(words_ratio)
        .label(format!("{:.0}%", words_ratio * 100.0));
    f.render_widget(words_gauge, progress_chunks[0]);

    let lines_gauge = Gauge::default()
        .block(
            Block::default()
                .borders(Borders::ALL)
                .title("Lines Progress")
                .border_style(Style::default().fg(Color::Rgb(180, 220, 200))), // Pastel Mint
        )
        .gauge_style(
            Style::default()
                .fg(Color::Rgb(180, 220, 200)) // Pastel Mint
                .bg(Color::Rgb(80, 80, 100)) // Darker Pastel Purple/Blue
                .add_modifier(Modifier::ITALIC),
        )
        .ratio(lines_ratio)
        .label(format!("{:.0}%", lines_ratio * 100.0));
    f.render_widget(lines_gauge, progress_chunks[1]);
}

fn render_keywords(f: &mut Frame, area: Rect, state: &AppState) {
    let items: Vec<ListItem> = state
        .keywords
        .iter()
        .enumerate()
        .map(|(i, (keyword, count))| {
            ListItem::new(Line::from(vec![
                Span::styled(
                    format!("{:02}. ", i + 1),
                    Style::default().fg(Color::Rgb(150, 150, 150)), // Medium Gray
                ),
                Span::styled(
                    keyword,
                    Style::default()
                        .fg(Color::Rgb(180, 255, 180)) // Pastel Green
                        .add_modifier(Modifier::BOLD),
                ),
                Span::styled(
                    format!(" ({})", count),
                    Style::default().fg(Color::Rgb(180, 220, 255)), // Light Pastel Blue
                ),
            ]))
        })
        .collect();

    let keywords_list = List::new(items)
        .block(
            Block::default()
                .borders(Borders::ALL)
                .border_style(Style::default().fg(Color::Rgb(255, 230, 200))) // Light Pastel Peach
                .title(Span::styled(
                    "🔑 Top Keywords 🔑",
                    Style::default()
                        .fg(Color::Rgb(255, 230, 200)) // Light Pastel Peach
                        .add_modifier(Modifier::BOLD),
                )),
        )
        .highlight_style(
            Style::default()
                .bg(Color::Rgb(255, 220, 180)) // Pastel Peach
                .fg(Color::Rgb(60, 60, 80)) // Dark Purple/Blue
                .add_modifier(Modifier::BOLD),
        )
        .highlight_symbol(">> ");

    f.render_widget(keywords_list, area);
}

fn render_search(f: &mut Frame, area: Rect, state: &mut AppState) {
    let chunks = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Length(3), // Search info
            Constraint::Min(5),    // Results
        ])
        .split(area);

    // Search info
    let search_info = Paragraph::new(vec![
        Line::from(vec![
            Span::styled("🔍 Query: ", Style::default().fg(Color::Rgb(170, 200, 255))), // Pastel Blue
            Span::styled(
                &state.search_query,
                Style::default().fg(Color::Rgb(255, 220, 180)),
            ), // Pastel Peach
        ]),
        Line::from(vec![
            Span::styled(
                "🎯 Results: ",
                Style::default().fg(Color::Rgb(170, 200, 255)),
            ), // Pastel Blue
            Span::styled(
                state.search_results.len().to_string(),
                Style::default().fg(Color::Rgb(180, 255, 180)), // Pastel Green
            ),
        ]),
    ])
    .block(
        Block::default()
            .borders(Borders::ALL)
            .border_style(Style::default().fg(Color::Rgb(220, 180, 255))) // Pastel Purple
            .title(Span::styled(
                "🔎 Search Info 🔎",
                Style::default()
                    .fg(Color::Rgb(220, 180, 255)) // Pastel Purple
                    .add_modifier(Modifier::BOLD),
            )),
    );
    f.render_widget(search_info, chunks[0]);

    // Results
    if state.search_results.is_empty() {
        let no_results = Paragraph::new("🤷 No results found. Press '/' to start searching. 🤷")
            .block(
                Block::default()
                    .borders(Borders::ALL)
                    .border_style(Style::default().fg(Color::Rgb(120, 120, 120))) // Dark Gray
                    .title("Search Results"),
            )
            .style(Style::default().fg(Color::Rgb(150, 150, 150))) // Medium Gray
            .alignment(Alignment::Center);
        f.render_widget(no_results, chunks[1]);
    } else {
        let items: Vec<ListItem> = state
            .search_results
            .iter()
            .enumerate()
            .map(|(i, line)| {
                ListItem::new(Line::from(vec![
                    Span::styled(
                        format!("{:02}. ", i + 1),
                        Style::default().fg(Color::Rgb(150, 150, 150)), // Medium Gray
                    ),
                    Span::styled(line, Style::default().fg(Color::Rgb(220, 220, 220))), // Light Gray
                ]))
            })
            .collect();

        let results_list = List::new(items)
            .block(
                Block::default()
                    .borders(Borders::ALL)
                    .border_style(Style::default().fg(Color::Rgb(180, 255, 180))) // Pastel Green
                    .title(Span::styled(
                        "📜 Search Results 📜",
                        Style::default()
                            .fg(Color::Rgb(180, 255, 180)) // Pastel Green
                            .add_modifier(Modifier::BOLD),
                    )),
            )
            .highlight_style(
                Style::default()
                    .bg(Color::Rgb(180, 255, 180)) // Pastel Green
                    .fg(Color::Rgb(60, 60, 80)) // Dark Purple/Blue
                    .add_modifier(Modifier::BOLD),
            )
            .highlight_symbol("-> ");

        f.render_stateful_widget(results_list, chunks[1], &mut state.result_list_state);
    }
}

fn render_content(f: &mut Frame, area: Rect, state: &AppState) {
    let content = if state.file_content.is_empty() {
        "🚫 No file loaded. Load a file to see its content here. 🚫".to_string()
    } else {
        // İlk 50 satırı göster
        state
            .file_content
            .lines()
            .take(50)
            .collect::<Vec<_>>()
            .join("\n")
    };

    let paragraph = Paragraph::new(content)
        .block(
            Block::default()
                .borders(Borders::ALL)
                .border_style(Style::default().fg(Color::Rgb(200, 220, 255))) // Light Pastel Blue
                .title(Span::styled(
                    "📄 File Content (First 50 lines) 📄",
                    Style::default()
                        .fg(Color::Rgb(200, 220, 255)) // Light Pastel Blue
                        .add_modifier(Modifier::BOLD),
                )),
        )
        .wrap(Wrap { trim: true })
        .style(Style::default().fg(Color::Rgb(210, 210, 210))); // Light Gray

    f.render_widget(paragraph, area);
}

fn render_search_input(f: &mut Frame, state: &AppState) {
    let popup_area = centered_rect(60, 25, f.area());
    f.render_widget(Clear, popup_area);
    let input_text = format!("Search 🔎: {}_", state.search_query);
    let input = Paragraph::new(input_text)
        .style(
            Style::default()
                .fg(Color::Rgb(255, 220, 180)) // Pastel Peach
                .bg(Color::Rgb(60, 60, 80)), // Dark Pastel Purple/Blue
        )
        .block(
            Block::default()
                .borders(Borders::ALL)
                .title(Span::styled(
                    "✏️ Enter Search Query ✏️",
                    Style::default()
                        .fg(Color::Rgb(255, 180, 220)) // Pastel Pink
                        .add_modifier(Modifier::BOLD),
                ))
                .border_style(Style::default().fg(Color::Rgb(255, 180, 220))), // Pastel Pink
        )
        .alignment(Alignment::Center);
    f.render_widget(input, popup_area);
}

fn centered_rect(percent_x: u16, percent_y: u16, r: Rect) -> Rect {
    let popup_layout = Layout::default()
        .direction(Direction::Vertical)
        .constraints([
            Constraint::Percentage((100 - percent_y) / 2),
            Constraint::Percentage(percent_y),
            Constraint::Percentage((100 - percent_y) / 2),
        ])
        .split(r);

    Layout::default()
        .direction(Direction::Horizontal)
        .constraints([
            Constraint::Percentage((100 - percent_x) / 2),
            Constraint::Percentage(percent_x),
            Constraint::Percentage((100 - percent_x) / 2),
        ])
        .split(popup_layout[1])[1]
}