Write file

This commit is contained in:
Greg Shuflin committed 2024-03-03 22:19:01 -08:00
1 parent 3489e1484a
commit 496fb4f3fd
3 files changed
+33 -11

No files matched your search

+28 -9
View File
@@ -5,14 +5,21 @@ pub(crate) mod commands;
mod cursor;
mod error;
use std::{collections::HashMap, path::Path};
use std::{
collections::HashMap,
io::Write,
path::{Path, PathBuf},
};
use tokio::sync::mpsc::UnboundedReceiver;
use tracing::{error, info};
use crate::{
configuration::Configuration,
core::{actions::{CommandAction, EditorAction}, error::BasicErrorKind},
core::{
actions::{CommandAction, EditorAction},
error::BasicErrorKind,
},
};
use self::{
@@ -102,14 +109,26 @@ impl EditorCore {
Ok(())
}
fn write_current_buffer(&mut self) -> Result<(), BasicError> {
fn write_current_buffer(&mut self, path: Option<PathBuf>) -> Result<(), BasicError> {
let active_buffer = self.active_buffer();
if let Some(ref path) = active_buffer.backing_file {
todo!()
} else {
error!("No path to write buffer to");
return Err(BasicErrorKind::InvalidArgument.to_error());
let filename = match (&active_buffer.backing_file, &path) {
(_, Some(ref path)) => path,
(Some(ref path), None) => path,
(None, None) => {
error!("No path to write buffer to");
return Err(BasicErrorKind::InvalidArgument.to_error());
}
};
//TODO write file and then atomically move
//TODO cf. https://docs.rs/crop/latest/crop/index.html example, do this write
//asynchronously
let mut file = std::fs::File::create(&filename)?;
for chunk in active_buffer.text.chunks() {
file.write_all(chunk.as_bytes())?;
}
Ok(())
}
pub(crate) fn process_commands(&mut self) {
@@ -123,7 +142,7 @@ impl EditorCore {
CommandOutput::Error(err) => {
error!(err, "Command Output Error");
}
CommandOutput::WriteBuffer => log_only(self.write_current_buffer()),
CommandOutput::WriteBuffer(path) => log_only(self.write_current_buffer(path)),
};
self.handle_action(EditorAction::TransitionToMode(Mode::Normal));
+1 -1
View File
@@ -10,7 +10,7 @@ use self::api::CommandAPI;
#[derive(Debug)]
pub(crate) enum CommandOutput {
OpenFile(PathBuf),
WriteBuffer,
WriteBuffer(Option<PathBuf>),
Quit,
Error(String),
}
+4 -1
View File
@@ -20,7 +20,10 @@ impl CommandAPI {
};
match operation {
Command::Write => Some(CommandOutput::WriteBuffer),
Command::Write => {
let path = commands.next().map(PathBuf::from);
Some(CommandOutput::WriteBuffer(path))
}
Command::Open => {
let path = match commands.next() {
Some(p) => PathBuf::from(p),