summaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJesse Luehrs <doy@tozt.net>2022-02-27 16:07:56 -0500
committerJesse Luehrs <doy@tozt.net>2022-02-27 16:07:56 -0500
commit68691cedbfc82f7c2a57f6aaa0f2552f8ad1f8ba (patch)
tree46ddfcc38901244b1560255649e401390d62823c /src
parentdfd5ebfa25d799399de0d15cb38ba47f5069647b (diff)
downloadnbsh-68691cedbfc82f7c2a57f6aaa0f2552f8ad1f8ba.tar.gz
nbsh-68691cedbfc82f7c2a57f6aaa0f2552f8ad1f8ba.zip
simplify
Diffstat (limited to 'src')
-rw-r--r--src/main.rs1
-rw-r--r--src/shell/event.rs76
2 files changed, 31 insertions, 46 deletions
diff --git a/src/main.rs b/src/main.rs
index 65f1e52..f8db695 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -6,6 +6,7 @@
#![warn(clippy::get_unwrap)]
#![allow(clippy::cognitive_complexity)]
#![allow(clippy::missing_const_for_fn)]
+#![allow(clippy::option_option)]
#![allow(clippy::similar_names)]
#![allow(clippy::struct_excessive_bools)]
#![allow(clippy::too_many_arguments)]
diff --git a/src/shell/event.rs b/src/shell/event.rs
index 8f21081..b159a95 100644
--- a/src/shell/event.rs
+++ b/src/shell/event.rs
@@ -35,8 +35,20 @@ impl Writer {
pub struct Reader(std::sync::Arc<InnerReader>);
impl Reader {
- pub fn new(input: tokio::sync::mpsc::UnboundedReceiver<Event>) -> Self {
- Self(InnerReader::new(input))
+ pub fn new(
+ mut input: tokio::sync::mpsc::UnboundedReceiver<Event>,
+ ) -> Self {
+ let inner = std::sync::Arc::new(InnerReader::new());
+ {
+ let inner = inner.clone();
+ tokio::task::spawn(async move {
+ while let Some(event) = input.recv().await {
+ inner.new_event(Some(event)).await;
+ }
+ inner.new_event(None).await;
+ });
+ }
+ Self(inner)
}
pub async fn recv(&self) -> Option<Event> {
@@ -50,40 +62,24 @@ struct InnerReader {
}
impl InnerReader {
- fn new(
- mut input: tokio::sync::mpsc::UnboundedReceiver<Event>,
- ) -> std::sync::Arc<Self> {
- let this = Self {
+ fn new() -> Self {
+ Self {
pending: tokio::sync::Mutex::new(Pending::new()),
cvar: tokio::sync::Notify::new(),
- };
- let this = std::sync::Arc::new(this);
- {
- let this = this.clone();
- tokio::task::spawn(async move {
- while let Some(event) = input.recv().await {
- this.new_event(Some(event)).await;
- }
- this.new_event(None).await;
- });
}
- this
}
async fn recv(&self) -> Option<Event> {
loop {
- let mut pending = self.pending.lock().await;
- if pending.has_event() {
- return pending.get_event();
+ if let Some(event) = self.pending.lock().await.get_event() {
+ return event;
}
- drop(pending);
self.cvar.notified().await;
}
}
async fn new_event(&self, event: Option<Event>) {
- let mut pending = self.pending.lock().await;
- pending.new_event(event);
+ self.pending.lock().await.new_event(event);
self.cvar.notify_one();
}
}
@@ -107,53 +103,41 @@ impl Pending {
Self::default()
}
- fn has_event(&self) -> bool {
- self.done
- || !self.key.is_empty()
- || self.size.is_some()
- || self.pty_output
- || self.pty_close
- || !self.child_run_pipeline.is_empty()
- || !self.child_suspend.is_empty()
- || self.git_info.is_some()
- || self.clock_timer
- }
-
- fn get_event(&mut self) -> Option<Event> {
+ fn get_event(&mut self) -> Option<Option<Event>> {
if self.done {
- return None;
+ return Some(None);
}
if let Some(key) = self.key.pop_front() {
- return Some(Event::Key(key));
+ return Some(Some(Event::Key(key)));
}
if let Some(size) = self.size.take() {
- return Some(Event::Resize(size));
+ return Some(Some(Event::Resize(size)));
}
if self.pty_close {
self.pty_close = false;
- return Some(Event::PtyClose);
+ return Some(Some(Event::PtyClose));
}
if let Some((idx, span)) = self.child_run_pipeline.pop_front() {
- return Some(Event::ChildRunPipeline(idx, span));
+ return Some(Some(Event::ChildRunPipeline(idx, span)));
}
if let Some(idx) = self.child_suspend.pop_front() {
- return Some(Event::ChildSuspend(idx));
+ return Some(Some(Event::ChildSuspend(idx)));
}
if let Some(info) = self.git_info.take() {
- return Some(Event::GitInfo(info));
+ return Some(Some(Event::GitInfo(info)));
}
if self.clock_timer {
self.clock_timer = false;
- return Some(Event::ClockTimer);
+ return Some(Some(Event::ClockTimer));
}
// process_output should be last because it will often be the case
// that there is ~always new process output (cat on large files, yes,
// etc) and that shouldn't prevent other events from happening
if self.pty_output {
self.pty_output = false;
- return Some(Event::PtyOutput);
+ return Some(Some(Event::PtyOutput));
}
- unreachable!()
+ None
}
fn new_event(&mut self, event: Option<Event>) {