diff --git a/Cargo.lock b/Cargo.lock index 0ff3bfd..8a11de7 100644 --- a/Cargo.lock +++ b/Cargo.lock @@ -4,13 +4,14 @@ version = 3 [[package]] name = "ahash" -version = "0.8.3" +version = "0.8.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2c99f64d1e06488f620f932677e24bc6e2897582980441ae90a671415bd7ec2f" +checksum = "42cd52102d3df161c77a887b608d7a4897d7cc112886a9537b738a887a03aaff" dependencies = [ "cfg-if", "once_cell", "version_check", + "zerocopy", ] [[package]] @@ -63,7 +64,7 @@ dependencies = [ "async-lock", "async-task", "concurrent-queue", - "fastrand", + "fastrand 1.9.0", "futures-lite", "slab", ] @@ -97,7 +98,7 @@ dependencies = [ "log", "parking", "polling", - "rustix", + "rustix 0.37.23", "slab", "socket2", "waker-fn", @@ -164,9 +165,9 @@ checksum = "bef38d45163c2f1dde094a7dfd33ccf595c92905c8f8f4fdc18d06fb1037718a" [[package]] name = "bitflags" -version = "2.3.3" +version = "2.4.2" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "630be753d4e58660abd17930c71b647fe46c27ea6b63cc59e1e3851406972e42" +checksum = "ed570934406eb16438a4e976b1b4500774099c13b8cb96eec99f620f05090ddf" [[package]] name = "blocking" @@ -178,7 +179,7 @@ dependencies = [ "async-lock", "async-task", "atomic-waker", - "fastrand", + "fastrand 1.9.0", "futures-lite", "log", ] @@ -203,9 +204,9 @@ checksum = "baf1de4339761588bc0619e3cbc0120ee582ebb74b53b4efbf79117bd2da40fd" [[package]] name = "chrono" -version = "0.4.33" +version = "0.4.34" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "9f13690e35a5e4ace198e7beea2895d29f3a9cc55015fcebe6336bd2010af9eb" +checksum = "5bc015644b92d5890fab7489e49d21f879d5c990186827d42ec511919404f38b" dependencies = [ "android-tzdata", "iana-time-zone", @@ -226,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]] @@ -243,7 +244,7 @@ version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "37e93977247fb916abeee1ff8c6594c9b421fd9c26c9b720a3944acb2a7de27b" dependencies = [ - "crossterm 0.27.0", + "crossterm", ] [[package]] @@ -259,7 +260,7 @@ source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "11650f38fa0dc9fae9b4ac5243c4d32992351f82bb5452c5c1fd7e639057b732" dependencies = [ "crokey-proc_macros", - "crossterm 0.27.0", + "crossterm", "once_cell", "serde", "strict", @@ -271,7 +272,7 @@ version = "0.6.3" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "3588f310d39d0c6f546aaca1e9df18d1dfb961a8bb4042a11b6c5a6b449c1894" dependencies = [ - "crossterm 0.27.0", + "crossterm", "proc-macro2", "quote", "strict", @@ -345,29 +346,13 @@ dependencies = [ "cfg-if", ] -[[package]] -name = "crossterm" -version = "0.25.0" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "e64e6c0fbe2c17357405f7c758c1ef960fce08bdfb2c03d88d2a18d7e09c4b67" -dependencies = [ - "bitflags 1.3.2", - "crossterm_winapi", - "libc", - "mio", - "parking_lot", - "signal-hook", - "signal-hook-mio", - "winapi", -] - [[package]] name = "crossterm" version = "0.27.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "f476fe445d41c9e991fd07515a6f463074b782242ccf4a5b7b1d1012e70824df" dependencies = [ - "bitflags 2.3.3", + "bitflags 2.4.2", "crossterm_winapi", "libc", "mio", @@ -420,12 +405,6 @@ dependencies = [ "windows-sys 0.48.0", ] -[[package]] -name = "dyn-clone" -version = "1.0.12" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "304e6508efa593091e97a9abbc10f90aa7ca635b6d2784feff3c89d41dd12272" - [[package]] name = "encode_unicode" version = "0.3.6" @@ -434,23 +413,12 @@ checksum = "a357d28ed41a50f9c765dbfe56cbc04a64e53e5fc58ba79fbc34c10ef3df831f" [[package]] name = "errno" -version = "0.3.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "4bcfec3a70f97c962c307b2d2c56e358cf1d00b558d74262b5f929ee8cc7e73a" -dependencies = [ - "errno-dragonfly", - "libc", - "windows-sys 0.48.0", -] - -[[package]] -name = "errno-dragonfly" -version = "0.1.2" +version = "0.3.8" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "aa68f1b12764fab894d2755d2518754e71b4fd80ecfb822714a1206c2aab39bf" +checksum = "a258e46cdc063eb8519c00b9fc845fc47bcfca4130e2f08e88665ceda8474245" dependencies = [ - "cc", "libc", + "windows-sys 0.52.0", ] [[package]] @@ -480,6 +448,12 @@ dependencies = [ "instant", ] +[[package]] +name = "fastrand" +version = "2.0.1" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "25cbce373ec4653f1a01a31e8a5e5ec0c622dc27ff9c4e6606eefef5cbbed4a5" + [[package]] name = "futures-channel" version = "0.3.28" @@ -507,7 +481,7 @@ version = "1.13.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "49a9d51ce47660b1e808d3c990b4709f2f415d928835a17dfd16991515c46bce" dependencies = [ - "fastrand", + "fastrand 1.9.0", "futures-core", "futures-io", "memchr", @@ -541,9 +515,9 @@ dependencies = [ [[package]] name = "hashbrown" -version = "0.14.0" +version = "0.14.3" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "2c6201b9ff9fd90a5a3bac2e56a830d0caa509576f0e503818ee82c181b3437a" +checksum = "290f1a1d9242c78d09ce40a5e87e7554ee637af1351968159f4952f028f75604" dependencies = [ "ahash", "allocator-api2", @@ -551,9 +525,9 @@ dependencies = [ [[package]] name = "hashlink" -version = "0.8.3" +version = "0.9.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "312f66718a2d7789ffef4f4b7b213138ed9f1eb3aa1d0d82fc99f88fb3ffd26f" +checksum = "692eaaf7f7607518dd3cef090f1474b61edc5301d8012f09579920df68b725ee" dependencies = [ "hashbrown", ] @@ -587,22 +561,6 @@ dependencies = [ "cc", ] -[[package]] -name = "inquire" -version = "0.6.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "c33e7c1ddeb15c9abcbfef6029d8e29f69b52b6d6c891031b88ed91b5065803b" -dependencies = [ - "bitflags 1.3.2", - "crossterm 0.25.0", - "dyn-clone", - "lazy_static", - "newline-converter", - "thiserror", - "unicode-segmentation", - "unicode-width", -] - [[package]] name = "instant" version = "0.1.12" @@ -661,7 +619,7 @@ dependencies = [ "proc-macro2", "quote", "regex", - "syn 2.0.26", + "syn 2.0.32", ] [[package]] @@ -672,15 +630,15 @@ checksum = "e2abad23fbc42b3700f2f279844dc832adb2b2eb069b2df918f455c4e18cc646" [[package]] name = "libc" -version = "0.2.147" +version = "0.2.153" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "b4668fb0ea861c1df094127ac5f1da3409a82116a4ba74fca2e58ef927159bb3" +checksum = "9c198f91728a82281a64e1f4f9eeb25d82cb32a5de251c6bd1b5154d63a8e7bd" [[package]] name = "libsqlite3-sys" -version = "0.27.0" +version = "0.28.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "cf4e226dcd58b4be396f7bd3c20da8fdee2911400705297ba7d2d7cc2c30f716" +checksum = "0c10584274047cb335c23d3e61bcef8e323adae7c5c8c760540f73610177fc3f" dependencies = [ "cc", "pkg-config", @@ -693,6 +651,12 @@ version = "0.3.8" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "ef53942eb7bf7ff43a617b3e2c1c4a5ecf5944a7c1bc12d7ee39bbb15e5c1519" +[[package]] +name = "linux-raw-sys" +version = "0.4.13" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "01cda141df6706de531b6c46c3a33ecca755538219bd484262fa09410c13539c" + [[package]] name = "lock_api" version = "0.4.10" @@ -748,26 +712,17 @@ dependencies = [ "windows-sys 0.48.0", ] -[[package]] -name = "newline-converter" -version = "0.2.2" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1f71d09d5c87634207f894c6b31b6a2b2c64ea3bdcf71bd5599fdbbe1600c00f" -dependencies = [ - "unicode-segmentation", -] - [[package]] name = "notabena" -version = "0.1.0" +version = "0.1.1" dependencies = [ "async-std", "chrono", - "crossterm 0.27.0", + "crossterm", "dialoguer", "directories", - "inquire", "rusqlite", + "tempfile", "termimad", ] @@ -933,11 +888,11 @@ checksum = "dbb5fb1acd8a1a18b3dd5be62d25485eb770e05afb408a9627d14d451bae12da" [[package]] name = "rusqlite" -version = "0.30.0" +version = "0.31.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "a78046161564f5e7cd9008aff3b2990b3850dc8e0349119b98e8f251e099f24d" +checksum = "b838eba278d213a8beaf485bd313fd580ca4505a00d5871caeb1457c55322cae" dependencies = [ - "bitflags 2.3.3", + "bitflags 2.4.2", "fallible-iterator", "fallible-streaming-iterator", "hashlink", @@ -955,10 +910,23 @@ dependencies = [ "errno", "io-lifetimes", "libc", - "linux-raw-sys", + "linux-raw-sys 0.3.8", "windows-sys 0.48.0", ] +[[package]] +name = "rustix" +version = "0.38.31" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "6ea3e1a662af26cd7a3ba09c0297a31af215563ecf42817c98df621387f4e949" +dependencies = [ + "bitflags 2.4.2", + "errno", + "libc", + "linux-raw-sys 0.4.13", + "windows-sys 0.52.0", +] + [[package]] name = "scopeguard" version = "1.2.0" @@ -982,7 +950,7 @@ checksum = "389894603bd18c46fa56231694f8d827779c0951a667087194cf9de94ed24682" dependencies = [ "proc-macro2", "quote", - "syn 2.0.26", + "syn 2.0.32", ] [[package]] @@ -1065,9 +1033,9 @@ dependencies = [ [[package]] name = "syn" -version = "2.0.26" +version = "2.0.32" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "45c3457aacde3c65315de5031ec191ce46604304d2446e803d71ade03308d970" +checksum = "239814284fd6f1a4ffe4ca893952cdd93c224b6a1571c9a9eadd670295c0c9e2" dependencies = [ "proc-macro2", "quote", @@ -1076,16 +1044,14 @@ dependencies = [ [[package]] name = "tempfile" -version = "3.6.0" +version = "3.10.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "31c0432476357e58790aaa47a8efb0c5138f137343f3b5f23bd36a27e3b0a6d6" +checksum = "a365e8cd18e44762ef95d87f284f4b5cd04107fec2ff3052bd6a3e6069669e67" dependencies = [ - "autocfg", "cfg-if", - "fastrand", - "redox_syscall 0.3.5", - "rustix", - "windows-sys 0.48.0", + "fastrand 2.0.1", + "rustix 0.38.31", + "windows-sys 0.52.0", ] [[package]] @@ -1121,7 +1087,7 @@ checksum = "463fe12d7993d3b327787537ce8dd4dfa058de32fc2b195ef3cde03dc4771e8f" dependencies = [ "proc-macro2", "quote", - "syn 2.0.26", + "syn 2.0.32", ] [[package]] @@ -1130,12 +1096,6 @@ version = "1.0.11" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "301abaae475aa91687eb82514b328ab47a211a533026cb25fc3e519b86adfc3c" -[[package]] -name = "unicode-segmentation" -version = "1.10.1" -source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "1dd624098567895118886609431a7c3b8f516e41d30e0643f03d94592a147e36" - [[package]] name = "unicode-width" version = "0.1.10" @@ -1193,7 +1153,7 @@ dependencies = [ "once_cell", "proc-macro2", "quote", - "syn 2.0.26", + "syn 2.0.32", "wasm-bindgen-shared", ] @@ -1227,7 +1187,7 @@ checksum = "54681b18a46765f095758388f2d0cf16eb8d4169b639ab575a8f5693af210c7b" dependencies = [ "proc-macro2", "quote", - "syn 2.0.26", + "syn 2.0.32", "wasm-bindgen-backend", "wasm-bindgen-shared", ] @@ -1279,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" @@ -1298,18 +1249,12 @@ dependencies = [ ] [[package]] -name = "windows-targets" -version = "0.42.2" +name = "windows-sys" +version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" -checksum = "8e5180c00cd44c9b1c88adb3693291f1cd93605ded80c250a75d472756b4d071" +checksum = "282be5f36a8ce781fad8c8ae18fa3f9beff57ec1b52cb3de0789201425d9a33d" 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", + "windows-targets 0.52.0", ] [[package]] @@ -1342,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" @@ -1360,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" @@ -1378,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" @@ -1396,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" @@ -1414,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" @@ -1432,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" @@ -1450,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" @@ -1468,6 +1371,26 @@ version = "0.52.0" source = "registry+https://github.com/rust-lang/crates.io-index" checksum = "dff9641d1cd4be8d1a070daf9e3773c5f67e78b4d9d42263020c057706765c04" +[[package]] +name = "zerocopy" +version = "0.7.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "74d4d3961e53fa4c9a25a8637fc2bfaf2595b3d3ae34875568a5cf64787716be" +dependencies = [ + "zerocopy-derive", +] + +[[package]] +name = "zerocopy-derive" +version = "0.7.32" +source = "registry+https://github.com/rust-lang/crates.io-index" +checksum = "9ce1b18ccd8e73a9321186f97e46f9f04b778851177567b1975109d26a08d2a6" +dependencies = [ + "proc-macro2", + "quote", + "syn 2.0.32", +] + [[package]] name = "zeroize" version = "1.6.0" diff --git a/Cargo.toml b/Cargo.toml index 2aa1a30..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" @@ -14,9 +14,9 @@ categories = [ "command-line-utilities", "gui", "text-processing" ] [dependencies] async-std = "1.12.0" directories = "5.0.1" -chrono = "0.4.33" +chrono = "0.4.34" dialoguer = "0.11.0" -inquire = "0.6.2" crossterm = "0.27.0" -rusqlite = { version = "0.30.0", features = ["bundled"] } +rusqlite = { version = "0.31.0", features = ["bundled"] } termimad = "0.29.1" +tempfile = "3.10.0" diff --git a/src/main.rs b/src/main.rs index 592edd3..e3d839c 100644 --- a/src/main.rs +++ b/src/main.rs @@ -2,13 +2,14 @@ mod api; mod note; mod prompts; mod return_to_main; +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 75cf186..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,27 +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> { @@ -74,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)?; @@ -81,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(), }; @@ -91,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 c032748..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}; +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('r'), - modifiers: event::KeyModifiers::CONTROL, - 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 new file mode 100644 index 0000000..e70f56e --- /dev/null +++ b/src/tests/api.rs @@ -0,0 +1,72 @@ +#[cfg(test)] +use crate::{api, note::Note}; +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(()) +} + +#[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 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(()) +} + +#[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 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()); +} + +#[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 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(()) +} diff --git a/src/tests/mod.rs b/src/tests/mod.rs new file mode 100644 index 0000000..b32f9e2 --- /dev/null +++ b/src/tests/mod.rs @@ -0,0 +1 @@ +mod api;