From 7296559c16f823e7a8c25d50c682dc23705d9924 Mon Sep 17 00:00:00 2001 From: osamu-kj Date: Wed, 19 Apr 2023 20:57:41 +0200 Subject: [PATCH] Calendar: Create basic object structure --- CMakeLists.txt | 15 +++++++------- RESOURCES.md | 7 +++++++ src/calendar.cpp | 51 ++++++++++++++++++++++++++++++++++++++++++++++++ src/calendar.hpp | 21 ++++++++++++++++++++ src/main.cpp | 40 ++++++++++++------------------------- 5 files changed, 98 insertions(+), 36 deletions(-) create mode 100644 RESOURCES.md create mode 100644 src/calendar.cpp create mode 100644 src/calendar.hpp diff --git a/CMakeLists.txt b/CMakeLists.txt index 2a5fb5f..f690441 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -22,11 +22,10 @@ project(vis set(CMAKE_EXPORT_COMPILE_COMMANDS 1) -add_executable(vis src/main.cpp) -target_include_directories(vis PRIVATE src) - -target_link_libraries(vis - PRIVATE ftxui::screen - PRIVATE ftxui::dom - PRIVATE ftxui::component # Not needed for this example. -) +add_executable(vis + src/main.cpp + src/calendar.cpp + ) + +find_library(NCURSES_LIB NAMES ncurses) +target_link_libraries(vis ncurses) diff --git a/RESOURCES.md b/RESOURCES.md new file mode 100644 index 0000000..2c664f5 --- /dev/null +++ b/RESOURCES.md @@ -0,0 +1,7 @@ +# Resources for development + +> **Note:** VIS mainly uses the NCURSES library, therefore, expect the majority of resources to be some sort of documentation for NCURSES. + +- [INVISIBLE_ISLAND - Ncurses Docs](https://invisible-island.net/ncurses/man/ncurses.3x.html) + - [TLDP - NCURSES Programming HOWTO](https://tldp.org/HOWTO/NCURSES-Programming-HOWTO/) + - [JBWYATT - Ncurses Programming Guide](https://jbwyatt.com/ncurses.html) diff --git a/src/calendar.cpp b/src/calendar.cpp new file mode 100644 index 0000000..56c779f --- /dev/null +++ b/src/calendar.cpp @@ -0,0 +1,51 @@ +#include +#include "calendar.hpp" + +calendar_information::calendar_information() { + time_t current_time = time(NULL); + struct tm *datetime = localtime(¤t_time); + + current_day = datetime->tm_mday; + current_month = datetime->tm_mon+1; + current_year = datetime->tm_year + 1900; + current_month_days = Calendar::get_days_in_month(current_month, current_year); +} + +int Calendar::get_days_in_month(int month, int year) { + tm date = {0}; + date.tm_year = year - 1900; + date.tm_mon = month; + date.tm_mday = 0; + + mktime(&date); + return date.tm_mday; +} + +void Calendar::set_day(int day) { + if (day > this->info.current_month_days) return; + this->info.current_day = day; +} + +void Calendar::set_month(int month) { + if (month > 12) return; + this->info.current_month = month; + this->info.current_month_days = Calendar::get_days_in_month(this->info.current_month, this->info.current_year); + if (this->info.current_day > this->info.current_month_days) + this->info.current_day = this->info.current_month_days; +} + +void Calendar::set_year(int year) { + this->info.current_year = year; + this->info.current_month_days = Calendar::get_days_in_month(this->info.current_month, this->info.current_year); + if (this->info.current_day > this->info.current_month_days) + this->info.current_day = this->info.current_month_days; +} + +Calendar::Calendar(calendar_information *preinfo) { + this->info = *preinfo; + if (preinfo != nullptr) { + return; + } + + this->info = calendar_information(); +} diff --git a/src/calendar.hpp b/src/calendar.hpp new file mode 100644 index 0000000..8b9c7da --- /dev/null +++ b/src/calendar.hpp @@ -0,0 +1,21 @@ +struct calendar_information { + int current_day; + int current_month; + int current_month_days; + int current_year; + + calendar_information(); +}; + +class Calendar { + public: + static int get_days_in_month(int month, int year); + void set_day(int day); + void set_month(int month); + void set_year(int year); + calendar_information get_info(); + + Calendar(calendar_information *preinfo = nullptr); + private: + calendar_information info; +}; diff --git a/src/main.cpp b/src/main.cpp index c1fd7bf..c3baf49 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,39 +1,23 @@ -#include #include -#include -#include -#include -#include +#include +#include +#include +#include "calendar.hpp" + +#define MIN_WIDTH 100 +#define MIN_HEIGHT 40 using namespace std; -using namespace ftxui; int main() { + initscr(); - auto screen = ScreenInteractive::FitComponent(); + int active_cell = 0; - auto component = Renderer([&screen] { - auto make_box = [](int x, int y, int i) { - string title = to_string(i); - return window(text(""), text(title) | hcenter | size(WIDTH, EQUAL, x) | size(HEIGHT, EQUAL, y)); - }; + for (;;) { - Element calendar = hflow({ - make_box(12,7,0), - make_box(7,7,1), - make_box(7,7,2), - make_box(7,7,3), - make_box(7,7,4), - make_box(7,7,5), - make_box(7,7,6), - make_box(7,7,7) - }); - - return window(text("Vi Scheduler"), vbox(calendar)); - }); - - - screen.Loop(component); + refresh(); + } return 0; }