diff --git a/src/app/clipboard.rs b/src/app/clipboard.rs index a6d152f..bb2c1e6 100644 --- a/src/app/clipboard.rs +++ b/src/app/clipboard.rs @@ -83,6 +83,28 @@ impl Clipboard { } self.last_paste_cell = (low_x, low_y); } + + pub fn clipboard_cut(&mut self, start: (usize, usize), end: (usize, usize), from: &mut Grid) { + let (x1, y1) = start; + let (x2, y2) = end; + + let (low_x, hi_x) = if x1 < x2 { (x1, x2) } else { (x2, x1) }; + let (low_y, hi_y) = if y1 < y2 { (y1, y2) } else { (y2, y1) }; + + // size the clipboard appropriately + self.clipboard.clear(); + // clone data into clipboard + for x in low_x..=hi_x { + let mut col = Vec::new(); + for y in low_y..=hi_y { + let a = from.get_cell_raw(x, y); + col.push(a.clone()); + from.set_cell_raw::((x,y), None); + } + self.clipboard.push(col); + } + self.last_paste_cell = (low_x, low_y); + } } #[test] diff --git a/src/app/mode.rs b/src/app/mode.rs index 839560e..8374268 100644 --- a/src/app/mode.rs +++ b/src/app/mode.rs @@ -182,16 +182,9 @@ impl Mode { // TODO visual copy, paste, etc let (x2, y2) = app.grid.cursor(); - let (low_x, hi_x) = if x1 < x2 { (x1, x2) } else { (x2, x1) }; - let (low_y, hi_y) = if y1 < y2 { (y1, y2) } else { (y2, y1) }; - match key { - 'd' => { - for x in low_x..=hi_x { - for y in low_y..=hi_y { - app.grid.set_cell_raw::((x, y), None); - } - } + 'd' | 'x' => { + app.clipboard.clipboard_cut((x1,y1), (x2,y2), &mut app.grid); app.mode = Mode::Normal } 'y' => { @@ -237,7 +230,7 @@ impl Mode { // delete cell under cursor ("d", ' ') | ("d", 'w') => { let loc = app.grid.cursor(); - app.grid.set_cell_raw::(loc, None); + app.clipboard.clipboard_cut(loc, loc, &mut app.grid); app.mode = Mode::Normal; } // go to top of row