-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: add version info to bottom right of screen
- as this is fast-changing I have added version info to the bottom right of the screen - If enabled in config, will attempt to fetch tag of latest version on load - if latest version is not the same as the current version, will indicate that the current version is out of date - can turn this off in the config, at which point it will just show the latest version
- Loading branch information
1 parent
32934ee
commit c85b3a6
Showing
8 changed files
with
463 additions
and
24 deletions.
There are no files selected for viewing
Large diffs are not rendered by default.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -6,3 +6,4 @@ pub mod header; | |
pub mod help; | ||
pub mod resize_notice; | ||
pub mod text_input_wrapper; | ||
pub mod version; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,106 @@ | ||
use crate::{config::Config, traits::Component}; | ||
use color_eyre::eyre::Result; | ||
use ratatui::{ | ||
layout::{Margin, Rect}, | ||
style::Style, | ||
text::{Line, Span}, | ||
Frame, | ||
}; | ||
use reqwest::header::USER_AGENT; | ||
|
||
const VERSION: &str = env!("CARGO_PKG_VERSION"); | ||
const TAGS_URL: &str = "https://api.github.com/repos/robertpsoane/ducker/tags"; | ||
|
||
#[derive(Debug)] | ||
pub struct VersionComponent { | ||
config: Box<Config>, | ||
version: String, | ||
update_to: Option<String>, | ||
} | ||
|
||
impl VersionComponent { | ||
pub async fn new(config: Box<Config>) -> Self { | ||
let version = format!("v{VERSION}"); | ||
|
||
let update_to = if config.check_for_update { | ||
get_update_to(&version).await | ||
} else { | ||
None | ||
}; | ||
|
||
Self { | ||
config, | ||
version, | ||
update_to, | ||
} | ||
} | ||
} | ||
|
||
impl Component for VersionComponent { | ||
fn draw(&mut self, f: &mut Frame<'_>, area: Rect) { | ||
let area = area.inner(Margin { | ||
vertical: 0, | ||
horizontal: 1, | ||
}); | ||
|
||
let current_version = Span::from(self.version.clone()); | ||
|
||
let spans = if let Some(update) = &self.update_to { | ||
let update_to_span = Span::from(update); | ||
let arrow = Span::from(" > "); | ||
vec![ | ||
current_version.style(Style::default().fg(self.config.theme.negative_highlight())), | ||
arrow, | ||
update_to_span.style(Style::default().fg(self.config.theme.positive_highlight())), | ||
] | ||
} else { | ||
vec![current_version.style(Style::default().fg(self.config.theme.positive_highlight()))] | ||
}; | ||
|
||
f.render_widget( | ||
Line::from(spans).alignment(ratatui::layout::Alignment::Right), | ||
area, | ||
) | ||
} | ||
} | ||
|
||
async fn get_update_to(version: &str) -> Option<String> { | ||
let latest_version = match find_latest_version().await { | ||
Ok(v) => v, | ||
Err(_) => return None, | ||
}; | ||
if version == latest_version { | ||
None | ||
} else { | ||
Some(latest_version) | ||
} | ||
} | ||
|
||
async fn find_latest_version() -> Result<String> { | ||
let client = reqwest::Client::new(); | ||
|
||
let body: serde_yml::Value = client | ||
.get(TAGS_URL) | ||
.header(USER_AGENT, format!("Ducker / {VERSION}")) | ||
.send() | ||
.await? | ||
.json() | ||
.await?; | ||
|
||
let release = match body.get(0) { | ||
Some(v) => v, | ||
None => panic!("could not parse response"), | ||
}; | ||
|
||
let release_name = match release.get("name") { | ||
Some(v) => v, | ||
None => panic!("could not parse response"), | ||
}; | ||
|
||
let release_name_value = match release_name.as_str() { | ||
Some(v) => String::from(v), | ||
None => panic!("could not parse response"), | ||
}; | ||
|
||
Ok(release_name_value) | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters