From 775f3486a9066dfc8cb1f31e6c5b26be253d05ed Mon Sep 17 00:00:00 2001 From: Akshay Date: Tue, 30 Jun 2020 10:11:56 +0530 Subject: access command mode via ':' and basic command parsing --- src/app.rs | 19 +++++++++++++++++++ src/main.rs | 7 +++++-- 2 files changed, 24 insertions(+), 2 deletions(-) diff --git a/src/app.rs b/src/app.rs index 5ba00bf..5b1fa9d 100644 --- a/src/app.rs +++ b/src/app.rs @@ -10,6 +10,7 @@ use cursive::{Printer, Vec2}; use chrono::NaiveDate; use crate::habit::{Bit, Count, Habit, HabitWrapper}; +use crate::Command; use crate::CONFIGURATION; use serde::{Deserialize, Serialize}; @@ -125,6 +126,24 @@ impl App { return serde_json::from_str(&j).unwrap(); } + pub fn parse_command(&mut self, input: &str) { + let c = Command::from_string(input); + match c { + Command::Add(name, kind, goal) => { + if kind == "count" { + self.add_habit(Box::new(Count::new(name, goal.unwrap_or(0)))); + eprintln!("Found COUNT!"); + } else if kind == "bit" { + self.add_habit(Box::new(Bit::new(name))); + eprintln!("Found BIT!"); + } + } + _ => { + eprintln!("UNKNOWN COMMAND!"); + } + } + } + // this function does IO // TODO: convert this into non-blocking async function fn save_state(&self) { diff --git a/src/main.rs b/src/main.rs index 3e838e5..418ec3b 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,14 +6,17 @@ use lazy_static::lazy_static; //use cursive::views::{Dialog, EditView, LinearLayout, ListView, SelectView}; use cursive::theme::{BaseColor, Color}; +use cursive::views::NamedView; use cursive::Cursive; mod habit; use crate::habit::{Bit, Count, Habit}; mod app; +mod command; mod theme; use crate::app::{App, ViewMode}; +use crate::command::{open_command_window, Command}; mod views; @@ -77,13 +80,13 @@ fn main() { // walking.insert_entry(NaiveDate::from_ymd(2020, 5, 14), false.into()); // walking.insert_entry(NaiveDate::from_ymd(2020, 5, 15), true.into()); - // let mut app = App::new(); // app.add_habit(Box::new(gymming)); // app.add_habit(Box::new(reading)); // app.add_habit(Box::new(walking)); let app = App::load_state(); - s.add_layer(app); + s.add_layer(NamedView::new("Main", app)); + s.add_global_callback(':', |s| open_command_window(s)); s.set_theme(theme::theme_gen()); s.run(); -- cgit v1.2.3