Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat: various cell operations #15

Open
wants to merge 5 commits into
base: main
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 10 additions & 1 deletion docs/index.md
Original file line number Diff line number Diff line change
Expand Up @@ -37,6 +37,8 @@ table and between the sheets using the following keybinds:
* `l` and, ➡️ will move one cell to the right.
* `j`, ⬇️, and `Enter` will move one cell down.
* `k` ⬆️, will move one cell up.
* `d` will delete the contents of the selected cell leaving style untouched
* `D` will delete the contents of the selected cell including any style

**Sheet Navigation**

Expand All @@ -61,6 +63,9 @@ will clear the numeric prefix if you want to cancel it.

* `Ctrl-r` will enter range selection mode
* `Ctrl-s` will save the sheet.
* `Ctrl-c`, `y` Copy the cell or range contents.
* `Ctrl-v`, `p` Paste into the sheet.
* `Ctrl-Shift-C` Copy the cell or range formatted content.
* `q` will exit the application.
* `:` will enter CommandMode.

Expand Down Expand Up @@ -122,12 +127,16 @@ will be discarded if you have not saved first.</aside>

### Range Select Mode

Range Select mode copies a range reference for use later. You can enter range
Range Select mode copies a range reference for use later or delete a range's contents. You can enter range
select mode from CellEdit mode with `CTRL-r`.

