arct-tui 0.2.2

Terminal UI for Arc Academy Terminal - interactive shell learning interface
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
//! Lesson panel - displays interactive lessons and tracks progress

use crate::icons;
use crate::theme::Theme;
use arct_core::{Lesson, LessonStep, StepType, ValidationResult, LessonValidator};
use ratatui::{
    layout::Rect,
    style::Style,
    text::{Line, Span},
    widgets::{Block, Borders, Paragraph, Wrap},
    Frame,
};

/// Lesson panel state
pub struct LessonPanel {
    pub current_lesson: Option<Lesson>,
    current_step_index: usize,
    user_input: String,
    validator: LessonValidator,
    last_validation: Option<ValidationResult>,
    completed_steps: Vec<usize>,
}

impl LessonPanel {
    pub fn new() -> Self {
        Self {
            current_lesson: None,
            current_step_index: 0,
            user_input: String::new(),
            validator: LessonValidator::new(),
            last_validation: None,
            completed_steps: Vec::new(),
        }
    }

    /// Load a lesson
    pub fn load_lesson(&mut self, lesson: Lesson) {
        self.current_lesson = Some(lesson);
        self.current_step_index = 0;
        self.user_input.clear();
        self.last_validation = None;
        self.completed_steps.clear();
    }

    /// Get current step
    fn current_step(&self) -> Option<&LessonStep> {
        self.current_lesson
            .as_ref()
            .and_then(|lesson| lesson.steps.get(self.current_step_index))
    }

    /// Check if user input is valid for current step
    pub fn validate_current_step(&mut self, input: &str) -> ValidationResult {
        if let Some(step) = self.current_step() {
            let result = match &step.step_type {
                StepType::CommandExercise {
                    expected_command,
                    validation,
                    success_message,
                } => {
                    let validation_result =
                        self.validator.validate_command(input, expected_command, validation);

                    if validation_result.is_success() {
                        ValidationResult::Success {
                            message: success_message.clone(),
                        }
                    } else {
                        validation_result
                    }
                }
                StepType::MultipleChoice {
                    correct_index, ..
                } => {
                    if let Ok(choice) = input.parse::<usize>() {
                        self.validator.validate_multiple_choice(choice, *correct_index)
                    } else {
                        ValidationResult::Failure {
                            message: "Please enter a number.".to_string(),
                            hint: None,
                        }
                    }
                }
                StepType::Information { .. } => {
                    // Information steps just need any key press to continue
                    ValidationResult::Success {
                        message: "Continue to next step.".to_string(),
                    }
                }
                _ => ValidationResult::Success {
                    message: "Continue.".to_string(),
                },
            };

            self.last_validation = Some(result.clone());
            result
        } else {
            ValidationResult::Failure {
                message: "No active step.".to_string(),
                hint: None,
            }
        }
    }

    /// Move to next step
    pub fn next_step(&mut self) -> bool {
        if let Some(lesson) = &self.current_lesson {
            if !self.completed_steps.contains(&self.current_step_index) {
                self.completed_steps.push(self.current_step_index);
            }

            if self.current_step_index + 1 < lesson.steps.len() {
                self.current_step_index += 1;
                self.user_input.clear();
                self.last_validation = None;
                true
            } else {
                false // Lesson complete
            }
        } else {
            false
        }
    }

    /// Move to previous step
    pub fn previous_step(&mut self) {
        if self.current_step_index > 0 {
            self.current_step_index -= 1;
            self.user_input.clear();
            self.last_validation = None;
        }
    }

    /// Get completion percentage
    pub fn completion_percentage(&self) -> f32 {
        if let Some(lesson) = &self.current_lesson {
            let total = lesson.steps.len();
            if total == 0 {
                return 0.0;
            }
            (self.completed_steps.len() as f32 / total as f32) * 100.0
        } else {
            0.0
        }
    }

