Browse Source

basic editing

fix-typo
Coin de Gamma 3 months ago
parent
commit
d0adf49fee
  1. 11
      src/editor.rs
  2. 20
      src/main.rs
  3. 7
      src/storage.rs

11
src/editor.rs

@ -7,10 +7,14 @@ use std::io::{self, Write};
const DEFAULT_EDITOR: &str = "nano";
pub fn open_to_edit() -> io::Result<String> {
// Use with content "" (empty string) for adding new item
pub fn open_to_edit(content: &String) -> io::Result<String> {
// Create a temporary file
let temp_file = NamedTempFile::new()?;
let mut temp_file = NamedTempFile::new()?;
// Write content
write!(temp_file, "{}", content)?;
// Get the user's preferred editor from the $EDITOR environment variable
// Default is 'nano'
let editor = env::var("EDITOR").unwrap_or_else(|_| DEFAULT_EDITOR.to_string());
@ -47,3 +51,4 @@ pub fn open_to_show(content: &String) -> io::Result<()> {
.status()?;
Ok(())
}

20
src/main.rs

@ -128,8 +128,9 @@ fn add(id: &String) -> io::Result<()> {
PROMPT::NO => return Ok(()),
}
}
let content = editor::open_to_edit()?;
// set empty string because there is no content yet
let content = editor::open_to_edit(&String::from(""))?;
st.add(Item::from(id.clone(), content));
st.dump()?;
@ -138,6 +139,21 @@ fn add(id: &String) -> io::Result<()> {
fn edit(id: &String) -> io::Result<()> {
// TODO: implement
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 mut item = (*st.get(id)).clone();
let new_content = editor::open_to_edit(&item.content)?;
item.content = new_content;
st.update(item);
st.dump()?;
Ok(())
}

7
src/storage.rs

@ -18,7 +18,7 @@ static STORAGE_PATH: Lazy<String> = Lazy::new(|| {
});
static PASSWORD_TEST_SALT: &str = "MyPasswordStorage1234567890";
#[derive(Clone)]
pub struct Item {
pub id: String,
pub content: String
@ -200,6 +200,11 @@ impl Storage {
self.items.insert(item);
}
pub fn update(&mut self, item: Item) {
self.items.remove(&item);
self.items.insert(item);
}
pub fn dump(&self) -> io::Result<()> {
let mut file = fs::OpenOptions::new()
.write(true)

Loading…
Cancel
Save