mathypad 0.1.7

A smart TUI calculator that understands units and makes complex calculations simple.
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
//! Application state and core logic

use crate::{
    Mode,
    expression::{evaluate_with_variables, update_line_references_in_text},
};
use std::collections::HashMap;
use std::time::Instant;

/// Animation state for a result line
#[derive(Clone, Debug)]
pub struct ResultAnimation {
    pub start_time: Instant,
    pub duration_ms: u64,
    pub animation_type: AnimationType,
}

#[derive(Clone, Debug)]
pub enum AnimationType {
    FadeIn,
}

impl ResultAnimation {
    pub fn new_fade_in() -> Self {
        Self {
            start_time: Instant::now(),
            duration_ms: 250, // 250ms fade-in
            animation_type: AnimationType::FadeIn,
        }
    }

    /// Get the animation progress (0.0 to 1.0)
    pub fn progress(&self) -> f32 {
        let elapsed = self.start_time.elapsed().as_millis() as f32;
        let progress = elapsed / self.duration_ms as f32;
        progress.min(1.0)
    }

    /// Check if animation is complete
    pub fn is_complete(&self) -> bool {
        self.progress() >= 1.0
    }

    /// Get the opacity for fade-in animation (0.0 to 1.0)
    pub fn opacity(&self) -> f32 {
        match self.animation_type {
            AnimationType::FadeIn => {
                let progress = self.progress();
                // Smooth ease-out animation
                1.0 - (1.0 - progress).powi(3)
            }
        }
    }
}

/// Main application state for the mathematical notepad
pub struct App {
    pub text_lines: Vec<String>,
    pub cursor_line: usize,
    pub cursor_col: usize,
    pub scroll_offset: usize,
    pub results: Vec<Option<String>>,
    pub variables: HashMap<String, String>, // variable_name -> value_string
    pub mode: Mode,
    pub result_animations: Vec<Option<ResultAnimation>>, // Animation state for each result
}

impl Default for App {
    fn default() -> App {
        App {
            text_lines: vec![String::new()],
            cursor_line: 0,
            cursor_col: 0,
            scroll_offset: 0,
            results: vec![None],
            variables: HashMap::new(),
            mode: Mode::Insert,            // Start in insert mode
            result_animations: vec![None], // Start with no animations
        }
    }
}

impl App {
    #[cfg(test)]
    pub fn test_scenario_line_splitting(&mut self) -> (String, String) {
        // Set up the scenario: "5" on line 1, "line1 + 1" on line 2
        self.text_lines = vec!["5".to_string(), "line1 + 1".to_string()];
        self.results = vec![None, None];
        self.cursor_line = 0;
        self.cursor_col = 0; // Position cursor OVER the "5" (at beginning)

        // Record the original state
        let before = format!(
            "Line 1: '{}', Line 2: '{}'",
            self.text_lines[0], self.text_lines[1]
        );

        // Hit enter when cursor is over the 5 (actually after it)
        self.new_line();

        // Record the new state
        let after = format!(
            "Line 1: '{}', Line 2: '{}', Line 3: '{}'",
            self.text_lines.get(0).unwrap_or(&"".to_string()),
            self.text_lines.get(1).unwrap_or(&"".to_string()),
            self.text_lines.get(2).unwrap_or(&"".to_string())
        );

        (before, after)
    }
    /// Insert a character at the current cursor position
    pub fn insert_char(&mut self, c: char) {
        if self.cursor_line < self.text_lines.len() {
            self.text_lines[self.cursor_line].insert(self.cursor_col, c);
            self.cursor_col += 1;
            self.update_result(self.cursor_line);
        }
    }

