Skip to content

Commit

Permalink
✨ Added opening && Fixed error dialog && Dump version
Browse files Browse the repository at this point in the history
  • Loading branch information
nwrenger committed Mar 26, 2024
1 parent 449e597 commit b67a1e9
Show file tree
Hide file tree
Showing 5 changed files with 113 additions and 59 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "omega"
version = "0.1.0"
version = "0.1.1"
repository = "https://github.com/nwrenger/omega"
documentation = "https://github.com/nwrenger/omega"
readme = "README.md"
Expand Down
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ When specifying a not existing/invalid path the editor displays that in the top
| Quitting | `Ctrl` + `q` |
| Force Quitting | `Ctrl` + `f` |
| Saving File | `Ctrl` + `s` |
| Opening a File | `Ctrl` + `o` |

| Editor | Keybinding |
| ------------------ | --------------------------------------------- |
Expand Down
2 changes: 1 addition & 1 deletion src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -49,7 +49,7 @@ impl From<std::io::Error> for Error {

impl Error {
/// Converts this error into a UI element for a Cursive application.
fn to_dialog(self, siv: &mut Cursive) {
pub fn to_dialog(self, siv: &mut Cursive) {
if let Some(pos) = siv.screen_mut().find_layer_from_name("error") {
siv.screen_mut().remove_layer(pos);
}
Expand Down
161 changes: 107 additions & 54 deletions src/events.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,10 +9,9 @@ use cursive::{
Cursive,
};

use crate::State;
use crate::PKG_NAME;
use crate::{
error::Result, PKG_AUTHORS, PKG_DESCRIPTION, PKG_LICENSE, PKG_REPOSITORY, PKG_VERSION,
error::{Error, Result},
State, PKG_AUTHORS, PKG_DESCRIPTION, PKG_LICENSE, PKG_NAME, PKG_REPOSITORY, PKG_VERSION,
};

/// Shows all commands
Expand Down Expand Up @@ -47,6 +46,7 @@ pub fn info(s: &mut Cursive) -> Result<()> {
.child("Quitting", TextView::new("Ctrl + q"))
.child("Force Quitting", TextView::new("Ctrl + f"))
.child("Saving File", TextView::new("Ctrl + s"))
.child("Opening a File", TextView::new("Ctrl + o"))
.delimiter()
// editor
.child("Copying Line", TextView::new("Ctrl + c"))
Expand Down Expand Up @@ -80,65 +80,62 @@ pub fn save(s: &mut Cursive) -> Result<bool> {
s.screen_mut().remove_layer(pos);
Ok(false)
} else {
let state = s.with_user_data(|state: &mut State| state.clone()).unwrap();
s.set_user_data(State {
saved: Ok(false),
..state
});
let state = s.with_user_data(|state: &mut State| state.clone()).unwrap();
if state.file_path.is_none() {
let path_str = env::current_dir()?.to_string_lossy().to_string();

s.add_layer(
Dialog::new()
.title("Save As")
.padding_lrtb(1, 1, 1, 0)
.content(EditView::new().content(path_str).with_name("filepath"))
.button("Save", move |s| {
let new_path = s
.call_on_name("filepath", |view: &mut EditView| {
PathBuf::from(view.get_content().to_string())
})
.unwrap_or_default();

let content = s
.call_on_name("editor", |view: &mut TextArea| {
view.get_content().to_string()
})
.unwrap_or_default();

match fs::write(new_path.clone(), content) {
Ok(_) => {}
Err(e) => {
s.set_user_data(State {
file_path: Some(new_path.clone()),
saved: Err(e.into()),
});
return;
.content(
EditView::new()
.content(path_str.clone())
.with_name("filepath"),
)
.button("Save", {
move |s: &mut Cursive| {
let new_path = s
.call_on_name("filepath", |view: &mut EditView| {
PathBuf::from(view.get_content().to_string())
})
.unwrap_or_default();

let content = s
.call_on_name("editor", |view: &mut TextArea| {
view.get_content().to_string()
})
.unwrap_or_default();

match fs::write(new_path.clone(), content) {
Ok(_) => {}
Err(e) => {
Into::<Error>::into(e).to_dialog(s);
return;
}
}
}

s.call_on_name(
"title_text",
|view: &mut Panel<
OnEventView<ResizedView<ScrollView<NamedView<TextArea>>>>,
>| {
view.set_title(new_path.to_string_lossy())
},
)
.unwrap_or_default();
s.call_on_name(
"title_text",
|view: &mut Panel<
OnEventView<ResizedView<ScrollView<NamedView<TextArea>>>>,
>| {
view.set_title(new_path.to_string_lossy())
},
)
.unwrap_or_default();

s.set_user_data(State {
file_path: Some(new_path.clone()),
saved: Ok(true),
});
s.set_user_data(State {
file_path: Some(new_path.clone()),
});

s.pop_layer();
s.pop_layer();
}
})
.dismiss_button("Cancel")
.full_width()
.fixed_width(path_str.len() * 2)
.with_name("save"),
);
Ok(false)
} else {
let content = s
.call_on_name("editor", |view: &mut TextArea| {
Expand All @@ -165,15 +162,71 @@ pub fn save(s: &mut Cursive) -> Result<bool> {
)
.unwrap_or_default();

s.set_user_data(State {
saved: Ok(true),
..state
});
Ok(true)
}
}
}

/// Opens a new file safely with saving the current file
pub fn open(s: &mut Cursive) -> Result<()> {
if let Some(pos) = s.screen_mut().find_layer_from_name("open") {
s.screen_mut().remove_layer(pos);
Ok(())
} else {
let path_str = env::current_dir()?.to_string_lossy().to_string();
s.add_layer(
Dialog::new()
.title("Open")
.padding_lrtb(1, 1, 1, 0)
.content(
ListView::new()
.child(
"Make sure that",
TextView::new("you've saved your progress via Ctrl + s"),
)
.delimiter()
.child(
"Path",
EditView::new()
.content(path_str.clone())
.with_name("open_new_path"),
),
)
.button("Open", move |s| {
let new_path = s
.call_on_name("open_new_path", |view: &mut EditView| {
PathBuf::from(view.get_content().to_string())
})
.unwrap_or_default();

s.with_user_data(|state: &mut State| state.clone())
.unwrap()
.saved
match fs::read_to_string(new_path.clone()) {
Ok(content) => {
s.call_on_name("editor", |text_area: &mut TextArea| {
text_area.set_content(content);
})
.unwrap_or_default();
}
Err(e) => {
Into::<Error>::into(e).to_dialog(s);
return;
}
};

s.call_on_name(
"title_text",
|view: &mut Panel<
OnEventView<ResizedView<ScrollView<NamedView<TextArea>>>>,
>| { view.set_title(new_path.to_string_lossy()) },
)
.unwrap_or_default();

s.pop_layer();
})
.dismiss_button("Cancel")
.fixed_width(path_str.len() * 2)
.with_name("open"),
);
Ok(())
}
}

Expand Down
6 changes: 3 additions & 3 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ use cursive::traits::*;
use cursive::views::Panel;
use cursive::views::{OnEventView, TextArea};

use error::{Result, ResultExt};
use error::ResultExt;

const PKG_NAME: &str = env!("CARGO_PKG_NAME");
const PKG_VERSION: &str = env!("CARGO_PKG_VERSION");
Expand All @@ -30,7 +30,6 @@ const PKG_LICENSE: &str = env!("CARGO_PKG_LICENSE");
#[derive(Clone, Debug)]
struct State {
file_path: Option<PathBuf>,
saved: Result<bool>,
}

fn main() {
Expand All @@ -53,7 +52,6 @@ fn main() {

siv.set_user_data(State {
file_path: file_path.clone(),
saved: Ok(false),
});

// disable/handle globally
Expand All @@ -64,12 +62,14 @@ fn main() {
siv.clear_global_callbacks(Event::CtrlChar('q'));
siv.clear_global_callbacks(Event::CtrlChar('f'));
siv.clear_global_callbacks(Event::CtrlChar('s'));
siv.clear_global_callbacks(Event::CtrlChar('o'));

siv.add_global_callback(Event::CtrlChar('z'), |s| events::info(s).handle(s));
siv.add_global_callback(Event::CtrlChar('d'), |s| s.toggle_debug_console());
siv.add_global_callback(Event::CtrlChar('q'), |s| events::quit(s).handle(s));
siv.add_global_callback(Event::CtrlChar('f'), |s| s.quit());
siv.add_global_callback(Event::CtrlChar('s'), |s| events::save(s).handle(s));
siv.add_global_callback(Event::CtrlChar('o'), |s| events::open(s).handle(s));

let text_area = TextArea::new()
.content(content.clone().unwrap_or_default())
Expand Down

0 comments on commit b67a1e9

Please sign in to comment.