revw 0.2.2

A vim-like TUI for managing notes and resources
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
use super::{App, FormatMode};
use super::super::json_ops::JsonOperations;
use serde_json::Value;

impl App {
    pub fn open_entry_overlay(&mut self) {
        self.start_editing_entry();
    }

    pub fn start_editing_entry(&mut self) {
        // Get the original index from the selected entry (accounts for filtering)
        let target_idx = if self.selected_entry_index < self.relf_entries.len() {
            self.relf_entries[self.selected_entry_index].original_index
        } else {
            return; // Invalid selection
        };

        // Load fields from JSON (not from rendered lines) to include empty fields
        if let Ok(json_value) = serde_json::from_str::<Value>(&self.json_input) {
            if let Some(obj) = json_value.as_object() {
                let mut current_idx = 0;

                // Check outside section
                if let Some(outside) = obj.get("outside") {
                    if let Some(outside_array) = outside.as_array() {
                        if target_idx < current_idx + outside_array.len() {
                            let local_idx = target_idx - current_idx;
                            if let Some(entry_obj) = outside_array[local_idx].as_object() {
                                // Load all fields including empty ones, use placeholder if empty
                                let name = entry_obj.get("name").and_then(|v| v.as_str()).unwrap_or("").to_string();
                                let context = entry_obj.get("context").and_then(|v| v.as_str()).unwrap_or("").to_string();
                                let url = entry_obj.get("url").and_then(|v| v.as_str()).unwrap_or("").to_string();
                                let percentage = entry_obj.get("percentage").and_then(|v| v.as_i64());

                                let name_is_empty = name.is_empty();
                                let context_is_empty = context.is_empty();
                                let url_is_empty = url.is_empty();

                                self.edit_buffer = vec![
                                    if name_is_empty { "name".to_string() } else { name },
                                    if context_is_empty { "context".to_string() } else { context },
                                    if url_is_empty { "url".to_string() } else { url },
                                    if let Some(pct) = percentage { pct.to_string() } else { "percentage".to_string() },
                                    "Exit".to_string(),
                                ];
                                self.edit_buffer_is_placeholder = vec![
                                    name_is_empty,
                                    context_is_empty,
                                    url_is_empty,
                                    percentage.is_none(),
                                    false, // Exit is never a placeholder
                                ];
                                self.edit_field_index = 0;
                                self.editing_entry = true;
                                self.edit_field_editing_mode = false;
                                self.edit_insert_mode = false;
                                self.edit_cursor_pos = 0;
                                return;
                            }
                        }
                        current_idx += outside_array.len();
                    }
                }

                // Check inside section
                if let Some(inside) = obj.get("inside") {
                    if let Some(inside_array) = inside.as_array() {
                        if target_idx < current_idx + inside_array.len() {
                            let local_idx = target_idx - current_idx;
                            if let Some(entry_obj) = inside_array[local_idx].as_object() {
                                // Load all fields including empty ones, use placeholder if empty
                                let date = entry_obj.get("date").and_then(|v| v.as_str()).unwrap_or("").to_string();
                                let context = entry_obj.get("context").and_then(|v| v.as_str()).unwrap_or("").to_string();

                                let date_is_empty = date.is_empty();
                                let context_is_empty = context.is_empty();

                                self.edit_buffer = vec![
                                    if date_is_empty { "date".to_string() } else { date },
                                    if context_is_empty { "context".to_string() } else { context },
                                    "Exit".to_string(),
                                ];
                                self.edit_buffer_is_placeholder = vec![
                                    date_is_empty,
                                    context_is_empty,
                                    false, // Exit is never a placeholder
                                ];
                                self.edit_field_index = 0;
                                self.editing_entry = true;
                                self.edit_field_editing_mode = false;
                                self.edit_insert_mode = false;
                                self.edit_cursor_pos = 0;
                                return;
                            }
                        }
                    }
                }
            }
        }
    }

