Browse Source

new line after editing

fix-typo
Coin de Gamma 3 months ago
parent
commit
421d861b2e
  1. 20
      src/editor.rs
  2. 37
      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
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(())
}

37
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(())
}

Loading…
Cancel
Save