Display current branch

This commit is contained in:
Piotr Siuszko 2022-09-25 23:21:41 +02:00
parent d812491883
commit 48a79eaa2f
4 changed files with 97 additions and 13 deletions

View File

@ -18,15 +18,24 @@ pub struct HubClient {
impl HubClient { impl HubClient {
pub fn new(_cc: &eframe::CreationContext<'_>) -> Self { pub fn new(_cc: &eframe::CreationContext<'_>) -> Self {
Self { let hub_option = confy::load("lwa_unity_hub", "config");
hub: confy::load("lwa_unity_hub", "config").unwrap(),
let hub = if hub_option.is_ok() {
hub_option.unwrap()
} else {
Hub::default()
};
let mut client = Self {
hub,
current_tab: WindowTab::Projects, current_tab: WindowTab::Projects,
} };
client.save_config(true);
client
} }
fn save_config(&mut self, rebuild: bool) { fn save_config(&mut self, rebuild: bool) {
if rebuild { if rebuild {
self.hub.config.rebuild(); self.hub.update_info();
} }
let _ = confy::store("lwa_unity_hub", "config", &self.hub); 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)) .cell_layout(egui::Layout::left_to_right(egui::Align::Center))
.column(Size::initial(150.0).at_least(150.0)) .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(40.0))
.column(Size::initial(90.0).at_least(90.0))
.column(Size::remainder().at_least(260.0)) .column(Size::remainder().at_least(260.0))
.resizable(false); .resizable(false);
table table
.header(20.0, |mut header| { .header(25.0, |mut header| {
header.col(|ui| { header.col(|ui| {
ui.heading("Name"); ui.heading("Name");
ui.add_space(VERTICAL_SPACING); ui.add_space(VERTICAL_SPACING);
@ -147,6 +157,10 @@ impl HubClient {
ui.heading("Version"); ui.heading("Version");
ui.add_space(VERTICAL_SPACING); ui.add_space(VERTICAL_SPACING);
}); });
header.col(|ui| {
ui.heading("Branch");
ui.add_space(VERTICAL_SPACING);
});
header.col(|ui| { header.col(|ui| {
ui.heading("Directory"); ui.heading("Directory");
ui.add_space(VERTICAL_SPACING); 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| { row.col(|ui| {
ui.with_layout( ui.with_layout(
Layout::top_down_justified(eframe::emath::Align::Max), Layout::top_down_justified(eframe::emath::Align::Max),
@ -245,7 +285,7 @@ impl HubClient {
ui.vertical_centered_justified(|ui| { ui.vertical_centered_justified(|ui| {
ui.add_space(VERTICAL_SPACING); ui.add_space(VERTICAL_SPACING);
if ui.button("Refresh").clicked() { if ui.button("Refresh").clicked() {
self.hub.config.rebuild(); self.hub.update_info();
} }
}); });
}); });

View File

@ -26,7 +26,7 @@ fn main() {
fullscreen: false, fullscreen: false,
drag_and_drop_support: false, drag_and_drop_support: false,
initial_window_size: Some(egui::vec2(820.0, 400.0)), 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), icon_data: Some(icon),
..NativeOptions::default() ..NativeOptions::default()
}; };

View File

@ -15,6 +15,13 @@ impl Hub {
Self { config, projects } 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) { pub fn run_project_nr(&self, nr: usize) {
let project = self.projects[nr].clone(); let project = self.projects[nr].clone();

View File

@ -5,6 +5,8 @@ pub struct UnityProject {
pub path: String, pub path: String,
pub title: String, pub title: String,
pub version: String, pub version: String,
pub branch: String,
pub is_valid: bool,
} }
impl PartialEq for UnityProject { impl PartialEq for UnityProject {
@ -52,16 +54,23 @@ impl UnityProject {
projects 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<UnityProject> { pub fn try_get_project_at_path(path: &str) -> Option<UnityProject> {
let path = path.trim_matches(char::from(0)).replace("/", "\\"); let path = path.trim_matches(char::from(0)).replace("/", "\\");
let second_path = Path::new(&path).join("ProjectSettings"); if !UnityProject::is_project_at_path(&path) {
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() {
return None; 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 project_version_file = project_version_file.unwrap();
let mut iter = project_version_file.split_whitespace(); let mut iter = project_version_file.split_whitespace();
iter.next(); iter.next();
@ -71,6 +80,34 @@ impl UnityProject {
path: path.to_string(), path: path.to_string(),
title: path.split("\\").last().unwrap().to_string(), title: path.split("\\").last().unwrap().to_string(),
version: project_version, 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();
}
}
}
} }