    pub fn save_edited_entry(&mut self) {
        // Save the edited entry back to JSON
        if self.edit_buffer.is_empty() {
            self.editing_entry = false;
            return;
        }

        // Get the original index from the selected entry (accounts for filtering)
        let target_idx = if self.selected_entry_index < self.relf_entries.len() {
            self.relf_entries[self.selected_entry_index].original_index
        } else {
            self.editing_entry = false;
            return; // Invalid selection
        };

        match serde_json::from_str::<Value>(&self.json_input) {
            Ok(mut json_value) => {
                if let Some(obj) = json_value.as_object_mut() {
                    let mut current_idx = 0;
                    let mut found = false;

                    // Check outside section
                    if let Some(outside) = obj.get_mut("outside") {
                        if let Some(outside_array) = outside.as_array_mut() {
                            if target_idx < current_idx + outside_array.len() {
                                let local_idx = target_idx - current_idx;
                                if let Some(entry_obj) = outside_array[local_idx].as_object_mut() {
                                    // Update fields - use placeholder flags to determine if value is placeholder
                                    if self.edit_buffer.len() >= 1 && self.edit_buffer_is_placeholder.len() >= 1 {
                                        let name_val = &self.edit_buffer[0];
                                        let is_placeholder = self.edit_buffer_is_placeholder[0];
                                        entry_obj.insert("name".to_string(),
                                            Value::String(if is_placeholder { String::new() } else { name_val.clone() }));
                                    }
                                    if self.edit_buffer.len() >= 2 && self.edit_buffer_is_placeholder.len() >= 2 {
                                        let context_val = &self.edit_buffer[1];
                                        let is_placeholder = self.edit_buffer_is_placeholder[1];
                                        entry_obj.insert("context".to_string(),
                                            Value::String(if is_placeholder { String::new() } else { context_val.clone() }));
                                    }
                                    if self.edit_buffer.len() >= 3 && self.edit_buffer_is_placeholder.len() >= 3 {
                                        let url_val = &self.edit_buffer[2];
                                        let is_placeholder = self.edit_buffer_is_placeholder[2];
                                        entry_obj.insert("url".to_string(),
                                            Value::String(if is_placeholder { String::new() } else { url_val.clone() }));
                                    }
                                    if self.edit_buffer.len() >= 4 && self.edit_buffer_is_placeholder.len() >= 4 {
                                        // Parse percentage - save null if placeholder
                                        let pct_val = &self.edit_buffer[3];
                                        let is_placeholder = self.edit_buffer_is_placeholder[3];
                                        if is_placeholder {
                                            entry_obj.insert("percentage".to_string(), Value::Null);
                                        } else if let Ok(pct) = pct_val.trim_end_matches('%').parse::<i64>() {
                                            entry_obj.insert("percentage".to_string(), Value::Number(pct.into()));
                                        }
                                    }
                                    found = true;
                                }
                            } else {
                                current_idx += outside_array.len();
                            }
                        }
                    }

                    // Check inside section
                    if !found {
                        if let Some(inside) = obj.get_mut("inside") {
                            if let Some(inside_array) = inside.as_array_mut() {
                                let local_idx = target_idx - current_idx;
                                if local_idx < inside_array.len() {
                                    if let Some(entry_obj) = inside_array[local_idx].as_object_mut() {
                                        // Update fields (date and context for inside) - use placeholder flags
                                        if self.edit_buffer.len() >= 1 && self.edit_buffer_is_placeholder.len() >= 1 {
                                            let date_val = &self.edit_buffer[0];
                                            let is_placeholder = self.edit_buffer_is_placeholder[0];
                                            entry_obj.insert("date".to_string(),
                                                Value::String(if is_placeholder { String::new() } else { date_val.clone() }));
                                        }
                                        if self.edit_buffer.len() >= 2 && self.edit_buffer_is_placeholder.len() >= 2 {
                                            let context_val = &self.edit_buffer[1];
                                            let is_placeholder = self.edit_buffer_is_placeholder[1];
                                            entry_obj.insert("context".to_string(),
                                                Value::String(if is_placeholder { String::new() } else { context_val.clone() }));
                                        }
                                        found = true;
                                    }
                                }
                            }
                        }
                    }

                    if found {
                        match serde_json::to_string_pretty(&json_value) {
                            Ok(formatted) => {
                                self.json_input = formatted;
                                self.is_modified = true;
                                self.convert_json();
                                self.set_status("Entry updated");
                                // Auto-save after editing
                                self.save_file();
                            }
                            Err(e) => self.set_status(&format!("Error formatting JSON: {}", e)),
                        }
                    }
                }
            }
            Err(e) => self.set_status(&format!("Invalid JSON: {}", e)),
        }

        self.editing_entry = false;
    }

