use std::path::PathBuf;
use std::time::Duration;
use crossterm::event::{self, Event, KeyEvent, MouseEvent};
use tokio::sync::mpsc;
use crate::hydrate::{AmbiguousMatch, HydrationProgress};
use crate::store::Store;
#[derive(Debug)]
pub struct GistImportData {
pub content: String,
pub filename: String,
pub entry: crate::store::FileEntry,
}
#[derive(Debug)]
pub struct HydrationDoneData {
pub matched: usize,
pub ambiguous: Vec<AmbiguousMatch>,
pub store: Box<Store>,
pub root: PathBuf,
pub new_cursor: chrono::DateTime<chrono::Utc>,
}
#[derive(Debug)]
pub enum AsyncEvent {
PushDone {
root: PathBuf,
rel_path: String,
result: std::result::Result<crate::store::FileEntry, String>,
},
PullDone {
root: PathBuf,
rel_path: String,
expected_local_sha256: String,
result: std::result::Result<(String, crate::store::FileEntry), String>,
},
PushBlocked {
root: PathBuf,
rel_path: String,
remote_sha256: String,
remote_updated_at: chrono::DateTime<chrono::Utc>,
},
RemoteCheckDone {
root: PathBuf,
started: chrono::DateTime<chrono::Utc>,
result: std::result::Result<crate::remote::RemoteCheckOutcome, String>,
},
HydrationUpdate(HydrationProgress),
HydrationDone(std::result::Result<HydrationDoneData, String>),
StatusCheck {
root: PathBuf,
rel_path: String,
started: chrono::DateTime<chrono::Utc>,
result: std::result::Result<crate::sync::FullStatus, String>,
},
GdocFetched(std::result::Result<String, String>),
GistImportFetched(std::result::Result<GistImportData, String>),
DeleteDone {
root: PathBuf,
rel_path: String,
result: std::result::Result<(), String>,
},
LinkDone {
root: PathBuf,
rel_path: String,
result: std::result::Result<crate::store::FileEntry, String>,
},
RenameRemoteDone {
rel_path: String,
result: std::result::Result<(), String>,
},
}
#[derive(Debug)]
pub enum UiEvent {
Key(KeyEvent),
Mouse(MouseEvent),
}
pub fn poll_event(timeout: Duration) -> Option<UiEvent> {
if !event::poll(timeout).ok()? {
return None;
}
match event::read().ok()? {
Event::Key(k) => Some(UiEvent::Key(k)),
Event::Mouse(m) => Some(UiEvent::Mouse(m)),
_ => None,
}
}
pub type AsyncSender = mpsc::UnboundedSender<AsyncEvent>;
pub type AsyncReceiver = mpsc::UnboundedReceiver<AsyncEvent>;
pub fn async_channel() -> (AsyncSender, AsyncReceiver) {
mpsc::unbounded_channel()
}