summaryrefslogtreecommitdiffstats
path: root/src/context
diff options
context:
space:
mode:
authorJeff Zhao <jeff.no.zhao@gmail.com>2021-06-22 22:34:42 -0400
committerJeff Zhao <jeff.no.zhao@gmail.com>2021-06-22 22:34:42 -0400
commita11ed197df4fc3830387931c684346975333baf3 (patch)
treec11b81c58ff0ac52415684e1e383ebac92b94696 /src/context
parent72aaf0c5d10db0004d48e27c58d18d8f2c568f8f (diff)
rudimentary file preview support
- this currently has only been tested with text files - no line formatting is done yet - only prints the preview as a single line - folder previews can now be pushed onto a separate thread if needed
Diffstat (limited to 'src/context')
-rw-r--r--src/context/app_context.rs19
-rw-r--r--src/context/mod.rs2
-rw-r--r--src/context/preview_context.rs27
-rw-r--r--src/context/worker_context.rs10
4 files changed, 50 insertions, 8 deletions
diff --git a/src/context/app_context.rs b/src/context/app_context.rs
index e03079a..5c206e5 100644
--- a/src/context/app_context.rs
+++ b/src/context/app_context.rs
@@ -2,16 +2,16 @@ use std::collections::VecDeque;
use std::sync::mpsc;
use crate::config;
-use crate::context::{LocalStateContext, TabContext, WorkerContext};
+use crate::context::{LocalStateContext, PreviewContext, TabContext, WorkerContext};
use crate::event::{AppEvent, Events};
use crate::util::search::SearchPattern;
pub struct AppContext {
pub exit: bool,
- // app config
- config: config::AppConfig,
// event loop querying
pub events: Events,
+ // app config
+ config: config::AppConfig,
// context related to tabs
tab_context: TabContext,
// context related to local file state
@@ -22,6 +22,8 @@ pub struct AppContext {
message_queue: VecDeque<String>,
// context related to io workers
worker_context: WorkerContext,
+ // context related to previews
+ preview_context: PreviewContext,
}
impl AppContext {
@@ -36,6 +38,7 @@ impl AppContext {
search_context: None,
message_queue: VecDeque::with_capacity(4),
worker_context: WorkerContext::new(event_tx),
+ preview_context: PreviewContext::new(),
config,
}
}
@@ -47,6 +50,9 @@ impl AppContext {
pub fn flush_event(&self) {
self.events.flush();
}
+ pub fn clone_event_tx(&self) -> mpsc::Sender<AppEvent> {
+ self.events.event_tx.clone()
+ }
pub fn config_ref(&self) -> &config::AppConfig {
&self.config
@@ -87,6 +93,13 @@ impl AppContext {
self.search_context = Some(pattern);
}
+ pub fn preview_context_ref(&self) -> &PreviewContext {
+ &self.preview_context
+ }
+ pub fn preview_context_mut(&mut self) -> &mut PreviewContext {
+ &mut self.preview_context
+ }
+
pub fn worker_context_ref(&self) -> &WorkerContext {
&self.worker_context
}
diff --git a/src/context/mod.rs b/src/context/mod.rs
index a1b8ed9..0a5f6c2 100644
--- a/src/context/mod.rs
+++ b/src/context/mod.rs
@@ -1,9 +1,11 @@
mod app_context;
mod local_state;
+mod preview_context;
mod tab_context;
mod worker_context;
pub use self::app_context::AppContext;
pub use self::local_state::LocalStateContext;
+pub use self::preview_context::PreviewContext;
pub use self::tab_context::TabContext;
pub use self::worker_context::WorkerContext;
diff --git a/src/context/preview_context.rs b/src/context/preview_context.rs
new file mode 100644
index 0000000..d344323
--- /dev/null
+++ b/src/context/preview_context.rs
@@ -0,0 +1,27 @@
+use std::collections::HashMap;
+use std::io;
+use std::path;
+use std::process;
+
+use crate::event::AppEvent;
+use crate::preview::preview_file::FilePreview;
+
+pub struct PreviewContext {
+ pub previews: HashMap<path::PathBuf, FilePreview>,
+}
+
+impl PreviewContext {
+ pub fn new() -> Self {
+ Self {
+ previews: HashMap::new(),
+ }
+ }
+
+ pub fn get_preview(&self, p: &path::Path) -> Option<&FilePreview> {
+ self.previews.get(p)
+ }
+
+ pub fn insert_preview(&mut self, p: path::PathBuf, file_preview: FilePreview) {
+ self.previews.insert(p, file_preview);
+ }
+}
diff --git a/src/context/worker_context.rs b/src/context/worker_context.rs
index bd0802c..10b464b 100644
--- a/src/context/worker_context.rs
+++ b/src/context/worker_context.rs
@@ -7,23 +7,23 @@ use crate::event::AppEvent;
use crate::io::{IoWorkerObserver, IoWorkerProgress, IoWorkerThread};
pub struct WorkerContext {
+ // to send info
+ event_tx: mpsc::Sender<AppEvent>,
// queue of IO workers
worker_queue: VecDeque<IoWorkerThread>,
// current worker
worker: Option<IoWorkerObserver>,
- // to send info
- event_tx: mpsc::Sender<AppEvent>,
}
impl WorkerContext {
pub fn new(event_tx: mpsc::Sender<AppEvent>) -> Self {
Self {
+ event_tx,
worker_queue: VecDeque::new(),
worker: None,
- event_tx,
}
}
- pub fn get_event_tx(&self) -> mpsc::Sender<AppEvent> {
+ pub fn clone_event_tx(&self) -> mpsc::Sender<AppEvent> {
self.event_tx.clone()
}
// worker related
@@ -61,7 +61,7 @@ impl WorkerContext {
}
pub fn start_next_job(&mut self) {
- let tx = self.get_event_tx();
+ let tx = self.clone_event_tx();
if let Some(worker) = self.worker_queue.pop_front() {
let src = worker.paths[0].parent().unwrap().to_path_buf();