Resizing: re-draw at terminal resize

This commit is contained in:
osamu-kj 2023-04-22 11:42:54 +02:00
parent 7296559c16
commit db58c196a9
10 changed files with 160 additions and 17 deletions

View File

@ -22,10 +22,7 @@ project(vis
set(CMAKE_EXPORT_COMPILE_COMMANDS 1) set(CMAKE_EXPORT_COMPILE_COMMANDS 1)
add_executable(vis add_subdirectory(src)
src/main.cpp
src/calendar.cpp
)
find_library(NCURSES_LIB NAMES ncurses) find_library(NCURSES_LIB NAMES ncurses)
target_link_libraries(vis ncurses) target_link_libraries(vis ncurses)

View File

@ -2,6 +2,6 @@
> **Note:** VIS mainly uses the NCURSES library, therefore, expect the majority of resources to be some sort of documentation for NCURSES. > **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) - [INVISIBLE_ISLAND - Ncurses Docs](https://invisible-island.net/ncurses/man/ncurses.3x.html)
- [TLDP - NCURSES Programming HOWTO](https://tldp.org/HOWTO/NCURSES-Programming-HOWTO/) - [TLDP - NCURSES Programming HOWTO](https://tldp.org/HOWTO/NCURSES-Programming-HOWTO/)
- [JBWYATT - Ncurses Programming Guide](https://jbwyatt.com/ncurses.html) - [JBWYATT - Ncurses Programming Guide](https://jbwyatt.com/ncurses.html)

7
src/CMakeLists.txt Normal file
View File

@ -0,0 +1,7 @@
add_subdirectory(engine)
add_subdirectory(calendar)
add_executable(vis main.cpp)
target_link_libraries(vis engine)
target_link_libraries(vis calendar)

View File

@ -0,0 +1 @@
add_library(calendar SHARED calendar.cpp)

View File

@ -41,11 +41,15 @@ void Calendar::set_year(int year) {
this->info.current_day = this->info.current_month_days; this->info.current_day = this->info.current_month_days;
} }
calendar_information Calendar::get_info() {
return this->info;
}
Calendar::Calendar(calendar_information *preinfo) { Calendar::Calendar(calendar_information *preinfo) {
this->info = *preinfo; this->info = *preinfo;
if (preinfo != nullptr) {
if (preinfo != nullptr)
return; return;
}
this->info = calendar_information(); this->info = calendar_information();
} }

View File

@ -1,3 +1,6 @@
#ifndef CALENDAR_HPP
#define CALENDAR_HPP
struct calendar_information { struct calendar_information {
int current_day; int current_day;
int current_month; int current_month;
@ -16,6 +19,8 @@ class Calendar {
calendar_information get_info(); calendar_information get_info();
Calendar(calendar_information *preinfo = nullptr); Calendar(calendar_information *preinfo = nullptr);
private:
private:
calendar_information info; calendar_information info;
}; };
#endif

View File

@ -0,0 +1 @@
add_library(engine SHARED engine.cpp)

42
src/engine/engine.cpp Normal file
View File

@ -0,0 +1,42 @@
#include <stdlib.h>
#include <ncurses.h>
#include "engine.hpp"
Engine::Engine(int padding, calendar_view_mode view_mode) {
this->padding = padding;
this->view_mode = view_mode;
this->cells_table = std::vector<engine_child>();
calendar_information date_info = calendar_information();
this->calendar = new Calendar(&date_info);
}
Engine::~Engine() {
delete this->calendar;
}
void Engine::draw(WINDOW *win) {
this->cells_table.clear();
box(win, 0, 0);
calendar_information date_info = this->calendar->get_info();
switch (this->view_mode) {
case MONTH_VIEW:
for (int i = 1; i <= date_info.current_month_days; i++)
mvwprintw(win, i, 3, "D%d", i);
break;
case MONTHS_VIEW:
for (int i = 1; i <= 12; i++)
wprintw(win, "M%d", i);
break;
}
wprintw(win, "\n");
mvwprintw(win, LINES-1, 0, "Year: %d, Month: %d, Day: %d", date_info.current_year, date_info.current_month, date_info.current_day);
wrefresh(win);
}

37
src/engine/engine.hpp Normal file
View File

@ -0,0 +1,37 @@
#ifndef ENGINE_HPP
#define ENGINE_HPP
#include <ncurses.h>
#include <vector>
#include "../calendar/calendar.hpp"
struct engine_child {
int x;
int y;
};
enum calendar_view_mode {
MONTH_VIEW = 0,
MONTHS_VIEW,
};
// title: drawing engine
// description: use for drawing the TUI and interact with the cells
class Engine {
public:
Engine(int padding = 10, calendar_view_mode view_mode = MONTH_VIEW);
~Engine();
void draw(WINDOW *win);
Calendar *calendar;
calendar_view_mode view_mode;
int padding; // padding
private:
std::vector<engine_child> cells_table;
};
#endif

View File

@ -1,23 +1,72 @@
#include <iostream> #include <iostream>
#include <string.h> #include <string.h>
#include <signal.h>
#include <ncurses.h> #include <ncurses.h>
#include <curses.h> #include "calendar/calendar.hpp"
#include "calendar.hpp" #include "engine/engine.hpp"
#define MIN_WIDTH 100 Engine engine = Engine(10, MONTH_VIEW);
#define MIN_HEIGHT 40 WINDOW *main_win;
using namespace std; void sig_winch(int sig) {
if (isendwin()) return;
endwin();
wclear(main_win);
wrefresh(main_win);
wresize(main_win, LINES, COLS);
// redraw the TUI after the resize signal
engine.draw(main_win);
flushinp();
}
void signals() {
signal(SIGWINCH, sig_winch);
}
int main() { int main() {
initscr(); initscr();
refresh();
noecho();
int active_cell = 0; main_win = newwin(LINES, COLS, 0, 0);
// handle required signals
signals();
for (;;) { for (;;) {
wclear(main_win);
wrefresh(main_win);
engine.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;
}
refresh(); 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;
}
} }
endwin();
return 0; return 0;
} }