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

Commit

Permalink
Merge pull request #59 from ThatFrogDev/dev
Browse files Browse the repository at this point in the history
Sort notes by newest first
  • Loading branch information
ThatFrogDev authored Mar 18, 2024
2 parents e6870ff + 3d83f24 commit bb1aed4
Show file tree
Hide file tree
Showing 5 changed files with 75 additions and 31 deletions.
14 changes: 14 additions & 0 deletions .github/FUNDING.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,14 @@
# These are supported funding model platforms

github: # Replace with up to 4 GitHub Sponsors-enabled usernames e.g., [user1, user2]
patreon: # Replace with a single Patreon username
open_collective: # Replace with a single Open Collective username
ko_fi: # Replace with a single Ko-fi username
tidelift: # Replace with a single Tidelift platform-name/package-name e.g., npm/babel
community_bridge: # Replace with a single Community Bridge project-name e.g., cloud-foundry
liberapay: # Replace with a single Liberapay username
issuehunt: # Replace with a single IssueHunt username
lfx_crowdfunding: # Replace with a single LFX Crowdfunding project-name e.g., cloud-foundry
polar: # Replace with a single Polar username
buy_me_a_coffee: # Replace with a single Buy Me a Coffee username
custom: 'paypal.me/notabena'
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)
}
12 changes: 5 additions & 7 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -12,9 +12,7 @@ use crate::{
prompts::{multiselect::multiselect, select::select},
return_to_main::return_to_main,
utilities::{
cursor_to_origin::cursor_to_origin,
format_md::{inline, paragraph},
truncate_note::truncate_note,
cursor_to_origin::cursor_to_origin, format_md::paragraph, truncate_note::truncate_note,
},
};
use async_std::path::PathBuf;
Expand Down Expand Up @@ -54,19 +52,19 @@ fn display_about() -> Result<(), Box<dyn std::error::Error>> {
let skin: MadSkin = MadSkin::default();

cursor_to_origin()?;
println!("{}", paragraph(&skin, &format!("# About Notabena")));
println!("{}", paragraph(&skin, "# About Notabena"));
println!(
"{}",
inline(
paragraph(
&skin,
"**Notabena** is a FOSS note-taking CLI tool, written in Rust.\n"
"**Notabena** is a FOSS note-taking CLI tool, written in Rust.\nDonations are always a great way to help us keeping the project alive. It can be done here: https://paypal.me/Notabena (ctrl+click to follow link)."
)
);
println!(
"version: v{}, licensed under: GPL v3",
env!("CARGO_PKG_VERSION")
);
println!("COPYRIGHT (c) 2023-PRESENT NOTABENA ORGANISATION\nPROJECT LEADS @ThatFrogDev, @MrSerge01, GITHUB CONTRIBUTORS\n");
println!("COPYRIGHT (c) 2023-PRESENT NOTABENA ORGANISATION\nPROJECT LEADS @ThatFrogDev, @MrSerge01, GITHUB CONTRIBUTORS\n\n(scroll up if you can't read everything)");

Ok(())
}
60 changes: 43 additions & 17 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,46 @@ 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(),
name: input("Name:", "".to_string())?,

let mut name: String;
loop {
name = input("Name:", "".to_string())?;
if name.len() > 64 {
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.\n\n\
error: The name is too long, it must be 64 characters or less.\r"
);
} else {
break;
}
}
let inputted_note = Note {
id,
name,
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 All @@ -55,10 +83,10 @@ impl Note {
let mut options: Vec<String> = Vec::new();
truncate_note(&mut options, db_file)?;
let selection = select("Select the note that you want to view:", &options);
let mut selected_note = &saved_notes[selection];
let selected_note = &saved_notes[selection];
cursor_to_origin()?;

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

Expand All @@ -79,7 +107,7 @@ impl Note {
let selection = select("Select the note that you want to edit:", &options);
let selected_note = &saved_notes[selection];
let updated_note = Note {
id: selected_note.id.clone(),
id: selected_note.id,
name: input("Name:", selected_note.name.clone())?,
content: input("Content:", selected_note.content.clone())?,
created: selected_note.created.clone(),
Expand Down Expand Up @@ -120,16 +148,14 @@ impl Note {
if selections.is_empty() {
println!("You didn't select any notes.");
Ok(())
} else if confirm(prompt) {
api::delete_notes(selections, db_file)?;
cursor_to_origin()?;
println!("Notes deleted successfully.");
Ok(())
} else {
if confirm(prompt) {
api::delete_notes(selections, db_file)?;
cursor_to_origin()?;
println!("Notes deleted successfully.");
Ok(())
} else {
cursor_to_origin()?;
Ok(())
}
cursor_to_origin()?;
Ok(())
}
}
}
16 changes: 9 additions & 7 deletions src/utilities/cursor_to_origin.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
use std::process::Command;

#[cfg(target_os = "windows")]
pub fn cursor_to_origin() -> Result<(), Box<dyn std::error::Error>> {
if cfg!(target_os = "windows") {
Command::new("cmd").args(["/c", "cls"]).spawn()?.wait()?;
Ok(())
} else {
Command::new("clear").spawn()?.wait()?;
Ok(())
}
Command::new("cmd").args(["/c", "cls"]).spawn()?.wait()?;
Ok(())
}

#[cfg(not(target_os = "windows"))]
pub fn cursor_to_origin() -> Result<(), Box<dyn std::error::Error>> {
Command::new("clear").spawn()?.wait()?;
Ok(())
}

0 comments on commit bb1aed4

Please sign in to comment.