Store file path in savefile struct
This commit is contained in:
parent
568fd08dcb
commit
c094c646f5
@ -6,7 +6,9 @@ mod test;
|
|||||||
|
|
||||||
|
|
||||||
use core::fmt;
|
use core::fmt;
|
||||||
use std::io::{Read, Write};
|
use std::fs::File;
|
||||||
|
use std::io::{self, Read, Write};
|
||||||
|
use std::path::{Path, PathBuf};
|
||||||
|
|
||||||
use binrw::{until_eof, BinRead, BinReaderExt, BinWriterExt};
|
use binrw::{until_eof, BinRead, BinReaderExt, BinWriterExt};
|
||||||
use chrono::{DateTime, NaiveDateTime, Utc};
|
use chrono::{DateTime, NaiveDateTime, Utc};
|
||||||
@ -43,6 +45,9 @@ pub enum Error {
|
|||||||
|
|
||||||
#[error("Failed to serialize savefile")]
|
#[error("Failed to serialize savefile")]
|
||||||
SerializationFailed(binrw::Error),
|
SerializationFailed(binrw::Error),
|
||||||
|
|
||||||
|
#[error("Failed to read file")]
|
||||||
|
FileReadingFailed(io::Error),
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
@ -66,6 +71,9 @@ impl fmt::Display for RobeColor {
|
|||||||
#[derive(Debug, Clone)]
|
#[derive(Debug, Clone)]
|
||||||
#[brw(little)]
|
#[brw(little)]
|
||||||
pub struct Savefile {
|
pub struct Savefile {
|
||||||
|
#[brw(ignore)]
|
||||||
|
pub path: PathBuf,
|
||||||
|
|
||||||
#[br(count = 8)]
|
#[br(count = 8)]
|
||||||
_unknown0: Vec<u8>,
|
_unknown0: Vec<u8>,
|
||||||
|
|
||||||
@ -124,6 +132,19 @@ pub struct Savefile {
|
|||||||
}
|
}
|
||||||
|
|
||||||
impl Savefile {
|
impl Savefile {
|
||||||
|
pub fn from_path<P>(path: P) -> Result<Self>
|
||||||
|
where
|
||||||
|
P: AsRef<Path>,
|
||||||
|
{
|
||||||
|
let file = File::open(&path).map_err(Error::FileReadingFailed)?;
|
||||||
|
let savefile = Self {
|
||||||
|
path: path.as_ref().to_owned(),
|
||||||
|
..Self::from_reader(file)?
|
||||||
|
};
|
||||||
|
|
||||||
|
Ok(savefile)
|
||||||
|
}
|
||||||
|
|
||||||
pub fn from_reader<R>(mut reader: R) -> Result<Self>
|
pub fn from_reader<R>(mut reader: R) -> Result<Self>
|
||||||
where
|
where
|
||||||
R: Read + BinReaderExt,
|
R: Read + BinReaderExt,
|
||||||
|
@ -1,8 +1,7 @@
|
|||||||
#![cfg(feature = "tui")]
|
#![cfg(feature = "tui")]
|
||||||
|
|
||||||
use std::fs::File;
|
|
||||||
use std::io::{self, Stdout};
|
use std::io::{self, Stdout};
|
||||||
use std::path::{Path, PathBuf};
|
use std::path::PathBuf;
|
||||||
use std::sync::mpsc::{self, TryRecvError};
|
use std::sync::mpsc::{self, TryRecvError};
|
||||||
use std::time::Duration;
|
use std::time::Duration;
|
||||||
|
|
||||||
@ -32,7 +31,6 @@ pub struct Args {
|
|||||||
|
|
||||||
|
|
||||||
struct State {
|
struct State {
|
||||||
current_path: PathBuf,
|
|
||||||
current_file: Savefile,
|
current_file: Savefile,
|
||||||
mode: Mode,
|
mode: Mode,
|
||||||
file_select: Input,
|
file_select: Input,
|
||||||
@ -76,11 +74,10 @@ type Frame<'a> = ratatui::Frame<'a, CrosstermBackend<Stdout>>;
|
|||||||
pub(crate) fn execute(_app_args: &AppArgs, args: &Args) -> Result<()> {
|
pub(crate) fn execute(_app_args: &AppArgs, args: &Args) -> Result<()> {
|
||||||
let mut terminal = setup()?;
|
let mut terminal = setup()?;
|
||||||
|
|
||||||
let savefile = load_savefile(&args.path)?;
|
let savefile = Savefile::from_path(&args.path)?;
|
||||||
|
|
||||||
// TODO: prompt file path
|
// TODO: prompt file path
|
||||||
let state = State {
|
let state = State {
|
||||||
current_path: args.path.clone(),
|
|
||||||
current_file: savefile,
|
current_file: savefile,
|
||||||
mode: Mode::default(),
|
mode: Mode::default(),
|
||||||
file_select: Input::default(),
|
file_select: Input::default(),
|
||||||
@ -123,8 +120,7 @@ fn run(terminal: &mut Terminal, mut state: State) -> Result<()> {
|
|||||||
Message::LoadFile => {
|
Message::LoadFile => {
|
||||||
let path = PathBuf::from(state.file_select.value());
|
let path = PathBuf::from(state.file_select.value());
|
||||||
|
|
||||||
state.current_file = load_savefile(&path)?;
|
state.current_file = Savefile::from_path(&path)?;
|
||||||
state.current_path = path;
|
|
||||||
|
|
||||||
#[cfg(feature = "watch")]
|
#[cfg(feature = "watch")]
|
||||||
if state.file_watcher.is_some() {
|
if state.file_watcher.is_some() {
|
||||||
@ -146,7 +142,7 @@ fn run(terminal: &mut Terminal, mut state: State) -> Result<()> {
|
|||||||
let callback = move || {
|
let callback = move || {
|
||||||
evq_tx.send(Message::ReloadFile).unwrap();
|
evq_tx.send(Message::ReloadFile).unwrap();
|
||||||
};
|
};
|
||||||
let file_watcher = FileWatcher::new(&state.current_path, callback);
|
let file_watcher = FileWatcher::new(&state.current_file.path, callback);
|
||||||
state.file_watcher = Some(file_watcher);
|
state.file_watcher = Some(file_watcher);
|
||||||
} else {
|
} else {
|
||||||
state.file_watcher = None;
|
state.file_watcher = None;
|
||||||
@ -155,7 +151,7 @@ fn run(terminal: &mut Terminal, mut state: State) -> Result<()> {
|
|||||||
|
|
||||||
#[cfg(feature = "watch")]
|
#[cfg(feature = "watch")]
|
||||||
Message::ReloadFile => {
|
Message::ReloadFile => {
|
||||||
let savefile = load_savefile(&state.current_path)?;
|
let savefile = Savefile::from_path(&state.current_file.path)?;
|
||||||
state.current_file = savefile;
|
state.current_file = savefile;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@ -244,17 +240,6 @@ fn reset(mut terminal: Terminal) -> Result<()> {
|
|||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
fn load_savefile<P>(path: P) -> Result<Savefile>
|
|
||||||
where
|
|
||||||
P: AsRef<Path>,
|
|
||||||
{
|
|
||||||
let file = File::open(path)?;
|
|
||||||
let savefile = Savefile::from_reader(file)?;
|
|
||||||
|
|
||||||
Ok(savefile)
|
|
||||||
}
|
|
||||||
|
|
||||||
|
|
||||||
fn render(state: &State, mut frame: &mut Frame) {
|
fn render(state: &State, mut frame: &mut Frame) {
|
||||||
let rows = Layout::default()
|
let rows = Layout::default()
|
||||||
.direction(Direction::Vertical)
|
.direction(Direction::Vertical)
|
||||||
@ -268,13 +253,13 @@ fn render(state: &State, mut frame: &mut Frame) {
|
|||||||
match state.mode {
|
match state.mode {
|
||||||
#[cfg(feature = "watch")]
|
#[cfg(feature = "watch")]
|
||||||
Mode::Normal if state.file_watcher.is_some() => {
|
Mode::Normal if state.file_watcher.is_some() => {
|
||||||
let text = format!("Watching file: {}", state.current_path.display());
|
let text = format!("Watching file: {}", state.current_file.path.display());
|
||||||
let status = Paragraph::new(text).block(status_block);
|
let status = Paragraph::new(text).block(status_block);
|
||||||
frame.render_widget(status, rows[1]);
|
frame.render_widget(status, rows[1]);
|
||||||
}
|
}
|
||||||
|
|
||||||
Mode::Normal => {
|
Mode::Normal => {
|
||||||
let text = format!("Showing file: {}", state.current_path.display());
|
let text = format!("Showing file: {}", state.current_file.path.display());
|
||||||
let status = Paragraph::new(text).block(status_block);
|
let status = Paragraph::new(text).block(status_block);
|
||||||
frame.render_widget(status, rows[1]);
|
frame.render_widget(status, rows[1]);
|
||||||
}
|
}
|
||||||
|
Loading…
Reference in New Issue
Block a user