    pub fn cancel_editing_entry(&mut self) {
        self.editing_entry = false;
        self.edit_buffer.clear();
        self.edit_buffer_is_placeholder.clear();
        self.edit_field_index = 0;
        self.edit_insert_mode = false;
        self.edit_cursor_pos = 0;
    }

    pub fn insert_char(&mut self, c: char) {
        if self.format_mode == FormatMode::Edit {
            // Save undo state before modification
            self.save_undo_state();

            let mut lines = self.get_json_lines();
            if lines.is_empty() {
                lines.push(String::new());
                self.content_cursor_line = 0;
                self.content_cursor_col = 0;
            }

            // Ensure cursor is within bounds
            if self.content_cursor_line >= lines.len() {
                self.content_cursor_line = lines.len().saturating_sub(1);
            }

            let line = &mut lines[self.content_cursor_line];
            let mut chars: Vec<char> = line.chars().collect();
            let pos = self.content_cursor_col.min(chars.len());
            chars.insert(pos, c);

            // Update the line with the new character
            lines[self.content_cursor_line] = chars.into_iter().collect();
            self.content_cursor_col += 1;
            self.set_json_from_lines(lines);
            self.ensure_cursor_visible();
        }
    }

    pub fn insert_newline(&mut self) {
        if self.format_mode == FormatMode::Edit {
            // Save undo state before modification
            self.save_undo_state();

            let mut lines = self.get_json_lines();
            if lines.is_empty() {
                lines.push(String::new());
                lines.push(String::new());
                self.content_cursor_line = 1;
                self.content_cursor_col = 0;
            } else {
                // Ensure cursor is within bounds
                if self.content_cursor_line >= lines.len() {
                    self.content_cursor_line = lines.len().saturating_sub(1);
                }

                let line = lines[self.content_cursor_line].clone();
                let split_pos = self.content_cursor_col.min(line.len());
                let (left, right) = line.split_at(split_pos);
                lines[self.content_cursor_line] = left.to_string();
                lines.insert(self.content_cursor_line + 1, right.to_string());
                self.content_cursor_line += 1;
                self.content_cursor_col = 0;
            }
            self.set_json_from_lines(lines);
            self.ensure_cursor_visible();
        }
    }

    pub fn backspace(&mut self) {
        if self.format_mode == FormatMode::Edit {
            // Save undo state before modification
            self.save_undo_state();

            let mut lines = self.get_json_lines();
            if lines.is_empty() {
                return;
            }
            if self.content_cursor_col > 0 && self.content_cursor_line < lines.len() {
                // Remove character before cursor (handle multi-byte chars)
                let mut chars: Vec<char> = lines[self.content_cursor_line].chars().collect();
                if self.content_cursor_col > 0 && self.content_cursor_col <= chars.len() {
                    chars.remove(self.content_cursor_col - 1);
                    lines[self.content_cursor_line] = chars.into_iter().collect();
                    self.content_cursor_col -= 1;
                    self.set_json_from_lines(lines);
                }
            } else if self.content_cursor_col == 0 && self.content_cursor_line > 0 {
                // Join with previous line
                let current_line = lines.remove(self.content_cursor_line);
                self.content_cursor_line -= 1;
                let prev_line_len = lines[self.content_cursor_line].chars().count();
                lines[self.content_cursor_line].push_str(&current_line);
                self.content_cursor_col = prev_line_len;
                self.set_json_from_lines(lines);
            }
        }
    }

    pub fn delete_char(&mut self) {
        if self.format_mode == FormatMode::Edit {
            // Save undo state before modification
            self.save_undo_state();

            let mut lines = self.get_json_lines();
            if lines.is_empty() {
                return;
            }
            if self.content_cursor_line < lines.len() {
                let mut chars: Vec<char> = lines[self.content_cursor_line].chars().collect();
                if self.content_cursor_col < chars.len() {
                    chars.remove(self.content_cursor_col);
                    lines[self.content_cursor_line] = chars.into_iter().collect();
                    self.set_json_from_lines(lines);
                } else if self.content_cursor_line + 1 < lines.len() {
                    // Join with next line
                    let next_line = lines.remove(self.content_cursor_line + 1);
                    lines[self.content_cursor_line].push_str(&next_line);
                    self.set_json_from_lines(lines);
                }
            }
        }
    }

