diff options
author | iximeow <me@iximeow.net> | 2017-11-11 03:25:04 -0800 |
---|---|---|
committer | iximeow <me@iximeow.net> | 2017-11-11 03:25:04 -0800 |
commit | 1277e05c931608f7f07a41dff6bae92b8d262299 (patch) | |
tree | 342631e076e07a6811854208f01d0bdc56021a50 /src | |
parent | bfc3232b5dae3ac8fa0fba3f50d771d75beda08a (diff) |
graceful shutdown, fix profile corruption bug
profile corruption was trailing json left by not truncating when profile shrinks in size
Diffstat (limited to 'src')
-rw-r--r-- | src/commands/quit.rs | 4 | ||||
-rw-r--r-- | src/main.rs | 10 | ||||
-rw-r--r-- | src/tw/mod.rs | 25 |
3 files changed, 24 insertions, 15 deletions
diff --git a/src/commands/quit.rs b/src/commands/quit.rs index 716c412..5bd8c18 100644 --- a/src/commands/quit.rs +++ b/src/commands/quit.rs @@ -12,7 +12,5 @@ pub static QUIT: Command = Command { }; fn quit(_line: String, tweeter: &mut tw::TwitterCache, _queryer: &mut Queryer) { - tweeter.display_info.status("Bye bye!".to_owned()); - tweeter.store_cache(); - exit(0); + tweeter.state = tw::AppState::Shutdown; } diff --git a/src/main.rs b/src/main.rs index d69d67d..37f190e 100644 --- a/src/main.rs +++ b/src/main.rs @@ -383,7 +383,15 @@ fn do_ui(ui_rx_orig: chan::Receiver<Result<termion::event::Event, std::io::Error tw::AppState::Reconnect => { tweeter.state = tw::AppState::View; return Some((ui_rx_orig.clone(), tweeter.profile.clone().map(|creds| connect_twitter_stream(tweeter.app_key.clone(), creds)))); - } + }, + tw::AppState::Shutdown => { + tweeter.display_info.status("Saving cache...".to_owned()); + display::paint(tweeter).unwrap(); + tweeter.store_cache(); + tweeter.display_info.status("Bye bye!".to_owned()); + display::paint(tweeter).unwrap(); + return None + }, _ => () }; } diff --git a/src/tw/mod.rs b/src/tw/mod.rs index 767aae9..e2b8b03 100644 --- a/src/tw/mod.rs +++ b/src/tw/mod.rs @@ -23,6 +23,7 @@ pub mod user; use self::user::User; pub enum AppState { + Shutdown, Reconnect, Compose, View @@ -349,18 +350,20 @@ impl TwitterCache { } } pub fn store_cache(&mut self) { - if Path::new(TwitterCache::PROFILE_DIR).is_dir() { - let profile = OpenOptions::new() - .write(true) - .create(true) - .append(false) - .open(TwitterCache::PROFILE_CACHE) - .unwrap(); - serde_json::to_writer(profile, self).unwrap(); - } else { - self.display_info.status("No cache dir exists...".to_owned()); + if self.caching_permitted { + if Path::new(TwitterCache::PROFILE_DIR).is_dir() { + let profile = OpenOptions::new() + .write(true) + .create(true) + .append(false) + .truncate(true) // since this one can become smaller, lop off trailing characters + .open(TwitterCache::PROFILE_CACHE) + .unwrap(); + serde_json::to_writer(profile, self).unwrap(); + } else { + self.display_info.status("No cache dir exists...".to_owned()); + } } - // store cache } fn number_and_insert_tweet(&mut self, mut tw: Tweet) { if !self.tweets.contains_key(&tw.id.to_owned()) { |