    /// Render the lesson panel
    pub fn render(&self, frame: &mut Frame, area: Rect, focused: bool, theme: &Theme) {
        let border_style = if focused {
            theme.style_border_focused()
        } else {
            theme.style_border()
        };

        if let Some(lesson) = &self.current_lesson {
            // Render current step (includes header info in title)
            if let Some(step) = self.current_step() {
                self.render_step(frame, area, lesson, step, theme, border_style);
            }
        } else {
            // No lesson loaded - show lesson selection screen
            self.render_lesson_selection(frame, area, theme, border_style);
        }
    }

    fn render_step(
        &self,
        frame: &mut Frame,
        area: Rect,
        lesson: &Lesson,
        step: &LessonStep,
        theme: &Theme,
        border_style: Style,
    ) {
        let mut lines = Vec::new();

        // Step title - always show
        lines.push(Line::from(vec![
            Span::styled(format!("Step {}: ", step.step_number), theme.style_accent()),
            Span::styled(&step.title, theme.style_header()),
        ]));

        // Render based on step type
        match &step.step_type {
            StepType::CommandExercise { .. } => {
                // HOW TO instruction
                lines.push(Line::from(vec![
                    Span::styled("▶ Type command in Shell → Enter", theme.style_warning()),
                ]));
                // Task instruction
                if !step.instruction.is_empty() {
                    lines.push(Line::from(vec![
                        Span::styled("Task: ", theme.style_accent()),
                        Span::styled(&step.instruction, theme.style_normal()),
                    ]));
                }
                // Hint
                if let Some(hint) = &step.hint {
                    lines.push(Line::from(vec![
                        icons::hint(),
                        Span::styled(hint, theme.style_dim()),
                    ]));
                }
                // Validation result
                if let Some(validation) = &self.last_validation {
                    match validation {
                        ValidationResult::Success { message } => {
                            lines.push(Line::from(vec![
                                icons::success(),
                                Span::styled(message, theme.style_success()),
                            ]));
                        }
                        ValidationResult::Failure { message, hint } => {
                            lines.push(Line::from(vec![
                                icons::error(),
                                Span::styled(message, theme.style_error()),
                            ]));
                            if let Some(h) = hint {
                                lines.push(Line::from(vec![
                                    icons::hint(),
                                    Span::styled(h, theme.style_dim()),
                                ]));
                            }
                        }
                        ValidationResult::Partial { message, progress } => {
                            lines.push(Line::from(vec![
                                icons::warning(),
                                Span::styled(
                                    format!("{} ({:.0}%)", message, progress),
                                    theme.style_warning(),
                                ),
                            ]));
                        }
                    }
                }
            }
            StepType::MultipleChoice {
                question,
                options,
                explanation,
                ..
            } => {
                // HOW TO + Question combined
                lines.push(Line::from(vec![
                    Span::styled(format!("▶ Type 0-{}", options.len() - 1), theme.style_warning()),
                    icons::question(),
                    Span::styled(question, theme.style_normal()),
                ]));
                // Options
                for (i, option) in options.iter().enumerate() {
                    lines.push(Line::from(vec![
                        Span::styled(format!("  {}. ", i), theme.style_accent()),
                        Span::styled(option, theme.style_normal()),
                    ]));
                }
                // Show explanation if answered correctly
                if let Some(ValidationResult::Success { .. }) = &self.last_validation {
                    lines.push(Line::from(vec![
                        icons::success(),
                        Span::styled(explanation, theme.style_success()),
                    ]));
                }
            }
            StepType::Information { content } => {
                // HOW TO
                lines.push(Line::from(vec![
                    Span::styled("▶ Press Enter to continue", theme.style_warning()),
                ]));
                // Display information
                for line in content.lines() {
                    lines.push(Line::from(vec![Span::styled(line, theme.style_normal())]));
                }
            }
            StepType::FillInBlank { template, .. } => {
                // HOW TO
                lines.push(Line::from(vec![
                    Span::styled("▶ Fill blank, type in Shell → Enter", theme.style_warning()),
                ]));
                lines.push(Line::from(vec![
                    icons::note(),
                    Span::styled(&step.instruction, theme.style_normal()),
                ]));
                lines.push(Line::from(vec![
                    Span::styled("Template: ", theme.style_accent()),
                    Span::styled(template, theme.style_dim()),
                ]));
            }
            StepType::Practice { goal, hints, .. } => {
                // HOW TO + Goal combined
                lines.push(Line::from(vec![
                    Span::styled("▶ Try commands → ", theme.style_warning()),
                    icons::target(),
                    Span::styled(goal, theme.style_normal()),
                ]));
                // Hints inline
                if !hints.is_empty() {
                    for hint in hints {
                        lines.push(Line::from(vec![
                            icons::hint(),
                            Span::styled(hint, theme.style_dim()),
                        ]));
                    }
                }
            }
        }

        // Build title with progress info (compact)
        let progress = self.completion_percentage();
        let title = format!(
            " {} {}/{} | {:.0}% ",
            lesson.title,
            self.current_step_index + 1,
            lesson.steps.len(),
            progress
        );

        let block = Block::default()
            .title(title)
            .borders(Borders::ALL)
            .border_style(border_style)
            .style(theme.style_block());

        let paragraph = Paragraph::new(lines)
            .block(block)
            .wrap(Wrap { trim: false });
        frame.render_widget(paragraph, area);
    }

