Skip to content

Commit

Permalink
feat(term): inform latest release
Browse files Browse the repository at this point in the history
close #40
  • Loading branch information
ymgyt committed May 29, 2024
1 parent e7815ad commit a65eb66
Show file tree
Hide file tree
Showing 4 changed files with 105 additions and 1 deletion.
55 changes: 54 additions & 1 deletion Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions crates/synd_term/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -50,6 +50,7 @@ tracing-appender = "0.2.3"
tracing-subscriber = { workspace = true }
tui-big-text = "0.4.3"
unicode-segmentation = "1.10.1"
update-informer = { version = "1.1.0", default-features = false, features = ["crates", "reqwest", "rustls-tls"] }
url = { workspace = true }
# https://github.com/arkbig/throbber-widgets-tui/pull/5
# throbber-widgets-tui = "0.3.0"
Expand Down
46 changes: 46 additions & 0 deletions crates/synd_term/src/application/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use synd_auth::device_flow::{
};
use synd_feed::types::FeedUrl;
use tokio::time::{Instant, Sleep};
use update_informer::Version;

use crate::{
application::event::KeyEventResult,
Expand Down Expand Up @@ -101,6 +102,7 @@ pub struct Application {
config: Config,
key_handlers: event::KeyHandlers,
categories: Categories,
latest_release: Option<Version>,

screen: Screen,
flags: Should,
Expand Down Expand Up @@ -139,6 +141,7 @@ impl Application {
config,
key_handlers,
categories,
latest_release: None,
flags: Should::empty(),
}
}
Expand Down Expand Up @@ -175,6 +178,7 @@ impl Application {
pub fn handle_initial_credential(&mut self, cred: Credential) {
self.set_credential(cred);
self.initial_fetch();
self.check_latest_release();
self.components.auth.authenticated();
self.keymaps().disable(KeymapId::Login);
self.keymaps().enable(KeymapId::Tabs);
Expand Down Expand Up @@ -212,6 +216,8 @@ impl Application {

self.terminal.restore()?;

self.inform_latest_release();

Ok(())
}

Expand Down Expand Up @@ -274,6 +280,7 @@ impl Application {
// should detect infinite loop ?
while let Some(command) = next.take() {
match command {
Command::Nop => {}
Command::Quit => self.flags.insert(Should::Quit),
Command::ResizeTerminal { .. } => {
self.should_render();
Expand Down Expand Up @@ -564,6 +571,9 @@ impl Application {
self.rotate_theme();
self.should_render();
}
Command::InformLatestRelease(version) => {
self.latest_release = Some(version);
}
Command::HandleError {
message,
request_seq,
Expand Down Expand Up @@ -953,6 +963,42 @@ impl Application {
}
}

impl Application {
fn check_latest_release(&mut self) {
use update_informer::{registry, Check};

// update informer use reqwest::blocking
let check = tokio::task::spawn_blocking(|| {
let name = env!("CARGO_PKG_NAME");
let version = env!("CARGO_PKG_VERSION");
#[cfg(not(test))]
let informer = update_informer::new(registry::Crates, name, version)
.interval(Duration::from_secs(60 * 60 * 24))
.timeout(Duration::from_secs(5));

#[cfg(test)]
let informer = update_informer::fake(registry::Crates, name, version, "v1.0.0");

informer.check_version().ok().flatten()
});
let fut = async move {
match check.await {
Ok(Some(version)) => Ok(Command::InformLatestRelease(version)),
_ => Ok(Command::Nop),
}
}
.boxed();
self.jobs.futures.push(fut);
}

fn inform_latest_release(&self) {
let current_version = env!("CARGO_PKG_VERSION");
if let Some(new_version) = &self.latest_release {
println!("A new release of synd is available: v{current_version} -> {new_version}");
}
}
}

impl Application {
fn handle_idle(&mut self) {
self.clear_idle_timer();
Expand Down
4 changes: 4 additions & 0 deletions crates/synd_term/src/command.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,7 @@ use crate::{

#[derive(Debug, Clone)]
pub(crate) enum Command {
Nop,
Quit,
ResizeTerminal {
_columns: u16,
Expand Down Expand Up @@ -103,6 +104,9 @@ pub(crate) enum Command {
// Theme
RotateTheme,

// Latest release check
InformLatestRelease(update_informer::Version),

HandleError {
message: String,
request_seq: Option<RequestSequence>,
Expand Down

0 comments on commit a65eb66

Please sign in to comment.