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

Git fixes #20

Merged
merged 6 commits into from
Jun 10, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 0 additions & 2 deletions settings.toml

This file was deleted.

326 changes: 136 additions & 190 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 @@ -158,40 +153,6 @@ impl IronCoderApp {
}
}

// Load settings from settings.toml if it exists
info!("reading settings and applying to app state...");
let mut settings_file = match File::open("settings.toml") {
Err(why) => panic!("couldn't open settings.toml: {}", why),
Ok(file) => file,
};

let mut settings_string = String::new();
match settings_file.read_to_string(&mut settings_string) {
Err(why) => panic!("couldn't read settings.toml: {}", why),
Ok(_) => print!("settings.toml contains:\n{}", settings_string),
}

if (settings_string != "") {
// Sets the scale for the app from settings.toml
let scale = settings_string.lines().nth(0).unwrap().split("=").nth(1).unwrap().trim().parse::<f32>().unwrap();
info!("setting ui scale to {}", scale);
cc.egui_ctx.set_pixels_per_point(scale);


// Sets the color scheme for the app from settings.toml
let mut colorscheme_name = settings_string.lines().nth(1).unwrap().split("=").nth(1).unwrap().trim().to_string();
info!("setting colorscheme to {}", colorscheme_name);
colorscheme_name = colorscheme_name.trim_matches('"').to_string();
let mut colorscheme = colorscheme::INDUSTRIAL_DARK;
for cs in colorscheme::SYSTEM_COLORSCHEMES.iter() {
if cs.name == colorscheme_name {
colorscheme = cs.clone();
}
}
app.colorscheme = colorscheme.clone();
colorscheme::set_colorscheme(&cc.egui_ctx, colorscheme.clone());
}

app.options = options;
info!("Reloading current project and assets...");
app.set_colorscheme(&cc.egui_ctx);
Expand Down Expand Up @@ -396,16 +357,19 @@ impl IronCoderApp {
ui.text_edit_singleline(&mut ui_scale_string);
ctx.data_mut(|data| data.insert_temp(id, ui_scale_string.clone()));
// if the string is parsable into f32, update the global scale
match ui_scale_string.parse::<f32>() {
Ok(scale) if scale >=0.7 => {
ctx.set_pixels_per_point(scale);
},
Ok(_scale) => {
warn!("scale can't be below 0.7!");
if ui.button("Apply").clicked() {
match ui_scale_string.parse::<f32>() {
Ok(scale) if scale >=0.7 && scale <=2.5 => {
ctx.set_pixels_per_point(scale);
info!("native pixels per point: {:?}", ctx.native_pixels_per_point());
}
Ok(_scale) => {
warn!("scale can't be below 0.7!");
},
Err(_e) => {
warn!("scale not parsed as f32.");
},
}
Err(_e) => {
warn!("scale not parsed as f32.");
},
}

// Create radio buttons for colorscheme selection
Expand All @@ -416,6 +380,7 @@ impl IronCoderApp {
let rb = egui::RadioButton::new(*colorscheme == cs.clone(), cs.name.clone());
if ui.add(rb).clicked() {
*colorscheme = cs.clone();
colorscheme::set_colorscheme(ctx, colorscheme.clone());
}
}

Expand Down Expand Up @@ -464,42 +429,6 @@ impl IronCoderApp {
ui.heading("Account Settings:");
ui.label("Add github account here.");
// ctx.set_visuals(visuals);


// Create a button to apply the settings
if ui.button("Apply").clicked() {
// Change settings when Apply button is pressed
// Change the colorscheme
colorscheme::set_colorscheme(ctx, colorscheme.clone());

// Set the ui scale
match ui_scale_string.parse::<f32>() {
Ok(scale) => {
ctx.set_pixels_per_point(scale);
},
Err(_e) => (),
}


// Write the settings to settings.toml
let mut settings_file = match File::create("settings.toml") {
Err(why) => panic!("couldn't create settings.toml: {}", why),
Ok(file) => file,
};

let mut settings_string = String::new();
settings_string.push_str("ui_scale = ");
settings_string.push_str(&ui_scale_string);
settings_string.push_str("\n");
settings_string.push_str("colorscheme = \"");
settings_string.push_str(&colorscheme.name);
settings_string.push_str("\"\n");

match settings_file.write_all(settings_string.as_bytes()) {
Err(why) => panic!("couldn't write to settings.toml: {}", why),
Ok(_) => println!("successfully wrote to settings.toml"),
}
}
});
// unwrap ok here because window must be open for us to get here.
// ctx.move_to_top(window_response.unwrap().response.layer_id);
Expand Down Expand Up @@ -597,123 +526,140 @@ 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();
}
}
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();
}
.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();
}
self.git_things.changes.retain(|change| !unstaged_to_remove.contains(change));
});
}
self.git_things.staged_changes.retain(|change| !staged_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);

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]
) {
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(_) => {
info!("commit successful!");
// add_path succeeded, do nothing
},
Err(e) => {
error!("error committing changes to git: {:?}", e);
Err(_) => {
// add_path failed, try add_all
index.add_all([change.clone()].iter(), git2::IndexAddOption::DEFAULT, None).unwrap();
}
}

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;
index.write().unwrap();
}
}
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);

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();
// 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;
}
}
});
});

// 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.commit_name.clear();
self.git_things.commit_email.clear();
self.git_things.changes.clear();
self.git_things.staged_changes.clear();
}


}
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
Loading
Loading