Skip to content
This repository has been archived by the owner on Nov 18, 2024. It is now read-only.

Commit

Permalink
sort notes by newest first
Browse files Browse the repository at this point in the history
  • Loading branch information
ThatFrogDev committed Mar 18, 2024
1 parent d6b5409 commit 9f4f9e5
Show file tree
Hide file tree
Showing 2 changed files with 24 additions and 5 deletions.
4 changes: 4 additions & 0 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,5 +65,9 @@ pub fn get_notes(db_file: &PathBuf) -> Result<Vec<Note>> {
notes.push(note?);
}

// sort notes by date: newest first
notes.sort_by(|a, b| a.created.cmp(&b.created));
notes.reverse();

Ok(notes)
}
25 changes: 20 additions & 5 deletions src/note.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
use crate::{
api, multiselect,
prompts::{confirm::confirm, input::input, select::select},
return_to_main, truncate_note,
truncate_note,
utilities::{cursor_to_origin::cursor_to_origin, display::display},
};
use async_std::path::PathBuf;
Expand All @@ -18,18 +18,33 @@ pub struct Note {

impl Note {
pub fn create(db_file: &PathBuf) -> Result<(), Box<dyn std::error::Error>> {
let sqlite = Connection::open(db_file)?;

// fetch IDs from database, sort and find the first gap. if it does not exist, use the length of the array + 1
let mut stmt = sqlite.prepare("SELECT id FROM saved_notes")?;
let ids: Result<Vec<usize>, _> = stmt
.query_map(params![], |row| row.get(0))?
.collect();
let mut ids = ids?;
ids.sort_unstable();
let id = ids.clone()
.into_iter()
.enumerate()
.find(|(i, id)| i + 1 != *id)
.map_or_else(|| ids.len() + 1, |(i, _)| i + 1);

cursor_to_origin()?;
println!(
"If you're done inputting a field, you can press Enter twice to continue or save, or Alt/Option-Q to return to the main menu.\r"
);
let mut inputted_note = Note {
id: api::get_notes(db_file)?.len(),
let inputted_note = Note {
id: id,
name: input("Name:", "".to_string())?,
content: input("Content:", "".to_string())?,
created: format!("{}", Local::now().format("%A %e %B, %H:%M")),
};

Connection::open(db_file)?.execute(
sqlite.execute(
"INSERT INTO saved_notes (id, name, content, created) VALUES (?1, ?2, ?3, ?4);",
params![
&inputted_note.id,
Expand Down Expand Up @@ -58,7 +73,7 @@ impl Note {
let mut selected_note = &saved_notes[selection];
cursor_to_origin()?;

display(&mut selected_note);
display(&mut selected_note)?;
Ok(())
}

Expand Down

0 comments on commit 9f4f9e5

Please sign in to comment.