Skip to content

Commit

Permalink
add database
Browse files Browse the repository at this point in the history
  • Loading branch information
arch-il committed Aug 6, 2024
1 parent 5954fc2 commit 017c4f3
Show file tree
Hide file tree
Showing 9 changed files with 57 additions and 19 deletions.
Binary file added notes/database.db3
Binary file not shown.
4 changes: 3 additions & 1 deletion src/calendar/calendar_position.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ use chrono::{DateTime, Days, Local, Months};
pub struct CalendarPosition {
pub date: DateTime<Local>,
pub editing: CurrentlyEditing,
pub open: bool,
}

#[derive(Debug)]
Expand All @@ -18,6 +19,7 @@ impl CalendarPosition {
Self {
date: Local::now(),
editing: CurrentlyEditing::Month,
open: false,
}
}

Expand Down Expand Up @@ -73,7 +75,7 @@ impl CalendarPosition {
match self.editing {
CurrentlyEditing::Year => self.editing = CurrentlyEditing::Month,
CurrentlyEditing::Month => self.editing = CurrentlyEditing::Day,
CurrentlyEditing::Day => todo!(),
CurrentlyEditing::Day => self.open = true,
}
}

Expand Down
3 changes: 3 additions & 0 deletions src/calendar/calendar_state.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,10 @@
use chrono::{DateTime, Local};

use super::CalendarPosition;

#[derive(Debug)]
pub enum CalendarState {
Browse(CalendarPosition),
Open(DateTime<Local>),
Exit,
}
44 changes: 31 additions & 13 deletions src/database.rs
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
use std::error::Error;

use chrono::{DateTime, Local};
use rusqlite::Connection;

use crate::note::Note;
Expand All @@ -10,17 +9,18 @@ pub struct Database {

impl Database {
pub fn new() -> Self {
let conn = Connection::open_in_memory().expect("Failed to create connection");
conn.execute(
"CREATE TABLE note(
id INTEGER PRIMARY KEY,
text TEXT NOT NULL,
creation_date DATETIME NOT NULL,
last_edited DATETIME NOT NULL
)",
(),
)
.expect("Failed to create table");
// let conn = Connection::open_in_memory().expect("Failed to create connection");
let conn = Connection::open("./notes/database.db3").expect("Failed to connect to database");
// conn.execute(
// "CREATE TABLE note(
// id INTEGER PRIMARY KEY,
// text TEXT NOT NULL,
// creation_date DATETIME NOT NULL,
// last_edited DATETIME NOT NULL
// )",
// (),
// )
// .expect("Failed to create table");

Self { conn }
}
Expand Down Expand Up @@ -51,4 +51,22 @@ impl Database {
.map(|note| note.unwrap())
.collect()
}

pub fn get_or_create_note(&self, date: &DateTime<Local>) -> Note {
if let Some(note) = self
.get_all_notes()
.iter()
.find(|note| &note.creation_date == date)
{
return note.clone();
}
let note = Note {
id: 0,
text: String::from(" "),
creation_date: date.clone(),
last_edited: date.clone(),
};
self.insert_note(&note);
note
}
}
9 changes: 7 additions & 2 deletions src/editor.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,22 @@
use chrono::{DateTime, Local};
pub use cursor::Cursor;

mod copy_paste;
mod cursor;
mod editor_state;
mod scroll;
mod search;
mod editor_state;
mod text;

pub use search::Search;
pub use editor_state::EditorState;
pub use search::Search;
pub use text::Text;

#[derive(Debug)]
pub struct Editor {
pub state: EditorState,
pub text: Text,
pub date: DateTime<Local>,
pub current_file: Option<String>,
pub scroll_offset: (u16, u16),
pub screen_size: (u16, u16),
Expand All @@ -26,6 +28,7 @@ impl Editor {
Editor {
state: EditorState::Edit,
text: Text::new(),
date: Local::now(),
current_file: None,
scroll_offset: (0, 0),
screen_size: (0, 0),
Expand All @@ -36,6 +39,7 @@ impl Editor {
Editor {
state: EditorState::Edit,
text: Text::from_string(text),
date: Local::now(),
current_file: None,
scroll_offset: (0, 0),
screen_size: (0, 0),
Expand All @@ -46,6 +50,7 @@ impl Editor {
Editor {
state: EditorState::Edit,
text: Text::new(),
date: Local::now(),
current_file: None,
scroll_offset: (0, 0),
screen_size: (0, 0),
Expand Down
4 changes: 4 additions & 0 deletions src/input.rs
Original file line number Diff line number Diff line change
Expand Up @@ -84,6 +84,10 @@ pub fn take_calendar_input(state: &mut CalendarState) {
return;
}
if let CalendarState::Browse(ref mut cal_position) = state {
if cal_position.open {
*state = CalendarState::Open(cal_position.date);
return;
}
match key_event.code {
KeyCode::Left => cal_position.move_left(),
KeyCode::Char('h') => cal_position.move_left(),
Expand Down
8 changes: 7 additions & 1 deletion src/main.rs
Original file line number Diff line number Diff line change
@@ -1,7 +1,6 @@
use std::io;

use calendar::{CalendarPosition, CalendarState};
use chrono::Local;
use database::Database;
use editor::EditorState;
use state::State;
Expand All @@ -22,6 +21,7 @@ use crate::editor::Editor;
fn main() -> io::Result<()> {
let mut terminal = terminal::init()?;

let database = Database::new();
// let file_name = "notes/note.md";
// let text = fs::read_to_string(file_name)?;
// let temp = Editor::from_string(text);
Expand Down Expand Up @@ -59,6 +59,12 @@ fn main() -> io::Result<()> {
})?;
input::take_calendar_input(cal_state);
}
CalendarState::Open(date) => {
let note = database.get_or_create_note(&date);
let mut editor = Editor::from_string(note.text);
editor.date = *date;
state = State::Editor(editor);
}
CalendarState::Exit => state = State::Exit,
},
State::Exit => todo!(),
Expand Down
2 changes: 1 addition & 1 deletion src/note.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
use chrono::{DateTime, Local};

#[derive(Debug)]
#[derive(Debug, Clone)]
pub struct Note {
pub id: i32,
pub text: String,
Expand Down
2 changes: 1 addition & 1 deletion src/ui/calendar.rs
Original file line number Diff line number Diff line change
Expand Up @@ -94,7 +94,7 @@ fn get_month_paragraph(

let now = Local::now();

let start_offset = (date.weekday() as i16 - date.day0() as i16 % 7) as usize * 3;
let start_offset = ((7 + date.weekday() as i16 - date.day0() as i16 % 7) % 7 * 3) as usize;
let mut start = Local
.with_ymd_and_hms(date.year(), date.month(), 1, 0, 0, 0)
.unwrap();
Expand Down

0 comments on commit 017c4f3

Please sign in to comment.