    /// Delete the character before the cursor
    pub fn delete_char(&mut self) {
        if self.cursor_line < self.text_lines.len() {
            if self.cursor_col > 0 {
                // Delete character within the current line
                self.text_lines[self.cursor_line].remove(self.cursor_col - 1);
                self.cursor_col -= 1;
                self.update_result(self.cursor_line);
            } else if self.cursor_line > 0 {
                // Cursor is at beginning of line - merge with previous line
                let current_line = self.text_lines.remove(self.cursor_line);
                self.results.remove(self.cursor_line);

                // Remove corresponding animation if it exists
                if self.cursor_line < self.result_animations.len() {
                    self.result_animations.remove(self.cursor_line);
                }

                // Check if the previous line is empty - if so, we conceptually want to delete
                // the previous line rather than the current line
                let prev_line_empty = self.text_lines[self.cursor_line - 1].is_empty();

                if prev_line_empty && !current_line.is_empty() {
                    // Previous line is empty, current line has content
                    // Delete the previous line (conceptually what the user wants)
                    self.text_lines[self.cursor_line - 1] = current_line;
                    self.update_line_references_for_deletion(self.cursor_line - 1);
                    self.cursor_line -= 1;
                    self.cursor_col = 0;
                } else {
                    // Normal case: merge current line into previous line
                    self.update_line_references_for_deletion(self.cursor_line);
                    self.cursor_line -= 1;
                    self.cursor_col = self.text_lines[self.cursor_line].len();
                    self.text_lines[self.cursor_line].push_str(&current_line);
                }

                // Re-evaluate all lines after deletion to ensure line references resolve correctly
                self.update_result(self.cursor_line);
                for i in (self.cursor_line + 1)..self.text_lines.len() {
                    self.update_result(i);
                }
            }
        }
    }

    /// Delete the word before the cursor (Ctrl+W behavior)
    pub fn delete_word(&mut self) {
        if self.cursor_line < self.text_lines.len() && self.cursor_col > 0 {
            let line = &self.text_lines[self.cursor_line];
            let mut new_col = self.cursor_col;

            // Skip trailing whitespace
            while new_col > 0 && line.chars().nth(new_col - 1).unwrap_or(' ').is_whitespace() {
                new_col -= 1;
            }

            // Delete word characters (alphanumeric and underscore)
            while new_col > 0 {
                let ch = line.chars().nth(new_col - 1).unwrap_or(' ');
                if ch.is_alphanumeric() || ch == '_' {
                    new_col -= 1;
                } else {
                    break;
                }
            }

            // If no word was found, delete non-word characters until whitespace
            if new_col == self.cursor_col {
                while new_col > 0 {
                    let ch = line.chars().nth(new_col - 1).unwrap_or(' ');
                    if ch.is_whitespace() || ch.is_alphanumeric() || ch == '_' {
                        break;
                    }
                    new_col -= 1;
                }
            }

            // Delete the characters from new_col to cursor_col
            if new_col < self.cursor_col {
                self.text_lines[self.cursor_line].drain(new_col..self.cursor_col);
                self.cursor_col = new_col;
                self.update_result(self.cursor_line);
            }
        }
    }

    /// Insert a new line at the cursor position
    pub fn new_line(&mut self) {
        if self.cursor_line < self.text_lines.len() {
            let current_line = self.text_lines[self.cursor_line].clone();
            let (left, right) = current_line.split_at(self.cursor_col);
            self.text_lines[self.cursor_line] = left.to_string();
            self.text_lines
                .insert(self.cursor_line + 1, right.to_string());
            self.results.insert(self.cursor_line + 1, None);

            // Insert corresponding empty animation slot
            if self.cursor_line + 1 < self.result_animations.len() {
                self.result_animations.insert(self.cursor_line + 1, None);
            } else {
                // Ensure animations vector is large enough
                while self.result_animations.len() <= self.cursor_line + 1 {
                    self.result_animations.push(None);
                }
            }

            // Handle line reference updates for insertion
            let insertion_point = self.cursor_line + 1; // 0-based index of newly inserted line

            // Handle line reference updates for line splitting
            // When splitting, we need to consider where content ends up
            let left_empty = left.trim().is_empty();
            let right_empty = right.trim().is_empty();

            if left_empty && !right_empty {
                // Content moved from cursor_line to insertion_point
                // Use combined update that handles both content move and position shifts
                self.update_line_references_for_line_split_with_content_move(
                    self.cursor_line,
                    insertion_point,
                );
            } else {
                // Standard insertion: just shift references
                self.update_line_references_for_standard_insertion(insertion_point);
            }

            self.cursor_line += 1;
            self.cursor_col = 0;

            // Make sure to evaluate all lines in the correct order
            // First evaluate the lines that were directly affected by the split
            self.update_result(self.cursor_line - 1); // Line 0
            self.update_result(self.cursor_line); // Line 1

            // Then re-evaluate any lines that had their references updated
            // This ensures line references can resolve correctly
            for i in (self.cursor_line + 1)..self.text_lines.len() {
                self.update_result(i);
            }
        }
    }

    /// Move cursor up one line
    pub fn move_cursor_up(&mut self) {
        if self.cursor_line > 0 {
            self.cursor_line -= 1;
            self.cursor_col = self.cursor_col.min(self.text_lines[self.cursor_line].len());
        }
    }

