feat: auto hover the file after it created (#10)

This commit is contained in:
三咲雅 · Misaki Masa 2023-07-27 10:19:34 +08:00 committed by GitHub
parent a42da833fe
commit f01e1a272f
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 26 additions and 12 deletions

View File

@ -4,7 +4,7 @@ use anyhow::Result;
use crossterm::event::KeyEvent;
use tokio::sync::{mpsc::UnboundedSender, oneshot};
use super::{files::FilesOp, input::InputOpt, manager::PreviewData, select::SelectOpt};
use super::{files::{File, FilesOp}, input::InputOpt, manager::PreviewData, select::SelectOpt};
use crate::config::{keymap::{Control, KeymapLayer}, open::Opener};
static mut TX: Option<UnboundedSender<Event>> = None;
@ -24,7 +24,7 @@ pub enum Event {
Files(FilesOp),
Pages(usize),
Mimetype(BTreeMap<PathBuf, String>),
Hover,
Hover(Option<File>),
Preview(PathBuf, PreviewData),
// Input
@ -88,6 +88,12 @@ macro_rules! emit {
(Mimetype($mimes:expr)) => {
$crate::core::Event::Mimetype($mimes).emit();
};
(Hover) => {
$crate::core::Event::Hover(None).emit();
};
(Hover($file:expr)) => {
$crate::core::Event::Hover(Some($file)).emit();
};
(Preview($path:expr, $data:expr)) => {
$crate::core::Event::Preview($path, $data).emit();
};

View File

@ -146,19 +146,16 @@ impl Folder {
}
pub fn hover(&mut self, path: &Path) -> bool {
if matches!(self.hovered, Some(ref h) if h.path == path) {
return false;
}
let new = self.position(path).unwrap_or(self.cursor);
if new > self.cursor { self.next(new - self.cursor) } else { self.prev(self.cursor - new) }
}
pub fn hover_force(&mut self, file: File) -> bool {
if !self.hover(&file.path) && self.files.is_empty() {
self.hovered = Some(file);
if self.hover(&file.path) {
return true;
}
self.hovered = Some(file);
false
}
}

View File

@ -1,5 +1,6 @@
use std::{collections::{BTreeMap, BTreeSet, HashMap, HashSet}, env, mem, path::PathBuf};
use anyhow::Error;
use tokio::fs;
use super::{PreviewData, Tab, Tabs, Watcher};
@ -179,13 +180,21 @@ impl Manager {
if let Ok(name) = result.await {
let path = cwd.join(&name);
let hovered = path.components().take(cwd.components().count() + 1).collect::<PathBuf>();
if name.ends_with('/') {
fs::create_dir_all(path).await.ok();
fs::create_dir_all(path).await?;
} else {
fs::create_dir_all(path.parent().unwrap()).await.ok();
fs::File::create(path).await.ok();
fs::File::create(path).await?;
}
if let Ok(file) = File::from(&hovered).await {
emit!(Hover(file));
emit!(Refresh);
}
}
Ok::<(), Error>(())
});
false
}

View File

@ -125,8 +125,10 @@ impl App {
emit!(Render);
}
}
Event::Hover => {
if manager.preview() {
Event::Hover(file) => {
let mut b = file.map(|f| manager.current_mut().hover_force(f)).unwrap_or(false);
b |= manager.preview();
if b {
emit!(Render);
}
}