    pub fn move_cursor_left(&mut self) {
        if self.content_cursor_col > 0 {
            self.content_cursor_col -= 1;
        } else if self.content_cursor_line > 0 {
            self.content_cursor_line -= 1;
            let lines = self.get_json_lines();
            if self.content_cursor_line < lines.len() {
                self.content_cursor_col = lines[self.content_cursor_line].chars().count();
            }
        }
        self.ensure_cursor_visible();
    }

    pub fn move_cursor_right(&mut self) {
        let lines = self.get_json_lines();
        if lines.is_empty() {
            return;
        }
        if self.content_cursor_line < lines.len() {
            let line_len = lines[self.content_cursor_line].chars().count();
            if self.content_cursor_col < line_len {
                self.content_cursor_col += 1;
            } else if self.content_cursor_line + 1 < lines.len() {
                self.content_cursor_line += 1;
                self.content_cursor_col = 0;
            }
        }
        self.ensure_cursor_visible();
    }

    pub fn move_cursor_up(&mut self) {
        if self.content_cursor_line > 0 {
            self.content_cursor_line -= 1;
            let lines = self.get_json_lines();
            if self.content_cursor_line < lines.len() {
                let line_len = lines[self.content_cursor_line].chars().count();
                self.content_cursor_col = self.content_cursor_col.min(line_len);
            }
        }
        self.ensure_cursor_visible();
    }

    pub fn move_cursor_down(&mut self) {
        let lines = self.get_json_lines();
        if lines.is_empty() {
            return;
        }

        // Keep cursor within actual content lines
        let content_lines = if self.format_mode == FormatMode::Edit {
            lines.len()
        } else {
            self.rendered_content.len()
        };

        // Move cursor down if there's content, otherwise just scroll screen
        if self.content_cursor_line + 1 < content_lines {
            // Normal cursor movement within content
            self.content_cursor_line += 1;

            let line_len =
                if self.format_mode == FormatMode::Edit && self.content_cursor_line < lines.len() {
                    lines[self.content_cursor_line].chars().count()
                } else if self.content_cursor_line < self.rendered_content.len() {
                    self.rendered_content[self.content_cursor_line]
                        .chars()
                        .count()
                } else {
                    0
                };

            self.content_cursor_col = self.content_cursor_col.min(line_len);
        } else {
            // Cursor is at last line, just scroll the screen down (Mario style)
            // Don't add virtual padding in help mode
            let virtual_padding = if self.showing_help { 0 } else { 10 };
            let max_scroll =
                (content_lines as u16 + virtual_padding).saturating_sub(self.get_visible_height());
            if self.scroll < max_scroll {
                self.scroll += 1;
            }
        }
        self.ensure_cursor_visible();
    }

    pub fn append_inside(&mut self) {
        match JsonOperations::add_inside_entry(&self.json_input) {
            Ok((formatted, line, col, message)) => {
                self.json_input = formatted;
                self.is_modified = true;
                self.convert_json();

                // Jump to the new entry (don't open edit overlay or insert mode)
                if self.format_mode == FormatMode::View {
                    // New inside entry is added at the beginning of inside array
                    // Index = outside.length (start of INSIDE section)
                    if let Ok(json_value) = serde_json::from_str::<Value>(&self.json_input) {
                        if let Some(obj) = json_value.as_object() {
                            let outside_count = obj
                                .get("outside")
                                .and_then(|v| v.as_array())
                                .map(|arr| arr.len())
                                .unwrap_or(0);
                            // INSIDE section starts right after OUTSIDE
                            self.selected_entry_index = outside_count;
                            self.scroll = 0;
                        }
                    }
                } else {
                    // In Edit mode, just move cursor to the new entry
                    self.content_cursor_line = line;
                    self.content_cursor_col = col;
                    self.ensure_cursor_visible();
                }
                self.set_status(&message);
            }
            Err(e) => self.set_status(&format!("Error: {}", e)),
        }
    }