    /// Move cursor down one line
    pub fn move_cursor_down(&mut self) {
        if self.cursor_line + 1 < self.text_lines.len() {
            self.cursor_line += 1;
            self.cursor_col = self.cursor_col.min(self.text_lines[self.cursor_line].len());
        }
    }

    /// Move cursor left one character
    pub fn move_cursor_left(&mut self) {
        if self.cursor_col > 0 {
            self.cursor_col -= 1;
        }
    }

    /// Move cursor right one character
    pub fn move_cursor_right(&mut self) {
        if self.cursor_line < self.text_lines.len() {
            self.cursor_col = (self.cursor_col + 1).min(self.text_lines[self.cursor_line].len());
        }
    }

    /// Update the calculation result for a given line
    pub fn update_result(&mut self, line_index: usize) {
        if line_index < self.text_lines.len() && line_index < self.results.len() {
            let line = &self.text_lines[line_index];
            let (result, variable_assignment) =
                evaluate_with_variables(line, &self.variables, &self.results, line_index);

            // Check if result has changed to trigger animation
            let result_changed = self.results[line_index] != result;

            // If this is a variable assignment, store it and re-evaluate dependent lines
            if let Some((var_name, var_value)) = variable_assignment {
                // Check if this variable assignment actually changed the value
                let variable_changed = self.variables.get(&var_name) != Some(&var_value);

                self.variables.insert(var_name.clone(), var_value);

                // If the variable changed, re-evaluate all lines that might use this variable
                if variable_changed {
                    self.results[line_index] = result.clone();
                    if result_changed && result.is_some() {
                        self.start_result_animation(line_index);
                    }
                    self.re_evaluate_dependent_lines(&var_name, line_index);
                    return;
                }
            }

            self.results[line_index] = result.clone();

            // Start fade-in animation if result changed and is not None
            if result_changed && result.is_some() {
                self.start_result_animation(line_index);
            }
        } else {
            // This should never happen in normal operation, but let's be defensive
            eprintln!(
                "Warning: Attempted to update result for invalid line index {}",
                line_index
            );
        }
    }

    /// Re-evaluate all lines that might depend on the given variable
    fn re_evaluate_dependent_lines(&mut self, changed_variable: &str, assignment_line: usize) {
        // Re-evaluate all lines after the assignment line that might use this variable
        for line_idx in (assignment_line + 1)..self.text_lines.len() {
            if line_idx < self.results.len() {
                let line = &self.text_lines[line_idx];

                // Check if this line contains the variable name
                // This is a simple heuristic - we could make it more sophisticated
                if line.contains(changed_variable) {
                    let (result, nested_assignment) =
                        evaluate_with_variables(line, &self.variables, &self.results, line_idx);

                    // Handle nested variable assignments (variables that depend on other variables)
                    if let Some((nested_var_name, nested_var_value)) = nested_assignment {
                        let nested_changed =
                            self.variables.get(&nested_var_name) != Some(&nested_var_value);
                        self.variables
                            .insert(nested_var_name.clone(), nested_var_value);

                        // If this nested assignment changed, recursively update its dependents
                        if nested_changed {
                            self.results[line_idx] = result;
                            self.re_evaluate_dependent_lines(&nested_var_name, line_idx);
                        }
                    } else {
                        self.results[line_idx] = result;
                    }
                }
            }
        }
    }

    /// Update line references in all lines when a line is deleted
    /// All references > deleted_line need to be decremented by 1
    /// References to the deleted line become invalid
    fn update_line_references_for_deletion(&mut self, deleted_line: usize) {
        for i in 0..self.text_lines.len() {
            let updated_text =
                update_line_references_in_text(&self.text_lines[i], deleted_line, -1);
            if updated_text != self.text_lines[i] {
                self.text_lines[i] = updated_text;
                // Re-evaluate this line since its content changed
                self.update_result(i);
            }
        }
    }

    /// Update line references in all lines when a new line is inserted
    /// All references >= insertion_point need to be incremented by 1
    fn update_line_references_for_standard_insertion(&mut self, insertion_point: usize) {
        for i in 0..self.text_lines.len() {
            let updated_text =
                update_line_references_in_text(&self.text_lines[i], insertion_point, 1);
            if updated_text != self.text_lines[i] {
                self.text_lines[i] = updated_text;
                // Re-evaluate this line since its content changed
                self.update_result(i);
            }
        }
    }

