Skip to content

Commit

Permalink
Redo 'redraw' logic
Browse files Browse the repository at this point in the history
Switches from static mutable variable for signaling when to completely redraw the app, to using `InternalEvent` via the `App.queue`.
  • Loading branch information
jonstodle committed Jun 20, 2020
1 parent 4586246 commit d5171a7
Show file tree
Hide file tree
Showing 5 changed files with 34 additions and 35 deletions.
16 changes: 16 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,6 +43,9 @@ pub struct App {
stashlist_tab: StashList,
queue: Queue,
theme: Theme,

// "Flags"
requires_redraw: bool,
}

// public interface
Expand Down Expand Up @@ -74,6 +77,7 @@ impl App {
stashlist_tab: StashList::new(&queue, &theme),
queue,
theme,
requires_redraw: false,
}
}

Expand Down Expand Up @@ -220,6 +224,11 @@ impl App {
|| self.stashing_tab.anything_pending()
|| self.inspect_commit_popup.any_work_pending()
}

///
pub fn requires_redraw(&self) -> bool {
self.requires_redraw
}
}

// private impls
Expand Down Expand Up @@ -304,6 +313,10 @@ impl App {

fn process_queue(&mut self) -> Result<NeedsUpdate> {
let mut flags = NeedsUpdate::empty();

// Reset "flags"
self.requires_redraw = false;

loop {
let front = self.queue.borrow_mut().pop_front();
if let Some(e) = front {
Expand Down Expand Up @@ -362,6 +375,9 @@ impl App {
self.inspect_commit_popup.open(id)?;
flags.insert(NeedsUpdate::ALL | NeedsUpdate::COMMANDS)
}
InternalEvent::RequestRedraw => {
self.requires_redraw = true
}
};

Ok(flags)
Expand Down
27 changes: 10 additions & 17 deletions src/components/commit.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,11 +2,9 @@ use super::{
textinput::TextInputComponent, visibility_blocking,
CommandBlocking, CommandInfo, Component, DrawableComponent,
};
use crate::poll::QueueEvent;
use crate::strings::COMMIT_EDITOR_MSG;
use crate::{
get_app_config_path,
keys,
get_app_config_path, keys,
queue::{InternalEvent, NeedsUpdate, Queue},
strings,
ui::style::Theme,
Expand All @@ -16,8 +14,7 @@ use asyncgit::{
sync::{self, CommitId},
CWD,
};
use crossterm::event::Event;use asyncgit::{sync, CWD};
use crossterm::event::{Event, KeyCode};
use crossterm::event::Event;
use std::env;
use std::fs::File;
use std::io::{Read, Write};
Expand Down Expand Up @@ -156,13 +153,9 @@ impl CommitComponent {
crate::poll::SHOULD_DO_POLL
.fetch_add(1, std::sync::atomic::Ordering::SeqCst);
Command::new(command).args(editor).status()?;
// safety: this is set during app setup, and _should_ never be mutated again
unsafe {
crate::COMMANDS_CHANNEL
.as_ref()
.unwrap()
.send(QueueEvent::FullRedraw)?
}
self.queue
.borrow_mut()
.push_back(InternalEvent::RequestRedraw);
crate::poll::SHOULD_DO_POLL
.fetch_add(1, std::sync::atomic::Ordering::SeqCst);

Expand All @@ -174,13 +167,13 @@ impl CommitComponent {
std::fs::remove_file(&config_path)?;

let message: String =
message.lines().filter(|l| !l.starts_with("#")).collect();
message.lines().filter(|l| !l.starts_with('#')).collect();

if !message.chars().all(|c| c.is_whitespace()) {
self.commit_msg(message)
} else {
Ok(())
if !message.chars().all(char::is_whitespace) {
return self.commit_msg(message);
}

Ok(())
}

fn commit(&mut self) -> Result<()> {
Expand Down
23 changes: 6 additions & 17 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
// #![forbid(unsafe_code)]
#![forbid(unsafe_code)]
#![deny(clippy::cargo)]
//TODO: remove once crossterm upgraded to current mio:
//https://github.com/crossterm-rs/crossterm/issues/432
Expand Down Expand Up @@ -29,7 +29,7 @@ use clap::{
crate_authors, crate_description, crate_name, crate_version,
App as ClapApp, Arg,
};
use crossbeam_channel::{tick, unbounded, Receiver, Select, Sender};
use crossbeam_channel::{tick, unbounded, Receiver, Select};
use crossterm::{
terminal::{
disable_raw_mode, enable_raw_mode, EnterAlternateScreen,
Expand Down Expand Up @@ -58,8 +58,6 @@ use tui::{
static TICK_INTERVAL: Duration = Duration::from_secs(5);
static SPINNER_INTERVAL: Duration = Duration::from_millis(50);

static mut COMMANDS_CHANNEL: Option<Sender<QueueEvent>> = None;

fn main() -> Result<()> {
process_cmdline()?;

Expand All @@ -80,12 +78,6 @@ fn main() -> Result<()> {

let mut terminal = start_terminal(io::stdout())?;

let (tx_commands, rx_commands) = unbounded();
// safety: the app hasn't started running yet, so this variable is not yet in use
unsafe {
COMMANDS_CHANNEL = Some(tx_commands);
}

let (tx_git, rx_git) = unbounded();

let mut app = App::new(&tx_git);
Expand All @@ -105,7 +97,6 @@ fn main() -> Result<()> {
&rx_git,
&ticker,
&spinner_ticker,
&rx_commands,
)?;

{
Expand All @@ -122,9 +113,6 @@ fn main() -> Result<()> {
needs_draw = false;
spinner.update()
}
QueueEvent::FullRedraw => {
terminal.resize(terminal.size()?)?
}
}
}

Expand Down Expand Up @@ -159,6 +147,10 @@ fn draw<B: Backend>(
terminal: &mut Terminal<B>,
app: &mut App,
) -> io::Result<()> {
if app.requires_redraw() {
terminal.resize(terminal.size()?)?;
}

terminal.draw(|mut f| {
if let Err(e) = app.draw(&mut f) {
log::error!("failed to draw: {:?}", e)
Expand All @@ -176,7 +168,6 @@ fn select_event(
rx_git: &Receiver<AsyncNotification>,
rx_ticker: &Receiver<Instant>,
rx_spinner: &Receiver<Instant>,
rx_commands: &Receiver<QueueEvent>,
) -> Result<Vec<QueueEvent>> {
let mut events: Vec<QueueEvent> = Vec::new();

Expand All @@ -186,7 +177,6 @@ fn select_event(
sel.recv(rx_git);
sel.recv(rx_ticker);
sel.recv(rx_spinner);
sel.recv(rx_commands);

let oper = sel.select();
let index = oper.index();
Expand All @@ -202,7 +192,6 @@ fn select_event(
3 => oper
.recv(rx_spinner)
.map(|_| events.push(QueueEvent::SpinnerUpdate)),
4 => oper.recv(rx_commands).map(|ev| events.push(ev)),
_ => return Err(anyhow!("unknown select source")),
}?;

Expand Down
1 change: 0 additions & 1 deletion src/poll.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@ pub enum QueueEvent {
SpinnerUpdate,
GitEvent(AsyncNotification),
InputEvent(Event),
FullRedraw,
}

static MAX_POLL_DURATION: Duration = Duration::from_secs(2);
Expand Down
2 changes: 2 additions & 0 deletions src/queue.rs
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,8 @@ pub enum InternalEvent {
TabSwitch,
///
InspectCommit(CommitId),
///
RequestRedraw,
}

///
Expand Down

0 comments on commit d5171a7

Please sign in to comment.