Resizing: re-draw at terminal resize
This commit is contained in:
parent
7296559c16
commit
db58c196a9
|
@ -22,10 +22,7 @@ project(vis
|
|||
|
||||
set(CMAKE_EXPORT_COMPILE_COMMANDS 1)
|
||||
|
||||
add_executable(vis
|
||||
src/main.cpp
|
||||
src/calendar.cpp
|
||||
)
|
||||
add_subdirectory(src)
|
||||
|
||||
find_library(NCURSES_LIB NAMES ncurses)
|
||||
target_link_libraries(vis ncurses)
|
||||
|
|
|
@ -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.
|
||||
|
||||
- [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/)
|
||||
- [JBWYATT - Ncurses Programming Guide](https://jbwyatt.com/ncurses.html)
|
||||
|
|
7
src/CMakeLists.txt
Normal file
7
src/CMakeLists.txt
Normal 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)
|
1
src/calendar/CMakeLists.txt
Normal file
1
src/calendar/CMakeLists.txt
Normal file
|
@ -0,0 +1 @@
|
|||
add_library(calendar SHARED calendar.cpp)
|
|
@ -41,11 +41,15 @@ void Calendar::set_year(int year) {
|
|||
this->info.current_day = this->info.current_month_days;
|
||||
}
|
||||
|
||||
calendar_information Calendar::get_info() {
|
||||
return this->info;
|
||||
}
|
||||
|
||||
Calendar::Calendar(calendar_information *preinfo) {
|
||||
this->info = *preinfo;
|
||||
if (preinfo != nullptr) {
|
||||
this->info = *preinfo;
|
||||
|
||||
if (preinfo != nullptr)
|
||||
return;
|
||||
}
|
||||
|
||||
this->info = calendar_information();
|
||||
}
|
|
@ -1,3 +1,6 @@
|
|||
#ifndef CALENDAR_HPP
|
||||
#define CALENDAR_HPP
|
||||
|
||||
struct calendar_information {
|
||||
int current_day;
|
||||
int current_month;
|
||||
|
@ -16,6 +19,8 @@ class Calendar {
|
|||
calendar_information get_info();
|
||||
|
||||
Calendar(calendar_information *preinfo = nullptr);
|
||||
private:
|
||||
|
||||
private:
|
||||
calendar_information info;
|
||||
};
|
||||
#endif
|
1
src/engine/CMakeLists.txt
Normal file
1
src/engine/CMakeLists.txt
Normal file
|
@ -0,0 +1 @@
|
|||
add_library(engine SHARED engine.cpp)
|
42
src/engine/engine.cpp
Normal file
42
src/engine/engine.cpp
Normal 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
37
src/engine/engine.hpp
Normal 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
|
65
src/main.cpp
65
src/main.cpp
|
@ -1,23 +1,72 @@
|
|||
#include <iostream>
|
||||
#include <string.h>
|
||||
#include <signal.h>
|
||||
#include <ncurses.h>
|
||||
#include <curses.h>
|
||||
#include "calendar.hpp"
|
||||
#include "calendar/calendar.hpp"
|
||||
#include "engine/engine.hpp"
|
||||
|
||||
#define MIN_WIDTH 100
|
||||
#define MIN_HEIGHT 40
|
||||
Engine engine = Engine(10, MONTH_VIEW);
|
||||
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() {
|
||||
initscr();
|
||||
initscr();
|
||||
refresh();
|
||||
noecho();
|
||||
|
||||
int active_cell = 0;
|
||||
main_win = newwin(LINES, COLS, 0, 0);
|
||||
|
||||
// handle required signals
|
||||
signals();
|
||||
|
||||
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;
|
||||
}
|
||||
|
|
Loading…
Reference in New Issue
Block a user