    /// Combined update for line splitting with content movement
    /// Handles both content following and position-based shifts in one pass
    fn update_line_references_for_line_split_with_content_move(
        &mut self,
        content_from: usize,
        insertion_point: usize,
    ) {
        use crate::expression::extract_line_references;

        for i in 0..self.text_lines.len() {
            let references = extract_line_references(&self.text_lines[i]);
            let mut updated_text = self.text_lines[i].clone();

            // Process references in reverse order to maintain correct string positions
            for (start_pos, end_pos, line_num) in references.into_iter().rev() {
                let new_line_num = if line_num == content_from {
                    // Content moved from content_from to insertion_point
                    insertion_point
                } else if line_num >= insertion_point {
                    // Standard position shift for lines >= insertion_point
                    line_num + 1
                } else {
                    // Lines before insertion_point stay unchanged
                    line_num
                };

                if new_line_num != line_num {
                    let new_ref = format!("line{}", new_line_num + 1); // +1 for 1-based display
                    updated_text.replace_range(start_pos..end_pos, &new_ref);
                }
            }

            if updated_text != self.text_lines[i] {
                self.text_lines[i] = updated_text;
                // Re-evaluate this line since its content changed
                self.update_result(i);
            }
        }
    }

    /// Recalculate all lines in the notebook
    pub fn recalculate_all(&mut self) {
        // Clear variables to ensure fresh calculation
        self.variables.clear();

        // Recalculate each line in order
        for i in 0..self.text_lines.len() {
            self.update_result(i);
        }
    }

    /// Start a fade-in animation for a result
    fn start_result_animation(&mut self, line_index: usize) {
        // Ensure the animations vector is large enough
        while self.result_animations.len() <= line_index {
            self.result_animations.push(None);
        }

        // Only start animation if there's actually a result
        if line_index < self.results.len() && self.results[line_index].is_some() {
            self.result_animations[line_index] = Some(ResultAnimation::new_fade_in());
        }
    }

    /// Update all animations and remove completed ones
    pub fn update_animations(&mut self) {
        for animation in &mut self.result_animations {
            if let Some(anim) = animation {
                if anim.is_complete() {
                    *animation = None;
                }
            }
        }
    }