    pub fn append_outside(&mut self) {
        match JsonOperations::add_outside_entry(&self.json_input) {
            Ok((formatted, line, col, message)) => {
                self.json_input = formatted;
                self.is_modified = true;
                self.convert_json();

                // Jump to the new entry (don't open edit overlay or insert mode)
                if self.format_mode == FormatMode::View {
                    // New outside entry is added at the end of outside array
                    // Index = outside.length - 1 (last OUTSIDE entry)
                    if let Ok(json_value) = serde_json::from_str::<Value>(&self.json_input) {
                        if let Some(obj) = json_value.as_object() {
                            let outside_count = obj
                                .get("outside")
                                .and_then(|v| v.as_array())
                                .map(|arr| arr.len())
                                .unwrap_or(0);
                            // Last outside entry
                            self.selected_entry_index = outside_count.saturating_sub(1);
                            self.scroll = 0;
                        }
                    }
                } else {
                    // In Edit mode, just move cursor to the new entry
                    self.content_cursor_line = line;
                    self.content_cursor_col = col;
                    self.ensure_cursor_visible();
                }
                self.set_status(&message);
            }
            Err(e) => self.set_status(&format!("Error: {}", e)),
        }
    }

    pub fn ensure_cursor_visible(&mut self) {
        let lines = self.get_json_lines();
        if lines.is_empty() {
            self.content_cursor_line = 0;
            self.content_cursor_col = 0;
            return;
        }

        // Ensure cursor stays within actual content bounds
        let content_lines = if self.format_mode == FormatMode::Edit {
            lines.len()
        } else {
            self.rendered_content.len()
        };

        // Keep cursor within actual content lines (not in virtual padding)
        if self.content_cursor_line >= content_lines {
            self.content_cursor_line = content_lines.saturating_sub(1);
        }

        // Handle cursor column bounds
        let line_len =
            if self.format_mode == FormatMode::Edit && self.content_cursor_line < lines.len() {
                lines[self.content_cursor_line].chars().count()
            } else if self.content_cursor_line < self.rendered_content.len() {
                self.rendered_content[self.content_cursor_line]
                    .chars()
                    .count()
            } else {
                0
            };
        if self.content_cursor_col > line_len {
            self.content_cursor_col = line_len;
        }

        // Vertical scrolling
        let cursor_line = if self.format_mode == FormatMode::Edit {
            self.content_cursor_line as u16
        } else {
            self.calculate_cursor_visual_position().0
        };
        let visible_height = self.get_visible_height();
        let scrolloff = 3u16;
        if cursor_line < self.scroll {
            self.scroll = cursor_line;
        } else if visible_height > 0 && cursor_line >= self.scroll + visible_height {
            self.scroll = cursor_line.saturating_sub(visible_height - 1);
        } else if visible_height > scrolloff * 2 {
            if cursor_line < self.scroll + scrolloff {
                self.scroll = cursor_line.saturating_sub(scrolloff);
            } else if cursor_line > self.scroll + visible_height - scrolloff - 1 {
                self.scroll = cursor_line + scrolloff + 1 - visible_height;
            }
        }

        // Allow scrolling into virtual padding
        let virtual_padding = 10;
        let max_scroll = (content_lines as u16 + virtual_padding).saturating_sub(visible_height);
        if self.scroll > max_scroll {
            self.scroll = max_scroll;
        }

        // Horizontal follow for Edit mode
        if self.format_mode == FormatMode::Edit {
            if self.content_cursor_line < lines.len() {
                let current = &lines[self.content_cursor_line];
                let cursor_display_col = self.prefix_display_width(current, self.content_cursor_col) as u16;
                let w = self.get_content_width();

                // Add margin to scroll before cursor reaches edge
                let margin = 8u16;

                if cursor_display_col < self.hscroll + margin {
                    // Cursor near left edge - scroll left
                    self.hscroll = cursor_display_col.saturating_sub(margin);
                } else if cursor_display_col >= self.hscroll + w.saturating_sub(margin) {
                    // Cursor near right edge - scroll right
                    self.hscroll = cursor_display_col + margin - w + 1;
                }
            }
        }
    }

    pub fn order_entries(&mut self) {
        match JsonOperations::order_entries(&self.json_input) {
            Ok((formatted, message)) => {
                self.json_input = formatted;
                self.is_modified = true;
                self.convert_json();

                // Auto-save in view mode
                if self.format_mode == FormatMode::View {
                    self.save_file();
                }

                self.set_status(&message);
            }
            Err(e) => self.set_status(&format!("Error: {}", e)),
        }
    }

}