Skip to content

Commit

Permalink
event: conditionally capture mouse (#74)
Browse files Browse the repository at this point in the history
  • Loading branch information
miraclx authored Feb 17, 2021
1 parent b3f0918 commit f60accb
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 6 deletions.
19 changes: 19 additions & 0 deletions src/app.rs
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,25 @@ impl App {
}
}

pub struct EnvConfig {
pub show_debug: bool,
pub debug_mouse: bool,
}

impl EnvConfig {
#[inline]
fn env_match(key: &str, default: &str, expected: &str) -> bool {
std::env::var(key).ok().unwrap_or_else(|| default.into()) == expected
}

pub fn load() -> Self {
Self {
show_debug: Self::env_match("SHOW_DEBUG", "0", "1"),
debug_mouse: Self::env_match("DEBUG_MOUSE", "0", "1"),
}
}
}

#[derive(Debug)]
pub struct DebugInfo {
pub enabled: bool,
Expand Down
14 changes: 8 additions & 6 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ mod widget;

lazy_static! {
static ref CLIENT: api::Client = api::Client::new();
static ref DEBUG_LEVEL: app::EnvConfig = app::EnvConfig::load();
pub static ref OPTS: opts::Opts = opts::resolve_opts();
pub static ref UPDATE_INTERVAL: u64 = OPTS.update_interval.unwrap_or(1);
pub static ref TIME_FRAME: TimeFrame = OPTS.time_frame.unwrap_or(TimeFrame::Day1);
Expand Down Expand Up @@ -81,10 +82,7 @@ fn main() {
current_tab: 0,
hide_help: opts.hide_help,
debug: DebugInfo {
enabled: std::env::var("SHOW_DEBUG")
.ok()
.unwrap_or_else(|| String::from("0"))
== "1",
enabled: DEBUG_LEVEL.show_debug,
dimensions: (0, 0),
cursor_location: None,
last_event: None,
Expand Down Expand Up @@ -204,15 +202,19 @@ fn setup_terminal() {

execute!(stdout, terminal::Clear(terminal::ClearType::All)).unwrap();

execute!(stdout, crossterm::event::EnableMouseCapture).unwrap();
if DEBUG_LEVEL.debug_mouse {
execute!(stdout, crossterm::event::EnableMouseCapture).unwrap();
}

terminal::enable_raw_mode().unwrap();
}

fn cleanup_terminal() {
let mut stdout = io::stdout();

execute!(stdout, crossterm::event::DisableMouseCapture).unwrap();
if DEBUG_LEVEL.debug_mouse {
execute!(stdout, crossterm::event::DisableMouseCapture).unwrap();
}

execute!(stdout, cursor::MoveTo(0, 0)).unwrap();
execute!(stdout, terminal::Clear(terminal::ClearType::All)).unwrap();
Expand Down

0 comments on commit f60accb

Please sign in to comment.