diff --git a/src/editor.rs b/src/editor.rs index c1a8e3c..ba8672c 100644 --- a/src/editor.rs +++ b/src/editor.rs @@ -25,10 +25,20 @@ pub fn open_to_edit(content: &String) -> io::Result { // Open the file in the external editor Command::new(editor) .arg(&file_path) - .status()?; + .status() + .map_err(|e| io::Error::new( + io::ErrorKind::Other, + format!("Failed to launch editor: {}", e) + ))?; // Read the file content after editing - let edited_content = read_to_string(&file_path)?; + let mut edited_content = read_to_string(&file_path)?; + + // Remove only one trailing newline if it exists + // because editor like vim or nano adds to the end new line + if edited_content.ends_with('\n') { + edited_content.pop(); + } // Print the edited content Ok(edited_content) @@ -48,7 +58,11 @@ pub fn open_to_show(content: &String) -> io::Result<()> { // Open the file in the external editor Command::new(editor) .arg(&file_path) - .status()?; + .status() + .map_err(|e| io::Error::new( + io::ErrorKind::Other, + format!("Failed to launch editor: {}", e) + ))?; Ok(()) } diff --git a/src/main.rs b/src/main.rs index e75a7cd..0fd6169 100644 --- a/src/main.rs +++ b/src/main.rs @@ -90,18 +90,14 @@ fn init() -> io::Result<()> { } print!("Enter passphrase for storage: "); io::stdout().flush()?; - // TODO: rename to passphrase - let password = rpassword::read_password()?; + let ps = rpassword::read_password()?; print!("Reenter passphrase: "); io::stdout().flush()?; - let password2 = rpassword::read_password()?; - - if password != password2 { - return Err(io::Error::new(io::ErrorKind::InvalidInput, "Passwords must be equal")); + let ps2 = rpassword::read_password()?; + if ps != ps2 { + return Err(io::Error::new(io::ErrorKind::InvalidInput, "Passphrases must be equal")); } - - Storage::init(password)?; - + Storage::init(ps)?; Ok(()) } @@ -128,7 +124,6 @@ fn add(id: &String) -> io::Result<()> { PROMPT::NO => return Ok(()), } } - // set empty string because there is no content yet let content = editor::open_to_edit(&String::from(""))?; st.add(Item::from(id.clone(), content)); @@ -142,10 +137,14 @@ fn edit(id: &String) -> io::Result<()> { let passphrase = login()?; let mut st = Storage::from_db(passphrase)?; if !st.contains(id) { - return Err(io::Error::new( - io::ErrorKind::InvalidInput, - format!("Can`t find id: {}", id) - )); + let question = format!("Item [{}] exist. Do you want to add it instead?", id); + match get_prompt(&question)? { + PROMPT::YES => { + add(id)?; + return Ok(()); + } + PROMPT::NO => return Ok(()) + } } let mut item = (*st.get(id)).clone(); @@ -158,8 +157,9 @@ fn edit(id: &String) -> io::Result<()> { } fn show(id: &String) -> io::Result<()> { - let passphrase = login()?; - let mut st = Storage::from_db(passphrase)?; + // TODO: get storage from login + let passphrase = login()?; + let st = Storage::from_db(passphrase)?; if !st.contains(id) { return Err(io::Error::new( io::ErrorKind::InvalidInput, @@ -168,7 +168,10 @@ fn show(id: &String) -> io::Result<()> { } let item = st.get(id); - editor::open_to_show(&item.content)?; + //editor::open_to_show(&item.content)?; + println!("---------"); + println!("{}", item.content); + println!("---------"); Ok(()) }