reorganize file extension logic

This commit is contained in:
2025-11-14 09:40:53 -07:00
parent 9c44db0d92
commit 902af1311d
2 changed files with 48 additions and 21 deletions

View File

@@ -18,6 +18,8 @@ use crate::app::{
use crate::app::app::App;
pub const LEN: usize = 1000;
pub const CSV_EXT: &str = "csv";
pub const CUSTOM_EXT: &str = "nscim";
pub struct Grid {
// a b c ...
@@ -80,16 +82,13 @@ impl Grid {
/// Save file to `path` as a csv. Path with have `csv` appended to it if it
/// does not already have the extension.
pub fn save_to(&mut self, path: impl Into<PathBuf>) -> std::io::Result<()> {
let mut path = path.into();
const CSV: &str = "csv";
const CUSTOM_EXT: &str = "nscim";
let path = path.into();
let resolve_values;
match path.extension() {
Some(ext) => match ext.to_str() {
Some(CSV) => {
Some(CSV_EXT) => {
resolve_values = true;
}
Some(CUSTOM_EXT) => {
@@ -97,12 +96,12 @@ impl Grid {
}
_ => {
resolve_values = false;
path.add_extension(CUSTOM_EXT);
// path.add_extension(CUSTOM_EXT);
}
},
None => {
resolve_values = false;
path.add_extension(CUSTOM_EXT);
// path.add_extension(CUSTOM_EXT);
}
}

View File

@@ -1,4 +1,8 @@
use std::{cmp::{max, min}, fmt::Display, path::PathBuf};
use std::{
cmp::{max, min},
fmt::Display,
path::PathBuf,
};
use ratatui::{
prelude,
@@ -9,7 +13,10 @@ use ratatui::{
use crate::app::{
app::App,
error_msg::StatusMessage,
logic::{calc::LEN, cell::CellType},
logic::{
calc::{CSV_EXT, CUSTOM_EXT, LEN},
cell::CellType,
},
};
pub enum Mode {
@@ -59,13 +66,32 @@ impl Mode {
"w" => {
// first try the passed argument as file
if let Some(arg) = args.get(1) {
if let Err(e) = app.grid.save_to(arg) {
let mut path: PathBuf = arg.into();
match path.extension() {
Some(s) => {
match s.to_str() {
// leave the file alone, it already has
// a valid extension
Some(CSV_EXT) | Some(CUSTOM_EXT) => {}
_ => {
path.add_extension(CUSTOM_EXT);
}
}
}
None => {
path.add_extension(CUSTOM_EXT);
}
};
if let Err(e) = app.grid.save_to(&path) {
app.msg = StatusMessage::error(format!("{e}"));
} else {
// file saving was a success, adopt the provided file
// if we don't already have one (this is how vim works)
let path: PathBuf = arg.into();
app.msg = StatusMessage::info(format!("Saved file {}", path.file_name().map(|f| f.to_str().unwrap_or("n/a")).unwrap_or("n/a")));
app.msg = StatusMessage::info(format!(
"Saved file {}",
path.file_name().map(|f| f.to_str().unwrap_or("n/a")).unwrap_or("n/a")
));
if let None = app.file {
app.file = Some(path)
@@ -76,7 +102,10 @@ impl Mode {
if let Err(e) = app.grid.save_to(file) {
app.msg = StatusMessage::error(format!("{e}"));
} else {
app.msg = StatusMessage::info(format!("Saved file {}", file.file_name().map(|f| f.to_str().unwrap_or("n/a")).unwrap_or("n/a")));
app.msg = StatusMessage::info(format!(
"Saved file {}",
file.file_name().map(|f| f.to_str().unwrap_or("n/a")).unwrap_or("n/a")
));
}
// you need to provide a file from *somewhere*
} else {
@@ -168,13 +197,13 @@ impl Mode {
'A' => {
let c = app.grid.cursor();
app.grid.insert_column_after(c);
app.grid.mv_cursor_to(c.0+1, c.1);
app.grid.mv_cursor_to(c.0 + 1, c.1);
}
// insert row below
'o' => {
let c = app.grid.cursor();
app.grid.insert_row_below(c);
app.grid.mv_cursor_to(c.0, c.1+1);
app.grid.mv_cursor_to(c.0, c.1 + 1);
}
// insert row above
'O' => {
@@ -200,7 +229,7 @@ impl Mode {
match key {
'd' | 'x' => {
app.clipboard.clipboard_cut((x1,y1), (x2,y2), &mut app.grid);
app.clipboard.clipboard_cut((x1, y1), (x2, y2), &mut app.grid);
app.mode = Mode::Normal
}
'y' => {
@@ -284,7 +313,7 @@ impl Mode {
app.clipboard.paste(&mut app.grid, false);
app.grid.apply_momentum(app.clipboard.momentum());
app.mode = Mode::Normal;
let plural = if app.clipboard.qty() > 1 {"cells"} else {"cell"};
let plural = if app.clipboard.qty() > 1 { "cells" } else { "cell" };
app.msg = StatusMessage::info(format!("Pasted {plural}, no formatting"));
return;
}
@@ -294,8 +323,8 @@ impl Mode {
}
}
// IDK why it works but it does. Keystrokes are process somewhere else?
Mode::Insert(_chord) => {},
Mode::Command(_chord) => {},
Mode::Insert(_chord) => {}
Mode::Command(_chord) => {}
}
}
@@ -332,7 +361,6 @@ impl Mode {
Mode::Visual(_) => {}
}
}
}
pub struct Chord {