* `h`, `j`, `k`, `l` will navigate around the sheet.
* `Ctrl-n`, `Ctrl-p` will navigate between sheets.
* `Ctrl-c`, `y` Copy the cell or range contents.
* `Ctrl-Shift-C`, 'Y' Copy the cell or range formatted content.
* `The spacebar will select the start and end of the range respectively.
* `d` will delete the contents of the range leaving any style untouched
* `D` will delete the contents of the range including any style

When you have selected the end of the range you will exit range select mode and
the range reference will be placed into the cell contents you are editing.
Expand Down
57 changes: 54 additions & 3 deletions src/book/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -104,6 +104,48 @@ impl Book {
Ok(())
}

pub fn clear_current_cell(&mut self) -> Result<()> {
self.clear_cell_contents(self.current_sheet as u32, self.location.clone())
}

pub fn clear_current_cell_all(&mut self) -> Result<()> {
self.clear_cell_all(self.current_sheet as u32, self.location.clone())
}


pub fn clear_cell_contents(&mut self, sheet: u32, Address { row, col, }: Address) -> Result<()> {
Ok(self
.model
.cell_clear_contents(sheet, row as i32, col as i32)
.map_err(|s| anyhow!("Unable to clear cell contents {}", s))?)
}

pub fn clear_cell_range(&mut self, sheet: u32, start: Address, end: Address) -> Result<()> {
for row in start.row..=end.row {
for col in start.col..=end.col {
self.clear_cell_contents(sheet, Address { row, col })?;
}
}
Ok(())
}

pub fn clear_cell_all(&mut self, sheet: u32, Address { row, col, }: Address) -> Result<()> {
Ok(self
.model
.cell_clear_all(sheet, row as i32, col as i32)
.map_err(|s| anyhow!("Unable to clear cell contents {}", s))?)
}

pub fn clear_cell_range_all(&mut self, sheet: u32, start: Address, end: Address) -> Result<()> {
for row in start.row..=end.row {
for col in start.col..=end.col {
self.clear_cell_all(sheet, Address { row, col })?;
}
}
Ok(())
}


/// Get a cells formatted content.
pub fn get_current_cell_rendered(&self) -> Result<String> {
Ok(self.get_cell_addr_rendered(&self.location)?)
Expand All @@ -116,6 +158,15 @@ impl Book {
.get_formatted_cell_value(self.current_sheet, *row as i32, *col as i32)
.map_err(|s| anyhow!("Unable to format cell {}", s))?)
}

/// Get a cells actual content unformatted as a string.
pub fn get_cell_addr_contents(&self, Address { row, col }: &Address) -> Result<String> {
Ok(self
.model
.get_cell_content(self.current_sheet, *row as i32, *col as i32)
.map_err(|s| anyhow!("Unable to format cell {}", s))?)
}


/// Get a cells actual content as a string.
pub fn get_current_cell_contents(&self) -> Result<String> {
Expand All @@ -132,13 +183,13 @@ impl Book {
/// Update the current cell in a book.
/// This update won't be reflected until you call `Book::evaluate`.
pub fn edit_current_cell<S: Into<String>>(&mut self, value: S) -> Result<()> {
self.update_entry(&self.location.clone(), value)?;
self.update_cell(&self.location.clone(), value)?;
Ok(())
}

/// Update an entry in the current sheet for a book.
/// This update won't be reflected until you call `Book::evaluate`.
pub fn update_entry<S: Into<String>>(&mut self, location: &Address, value: S) -> Result<()> {
pub fn update_cell<S: Into<String>>(&mut self, location: &Address, value: S) -> Result<()> {
self.model
.set_user_input(
self.current_sheet,
Expand Down Expand Up @@ -307,7 +358,7 @@ impl Default for Book {
fn default() -> Self {
let mut book =
Book::new(Model::new_empty("default_name", "en", "America/New_York").unwrap());
book.update_entry(&Address { row: 1, col: 1 }, "").unwrap();
book.update_cell(&Address { row: 1, col: 1 }, "").unwrap();
book
}
}
10 changes: 5 additions & 5 deletions src/book/test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -36,7 +36,7 @@ fn test_book_default() {
#[test]
fn test_book_insert_cell_new_row() {
let mut book = Book::default();
book.update_entry(&Address { row: 2, col: 1 }, "1")
book.update_cell(&Address { row: 2, col: 1 }, "1")
.expect("failed to edit cell");
book.evaluate();
let WorksheetDimension {
Expand All @@ -52,7 +52,7 @@ fn test_book_insert_cell_new_row() {
#[test]
fn test_book_insert_cell_new_column() {
let mut book = Book::default();
book.update_entry(&Address { row: 1, col: 2 }, "1")
book.update_cell(&Address { row: 1, col: 2 }, "1")
.expect("failed to edit cell");
let WorksheetDimension {
min_row,
Expand All @@ -67,7 +67,7 @@ fn test_book_insert_cell_new_column() {
#[test]
fn test_book_insert_rows() {
let mut book = Book::default();
book.update_entry(&Address { row: 2, col: 2 }, "1")
book.update_cell(&Address { row: 2, col: 2 }, "1")
.expect("failed to edit cell");
book.move_to(&Address { row: 2, col: 2 })
.expect("Failed to move to location");
Expand All @@ -85,7 +85,7 @@ fn test_book_insert_rows() {
#[test]
fn test_book_insert_columns() {
let mut book = Book::default();
book.update_entry(&Address { row: 2, col: 2 }, "1")
book.update_cell(&Address { row: 2, col: 2 }, "1")
.expect("failed to edit cell");
book.move_to(&Address { row: 2, col: 2 })
.expect("Failed to move to location");
Expand All @@ -103,7 +103,7 @@ fn test_book_insert_columns() {
#[test]
fn test_book_col_size() {
let mut book = Book::default();
book.update_entry(&Address { row: 2, col: 2 }, "1")
book.update_cell(&Address { row: 2, col: 2 }, "1")
.expect("failed to edit cell");
book.set_col_size(1, 20).expect("Failed to set column size");
assert_eq!(20, book.get_col_size(1).expect("Failed to get column size"));
Expand Down
4 changes: 2 additions & 2 deletions src/ui/cmd.rs
Original file line number Diff line number Diff line change
Expand Up @@ -5,7 +5,7 @@ use slice_utils::{Measured, Peekable, Seekable, Span, StrCursor};
#[derive(Debug, PartialEq, Eq)]
pub enum Cmd<'a> {
Write(Option<&'a str>),
InsertRow(usize),
InsertRows(usize),
InsertColumns(usize),
RenameSheet(Option<usize>, &'a str),
NewSheet(Option<&'a str>),
Expand Down Expand Up @@ -155,7 +155,7 @@ fn try_consume_insert_row<'cmd, 'i: 'cmd>(
return Err("Invalid command: Did you mean to type `insert-rows <arg>`?");
}
let arg = input.span(0..).trim();
return Ok(Some(Cmd::InsertRow(if arg.is_empty() {
return Ok(Some(Cmd::InsertRows(if arg.is_empty() {
1
} else {
if let Ok(count) = arg.parse() {
Expand Down
Loading