    /// Get the animation for a specific line
    pub fn get_result_animation(&self, line_index: usize) -> Option<&ResultAnimation> {
        self.result_animations.get(line_index)?.as_ref()
    }
}

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

    #[test]
    fn test_line_splitting_with_line_references() {
        let mut app = App::default();
        let (before, after) = app.test_scenario_line_splitting();

        println!("Before: {}", before);
        println!("After: {}", after);

        // Expected behavior analysis:
        // Original: Line 1: "5", Line 2: "line1 + 1"
        // After hitting enter at position 1 in "5":
        // Line 1: "5", Line 2: "", Line 3: "line1 + 1" (shifted down from line 2)
        //
        // The question is: should "line1" in "line1 + 1" stay as "line1" or become "line2"?
        // User's expectation: it should become "line2" because the line that was originally
        // at position 2 is now at position 3, so references to lines >= 2 should be incremented.

        // Debug the actual state
        println!("App state after split:");
        for (i, line) in app.text_lines.iter().enumerate() {
            println!("  Line {}: '{}'", i + 1, line);
        }

        // The real issue: we're inserting at position 1, so lines at position 1 and after
        // should get their references incremented. "line1 + 1" was originally at position 1,
        // is now at position 2, and "line1" in it should become "line2" because ALL lines
        // shift down after insertion point.
        assert!(
            app.text_lines[2].contains("line2"),
            "Expected 'line1' to be updated to 'line2' but got: '{}'",
            app.text_lines[2]
        );
    }

    #[test]
    fn test_line_splitting_at_beginning() {
        let mut app = App::default();
        // Set up: "5" on line 1, "line1 + 1" on line 2
        app.text_lines = vec!["5".to_string(), "line1 + 1".to_string()];
        app.results = vec![None, None];
        app.cursor_line = 0;
        app.cursor_col = 0; // Position cursor at beginning of "5"

        app.new_line();

        // When hitting enter at beginning:
        // Line 1: "" (empty), Line 2: "5" (content moved down), Line 3: "line2 + 1"
        assert_eq!(app.text_lines[0], "");
        assert_eq!(app.text_lines[1], "5");
        assert!(
            app.text_lines[2].contains("line2"),
            "Expected 'line1' to be updated to 'line2' but got: '{}'",
            app.text_lines[2]
        );
    }

    #[test]
    fn test_user_reported_scenario() {
        let mut app = App::default();

        // Simulate exactly what the user described:
        // "i have the following notebook: 5, line1 + 1"
        app.text_lines = vec!["5".to_string(), "line1 + 1".to_string()];
        app.results = vec![None, None];
        app.update_result(0); // This should make line 1 result in "5"
        app.update_result(1); // This should make line 2 result in "6" (5 + 1)

        // Verify initial results work correctly
        assert_eq!(app.results[0], Some("5".to_string()));
        assert_eq!(app.results[1], Some("6".to_string()));

        // Now simulate "hit enter when the cursor is over the 5"
        app.cursor_line = 0;
        app.cursor_col = 0; // Position cursor at the beginning of "5"
        app.new_line();

        // Verify the results:
        // Line 1: "" (empty)
        // Line 2: "5"
        // Line 3: "line2 + 1" (updated reference)
        assert_eq!(app.text_lines[0], "");
        assert_eq!(app.text_lines[1], "5");
        assert!(
            app.text_lines[2].contains("line2"),
            "Expected line reference to be updated to 'line2', got: '{}'",
            app.text_lines[2]
        );

        // The key test: verify that the expression evaluates correctly automatically
        // line2 should now refer to "5" on line 2, so "line2 + 1" should be 6
        assert_eq!(
            app.results[2],
            Some("6".to_string()),
            "Expected 'line2 + 1' to evaluate to 6 automatically, got: {:?}",
            app.results[2]
        );
    }

    #[test]
    fn test_deletion_with_line_references() {
        let mut app = App::default();

        // Set up: after line splitting we have ["", "5", "line2 + 1"]
        app.text_lines = vec!["".to_string(), "5".to_string(), "line2 + 1".to_string()];
        app.results = vec![None, None, None];
        app.update_result(0);
        app.update_result(1);
        app.update_result(2);

        // Verify initial state works
        assert_eq!(app.results[1], Some("5".to_string()));
        assert_eq!(app.results[2], Some("6".to_string()));

        // Now delete the empty first line by positioning cursor at beginning of line 2 and hitting backspace
        app.cursor_line = 1;
        app.cursor_col = 0;
        app.delete_char(); // This should merge lines and update references

        // Expected result: ["5", "line1 + 1"] (reference should go back to line1)
        assert_eq!(app.text_lines.len(), 2);
        assert_eq!(app.text_lines[0], "5");
        assert!(
            app.text_lines[1].contains("line1"),
            "Expected 'line2' to be updated back to 'line1', got: '{}'",
            app.text_lines[1]
        );

        // The critical test: expression should still evaluate correctly
        assert_eq!(
            app.results[1],
            Some("6".to_string()),
            "Expected 'line1 + 1' to evaluate to 6 after deletion, got: {:?}",
            app.results[1]
        );
    }

    #[test]
    fn test_full_user_workflow_add_then_remove_lines() {
        let mut app = App::default();

        // Start with the user's original notebook
        app.text_lines = vec!["5".to_string(), "line1 + 1".to_string()];
        app.results = vec![None, None];
        app.update_result(0);
        app.update_result(1);

        // Verify initial state: 5, line1 + 1 = 6
        assert_eq!(app.results[0], Some("5".to_string()));
        assert_eq!(app.results[1], Some("6".to_string()));

        // Step 1: Hit enter at beginning of "5" (add lines)
        app.cursor_line = 0;
        app.cursor_col = 0;
        app.new_line();

        // Should have: ["", "5", "line2 + 1"] with line2 + 1 = 6
        assert_eq!(app.text_lines.len(), 3);
        assert_eq!(app.text_lines[0], "");
        assert_eq!(app.text_lines[1], "5");
        assert!(app.text_lines[2].contains("line2"));
        assert_eq!(app.results[2], Some("6".to_string()));

        // Step 2: Remove the empty line (delete lines)
        app.cursor_line = 1;
        app.cursor_col = 0;
        app.delete_char();

        // Should be back to: ["5", "line1 + 1"] with line1 + 1 = 6
        assert_eq!(app.text_lines.len(), 2);
        assert_eq!(app.text_lines[0], "5");
        assert!(app.text_lines[1].contains("line1"));
        assert_eq!(
            app.results[1],
            Some("6".to_string()),
            "Full workflow failed: expected line1 + 1 = 6 after add/remove cycle"
        );
    }
}