From 4c1bc842a47c07481289374d5e29cb8b570fc211 Mon Sep 17 00:00:00 2001 From: osamu-kj <64986162+0xdeadbeer@users.noreply.github.com> Date: Sat, 6 May 2023 22:57:15 +0200 Subject: [PATCH] Calendar: Add input handling, basic input mode, add README.md file, alongside other fixes --- README.md | 56 +++++++++++++++++ src/engine/engine.cpp | 143 +++++++++++++++++++++++++++++++++++++++++- src/engine/engine.hpp | 10 ++- src/global/limits.hpp | 0 src/main.cpp | 31 +++------ 5 files changed, 213 insertions(+), 27 deletions(-) create mode 100644 README.md create mode 100644 src/global/limits.hpp diff --git a/README.md b/README.md new file mode 100644 index 0000000..875b3cc --- /dev/null +++ b/README.md @@ -0,0 +1,56 @@ +# Vi Scheduler + +Vi Scheduler (VIS) is a lightweight tool that brings a Vim-like calendar to your terminal. It allows you to quickly view and edit your schedule, appointments, and tasks without leaving your command-line interface. + +Built using C++ and the Ncurses library, Vis offers a fast and efficient way to manage your time in the terminal. It suppors various navigation and editing commands inspired by Vim, such as navigating between days or months, adding and deleting events, and setting reminders. + +# Features + + - Vim-like navigation and editing commands + - Multiple view modes: month, week + +# Dependencies + +The following is a list of dependencies that your system needs in order to be able to run Vis. + - [The Ncurses library](https://invisible-island.net/ncurses/announce.html) + - [Moreutils](https://joeyh.name/code/moreutils/) + +# Building + +To build Vis, simply clone this repository and run `mkdir build; cd build; cmake ..; make` + +Resulting binary will be generated inside the `/build/src/` folder called. + +# Usage + +To start Vis simply run the `vis` binary in your terminal alongside the name of the calendar file. + +For example: `vis school.cal` + +This will open the default view mode (month) and display the current month's calendar. From there, you can use the following commands to navigate and edit the calendar: + - `h/j/k/l` - Move 1 day left/down/up/right + - `u/o` - Move 1 month left/right + - `gg` - Go to the beginning of the month + - `G` - Go to the end of the month + - `i` - Enter insert mode for the active day. + - `q` - Quit and save the calendar + +# Todos + +The following is a list of things that have been/still have to be completed. + + - basic prototype with vipe (in memory only) + - writing a calendar to file + - reading a calendar from a file + - alerting function + - logging + - read input from stdin + - set reminder + +# Contributions + +Contributions and feedback are welcome! If you have any ideas or suggestions for improving Vis, please submit a pull request or open an issue on Github. Let's make the FOSS community better, together! + +# License + +This project is licensed under the GNU General Public License v3.0. See the `LICENSE` file for more information. diff --git a/src/engine/engine.cpp b/src/engine/engine.cpp index 282accb..72acfa8 100644 --- a/src/engine/engine.cpp +++ b/src/engine/engine.cpp @@ -1,5 +1,6 @@ #include #include +#include #include "engine.hpp" Engine::Engine(int padding, calendar_view_mode view_mode) { @@ -9,6 +10,7 @@ Engine::Engine(int padding, calendar_view_mode view_mode) { calendar_information date_info = calendar_information(); this->calendar = new Calendar(&date_info); + this->active_cell = --date_info.current_day; } Engine::~Engine() { @@ -19,29 +21,60 @@ void Engine::ui_draw(WINDOW *win) { this->cells_table.clear(); box(win, 0, 0); + wrefresh(win); + + keypad(win, TRUE); + keypad(stdscr, TRUE); switch (this->view_mode) { case MONTH_VIEW: this->ui_month_draw(win); break; + case WEEK_VIEW: + this->ui_week_draw(win); + break; case MONTHS_VIEW: this->ui_months_draw(win); break; } + this->ui_top_draw(win); this->ui_bottom_draw(win); - wrefresh(win); + } void Engine::ui_month_draw(WINDOW *win) { - + calendar_information date_info = this->calendar->get_info(); + int z = COLS - this->padding; + int q = LINES - (2*this->padding); + for (int i = 0; i < date_info.current_month_days; i++) { + int x_location = this->padding + ((z/7) * (i%7)); + int y_location = this->padding + (q/(date_info.current_month_days/7) * (i/7)); + + if (this->active_cell == i) wattron(win, COLOR_PAIR(1)); + mvwprintw(win, y_location, x_location, "D%d", i+1); + if (this->active_cell == i) wattroff(win, COLOR_PAIR(1)); + } +} + +void Engine::ui_week_draw(WINDOW *win) { + } void Engine::ui_months_draw(WINDOW *win) { } +void Engine::ui_top_draw(WINDOW *win) { + if (VIS_COLORING) wattron(win, COLOR_PAIR(1)); + + mvwprintw(win, 1, 1, "Vi Scheduler"); + mvwprintw(win, 2, 1, "@0xdeadbeer"); + + if (VIS_COLORING) wattroff(win, COLOR_PAIR(1)); +} + void Engine::ui_bottom_draw(WINDOW *win) { if (VIS_COLORING) wattron(win, COLOR_PAIR(1)); @@ -50,3 +83,109 @@ void Engine::ui_bottom_draw(WINDOW *win) { if (VIS_COLORING) wattroff(win, COLOR_PAIR(1)); } + +void Engine::ui_dialog_draw(WINDOW *win) { + int lines = LINES/2, cols = COLS/2; + int y_center = (LINES/2)-(lines/2), x_center = (COLS/2)-(cols/2); + + this->dialog = newwin(lines, cols, y_center, x_center); + + box(this->dialog, 0, 0); + + char str[200]; + + mvwprintw(this->dialog, 1, 1, "Enter your name lol: "); + wrefresh(this->dialog); + + mvwgetnstr(this->dialog, 2, 1, str, 200); + + wprintw(this->dialog, "Press q to save or r to retake"); + + wrefresh(this->dialog); + +} + +void Engine::input_handle_month(WINDOW *win) { + char key = getch(); + + calendar_information date_info = this->calendar->get_info(); + + int day, month, res; + switch (key) { + case 'h': + if (this->calendar->get_info().current_day <= 1) break; + + day = this->calendar->get_info().current_day; + this->calendar->set_day(--day); + + --this->active_cell; + break; + case 'l': + if (this->calendar->get_info().current_day >= this->calendar->get_info().current_month_days) break; + + day = this->calendar->get_info().current_day; + this->calendar->set_day(++day); + + ++this->active_cell; + break; + case 'j': + if (this->calendar->get_info().current_day+7 > this->calendar->get_info().current_month_days) break; + + day = this->calendar->get_info().current_day; + this->calendar->set_day(day+7); + + this->active_cell += 7; + break; + case 'k': + if (this->calendar->get_info().current_day-7 < 1) break; + + day = this->calendar->get_info().current_day; + this->calendar->set_day(day-7); + + this->active_cell -= 7; + break; + case 'u': + if (this->calendar->get_info().current_month <= 1) break; + + month = this->calendar->get_info().current_month; + this->calendar->set_month(--month); + this->calendar->set_day(1); + + this->active_cell = 0; + break; + case 'o': + if (this->calendar->get_info().current_month >= 12) break; + + month = this->calendar->get_info().current_month; + this->calendar->set_month(++month); + this->calendar->set_day(1); + + this->active_cell = 0; + break; + case 'i': + endwin(); + int link[2]; + char foo[4096]; + + pipe(link); + pid_t pid = fork(); + + if (pid == 0) { + dup2(link[1], STDOUT_FILENO); + close(link[0]); + close(link[1]); + system("vipe"); + exit(EXIT_FAILURE); + } else { + close(link[1]); + int nbytes = read(link[0], foo, sizeof(foo)); + printw("Output of vipe: %s\n", foo); + } + + break; + } +} + +void Engine::input_handle_months(WINDOW *win) { + // TODO +} diff --git a/src/engine/engine.hpp b/src/engine/engine.hpp index c758519..a9b5f92 100644 --- a/src/engine/engine.hpp +++ b/src/engine/engine.hpp @@ -13,7 +13,8 @@ struct engine_child { enum calendar_view_mode { MONTH_VIEW = 0, - MONTHS_VIEW, + WEEK_VIEW, + MONTHS_VIEW }; // title: drawing engine @@ -25,10 +26,16 @@ class Engine { void ui_draw(WINDOW *win); void ui_month_draw(WINDOW *win); + void ui_week_draw(WINDOW *win); void ui_months_draw(WINDOW *win); void ui_bottom_draw(WINDOW *win); + void ui_top_draw(WINDOW *win); + + void input_handle_month(WINDOW *win); + void input_handle_months(WINDOW *win); Calendar *calendar; + int active_cell; calendar_view_mode view_mode; int padding; // padding @@ -39,3 +46,4 @@ class Engine { }; #endif + diff --git a/src/global/limits.hpp b/src/global/limits.hpp new file mode 100644 index 0000000..e69de29 diff --git a/src/main.cpp b/src/main.cpp index 8544f58..dd67ef5 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -17,10 +17,12 @@ void sig_winch(int sig) { wresize(main_win, LINES, COLS); + mvwin(main_win, 0, 0); // redraw the TUI after the resize signal engine.ui_draw(main_win); flushinp(); + return; } void signals() { @@ -30,13 +32,11 @@ void signals() { int main() { initscr(); refresh(); - noecho(); curs_set(0); // invisible cursor refresh(); main_win = newwin(LINES, COLS, 0, 0); - if (VIS_COLORING) { start_color(); use_default_colors(); @@ -51,33 +51,16 @@ int main() { // handle required signals signals(); + for (;;) { wclear(main_win); wrefresh(main_win); engine.ui_draw(main_win); - - char ch = getch(); - int month; - switch (ch) { - case 'k': - month = engine.calendar->get_info().current_month; - if (month >= 12) { - month = 12; - break; - } - engine.calendar->set_month(++month); - break; - case 'j': - month = engine.calendar->get_info().current_month; - if (month <= 1) { - month = 1; - break; - } - - engine.calendar->set_month(--month); - break; - } + if (engine.view_mode == MONTH_VIEW) + engine.input_handle_month(main_win); + else if (engine.view_mode == MONTHS_VIEW) + engine.input_handle_months(main_win); } endwin();