Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Persistence feature works #24

Closed
wants to merge 10 commits into from
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@ edition = "2021"
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html

[dependencies]
egui = { version = "0.26.2", features = ["serde"] }
egui = { version = "0.26.2", features = ["serde", "persistence"] }
eframe = { version = "0.26.2", default-features = false, features = [
"default_fonts", # Embed the default egui fonts.
"glow", # Use the glow rendering backend. Alternative: "wgpu".
Expand Down
249 changes: 135 additions & 114 deletions src/app/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -8,13 +8,8 @@ use log::{error, warn, info};

use std::path::{Path, PathBuf};
use std::sync::Arc;
use std::fs::File;
use std::io::Write;
use std::io::Read;
use std::string::String;
use clap::Parser;
use egui::{
Vec2,
RichText,
Label,
Color32,
Expand Down Expand Up @@ -80,6 +75,12 @@ pub struct Git {
pub repo : Option<git2::Repository>,
}

#[derive(serde::Deserialize, serde::Serialize)]
pub struct Settings {
pub colorscheme: ColorScheme,
pub ui_scale: f32,
}

/// The current GUI mode
#[non_exhaustive]
#[derive(serde::Deserialize, serde::Serialize, PartialEq)]
Expand All @@ -99,13 +100,13 @@ pub struct IronCoderApp {
// #[serde(skip)]
// modal: Option<Modal>,
mode: Mode,
colorscheme: ColorScheme,
#[serde(skip)]
boards: Vec<board::Board>,
options: IronCoderOptions,

warning_flags: Warnings,
git_things: Git,
settings: Settings,
}

impl Default for IronCoderApp {
Expand All @@ -121,7 +122,6 @@ impl Default for IronCoderApp {
// modal: None,
mode: Mode::EditProject,
boards: boards,
colorscheme: colorscheme::INDUSTRIAL_DARK,
options: IronCoderOptions::default(),
// Warning Flags
warning_flags: Warnings {
Expand All @@ -138,6 +138,10 @@ impl Default for IronCoderApp {
commit_message: String::new(),
repo: None,
},
settings: Settings {
colorscheme: colorscheme::INDUSTRIAL_DARK,
ui_scale: 1.0,
},
}
}
}
Expand All @@ -151,7 +155,7 @@ impl IronCoderApp {
install_image_loaders(&cc.egui_ctx);
// Load previous app state if it exists and is specified.
let mut app = IronCoderApp::default();
if options.persistence {
if !options.persistence {
if let Some(storage) = cc.storage {
info!("loading former app state from storage...");
app = eframe::get_value(storage, eframe::APP_KEY).unwrap_or_default();
Expand All @@ -172,7 +176,7 @@ impl IronCoderApp {

/// Set the colorscheme for the app
fn set_colorscheme(&self, ctx: &egui::Context) {
colorscheme::set_colorscheme(ctx, self.colorscheme.clone());
colorscheme::set_colorscheme(ctx, self.settings.colorscheme.clone());
}

/// Show the menu and app title
Expand Down Expand Up @@ -340,7 +344,10 @@ impl IronCoderApp {
pub fn display_settings_window(&mut self, ctx: &egui::Context) {
let Self {
display_settings,
colorscheme,
settings: Settings{
colorscheme,
ui_scale,
},
..
} = self;

Expand Down Expand Up @@ -532,123 +539,137 @@ impl IronCoderApp {

egui::Window::new("Commit")
.open(&mut display_git)
.collapsible(false)
.resizable(true)
.movable(true)
.show(ctx, |ui| {
let repo = self.git_things.repo.as_mut().unwrap();
let mut index = repo.index().unwrap();

egui::SidePanel::right("Unstaged Changes").show_inside(ui, |ui| {
ui.label("Staged Changes -- Currently doesn't work");
ui.separator();
ui.vertical(|ui| {
for (_i, change) in self.git_things.staged_changes.iter().enumerate() {
if ui.button(change.clone()).clicked() {
info!("Unstaging: {}", change.clone());
unstaged_to_add.push(change.clone());
staged_to_remove.push(change.clone());
index.remove_all([change.clone()].iter(), None).unwrap();
index.write().unwrap();
}
.collapsible(false)
.resizable(true)
.movable(true)
.show(ctx, |ui| {
let repo = self.git_things.repo.as_mut().unwrap();
let mut index = repo.index().unwrap();

egui::SidePanel::right("Unstaged Changes").show_inside(ui, |ui| {
ui.label("Staged Changes");
ui.separator();
ui.vertical(|ui| {
for (_i, change) in self.git_things.staged_changes.iter().enumerate() {
if ui.button(change.clone()).clicked() {
info!("Unstaging: {}", change.clone());
unstaged_to_add.push(change.clone());
staged_to_remove.push(change.clone());
index.remove_all([change.clone()].iter(), None).unwrap();
index.write().unwrap();
}
self.git_things.staged_changes.retain(|change| !staged_to_remove.contains(change));
});
ui.separator();
ui.label("Unstaged Changes");
// Display the files that have changed on the right side
ui.separator();
ui.vertical(|ui| {
// Create a button for each unstaged change in git_things.changes
for (_i, change) in self.git_things.changes.iter().enumerate() {
if ui.button(change.clone()).clicked() {
info!("Staging: {}", change.clone());
staged_to_add.push(change.clone());
unstaged_to_remove.push(change.clone());
//index.add_path(Path::new(change)).unwrap();
match index.add_path(Path::new(change)) {
Ok(_) => {
// add_path succeeded, do nothing
},
Err(_) => {
// add_path failed, try add_all
index.add_all([change.clone()].iter(), git2::IndexAddOption::DEFAULT, None).unwrap();
}
}
self.git_things.staged_changes.retain(|change| !staged_to_remove.contains(change));
});
ui.separator();
ui.label("Unstaged Changes");
// Display the files that have changed on the right side
ui.separator();
ui.vertical(|ui| {
// Create a button for each unstaged change in git_things.changes
for (_i, change) in self.git_things.changes.iter().enumerate() {
if ui.button(change.clone()).clicked() {
info!("Staging: {}", change.clone());
staged_to_add.push(change.clone());
unstaged_to_remove.push(change.clone());
//index.add_path(Path::new(change)).unwrap();
match index.add_path(Path::new(change)) {
Ok(_) => {
// add_path succeeded, do nothing
},
Err(_) => {
// add_path failed, try add_all
index.add_all([change.clone()].iter(), git2::IndexAddOption::DEFAULT, None).unwrap();
}
index.write().unwrap();
}
index.write().unwrap();
}
self.git_things.changes.retain(|change| !unstaged_to_remove.contains(change));
});
}
self.git_things.changes.retain(|change| !unstaged_to_remove.contains(change));
});
self.git_things.staged_changes.append(&mut staged_to_add);
self.git_things.changes.append(&mut unstaged_to_add);

egui::CentralPanel::default().show_inside(ui, |ui|{
// Have a text box for the commit message
// Have the text box take as much space as possible
ui.label("Commit Message:");
ui.text_edit_multiline(&mut self.git_things.commit_message);
ui.label("Name");
ui.text_edit_singleline(&mut self.git_things.commit_name);
ui.label("Email Address");
ui.text_edit_singleline(&mut self.git_things.commit_email);

});
self.git_things.staged_changes.append(&mut staged_to_add);
self.git_things.changes.append(&mut unstaged_to_add);

egui::CentralPanel::default().show_inside(ui, |ui|{
// Have a text box for the commit message
// Have the text box take as much space as possible
ui.label("Commit Message:");
ui.text_edit_multiline(&mut self.git_things.commit_message);
ui.label("Name");
ui.text_edit_singleline(&mut self.git_things.commit_name);
ui.label("Email Address");
ui.text_edit_singleline(&mut self.git_things.commit_email);



// Have a button to commit the changes
if ui.button("Commit").clicked() {
let name = self.git_things.commit_name.clone();
let email = self.git_things.commit_email.clone();
let commit_message = self.git_things.commit_message.clone();

// Have a button to commit the changes
if ui.button("Commit").clicked() {
if name != "" && email != "" && commit_message != "" {
info!("committing changes to git...");
info!("{}", self.git_things.commit_message.clone());

let signature = git2::Signature::now(&name, &email).unwrap();
let oid = index.write_tree().unwrap();
let tree = repo.find_tree(oid).unwrap();
let head = repo.head().unwrap();
let head_commit = repo.find_commit(head.target().unwrap()).unwrap();


match repo.commit(
// There is a problem with the head
Some("HEAD"),
&signature,
&signature,
&commit_message,
&tree,
&[&head_commit]
) {
Ok(_) => {
info!("commit successful!");
},
Err(e) => {
error!("error committing changes to git: {:?}", e);
if name != "" && email != "" && commit_message != "" {
info!("committing changes to git...");
info!("{}", self.git_things.commit_message.clone());

let signature = git2::Signature::now(&name, &email).unwrap();
let oid = index.write_tree().unwrap();
let tree = repo.find_tree(oid).unwrap();
// This line is the problem -- was called while doing initial commit, it shouldn't have been
let head = repo.head().unwrap();
let head_commit = repo.find_commit(head.target().unwrap()).unwrap();

match repo.commit(
// There is a problem with the head
Some("HEAD"),
&signature,
&signature,
&commit_message,
&tree,
&[]
) {
Ok(_) => {
info!("commit successful!");
},
Err(e) => {
error!("error committing changes to git: {:?}", e);
match repo.commit(
// There is a problem with the head
Some("HEAD"),
&signature,
&signature,
&commit_message,
&tree,
&[&head_commit]
) {
Ok(_) => {
info!("commit successful!");
},
Err(e) => {
error!("error committing changes to git: {:?}", e);
}
}
}

self.git_things.display = false;
self.git_things.commit_message.clear();
self.git_things.commit_name.clear();
self.git_things.commit_email.clear();
} else {
self.warning_flags.display_git_warning = true;
}



self.git_things.display = false;
self.git_things.commit_message.clear();
} else {
self.warning_flags.display_git_warning = true;
}
});
}
});
});

// Makes sure that both commit button and x button close the window
if self.git_things.display == false || display_git == false {
self.git_things.display = false;
display_git = false;
self.git_things.commit_message.clear();
self.git_things.commit_name.clear();
self.git_things.commit_email.clear();
self.git_things.changes.clear();
self.git_things.staged_changes.clear();
}
// Makes sure that both commit button and x button close the window
if self.git_things.display == false || display_git == false {
self.git_things.display = false;
self.git_things.commit_message.clear();
self.git_things.changes.clear();
self.git_things.staged_changes.clear();
}


}
Expand All @@ -658,7 +679,7 @@ impl eframe::App for IronCoderApp {

// Called by the framework to save state before shutdown.
fn save(&mut self, storage: &mut dyn eframe::Storage) {
if self.options.persistence {
if !self.options.persistence {
info!("saving program state.");
eframe::set_value(storage, eframe::APP_KEY, self);
}
Expand Down
1 change: 0 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use log::info;
use clap::Parser;
use tracing;
use std::str::FromStr;

use iron_coder::IronCoderOptions;
Expand Down
10 changes: 6 additions & 4 deletions src/project/display.rs
Original file line number Diff line number Diff line change
Expand Up @@ -200,7 +200,8 @@ impl Project {
// Check if there are any changes or new files and save them in a vector
let mut changes: Vec<String> = Vec::new();
for entry in repo_statuses.unwrap().iter() {
if entry.status().contains(git2::Status::WT_NEW) || entry.status().contains(git2::Status::WT_MODIFIED){
if entry.status().contains(git2::Status::WT_NEW) || entry.status().contains(git2::Status::WT_MODIFIED)
|| entry.status().contains(git2::Status::INDEX_MODIFIED){
changes.push(entry.path().unwrap().to_string());
}
}
Expand All @@ -211,11 +212,12 @@ impl Project {
info!("{}", change);
}

let mut index = repo.index().unwrap();
for change in changes.iter() {
let mut index = repo.index().unwrap();
index.remove_all([change.clone()].iter(), None).unwrap();
index.write().unwrap();
info!("Removing {} from the index", change);
index.remove_all([change.clone()].iter(), None).unwrap();
}
index.write().unwrap();

// Open a window to choose the changes to commit
git_things.display = true;
Expand Down
3 changes: 0 additions & 3 deletions src/project/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,7 @@ use std::io;
use std::fs;
use std::path::{Path, PathBuf};

use std::vec::Vec;

use rfd::FileDialog;
use toml;

use serde::{Serialize, Deserialize};

Expand Down
Loading
Loading