    fn render_lesson_selection(
        &self,
        frame: &mut Frame,
        area: Rect,
        theme: &Theme,
        border_style: Style,
    ) {
        let block = Block::default()
            .title(format!(" {}Interactive Lessons ", icons::lesson().content))
            .borders(Borders::ALL)
            .border_style(border_style)
            .style(theme.style_block());  // Set background for light themes

        let paragraph = Paragraph::new(vec![
            Line::from(""),
            Line::from(vec![
                icons::welcome(),
                Span::styled("Welcome to Interactive Lessons!", theme.style_accent()),
            ]),
            Line::from(""),
            Line::from(vec![
                Span::styled("🎓 ", theme.style_accent()),
                Span::styled("10 comprehensive lessons", theme.style_normal()),
                Span::styled(" available", theme.style_dim()),
            ]),
            Line::from(vec![
                Span::styled("🏆 ", theme.style_accent()),
                Span::styled("Track progress & earn achievements", theme.style_normal()),
            ]),
            Line::from(vec![
                Span::styled("🛡️  ", theme.style_accent()),
                Span::styled("Safe virtual filesystem", theme.style_normal()),
                Span::styled(" for hands-on practice", theme.style_dim()),
            ]),
            Line::from(""),
            Line::from(""),
            Line::from(vec![
                Span::styled("📚 Select a lesson:", theme.style_header()),
            ]),
            Line::from(""),
            Line::from(vec![
                Span::styled("  Press ", theme.style_dim()),
                Span::styled("m", theme.style_accent()),
                Span::styled(" to open the lesson menu", theme.style_dim()),
            ]),
            Line::from(vec![
                Span::styled("  Use ", theme.style_dim()),
                Span::styled("↑/↓", theme.style_accent()),
                Span::styled(" or ", theme.style_dim()),
                Span::styled("1-9,0", theme.style_accent()),
                Span::styled(" to select", theme.style_dim()),
            ]),
            Line::from(vec![
                Span::styled("  Press ", theme.style_dim()),
                Span::styled("Enter", theme.style_accent()),
                Span::styled(" to start learning!", theme.style_dim()),
            ]),
            Line::from(""),
            Line::from(""),
            Line::from(vec![
                Span::styled("💡 Tip: ", theme.style_accent()),
                Span::styled("Complete lessons to unlock achievements", theme.style_dim()),
            ]),
            Line::from(vec![
                Span::styled("       and build your learning streak!", theme.style_dim()),
            ]),
        ])
        .block(block)
        .wrap(Wrap { trim: false });

        frame.render_widget(paragraph, area);
    }
}

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