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; use crate::app::app::App;
pub const LEN: usize = 1000; pub const LEN: usize = 1000;
pub const CSV_EXT: &str = "csv";
pub const CUSTOM_EXT: &str = "nscim";
pub struct Grid { pub struct Grid {
// a b c ... // 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 /// Save file to `path` as a csv. Path with have `csv` appended to it if it
/// does not already have the extension. /// does not already have the extension.
pub fn save_to(&mut self, path: impl Into<PathBuf>) -> std::io::Result<()> { pub fn save_to(&mut self, path: impl Into<PathBuf>) -> std::io::Result<()> {
let mut path = path.into(); let path = path.into();
const CSV: &str = "csv";
const CUSTOM_EXT: &str = "nscim";
let resolve_values; let resolve_values;
match path.extension() { match path.extension() {
Some(ext) => match ext.to_str() { Some(ext) => match ext.to_str() {
Some(CSV) => { Some(CSV_EXT) => {
resolve_values = true; resolve_values = true;
} }
Some(CUSTOM_EXT) => { Some(CUSTOM_EXT) => {
@@ -97,12 +96,12 @@ impl Grid {
} }
_ => { _ => {
resolve_values = false; resolve_values = false;
path.add_extension(CUSTOM_EXT); // path.add_extension(CUSTOM_EXT);
} }
}, },
None => { None => {
resolve_values = false; 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::{ use ratatui::{
prelude, prelude,
@@ -9,7 +13,10 @@ use ratatui::{
use crate::app::{ use crate::app::{
app::App, app::App,
error_msg::StatusMessage, error_msg::StatusMessage,
logic::{calc::LEN, cell::CellType}, logic::{
calc::{CSV_EXT, CUSTOM_EXT, LEN},
cell::CellType,
},
}; };
pub enum Mode { pub enum Mode {
@@ -59,13 +66,32 @@ impl Mode {
"w" => { "w" => {
// first try the passed argument as file // first try the passed argument as file
if let Some(arg) = args.get(1) { 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}")); app.msg = StatusMessage::error(format!("{e}"));
} else { } else {
// file saving was a success, adopt the provided file // file saving was a success, adopt the provided file
// if we don't already have one (this is how vim works) // if we don't already have one (this is how vim works)
let path: PathBuf = arg.into(); app.msg = StatusMessage::info(format!(
app.msg = StatusMessage::info(format!("Saved file {}", path.file_name().map(|f| f.to_str().unwrap_or("n/a")).unwrap_or("n/a"))); "Saved file {}",
path.file_name().map(|f| f.to_str().unwrap_or("n/a")).unwrap_or("n/a")
));
if let None = app.file { if let None = app.file {
app.file = Some(path) app.file = Some(path)
@@ -76,7 +102,10 @@ impl Mode {
if let Err(e) = app.grid.save_to(file) { if let Err(e) = app.grid.save_to(file) {
app.msg = StatusMessage::error(format!("{e}")); app.msg = StatusMessage::error(format!("{e}"));
} else { } 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* // you need to provide a file from *somewhere*
} else { } else {
@@ -168,13 +197,13 @@ impl Mode {
'A' => { 'A' => {
let c = app.grid.cursor(); let c = app.grid.cursor();
app.grid.insert_column_after(c); 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 // insert row below
'o' => { 'o' => {
let c = app.grid.cursor(); let c = app.grid.cursor();
app.grid.insert_row_below(c); 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 // insert row above
'O' => { 'O' => {
@@ -200,7 +229,7 @@ impl Mode {
match key { match key {
'd' | 'x' => { '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 app.mode = Mode::Normal
} }
'y' => { 'y' => {
@@ -284,7 +313,7 @@ impl Mode {
app.clipboard.paste(&mut app.grid, false); app.clipboard.paste(&mut app.grid, false);
app.grid.apply_momentum(app.clipboard.momentum()); app.grid.apply_momentum(app.clipboard.momentum());
app.mode = Mode::Normal; 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")); app.msg = StatusMessage::info(format!("Pasted {plural}, no formatting"));
return; return;
} }
@@ -294,8 +323,8 @@ impl Mode {
} }
} }
// IDK why it works but it does. Keystrokes are process somewhere else? // IDK why it works but it does. Keystrokes are process somewhere else?
Mode::Insert(_chord) => {}, Mode::Insert(_chord) => {}
Mode::Command(_chord) => {}, Mode::Command(_chord) => {}
} }
} }
@@ -332,7 +361,6 @@ impl Mode {
Mode::Visual(_) => {} Mode::Visual(_) => {}
} }
} }
} }
pub struct Chord { pub struct Chord {