Skip to content

Commit

Permalink
Clear command and colored output
Browse files Browse the repository at this point in the history
  • Loading branch information
navinkarkera committed Dec 30, 2019
1 parent 2cf3356 commit 4d42507
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 12 deletions.
12 changes: 12 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

1 change: 1 addition & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,4 @@ serde_json = "1.0"
chrono = { version = "0.4", features = ["serde"] }
structopt = { version = "0.3", default-features = false }
dialoguer = "0.5"
colored = "1.8"
8 changes: 5 additions & 3 deletions src/data.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use chrono::prelude::*;
use colored::*;
use serde::{Deserialize, Serialize};
use std::fmt;

Expand All @@ -25,9 +26,10 @@ impl fmt::Display for Note {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"{}\n\nCreated at: {}",
self.note,
self.datetime.format("%v %r")
"{}\n\n{} {}",
self.note.cyan(),
"Created at:".green(),
self.datetime.format("%v %r").to_string().magenta()
)
}
}
36 changes: 27 additions & 9 deletions src/main.rs
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
use dialoguer::Editor;
use colored::*;
use dialoguer::{Confirmation, Editor};
use std::env;
use std::error::Error;
use std::fs::{read_to_string, File};
Expand All @@ -19,6 +20,7 @@ struct Cli {
enum Command {
List { number: Option<usize> },
Search { text: String },
Clear,
}

fn run() -> Result<(), Box<dyn Error>> {
Expand Down Expand Up @@ -47,6 +49,18 @@ fn run() -> Result<(), Box<dyn Error>> {
println!("No notes found!");
}
}
Command::Clear => {
if Confirmation::new()
.with_text("Do you want clear all notes?")
.default(false)
.interact()?
{
clear_notes();
println!("Cleared!!")
} else {
println!("Abort!");
}
}
},
None => {
if let Some(note_text) = Editor::new().edit("").expect("Please set a default editor") {
Expand Down Expand Up @@ -90,16 +104,16 @@ fn read_note_list() -> Result<Vec<data::Note>, std::io::Error> {
}

fn list_notes(note_list: &Vec<data::Note>, num: usize) -> String {
let number = if note_list.len() > num {
num
} else {
note_list.len()
};
note_list[..number]
note_list
.iter()
.rev()
.take(num)
.map(|n| n.to_string())
.collect::<Vec<String>>()
.join("\n===================================\n\n")
.collect::<Vec<_>>()
.join(&format!(
"\n{}\n\n",
"===================================".green()
))
}

fn write_notes(note_list: Vec<data::Note>) {
Expand All @@ -110,6 +124,10 @@ fn write_notes(note_list: Vec<data::Note>) {
.expect("Could not write to file");
}

fn clear_notes() {
write_notes(Vec::new());
}

fn main() {
if let Err(err) = run() {
println!("{}", err);
Expand Down

0 comments on commit 4d42507

Please sign in to comment.