excel-cli 1.3.2

Excel CLI for AI, scripting, and terminal users. Headless JSON API for automation, plus a Vim-like TUI for interactive browsing and editing.
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
use crate::actions::{
    ActionCommand, ActionExecutor, ActionType, CellAction, ColumnAction, MultiColumnAction,
    MultiRowAction, RowAction, SheetAction, SheetOperation,
};
use crate::app::AppState;
use crate::utils::index_to_col_name;
use anyhow::Result;
use std::rc::Rc;

impl AppState<'_> {
    pub fn undo(&mut self) -> Result<()> {
        if let Some(action) = self.undo_history.undo() {
            self.apply_action(&action, true)?;

            self.workbook.recalculate_max_rows();
            self.workbook.recalculate_max_cols();
            self.ensure_column_widths();

            self.clamp_selected_cell_to_excel_bounds();

            if self.undo_history.all_undone() {
                self.workbook.set_modified(false);
            } else {
                self.workbook.set_modified(true);
            }
        } else {
            self.add_notification("No operations to undo".to_string());
        }
        Ok(())
    }

    pub fn redo(&mut self) -> Result<()> {
        if let Some(action) = self.undo_history.redo() {
            self.apply_action(&action, false)?;

            self.workbook.recalculate_max_rows();
            self.workbook.recalculate_max_cols();
            self.ensure_column_widths();

            self.clamp_selected_cell_to_excel_bounds();

            self.workbook.set_modified(true);
        } else {
            self.add_notification("No operations to redo".to_string());
        }
        Ok(())
    }

    fn apply_action(&mut self, action: &Rc<ActionCommand>, is_undo: bool) -> Result<()> {
        match action.as_ref() {
            ActionCommand::Cell(cell_action) => {
                let value = if is_undo {
                    &cell_action.old_value
                } else {
                    &cell_action.new_value
                };
                self.apply_cell_action(cell_action, value, is_undo, &cell_action.action_type)?;
            }
            ActionCommand::Row(row_action) => {
                self.apply_row_action(row_action, is_undo)?;
            }
            ActionCommand::Column(column_action) => {
                self.apply_column_action(column_action, is_undo)?;
            }
            ActionCommand::Sheet(sheet_action) => {
                self.apply_sheet_action(sheet_action, is_undo)?;
            }
            ActionCommand::MultiRow(multi_row_action) => {
                self.apply_multi_row_action(multi_row_action, is_undo)?;
            }
            ActionCommand::MultiColumn(multi_column_action) => {
                self.apply_multi_column_action(multi_column_action, is_undo)?;
            }
        }
        Ok(())
    }

    fn apply_cell_action(
        &mut self,
        cell_action: &CellAction,
        value: &crate::excel::Cell,
        is_undo: bool,
        action_type: &ActionType,
    ) -> Result<()> {
        let current_sheet_index = self.workbook.get_current_sheet_index();

        if current_sheet_index != cell_action.sheet_index {
            if let Err(e) = self.switch_sheet_by_index(cell_action.sheet_index) {
                self.add_notification(format!(
                    "Cannot switch to sheet {}: {}",
                    cell_action.sheet_name, e
                ));
                return Ok(());
            }
        }

        self.workbook.get_current_sheet_mut().data[cell_action.row][cell_action.col] =
            value.clone();

        self.selected_cell = (cell_action.row, cell_action.col);
        self.handle_scrolling();

        let cell_ref = format!(
            "{}{}",
            crate::utils::index_to_col_name(cell_action.col),
            cell_action.row
        );

        let operation_text = match action_type {
            ActionType::Edit => "edit",
            ActionType::Cut => "cut",
            ActionType::Paste => "paste",
            _ => "cell operation",
        };

        if current_sheet_index != cell_action.sheet_index {
            let action_word = if is_undo { "Undid" } else { "Redid" };
            self.add_notification(format!(
                "{} {} operation on cell {} in sheet {}",
                action_word, operation_text, cell_ref, cell_action.sheet_name
            ));
        } else {
            let action_word = if is_undo { "Undid" } else { "Redid" };
            self.add_notification(format!(
                "{} {} operation on cell {}",
                action_word, operation_text, cell_ref
            ));
        }

        Ok(())
    }

    fn apply_row_action(&mut self, row_action: &RowAction, is_undo: bool) -> Result<()> {
        let current_sheet_index = self.workbook.get_current_sheet_index();

        if current_sheet_index != row_action.sheet_index {
            if let Err(e) = self.switch_sheet_by_index(row_action.sheet_index) {
                self.add_notification(format!(
                    "Cannot switch to sheet {}: {}",
                    row_action.sheet_name, e
                ));
                return Ok(());
            }
        }

        let sheet = self.workbook.get_current_sheet_mut();

        if is_undo {
            sheet
                .data
                .insert(row_action.row, row_action.row_data.clone());

            sheet.max_rows = sheet.max_rows.saturating_add(1);

            // Recalculate max_cols since restoring a row might affect the maximum column count
            // This is especially important if the row contained data beyond the current max_cols
            self.workbook.recalculate_max_cols();

            self.add_notification(format!("Undid row {} deletion", row_action.row));
        } else if row_action.row < sheet.data.len() {
            sheet.data.remove(row_action.row);
            sheet.max_rows = sheet.max_rows.saturating_sub(1);

            self.clamp_selected_cell_to_excel_bounds();

            self.add_notification(format!("Redid row {} deletion", row_action.row));
        }

        self.handle_scrolling();
        self.search_results.clear();
        self.current_search_idx = None;

        Ok(())
    }

    fn apply_column_action(&mut self, column_action: &ColumnAction, is_undo: bool) -> Result<()> {
        let current_sheet_index = self.workbook.get_current_sheet_index();

        if current_sheet_index != column_action.sheet_index {
            if let Err(e) = self.switch_sheet_by_index(column_action.sheet_index) {
                self.add_notification(format!(
                    "Cannot switch to sheet {}: {}",
                    column_action.sheet_name, e
                ));
                return Ok(());
            }
        }

        let sheet = self.workbook.get_current_sheet_mut();
        let col = column_action.col;

        if is_undo {
            let column_data = &column_action.column_data;

            for (i, row) in sheet.data.iter_mut().enumerate() {
                if i < column_data.len() {
                    if col <= row.len() {
                        row.insert(col, column_data[i].clone());
                    } else {
                        while row.len() < col {
                            row.push(crate::excel::Cell::empty());
                        }
                        row.push(column_data[i].clone());
                    }
                }
            }

            // Update both max_cols and max_rows when restoring a column
            sheet.max_cols = sheet.max_cols.saturating_add(1);

            // Recalculate max_rows since restoring a column might affect the maximum row count
            // This is especially important if the column contained data beyond the current max_rows
            self.workbook.recalculate_max_rows();

            if col < self.column_widths.len() {
                self.column_widths.insert(col, column_action.column_width);
                if !self.column_widths.is_empty() {
                    self.column_widths.pop();
                }
            } else {
                while self.column_widths.len() < col {
                    self.column_widths.push(15); // Default width
                }
                self.column_widths.push(column_action.column_width);
            }

            self.ensure_column_visible(col);
            self.add_notification(format!("Undid column {} deletion", index_to_col_name(col)));
        } else {
            for row in sheet.data.iter_mut() {
                if col < row.len() {
                    row.remove(col);
                }
            }

            sheet.max_cols = sheet.max_cols.saturating_sub(1);

            if self.column_widths.len() > col {
                self.column_widths.remove(col);
                self.column_widths.push(15);
            }

            self.clamp_selected_cell_to_excel_bounds();

            self.add_notification(format!("Redid column {} deletion", index_to_col_name(col)));
        }

        self.handle_scrolling();
        self.search_results.clear();
        self.current_search_idx = None;

        Ok(())
    }

    fn apply_sheet_action(&mut self, sheet_action: &SheetAction, is_undo: bool) -> Result<()> {
        match (sheet_action.operation, is_undo) {
            (SheetOperation::Delete, true) => {
                self.restore_sheet_from_action(
                    sheet_action,
                    format!("Undid sheet {} deletion", sheet_action.sheet_name),
                );
            }
            (SheetOperation::Delete, false) => {
                self.delete_sheet_from_action(
                    sheet_action,
                    format!("Redid deletion of sheet {}", sheet_action.sheet_name),
                );
            }
            (SheetOperation::Create, true) => {
                self.delete_sheet_from_action(
                    sheet_action,
                    format!("Undid creation of sheet {}", sheet_action.sheet_name),
                );
            }
            (SheetOperation::Create, false) => {
                self.restore_sheet_from_action(
                    sheet_action,
                    format!("Redid creation of sheet {}", sheet_action.sheet_name),
                );
            }
        }

        Ok(())
    }

    fn cleanup_after_sheet_deletion(&mut self, sheet_name: &str) {
        self.sheet_column_widths.remove(sheet_name);
        self.sheet_cell_positions.remove(sheet_name);

        let new_sheet_name = self.workbook.get_current_sheet_name();

        // Restore saved cell position for the new current sheet or use default
        if let Some(saved_position) = self.sheet_cell_positions.get(&new_sheet_name) {
            self.selected_cell = Self::clamp_cell_to_excel_bounds(saved_position.selected);
            self.start_row = saved_position.view.0;
            self.start_col = saved_position.view.1;
            self.clamp_selected_cell_to_excel_bounds();

            // Make sure the view position is valid relative to the selected cell
            self.handle_scrolling();
        } else {
            // If no saved position exists, use default position
            self.selected_cell = (1, 1);
            self.start_row = 1;
            self.start_col = 1;
        }

        if let Some(saved_widths) = self.sheet_column_widths.get(&new_sheet_name) {
            self.column_widths = saved_widths.clone();
        } else {
            let max_cols = self.workbook.get_current_sheet().max_cols;
            let default_width = 15;
            self.column_widths = vec![default_width; max_cols + 1];

            self.sheet_column_widths
                .insert(new_sheet_name.clone(), self.column_widths.clone());
        }

        self.search_results.clear();
        self.current_search_idx = None;
        self.update_row_number_width();

        let new_sheet_index = self.workbook.get_current_sheet_index();
        if self.workbook.is_lazy_loading() && !self.workbook.is_sheet_loaded(new_sheet_index) {
            self.input_mode = crate::app::InputMode::LazyLoading;
        } else {
            self.input_mode = crate::app::InputMode::Normal;
        }
    }

    fn restore_sheet_from_action(&mut self, sheet_action: &SheetAction, notification: String) {
        let sheet_index = sheet_action.sheet_index;

        if let Err(e) = self
            .workbook
            .insert_sheet_at_index(sheet_action.sheet_data.clone(), sheet_index)
        {
            self.add_notification(format!(
                "Failed to restore sheet {}: {}",
                sheet_action.sheet_name, e
            ));
            return;
        }

        self.sheet_column_widths.insert(
            sheet_action.sheet_name.clone(),
            sheet_action.column_widths.clone(),
        );

        self.sheet_cell_positions.insert(
            sheet_action.sheet_name.clone(),
            crate::app::CellPosition {
                selected: (1, 1),
                view: (1, 1),
            },
        );

        if let Err(e) = self.switch_sheet_by_index(sheet_index) {
            self.add_notification(format!(
                "Restored sheet {} but couldn't switch to it: {}",
                sheet_action.sheet_name, e
            ));
            return;
        }

        self.notification_messages.pop();
        self.add_notification(notification);
    }

    fn delete_sheet_from_action(&mut self, sheet_action: &SheetAction, notification: String) {
        if let Err(e) = self.switch_sheet_by_index(sheet_action.sheet_index) {
            self.add_notification(format!(
                "Cannot switch to sheet {} to delete it: {}",
                sheet_action.sheet_name, e
            ));
            return;
        }

        self.notification_messages.pop();

        if let Err(e) = self.workbook.delete_current_sheet() {
            self.add_notification(format!("Failed to delete sheet: {e}"));
            return;
        }

        self.cleanup_after_sheet_deletion(&sheet_action.sheet_name);
        self.add_notification(notification);
    }

    fn apply_multi_row_action(
        &mut self,
        multi_row_action: &MultiRowAction,
        is_undo: bool,
    ) -> Result<()> {
        let current_sheet_index = self.workbook.get_current_sheet_index();

        if current_sheet_index != multi_row_action.sheet_index {
            if let Err(e) = self.switch_sheet_by_index(multi_row_action.sheet_index) {
                self.add_notification(format!(
                    "Cannot switch to sheet {}: {}",
                    multi_row_action.sheet_name, e
                ));
                return Ok(());
            }
        }

        let start_row = multi_row_action.start_row;
        let end_row = multi_row_action.end_row;
        let rows_to_restore = end_row - start_row + 1;

        if is_undo {
            let rows_data = &multi_row_action.rows_data;
            let sheet = self.workbook.get_current_sheet_mut();

            // Optimized restore function
            Self::restore_rows(sheet, start_row, rows_data);

            sheet.max_rows = sheet.max_rows.saturating_add(rows_to_restore);

            // Recalculate max_cols since restoring rows might affect the maximum column count
            self.workbook.recalculate_max_cols();

            self.add_notification(format!("Undid rows {} to {} deletion", start_row, end_row));
        } else {
            self.workbook.delete_rows(start_row, end_row)?;

            self.clamp_selected_cell_to_excel_bounds();

            self.add_notification(format!("Redid rows {} to {} deletion", start_row, end_row));
        }

        self.handle_scrolling();
        self.search_results.clear();
        self.current_search_idx = None;

        Ok(())
    }

    fn apply_multi_column_action(
        &mut self,
        multi_column_action: &MultiColumnAction,
        is_undo: bool,
    ) -> Result<()> {
        let current_sheet_index = self.workbook.get_current_sheet_index();

        if current_sheet_index != multi_column_action.sheet_index {
            if let Err(e) = self.switch_sheet_by_index(multi_column_action.sheet_index) {
                self.add_notification(format!(
                    "Cannot switch to sheet {}: {}",
                    multi_column_action.sheet_name, e
                ));
                return Ok(());
            }
        }

        let start_col = multi_column_action.start_col;
        let end_col = multi_column_action.end_col;
        let cols_to_restore = end_col - start_col + 1;

        if is_undo {
            let columns_data = &multi_column_action.columns_data;
            let column_widths = &multi_column_action.column_widths;

            let sheet = self.workbook.get_current_sheet_mut();

            for col_idx in (0..cols_to_restore).rev() {
                if col_idx < columns_data.len() {
                    let column_data = &columns_data[col_idx];
                    Self::restore_column_at_position(sheet, start_col, column_data);

                    Self::restore_column_width(
                        &mut self.column_widths,
                        start_col,
                        col_idx,
                        column_widths,
                    );
                }
            }

            sheet.max_cols = sheet.max_cols.saturating_add(cols_to_restore);

            // Recalculate max_rows since restoring columns might affect the maximum row count
            self.workbook.recalculate_max_rows();

            Self::trim_column_widths(&mut self.column_widths, cols_to_restore);
            self.ensure_column_visible(start_col);

            self.add_notification(format!(
                "Undid columns {} to {} deletion",
                index_to_col_name(start_col),
                index_to_col_name(end_col)
            ));
        } else {
            self.workbook.delete_columns(start_col, end_col)?;

            Self::remove_column_widths(&mut self.column_widths, start_col, end_col);

            self.clamp_selected_cell_to_excel_bounds();

            self.add_notification(format!(
                "Redid columns {} to {} deletion",
                index_to_col_name(start_col),
                index_to_col_name(end_col)
            ));
        }

        self.handle_scrolling();
        self.search_results.clear();
        self.current_search_idx = None;

        Ok(())
    }

    fn restore_rows(
        sheet: &mut crate::excel::Sheet,
        position: usize,
        rows_data: &[Vec<crate::excel::Cell>],
    ) {
        // Pre-allocate space by extending the vector
        for row_data in rows_data.iter().rev() {
            sheet.data.insert(position, row_data.clone());
        }
    }

    fn restore_column_at_position(
        sheet: &mut crate::excel::Sheet,
        position: usize,
        column_data: &[crate::excel::Cell],
    ) {
        for (i, row) in sheet.data.iter_mut().enumerate() {
            if i < column_data.len() {
                if position <= row.len() {
                    row.insert(position, column_data[i].clone());
                } else {
                    let additional = position - row.len();
                    row.reserve(additional + 1);
                    while row.len() < position {
                        row.push(crate::excel::Cell::empty());
                    }
                    row.push(column_data[i].clone());
                }
            }
        }
    }

    fn restore_column_width(
        column_widths: &mut Vec<usize>,
        position: usize,
        col_idx: usize,
        width_values: &[usize],
    ) {
        if position < column_widths.len() {
            let width = if col_idx < width_values.len() {
                width_values[col_idx]
            } else {
                15 // Default width
            };
            column_widths.insert(position, width);
        }
    }

    fn trim_column_widths(column_widths: &mut Vec<usize>, count: usize) {
        if count >= column_widths.len() {
            return;
        }
        column_widths.truncate(column_widths.len() - count);
    }

    fn remove_column_widths(column_widths: &mut Vec<usize>, start_col: usize, end_col: usize) {
        let cols_to_remove = end_col - start_col + 1;

        // Pre-allocate space for new entries to avoid multiple resizes
        column_widths.reserve(cols_to_remove);

        for col in (start_col..=end_col).rev() {
            if column_widths.len() > col {
                column_widths.remove(col);
            }
        }

        // Add default widths in a single batch to avoid multiple resizes
        let mut defaults = vec![15; cols_to_remove];
        column_widths.append(&mut defaults);
    }
}

