2025-07-15 00:42:01 -04:00
|
|
|
use crate::app::tab::Tab;
|
2025-07-15 11:07:41 -04:00
|
|
|
use crate::app::TextEditor;
|
2025-07-05 14:42:45 -04:00
|
|
|
use std::fs;
|
|
|
|
|
use std::path::PathBuf;
|
|
|
|
|
|
|
|
|
|
pub(crate) fn new_file(app: &mut TextEditor) {
|
|
|
|
|
app.add_new_tab();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn open_file(app: &mut TextEditor) {
|
|
|
|
|
if let Some(path) = rfd::FileDialog::new()
|
|
|
|
|
.add_filter("Text files", &["*"])
|
|
|
|
|
.pick_file()
|
|
|
|
|
{
|
|
|
|
|
match fs::read_to_string(&path) {
|
|
|
|
|
Ok(content) => {
|
|
|
|
|
let should_replace_current_tab = if let Some(active_tab) = app.get_active_tab() {
|
|
|
|
|
active_tab.file_path.is_none()
|
|
|
|
|
&& active_tab.content.is_empty()
|
|
|
|
|
&& !active_tab.is_modified
|
|
|
|
|
} else {
|
|
|
|
|
false
|
|
|
|
|
};
|
|
|
|
|
|
|
|
|
|
if should_replace_current_tab {
|
|
|
|
|
if let Some(active_tab) = app.get_active_tab_mut() {
|
2025-07-16 17:20:09 -04:00
|
|
|
let title = path.file_name().and_then(|n| n.to_str()).unwrap_or("Untitled");
|
2025-07-05 14:42:45 -04:00
|
|
|
active_tab.content = content;
|
2025-07-16 17:20:09 -04:00
|
|
|
active_tab.file_path = Some(path.to_path_buf());
|
|
|
|
|
active_tab.title = title.to_string();
|
2025-07-16 13:27:31 -04:00
|
|
|
active_tab.mark_as_saved();
|
2025-07-05 14:42:45 -04:00
|
|
|
}
|
2025-07-16 13:27:31 -04:00
|
|
|
app.text_needs_processing = true;
|
2025-07-05 14:42:45 -04:00
|
|
|
} else {
|
|
|
|
|
let new_tab = Tab::new_with_file(content, path);
|
|
|
|
|
app.tabs.push(new_tab);
|
|
|
|
|
app.active_tab_index = app.tabs.len() - 1;
|
2025-07-16 13:27:31 -04:00
|
|
|
app.text_needs_processing = true;
|
|
|
|
|
}
|
|
|
|
|
if app.show_find && !app.find_query.is_empty() {
|
|
|
|
|
app.update_find_matches();
|
2025-07-05 14:42:45 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
Err(err) => {
|
2025-07-15 00:42:01 -04:00
|
|
|
eprintln!("Failed to open file: {err}");
|
2025-07-05 14:42:45 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn save_file(app: &mut TextEditor) {
|
|
|
|
|
if let Some(active_tab) = app.get_active_tab() {
|
|
|
|
|
if let Some(path) = &active_tab.file_path {
|
2025-07-16 17:20:09 -04:00
|
|
|
save_to_path(app, path.to_path_buf());
|
2025-07-05 14:42:45 -04:00
|
|
|
} else {
|
|
|
|
|
save_as_file(app);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn save_as_file(app: &mut TextEditor) {
|
|
|
|
|
if let Some(path) = rfd::FileDialog::new()
|
|
|
|
|
.add_filter("Text files", &["*"])
|
|
|
|
|
.save_file()
|
|
|
|
|
{
|
|
|
|
|
save_to_path(app, path);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
pub(crate) fn save_to_path(app: &mut TextEditor, path: PathBuf) {
|
|
|
|
|
if let Some(active_tab) = app.get_active_tab_mut() {
|
|
|
|
|
match fs::write(&path, &active_tab.content) {
|
|
|
|
|
Ok(()) => {
|
2025-07-16 17:20:09 -04:00
|
|
|
let title = path.file_name().and_then(|n| n.to_str()).unwrap_or("Untitled");
|
|
|
|
|
active_tab.file_path = Some(path.to_path_buf());
|
|
|
|
|
active_tab.title = title.to_string();
|
2025-07-05 14:42:45 -04:00
|
|
|
active_tab.mark_as_saved();
|
|
|
|
|
}
|
|
|
|
|
Err(err) => {
|
2025-07-15 00:42:01 -04:00
|
|
|
eprintln!("Failed to save file: {err}");
|
2025-07-05 14:42:45 -04:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|