Browse Source

new line after editing

fix-typo
Coin de Gamma 3 months ago
parent
commit
421d861b2e
  1. 20
      src/editor.rs
  2. 35
      src/main.rs

20
src/editor.rs

@ -25,10 +25,20 @@ pub fn open_to_edit(content: &String) -> io::Result<String> {
// Open the file in the external editor // Open the file in the external editor
Command::new(editor) Command::new(editor)
.arg(&file_path) .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 // 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 // Print the edited content
Ok(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 // Open the file in the external editor
Command::new(editor) Command::new(editor)
.arg(&file_path) .arg(&file_path)
.status()?; .status()
.map_err(|e| io::Error::new(
io::ErrorKind::Other,
format!("Failed to launch editor: {}", e)
))?;
Ok(()) Ok(())
} }

35
src/main.rs

@ -90,18 +90,14 @@ fn init() -> io::Result<()> {
} }
print!("Enter passphrase for storage: "); print!("Enter passphrase for storage: ");
io::stdout().flush()?; io::stdout().flush()?;
// TODO: rename to passphrase let ps = rpassword::read_password()?;
let password = rpassword::read_password()?;
print!("Reenter passphrase: "); print!("Reenter passphrase: ");
io::stdout().flush()?; io::stdout().flush()?;
let password2 = rpassword::read_password()?; let ps2 = rpassword::read_password()?;
if ps != ps2 {
if password != password2 { return Err(io::Error::new(io::ErrorKind::InvalidInput, "Passphrases must be equal"));
return Err(io::Error::new(io::ErrorKind::InvalidInput, "Passwords must be equal"));
} }
Storage::init(ps)?;
Storage::init(password)?;
Ok(()) Ok(())
} }
@ -128,7 +124,6 @@ fn add(id: &String) -> io::Result<()> {
PROMPT::NO => return Ok(()), PROMPT::NO => return Ok(()),
} }
} }
// set empty string because there is no content yet // set empty string because there is no content yet
let content = editor::open_to_edit(&String::from(""))?; let content = editor::open_to_edit(&String::from(""))?;
st.add(Item::from(id.clone(), content)); st.add(Item::from(id.clone(), content));
@ -142,10 +137,14 @@ fn edit(id: &String) -> io::Result<()> {
let passphrase = login()?; let passphrase = login()?;
let mut st = Storage::from_db(passphrase)?; let mut st = Storage::from_db(passphrase)?;
if !st.contains(id) { if !st.contains(id) {
return Err(io::Error::new( let question = format!("Item [{}] exist. Do you want to add it instead?", id);
io::ErrorKind::InvalidInput, match get_prompt(&question)? {
format!("Can`t find id: {}", id) PROMPT::YES => {
)); add(id)?;
return Ok(());
}
PROMPT::NO => return Ok(())
}
} }
let mut item = (*st.get(id)).clone(); let mut item = (*st.get(id)).clone();
@ -158,8 +157,9 @@ fn edit(id: &String) -> io::Result<()> {
} }
fn show(id: &String) -> io::Result<()> { fn show(id: &String) -> io::Result<()> {
// TODO: get storage from login
let passphrase = login()?; let passphrase = login()?;
let mut st = Storage::from_db(passphrase)?; let st = Storage::from_db(passphrase)?;
if !st.contains(id) { if !st.contains(id) {
return Err(io::Error::new( return Err(io::Error::new(
io::ErrorKind::InvalidInput, io::ErrorKind::InvalidInput,
@ -168,7 +168,10 @@ fn show(id: &String) -> io::Result<()> {
} }
let item = st.get(id); let item = st.get(id);
editor::open_to_show(&item.content)?; //editor::open_to_show(&item.content)?;
println!("---------");
println!("{}", item.content);
println!("---------");
Ok(()) Ok(())
} }

Loading…
Cancel
Save