reorganize file extension logic
This commit is contained in:
@@ -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);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
@@ -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 {
|
||||
@@ -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 {
|
||||
|
||||
Reference in New Issue
Block a user