Browse Source

basic editor

db-refactoring
Coin de Gamma 3 months ago
parent
commit
069bc02f80
  1. 1
      Cargo.toml
  2. 12
      src/db.rs
  3. 29
      src/editor.rs
  4. 10
      src/main.rs

1
Cargo.toml

@ -8,3 +8,4 @@ edition = "2021"
[dependencies] [dependencies]
clap = { version = "4.5.16", features = ["derive"] } clap = { version = "4.5.16", features = ["derive"] }
once_cell = "1.19.0" once_cell = "1.19.0"
tempfile = "3.12.0"

12
src/db.rs

@ -13,7 +13,10 @@ pub struct Item {
} }
impl Item { impl Item {
pub fn from(s: String) -> Item { pub fn from(s: String, c: String) -> Item {
Item { id: s, content: c }
}
pub fn from_d(s: String) -> Item {
Item { id: s, content: String::from("") } Item { id: s, content: String::from("") }
} }
} }
@ -65,7 +68,8 @@ impl DB {
for line in reader.lines() { for line in reader.lines() {
match line { match line {
Ok(id) => { Ok(id) => {
items.insert(Item::from(id)); // TODO: check content
items.insert(Item::from_d(id));
}, },
Err(e) => { Err(e) => {
eprintln!("Error reading line, {}", e); eprintln!("Error reading line, {}", e);
@ -80,7 +84,8 @@ impl DB {
} }
pub fn contains(&self, id: &String) -> bool { pub fn contains(&self, id: &String) -> bool {
let item = Item::from(id.clone()); // TODO: check with content
let item = Item::from_d(id.clone());
self.items.contains(&item) self.items.contains(&item)
} }
@ -98,4 +103,3 @@ impl DB {
} }
} }

29
src/editor.rs

@ -0,0 +1,29 @@
use std::env;
use std::fs::read_to_string;
use std::process::Command;
use tempfile::NamedTempFile;
use std::io;
pub fn open_to_edit() -> io::Result<String> {
// Create a temporary file
let temp_file = NamedTempFile::new()?;
// Get the user's preferred editor from the $EDITOR environment variable
// Default is 'nano'
let editor = env::var("EDITOR").unwrap_or_else(|_| "nano".to_string());
// Get the path of the temp file
let file_path = temp_file.path().to_str().unwrap().to_string();
// Open the file in the external editor
Command::new(editor)
.arg(&file_path)
.status()
.expect("Failed to open editor");
// Read the file content after editing
let edited_content = read_to_string(&file_path)?;
// Print the edited content
Ok(edited_content)
}

10
src/main.rs

@ -1,4 +1,5 @@
mod db; mod db;
mod editor;
use db::{DB, Item}; use db::{DB, Item};
@ -28,7 +29,7 @@ struct Cli {
#[derive(Subcommand)] #[derive(Subcommand)]
enum Commands { enum Commands {
/// Initialisation of storage and config, use this in first time of usage of mps /// Initialization of storage and config, use this in first time of usage of mps
Init, Init,
/// Adds new item with unique id to the db /// Adds new item with unique id to the db
@ -74,7 +75,12 @@ fn add(id: &String) -> io::Result<()> {
)) ))
} }
db.items.insert(Item::from(id.clone())); // TODO: implement encryption
let content = editor::open_to_edit()?;
println!("Content");
println!("--------");
println!("{}", content);
db.items.insert(Item::from(id.clone(), content));
db.dump()?; db.dump()?;
Ok(()) Ok(())

Loading…
Cancel
Save