From 48a79eaa2f77043bafd7bcd28c7c647cfefc102f Mon Sep 17 00:00:00 2001 From: Piotr Siuszko Date: Sun, 25 Sep 2022 23:21:41 +0200 Subject: [PATCH] Display current branch --- egui_client/src/hub_client.rs | 52 ++++++++++++++++++++++++++++++---- egui_client/src/main.rs | 2 +- rusty_hub/src/hub.rs | 7 +++++ rusty_hub/src/unity_project.rs | 49 ++++++++++++++++++++++++++++---- 4 files changed, 97 insertions(+), 13 deletions(-) diff --git a/egui_client/src/hub_client.rs b/egui_client/src/hub_client.rs index d1a71fa..00fd186 100644 --- a/egui_client/src/hub_client.rs +++ b/egui_client/src/hub_client.rs @@ -18,15 +18,24 @@ pub struct HubClient { impl HubClient { pub fn new(_cc: &eframe::CreationContext<'_>) -> Self { - Self { - hub: confy::load("lwa_unity_hub", "config").unwrap(), + let hub_option = confy::load("lwa_unity_hub", "config"); + + let hub = if hub_option.is_ok() { + hub_option.unwrap() + } else { + Hub::default() + }; + let mut client = Self { + hub, current_tab: WindowTab::Projects, - } + }; + client.save_config(true); + client } fn save_config(&mut self, rebuild: bool) { if rebuild { - self.hub.config.rebuild(); + self.hub.update_info(); } let _ = confy::store("lwa_unity_hub", "config", &self.hub); } @@ -134,11 +143,12 @@ impl HubClient { .cell_layout(egui::Layout::left_to_right(egui::Align::Center)) .column(Size::initial(150.0).at_least(150.0)) .column(Size::initial(90.0).at_least(40.0)) + .column(Size::initial(90.0).at_least(90.0)) .column(Size::remainder().at_least(260.0)) .resizable(false); table - .header(20.0, |mut header| { + .header(25.0, |mut header| { header.col(|ui| { ui.heading("Name"); ui.add_space(VERTICAL_SPACING); @@ -147,6 +157,10 @@ impl HubClient { ui.heading("Version"); ui.add_space(VERTICAL_SPACING); }); + header.col(|ui| { + ui.heading("Branch"); + ui.add_space(VERTICAL_SPACING); + }); header.col(|ui| { ui.heading("Directory"); ui.add_space(VERTICAL_SPACING); @@ -207,6 +221,32 @@ impl HubClient { }, ); }); + row.col(|ui| { + ui.with_layout( + Layout::top_down_justified(eframe::emath::Align::Max), + |ui| { + if project.branch.len() < 1 { + return; + } + ui.add_space(VERTICAL_SPACING); + const MAX_BRANCH_LEN: usize = 15; + let is_long = project.branch.len() > MAX_BRANCH_LEN; + let short = if !is_long { + project.branch.clone() + } else { + let mut result = + String::from(&project.branch[0..MAX_BRANCH_LEN]); + result.push_str("..."); + result + }; + + let label = ui.label(egui::RichText::new(short).small()); + if is_long { + label.on_hover_text(&project.branch); + } + }, + ); + }); row.col(|ui| { ui.with_layout( Layout::top_down_justified(eframe::emath::Align::Max), @@ -245,7 +285,7 @@ impl HubClient { ui.vertical_centered_justified(|ui| { ui.add_space(VERTICAL_SPACING); if ui.button("Refresh").clicked() { - self.hub.config.rebuild(); + self.hub.update_info(); } }); }); diff --git a/egui_client/src/main.rs b/egui_client/src/main.rs index 159e061..a550db4 100644 --- a/egui_client/src/main.rs +++ b/egui_client/src/main.rs @@ -26,7 +26,7 @@ fn main() { fullscreen: false, drag_and_drop_support: false, initial_window_size: Some(egui::vec2(820.0, 400.0)), - min_window_size: Some(egui::vec2(420.0, 400.0)), + min_window_size: Some(egui::vec2(720.0, 400.0)), icon_data: Some(icon), ..NativeOptions::default() }; diff --git a/rusty_hub/src/hub.rs b/rusty_hub/src/hub.rs index aa5125e..f349e7c 100644 --- a/rusty_hub/src/hub.rs +++ b/rusty_hub/src/hub.rs @@ -15,6 +15,13 @@ impl Hub { Self { config, projects } } + pub fn update_info(&mut self) { + self.config.rebuild(); + for project in self.projects.iter_mut() { + project.update_info(); + } + } + pub fn run_project_nr(&self, nr: usize) { let project = self.projects[nr].clone(); diff --git a/rusty_hub/src/unity_project.rs b/rusty_hub/src/unity_project.rs index 4b03dcf..342f8bd 100644 --- a/rusty_hub/src/unity_project.rs +++ b/rusty_hub/src/unity_project.rs @@ -5,6 +5,8 @@ pub struct UnityProject { pub path: String, pub title: String, pub version: String, + pub branch: String, + pub is_valid: bool, } impl PartialEq for UnityProject { @@ -52,16 +54,23 @@ impl UnityProject { projects } + fn is_project_at_path(path: &str) -> bool { + let one = Path::new(&path).join("ProjectSettings"); + let two = one.join("ProjectVersion.txt"); + + std::fs::metadata(&one).is_ok() && std::fs::metadata(&two).is_ok() + } + pub fn try_get_project_at_path(path: &str) -> Option { let path = path.trim_matches(char::from(0)).replace("/", "\\"); - let second_path = Path::new(&path).join("ProjectSettings"); - if std::fs::metadata(&second_path).is_err() { - return None; - } - let project_version_file = std::fs::read_to_string(second_path.join("ProjectVersion.txt")); - if project_version_file.is_err() { + if !UnityProject::is_project_at_path(&path) { return None; } + let project_version_file = std::fs::read_to_string( + Path::new(&path) + .join("ProjectSettings") + .join("ProjectVersion.txt"), + ); let project_version_file = project_version_file.unwrap(); let mut iter = project_version_file.split_whitespace(); iter.next(); @@ -71,6 +80,34 @@ impl UnityProject { path: path.to_string(), title: path.split("\\").last().unwrap().to_string(), version: project_version, + branch: String::new(), + is_valid: true, }) } + + pub fn update_info(&mut self) { + const HEAD_PREFIX: &str = "ref: refs/heads/"; + + let is_project = UnityProject::is_project_at_path(&self.path); + self.is_valid = is_project; + + if !is_project { + return; + } + + let mut base_path = Path::new(&self.path); + + while let Some(path) = base_path.parent() { + base_path = path; + let head_path = Path::new(&path).join(".git").join("HEAD"); + if !head_path.exists() { + continue; + } + let head_content = + std::fs::read_to_string(&head_path).expect("Could not read HEAD file"); + if head_content.contains(HEAD_PREFIX) { + self.branch = head_content.replace(HEAD_PREFIX, "").trim().to_string(); + } + } + } }