Skip to content

Commit

Permalink
Don't trigger updates for useless events
Browse files Browse the repository at this point in the history
This should limit CPU usage slightly, and also prevents the strange situation where the request timer ticks faster when wiggling the cursor.
  • Loading branch information
LucasPickering committed Sep 24, 2024
1 parent b28e36f commit 88221e0
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 6 deletions.
1 change: 1 addition & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ The format is based on [Keep a Changelog](https://keepachangelog.com/en/1.1.0/),
- Retain all request history when collection file is reloaded
- Previously, pending and failed requests were lost on reload within a single session. These will still be lost when a session is exited.
- Fix serialization of query parameter lists
- Don't update UI for useless events (e.g. cursor moves)

## [2.0.0] - 2024-09-06

Expand Down
19 changes: 13 additions & 6 deletions crates/tui/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ use crossterm::{
event::{DisableMouseCapture, EnableMouseCapture, Event, EventStream},
terminal::{EnterAlternateScreen, LeaveAlternateScreen},
};
use futures::StreamExt;
use futures::{pin_mut, StreamExt};
use notify::{event::ModifyKind, RecursiveMode, Watcher};
use ratatui::{prelude::CrosstermBackend, Terminal};
use slumber_config::{Action, Config};
Expand Down Expand Up @@ -148,7 +148,14 @@ impl Tui {

let input_engine = &TuiContext::get().input_engine;
// Stream of terminal input events
let mut input_stream = EventStream::new();
let input_stream =
// Events that don't map to a message (cursor move, focus, etc.)
// should be filtered out entirely so they don't trigger any updates
EventStream::new().filter_map(|event_result| async move {
let event = event_result.expect("Error reading terminal input");
input_engine.event_to_message(event)
});
pin_mut!(input_stream);

self.draw()?; // Initial draw

Expand All @@ -160,11 +167,11 @@ impl Tui {
// while the queue is empty so we don't waste CPU cycles. The
// timeout here makes sure we don't block forever, so things like
// time displays during in-flight requests will update.

let message = select! {
event_result = input_stream.next() => {
if let Some(event) = event_result {
let event = event.expect("Error reading terminal input");
input_engine.event_to_message(event)
event_option = input_stream.next() => {
if let Some(event) = event_option {
Some(event)
} else {
// We ran out of input, just end the program
break;
Expand Down

0 comments on commit 88221e0

Please sign in to comment.