diff --git a/Cargo.lock b/Cargo.lock index 3aa719e..8a11de7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -227,15 +227,15 @@ dependencies = [ [[package]] name = "console" -version = "0.15.7" +version = "0.15.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c926e00cc70edefdc64d3a5ff31cc65bb97a3460097762bd23afb4d8145fccf8" +checksum = "0e1f83fc076bd6dd27517eacdf25fef6c4dfe5f1d7448bafaaf3a26f13b5e4eb" dependencies = [ "encode_unicode", "lazy_static", "libc", "unicode-width", - "windows-sys 0.45.0", + "windows-sys 0.52.0", ] [[package]] @@ -714,7 +714,7 @@ dependencies = [ [[package]] name = "notabena" -version = "0.1.0" +version = "0.1.1" dependencies = [ "async-std", "chrono", @@ -1239,15 +1239,6 @@ dependencies = [ "windows-targets 0.48.1", ] -[[package]] -name = "windows-sys" -version = "0.45.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "75283be5efb2831d37ea142365f009c02ec203cd29a3ebecbc093d52315b66d0" -dependencies = [ - "windows-targets 0.42.2", -] - [[package]] name = "windows-sys" version = "0.48.0" @@ -1266,21 +1257,6 @@ dependencies = [ "windows-targets 0.52.0", ] -[[package]] -name = "windows-targets" -version = "0.42.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e5180c00cd44c9b1c88adb3693291f1cd93605ded80c250a75d472756b4d071" -dependencies = [ - "windows_aarch64_gnullvm 0.42.2", - "windows_aarch64_msvc 0.42.2", - "windows_i686_gnu 0.42.2", - "windows_i686_msvc 0.42.2", - "windows_x86_64_gnu 0.42.2", - "windows_x86_64_gnullvm 0.42.2", - "windows_x86_64_msvc 0.42.2", -] - [[package]] name = "windows-targets" version = "0.48.1" @@ -1311,12 +1287,6 @@ dependencies = [ "windows_x86_64_msvc 0.52.0", ] -[[package]] -name = "windows_aarch64_gnullvm" -version = "0.42.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "597a5118570b68bc08d8d59125332c54f1ba9d9adeedeef5b99b02ba2b0698f8" - [[package]] name = "windows_aarch64_gnullvm" version = "0.48.0" @@ -1329,12 +1299,6 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "cb7764e35d4db8a7921e09562a0304bf2f93e0a51bfccee0bd0bb0b666b015ea" -[[package]] -name = "windows_aarch64_msvc" -version = "0.42.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e08e8864a60f06ef0d0ff4ba04124db8b0fb3be5776a5cd47641e942e58c4d43" - [[package]] name = "windows_aarch64_msvc" version = "0.48.0" @@ -1347,12 +1311,6 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "bbaa0368d4f1d2aaefc55b6fcfee13f41544ddf36801e793edbbfd7d7df075ef" -[[package]] -name = "windows_i686_gnu" -version = "0.42.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c61d927d8da41da96a81f029489353e68739737d3beca43145c8afec9a31a84f" - [[package]] name = "windows_i686_gnu" version = "0.48.0" @@ -1365,12 +1323,6 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "a28637cb1fa3560a16915793afb20081aba2c92ee8af57b4d5f28e4b3e7df313" -[[package]] -name = "windows_i686_msvc" -version = "0.42.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "44d840b6ec649f480a41c8d80f9c65108b92d89345dd94027bfe06ac444d1060" - [[package]] name = "windows_i686_msvc" version = "0.48.0" @@ -1383,12 +1335,6 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ffe5e8e31046ce6230cc7215707b816e339ff4d4d67c65dffa206fd0f7aa7b9a" -[[package]] -name = "windows_x86_64_gnu" -version = "0.42.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8de912b8b8feb55c064867cf047dda097f92d51efad5b491dfb98f6bbb70cb36" - [[package]] name = "windows_x86_64_gnu" version = "0.48.0" @@ -1401,12 +1347,6 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3d6fa32db2bc4a2f5abeacf2b69f7992cd09dca97498da74a151a3132c26befd" -[[package]] -name = "windows_x86_64_gnullvm" -version = "0.42.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "26d41b46a36d453748aedef1486d5c7a85db22e56aff34643984ea85514e94a3" - [[package]] name = "windows_x86_64_gnullvm" version = "0.48.0" @@ -1419,12 +1359,6 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "1a657e1e9d3f514745a572a6846d3c7aa7dbe1658c056ed9c3344c4109a6949e" -[[package]] -name = "windows_x86_64_msvc" -version = "0.42.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9aec5da331524158c6d1a4ac0ab1541149c0b9505fde06423b02f5ef0106b9f0" - [[package]] name = "windows_x86_64_msvc" version = "0.48.0" diff --git a/Cargo.toml b/Cargo.toml index 556094c..36fb809 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,8 +1,8 @@ [package] name = "notabena" -version = "0.1.0" +version = "0.1.1" edition = "2021" -authors = [ "ThatFrogDev", "MrSerge01" ] +authors = [ "ThatFrogDev" ] license-file = "LICENSE.md" description = "A note-taking app for the command line. GUI support will be added (see README)." homepage = "https://github.com/thatfrogdev/notabena" diff --git a/src/main.rs b/src/main.rs index 50c24c4..e3d839c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,14 +2,14 @@ mod api; mod note; mod prompts; mod return_to_main; -mod utilities; mod tests; +mod utilities; use crate::{ note::Note, prompts::{multiselect::multiselect, select::select}, + return_to_main::return_to_main, utilities::{cursor_to_origin::cursor_to_origin, truncate_note::truncate_note}, - return_to_main::return_to_main }; use async_std::path::PathBuf; use directories::BaseDirs; diff --git a/src/note.rs b/src/note.rs index 4d97a56..27c8e49 100644 --- a/src/note.rs +++ b/src/note.rs @@ -1,7 +1,7 @@ use crate::{ api, multiselect, prompts::{confirm::confirm, input::input, select::select}, - truncate_note, + return_to_main, truncate_note, utilities::{cursor_to_origin::cursor_to_origin, display::display}, }; use async_std::path::PathBuf; @@ -19,10 +19,13 @@ pub struct Note { impl Note { pub fn create(db_file: &PathBuf) -> Result<(), Box> { cursor_to_origin()?; + println!( + "If you're done inputting a field, you can press Enter twice to continue or save, or Alt/Option-Q to return to the main menu.\r" + ); let mut inputted_note = Note { id: api::get_notes(db_file)?.len(), - name: input("Name:", "".to_string()), - content: input("Content:", "".to_string()), + name: input("Name:", "".to_string())?, + content: input("Content:", "".to_string())?, created: format!("{}", Local::now().format("%A %e %B, %H:%M")), }; @@ -30,26 +33,18 @@ impl Note { println!("This is the note you're about to create:"); display(&mut inputted_note)?; - match confirm("Do you want to save this note?") { - true => { - Connection::open(db_file)?.execute( - "INSERT INTO saved_notes (id, name, content, created) VALUES (?1, ?2, ?3, ?4);", - params![ - &inputted_note.id, - &inputted_note.name, - &inputted_note.content, - &inputted_note.created - ], - )?; - cursor_to_origin()?; - println!("Note created successfully."); - Ok(()) - } - false => { - cursor_to_origin()?; - Ok(()) - } - } + Connection::open(db_file)?.execute( + "INSERT INTO saved_notes (id, name, content, created) VALUES (?1, ?2, ?3, ?4);", + params![ + &inputted_note.id, + &inputted_note.name, + &inputted_note.content, + &inputted_note.created + ], + )?; + cursor_to_origin()?; + println!("Note created successfully."); + Ok(()) } pub fn show(db_file: &PathBuf) -> Result<(), Box> { @@ -73,6 +68,9 @@ impl Note { pub fn edit(db_file: &PathBuf) -> Result<(), Box> { cursor_to_origin()?; + println!( + "If you're done editing a field, you can press Enter twice to continue or save, or Alt/Option-Q to return to the main menu.\r" + ); let saved_notes = api::get_notes(db_file)?; let mut options: Vec = Vec::new(); truncate_note(&mut options, db_file)?; @@ -80,8 +78,8 @@ impl Note { let selected_note = &saved_notes[selection]; let updated_note = Note { id: selected_note.id.clone(), - name: input("Name:", selected_note.name.clone()), - content: input("Content:", selected_note.content.clone()), + name: input("Name:", selected_note.name.clone())?, + content: input("Content:", selected_note.content.clone())?, created: selected_note.created.clone(), }; @@ -90,15 +88,10 @@ impl Note { println!("You can't edit notes, because there are none."); } - match confirm("Are you sure that you want to edit this note?") { - true => { - cursor_to_origin()?; - api::save_note(&updated_note, db_file)?; // why the fuck whas this line not here yet - println!("Note updated successfully."); - Ok(()) - } - false => Ok(()), - } + cursor_to_origin()?; + api::save_note(&updated_note, db_file)?; // why the fuck whas this line not here yet + println!("Note updated successfully."); + Ok(()) } pub fn delete(db_file: &PathBuf) -> Result<(), Box> { diff --git a/src/prompts/input.rs b/src/prompts/input.rs index c97a3fc..710cf59 100644 --- a/src/prompts/input.rs +++ b/src/prompts/input.rs @@ -1,7 +1,9 @@ +use crate::return_to_main::{continue_event, return_event}; +use crate::{main, return_to_main}; use dialoguer::{theme::ColorfulTheme, Input}; -pub fn input(prompt: &str, initial_text: String) -> String { - match initial_text.as_str() { +pub fn input(prompt: &str, initial_text: String) -> Result> { + let result = match initial_text.as_str() { "" => Input::with_theme(&ColorfulTheme::default()) .with_prompt(prompt) .interact_text() @@ -11,5 +13,18 @@ pub fn input(prompt: &str, initial_text: String) -> String { .with_initial_text(initial_text) .interact_text() .unwrap(), + }; + + match return_to_main() { + Ok(value) => { + if value == return_event() { + main()?; + } else if value == continue_event() { + return Ok(result); + } + } + _ => return Ok(result), } + + Ok(result) } diff --git a/src/return_to_main.rs b/src/return_to_main.rs index d6bdf4a..29e71a4 100644 --- a/src/return_to_main.rs +++ b/src/return_to_main.rs @@ -1,23 +1,38 @@ use crossterm::event; -use crossterm::event::{Event, KeyCode, KeyEvent, KeyModifiers}; +use crossterm::event::{Event, KeyCode, KeyEvent, KeyEventKind, KeyEventState, KeyModifiers}; use crossterm::terminal::{disable_raw_mode, enable_raw_mode}; use std::time::Duration; -// TODO: make it not bitch -pub fn return_to_main() -> Result<(), Box> { +// very hacky way to make these values public +pub fn return_event() -> KeyEvent { + KeyEvent { + code: KeyCode::Char('q'), + modifiers: KeyModifiers::ALT, + kind: KeyEventKind::Press, + state: KeyEventState::NONE, + } +} + +pub fn continue_event() -> KeyEvent { + KeyEvent { + code: KeyCode::Enter, + modifiers: KeyModifiers::NONE, + kind: KeyEventKind::Press, + state: KeyEventState::NONE, + } +} + +pub fn return_to_main() -> Result> { enable_raw_mode()?; loop { if event::poll(Duration::from_millis(1000))? { if let Event::Key(event) = event::read()? { - match event { - KeyEvent { - code: KeyCode::Char('q'), - modifiers: KeyModifiers::ALT, - kind: _, - state: _, - } => return Ok(()), - _ => (), + if event == return_event() { + return Ok(return_event()); + } + if event == continue_event() { + return Ok(continue_event()); } } } diff --git a/src/tests/api.rs b/src/tests/api.rs index 68c04af..e70f56e 100644 --- a/src/tests/api.rs +++ b/src/tests/api.rs @@ -1,72 +1,72 @@ #[cfg(test)] use crate::{api, note::Note}; -use chrono::prelude::Local; use async_std::path::PathBuf; +use chrono::prelude::Local; use tempfile::tempdir; #[test] fn test_db_init() -> Result<(), Box> { - let dir = tempdir()?; - let data_directory = PathBuf::from(dir.path()); - let db_file = data_directory.join("notes.db"); - assert!(api::init_db(&data_directory, &db_file).is_ok()); - Ok(()) + let dir = tempdir()?; + let data_directory = PathBuf::from(dir.path()); + let db_file = data_directory.join("notes.db"); + assert!(api::init_db(&data_directory, &db_file).is_ok()); + Ok(()) } #[test] fn test_save_note() -> Result<(), Box> { - let dir = tempdir()?; - let data_directory = PathBuf::from(dir.path()); - let db_file: PathBuf = data_directory.join("notes.db"); - api::init_db(&data_directory, &db_file)?; + let dir = tempdir()?; + let data_directory = PathBuf::from(dir.path()); + let db_file: PathBuf = data_directory.join("notes.db"); + api::init_db(&data_directory, &db_file)?; - let note = Note { - id: 0, - name: "Test".to_string(), - content: "Test".to_string(), - created: format!("{}", Local::now().format("%A %e %B, %H:%M")), - }; + let note = Note { + id: 0, + name: "Test".to_string(), + content: "Test".to_string(), + created: format!("{}", Local::now().format("%A %e %B, %H:%M")), + }; - assert!(api::save_note(¬e, &db_file).is_ok()); - Ok(()) + assert!(api::save_note(¬e, &db_file).is_ok()); + Ok(()) } #[test] fn test_delete_notes() { - let dir = tempdir().unwrap(); - let data_directory = PathBuf::from(dir.path()); - let db_file = data_directory.join("notes.db"); - api::init_db(&data_directory, &db_file).unwrap(); + let dir = tempdir().unwrap(); + let data_directory = PathBuf::from(dir.path()); + let db_file = data_directory.join("notes.db"); + api::init_db(&data_directory, &db_file).unwrap(); - let note = Note { - id: 0, - name: "Test".to_string(), - content: "Test".to_string(), - created: format!("{}", Local::now().format("%A %e %B, %H:%M")), - }; + let note = Note { + id: 0, + name: "Test".to_string(), + content: "Test".to_string(), + created: format!("{}", Local::now().format("%A %e %B, %H:%M")), + }; - api::save_note(¬e, &db_file).unwrap(); - let notes = api::get_notes(&db_file).unwrap(); - let ids: Vec = notes.iter().map(|note| note.id).collect(); - api::delete_notes(ids, &db_file).unwrap(); - assert!(api::get_notes(&db_file).unwrap().is_empty()); + api::save_note(¬e, &db_file).unwrap(); + let notes = api::get_notes(&db_file).unwrap(); + let ids: Vec = notes.iter().map(|note| note.id).collect(); + api::delete_notes(ids, &db_file).unwrap(); + assert!(api::get_notes(&db_file).unwrap().is_empty()); } #[test] fn test_get_notes() -> Result<(), Box> { - let dir = tempdir()?; - let data_directory = PathBuf::from(dir.path()); - let db_file = data_directory.join("notes.db"); - api::init_db(&data_directory, &db_file)?; + let dir = tempdir()?; + let data_directory = PathBuf::from(dir.path()); + let db_file = data_directory.join("notes.db"); + api::init_db(&data_directory, &db_file)?; - let note = Note { - id: 0, - name: "Test".to_string(), - content: "Test".to_string(), - created: format!("{}", Local::now().format("%A %e %B, %H:%M")), - }; + let note = Note { + id: 0, + name: "Test".to_string(), + content: "Test".to_string(), + created: format!("{}", Local::now().format("%A %e %B, %H:%M")), + }; - api::save_note(¬e, &db_file)?; - assert!(!api::get_notes(&db_file)?.is_empty()); - Ok(()) -} \ No newline at end of file + api::save_note(¬e, &db_file)?; + assert!(!api::get_notes(&db_file)?.is_empty()); + Ok(()) +} diff --git a/src/tests/mod.rs b/src/tests/mod.rs index 50123e3..b32f9e2 100644 --- a/src/tests/mod.rs +++ b/src/tests/mod.rs @@ -1 +1 @@ -mod api; \ No newline at end of file +mod api;