impl ActionExecutor for AppState<'_> {
    fn execute_action(&mut self, action: &ActionCommand) -> Result<()> {
        match action {
            ActionCommand::Cell(action) => self.execute_cell_action(action),
            ActionCommand::Row(action) => self.execute_row_action(action),
            ActionCommand::Column(action) => self.execute_column_action(action),
            ActionCommand::Sheet(action) => self.execute_sheet_action(action),
            ActionCommand::MultiRow(action) => self.execute_multi_row_action(action),
            ActionCommand::MultiColumn(action) => self.execute_multi_column_action(action),
        }
    }

    fn execute_cell_action(&mut self, action: &CellAction) -> Result<()> {
        self.workbook
            .set_cell_value(action.row, action.col, action.new_value.value.clone())
    }

    fn execute_row_action(&mut self, action: &RowAction) -> Result<()> {
        self.workbook.delete_row(action.row)
    }

    fn execute_column_action(&mut self, action: &ColumnAction) -> Result<()> {
        self.workbook.delete_column(action.col)
    }

    fn execute_sheet_action(&mut self, action: &SheetAction) -> Result<()> {
        match action.operation {
            SheetOperation::Create => {
                self.workbook
                    .insert_sheet_at_index(action.sheet_data.clone(), action.sheet_index)?;
                self.sheet_column_widths
                    .insert(action.sheet_name.clone(), action.column_widths.clone());
                self.sheet_cell_positions.insert(
                    action.sheet_name.clone(),
                    crate::app::CellPosition {
                        selected: (1, 1),
                        view: (1, 1),
                    },
                );
                self.switch_sheet_by_index(action.sheet_index)
            }
            SheetOperation::Delete => {
                self.switch_sheet_by_index(action.sheet_index)?;
                self.workbook.delete_current_sheet()?;
                self.cleanup_after_sheet_deletion(&action.sheet_name);
                Ok(())
            }
        }
    }

    fn execute_multi_row_action(&mut self, action: &MultiRowAction) -> Result<()> {
        self.workbook.delete_rows(action.start_row, action.end_row)
    }

    fn execute_multi_column_action(&mut self, action: &MultiColumnAction) -> Result<()> {
        self.workbook
            .delete_columns(action.start_col, action.end_col)
    }
}