Code refactor

This commit is contained in:
admin committed 2026-05-29 21:10:06 +02:00
1 parent 673b58caec
commit c69c420781
7 files changed
+315 -289

No files matched your search

+11 -8
View File
@@ -1,20 +1,23 @@
#ifndef WINDOWS_H #ifndef WINDOWSBACK_H
#define WINDOWS_H #define WINDOWSBACK_H
#include <stdio.h> #include <stdio.h>
#include <stdlib.h> #include <stdlib.h>
#include <string.h> #include <string.h>
#include <ncurses.h> #include <ncurses.h>
int runMainMenu(); WINDOW *create_newwin(int height, int width, int starty, int startx);
void runOptionsMenu(); void destroy_win(WINDOW *local_win);
void wwrap_print(WINDOW *win, int start_y, int start_x, const char *str);
int uiToggleSwitch(WINDOW *win, int y, int x, int state);
void uiTextInput(WINDOW *win, int y, int x, char *input, int max_len);
int uiSelectMenu(WINDOW *win, int start_y, int start_x, char **options, int n_options);
void print_menu(WINDOW *menu_win, int y, int x, int highlight);
void warningf(const char *fmt, ...); void warningf(const char *fmt, ...);
void warning(char *string); void warning(char *string);
void statusf(const char *fmt, ...); void statusf(const char *fmt, ...);
void status(char *msg); void status(char *msg);
WINDOW *create_newwin(int height, int width, int starty, int startx);
void destroy_win(WINDOW *local_win);
void print_menu(WINDOW *menu_win, int y, int x, int highlight);
void wwrap_print(WINDOW *win, int start_y, int start_x, const char *str);
#endif #endif
+13
View File
@@ -0,0 +1,13 @@
#ifndef WINDOWSDECLARATION_H
#define WINDOWSDECLARATION_H
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ncurses.h>
int runMainMenu();
void runOptionsMenu();
void example_window();
#endif
+6 -3
View File
@@ -19,8 +19,9 @@
#include <string.h> #include <string.h>
#include <ncurses.h> #include <ncurses.h>
#include <windows.h> #include "../include/windowsDeclaration.h"
#include <logic.h> #include "../include/windowsBack.h"
#include "../include/logic.h"
void printLogoBG(); void printLogoBG();
@@ -49,6 +50,7 @@ int main(){
int running = 0; int running = 0;
while(running != 6){ while(running != 6){
printLogoBG();
refresh(); refresh();
running = runMainMenu(); running = runMainMenu();
switch(running) { switch(running) {
@@ -60,7 +62,8 @@ int main(){
case 3: case 3:
break; break;
case 4: case 4:
break; example_window();
break;
case 5: case 5:
runOptionsMenu(); runOptionsMenu();
break; break;
-2
View File
@@ -7,11 +7,9 @@
#include "../include/globals.h" #include "../include/globals.h"
int main() { int main() {
/* Working
printf("%d", existsFile("qwestoeuntest")); printf("%d", existsFile("qwestoeuntest"));
createFile("qwestoeuntest"); createFile("qwestoeuntest");
printf("%d", existsFile("qwestoeuntest")); printf("%d", existsFile("qwestoeuntest"));
*/
return 0; return 0;
} }
-276
View File
@@ -1,276 +0,0 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ncurses.h>
#include "../include/windows.h"
#include "../include/logic.h"
#include "../include/globals.h"
#define MAX_LEN 64
char *mainMenu[] = {
"Segna vendita",
"Vedi vendite",
"Vedi prodotti",
"Gestisci prodotti",
"Opzioni",
"Exit"
};
int n_mainMenu = sizeof(mainMenu) / sizeof(char *);
int runMainMenu(){
WINDOW *mainWin;
int startx, starty, width, height;
height = LINES / 2;
width = COLS / 2;
starty = 0;
startx = 0;
mainWin = create_newwin(height, width, starty, startx);
int highlight = 1;
int choice = 0;
int c;
keypad(mainWin, TRUE);
wwrap_print(mainWin, 1, 2, "Usa le FRECCETTE ed INVIO per scegliere un'opzione,\nin alternativa ci sono i movement di vim e SPAZIO.");
refresh();
print_menu(mainWin, height / 2 - 3, width / 2 - 9, highlight);
while(1)
{ c = wgetch(mainWin);
switch(c)
{ case KEY_UP:
case KEY_LEFT:
case 'h':
case 'k':
if(highlight == 1)
highlight = n_mainMenu;
else
--highlight;
break;
case KEY_DOWN:
case KEY_RIGHT:
case 'l':
case 'j':
if(highlight == n_mainMenu)
highlight = 1;
else
++highlight;
break;
case 10:
case 32:
choice = highlight;
break;
default:
statusf("Il tasto '%c' non e' un tasto valido", c);
refresh();
break;
}
print_menu(mainWin, height / 2 - 3, width / 2 - 9, highlight);
if(choice != 0) /* User did a choice come out of the infinite loop */
break;
}
statusf("Avvio opzione '%s'", mainMenu[choice - 1]);
clrtoeol();
refresh();
destroy_win(mainWin);
return choice;
}
void runOptionsMenu(){
WINDOW *optionsWin;
int startx, starty, width, height;
height = LINES / 2;
width = COLS / 2;
starty = 0;
startx = COLS / 2;
optionsWin = create_newwin(height, width, starty, startx);
char input[MAX_LEN + 1];
strcpy(input, productPricesFileLocation);
int ch, pos = strlen(input);
mvwprintw(optionsWin, 1, 2, "Posizione file prodotti:");
wattron(optionsWin, COLOR_PAIR(2));
for (int i = 0; i < MAX_LEN; i++) {
mvwaddch(optionsWin, 2, 2 + i, ' ');
}
mvwprintw(optionsWin, 2, 2, "%s", input);
wmove(optionsWin, 2, 2 + strlen(input));
wrefresh(optionsWin);
pos = strlen(input);
// Input nella finestra
curs_set(1);
while ((ch = wgetch(optionsWin)) != '\n') {
if (ch == KEY_BACKSPACE || ch == 127 || ch == '\b') {
if (pos > 0) {
pos--;
input[pos] = '\0';
mvwaddch(optionsWin, 2, 2 + pos, ' ');
wmove(optionsWin, 2, 2 + pos);
}
} else if (pos < MAX_LEN && ch >= 32 && ch <= 126) {
input[pos++] = ch;
input[pos] = '\0';
mvwaddch(optionsWin, 2, 2 + pos - 1, ch);
}
wrefresh(optionsWin);
}
curs_set(0);
wattroff(optionsWin, COLOR_PAIR(2));
if (existsFile(input)) {
statusf("Il file '%s' esiste e verra' utilizzato al posto di '%s'", input, productPricesFileLocation);
strcpy(productPricesFileLocation, input);
}
else {
warningf("Il file '%s' non esiste, utilizzo il vecchio file '%s'", input, productPricesFileLocation);
}
werase(optionsWin);
destroy_win(optionsWin);
}
void warningf(const char *fmt, ...) {
char buf[256];
va_list args;
va_start(args, fmt);
vsnprintf(buf, sizeof(buf), fmt, args);
va_end(args);
warning(buf);
}
void warning(char *string){
WINDOW *warningWin;
int startx, starty, width, height;
height = LINES / 2 - 2;
width = COLS / 2;
starty = LINES / 2;
startx = 0;
warningWin = create_newwin(height, width, starty, startx);
wbkgd(warningWin, COLOR_PAIR(1));
mvwprintw(warningWin, 2, 2, "ATTENZIONE!!! E' avvenuto un errore! ");
wwrap_print(warningWin, 4, 3, string);
mvwprintw(warningWin, height - 1, 3, " Premi un tasto per continuare . . . ");
wrefresh(warningWin);
getch();
werase(warningWin);
destroy_win(warningWin);
}
void statusf(const char *fmt, ...) {
char buf[256];
va_list args;
va_start(args, fmt);
vsnprintf(buf, sizeof(buf), fmt, args);
va_end(args);
status(buf);
}
void status(char *msg) {
attron(COLOR_PAIR(3));
mvprintw(LINES -2, 0, "%-*s", COLS, msg);
attroff(COLOR_PAIR(3));
refresh();
}
WINDOW *create_newwin(int height, int width, int starty, int startx)
{ WINDOW *local_win;
local_win = newwin(height, width, starty, startx);
box(local_win, 0 , 0);
wrefresh(local_win);
return local_win;
}
void destroy_win(WINDOW *local_win)
{
wborder(local_win, ':', ':', '.', '.', '.', '.', '.', '.');
wrefresh(local_win);
delwin(local_win);
}
void print_menu(WINDOW *menu_win, int y, int x, int highlight)
{
int i;
box(menu_win, 0, 0);
for(i = 0; i < n_mainMenu; ++i)
{ if(highlight == i + 1) /* High light the present choice */
{ wattron(menu_win, A_REVERSE);
mvwprintw(menu_win, y, x, "%s", mainMenu[i]);
wattroff(menu_win, A_REVERSE);
}
else
mvwprintw(menu_win, y, x, "%s", mainMenu[i]);
++y;
}
wrefresh(menu_win);
}
void wwrap_print(WINDOW *win, int start_y, int start_x, const char *str) {
int max_width = getmaxx(win) - (start_y + 1);
int y = start_y;
int x = start_x;
int line_length = 0;
int word_length = 0;
const char *word_start = str;
while (*str) {
// Trova la prossima parola
if (*str != ' ' && *str != '\n' && *str != '\t') {
str++;
continue;
}
// Calcola lunghezza parola corrente
word_length = str - word_start;
// Gestione wrap
if (line_length + word_length > max_width) {
y++;
x = start_x;
line_length = 0;
}
// Stampa parola
for (int i = 0; i < word_length; i++) {
mvwaddch(win, y, x++, word_start[i]);
}
line_length += word_length;
// Gestione spazi
if (*str == ' ' || *str == '\t') {
if (line_length < max_width) {
mvwaddch(win, y, x++, ' ');
line_length++;
} else {
y++;
x = start_x;
line_length = 0;
}
} else if (*str == '\n') {
y++;
x = start_x;
line_length = 0;
}
word_start = ++str;
}
// Stampa ultima parola
word_length = strlen(word_start);
if (line_length + word_length > max_width) {
y++;
x = start_x;
}
mvwprintw(win, y, x, "%s", word_start);
}
+196
View File
@@ -0,0 +1,196 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ncurses.h>
#include "../include/windowsBack.h"
#include "../include/logic.h"
#include "../include/globals.h"
#define MAX_LEN 64
void warningf(const char *fmt, ...) {
char buf[256];
va_list args;
va_start(args, fmt);
vsnprintf(buf, sizeof(buf), fmt, args);
va_end(args);
warning(buf);
}
void warning(char *string){
WINDOW *warningWin;
int startx, starty, width, height;
height = LINES / 2 - 2;
width = COLS / 2;
starty = LINES / 2;
startx = 0;
warningWin = create_newwin(height, width, starty, startx);
wbkgd(warningWin, COLOR_PAIR(1));
mvwprintw(warningWin, 2, 2, "ATTENZIONE!!! E' avvenuto un errore! ");
wwrap_print(warningWin, 4, 3, string);
mvwprintw(warningWin, height - 1, 3, " Premi un tasto per continuare . . . ");
wrefresh(warningWin);
getch();
werase(warningWin);
destroy_win(warningWin);
}
void statusf(const char *fmt, ...) {
char buf[256];
va_list args;
va_start(args, fmt);
vsnprintf(buf, sizeof(buf), fmt, args);
va_end(args);
status(buf);
}
void status(char *msg) {
attron(COLOR_PAIR(3));
mvprintw(LINES -2, 0, "%-*s", COLS, msg);
attroff(COLOR_PAIR(3));
refresh();
}
WINDOW *create_newwin(int height, int width, int starty, int startx)
{ WINDOW *local_win;
local_win = newwin(height, width, starty, startx);
box(local_win, 0 , 0);
wrefresh(local_win);
return local_win;
}
void destroy_win(WINDOW *local_win)
{
//wborder(local_win, ':', ':', '.', '.', '.', '.', '.', '.');
wrefresh(local_win);
delwin(local_win);
}
void wwrap_print(WINDOW *win, int start_y, int start_x, const char *str) {
int max_width = getmaxx(win) - (start_y + 1);
int y = start_y;
int x = start_x;
int line_length = 0;
int word_length = 0;
const char *word_start = str;
while (*str) {
// Trova la prossima parola
if (*str != ' ' && *str != '\n' && *str != '\t') {
str++;
continue;
}
// Calcola lunghezza parola corrente
word_length = str - word_start;
// Gestione wrap
if (line_length + word_length > max_width) {
y++;
x = start_x;
line_length = 0;
}
// Stampa parola
for (int i = 0; i < word_length; i++) {
mvwaddch(win, y, x++, word_start[i]);
}
line_length += word_length;
// Gestione spazi
if (*str == ' ' || *str == '\t') {
if (line_length < max_width) {
mvwaddch(win, y, x++, ' ');
line_length++;
} else {
y++;
x = start_x;
line_length = 0;
}
} else if (*str == '\n') {
y++;
x = start_x;
line_length = 0;
}
word_start = ++str;
}
// Stampa ultima parola
word_length = strlen(word_start);
if (line_length + word_length > max_width) {
y++;
x = start_x;
}
mvwprintw(win, y, x, "%s", word_start);
}
int uiSelectMenu(WINDOW *win, int start_y, int start_x, char **options, int n_options) {
int highlight = 1, choice = 0, c;
keypad(win, TRUE);
while(1) {
for(int i = 0; i < n_options; ++i) {
if(highlight == i + 1) wattron(win, A_REVERSE);
mvwprintw(win, start_y + i, start_x, "%s", options[i]);
if(highlight == i + 1) wattroff(win, A_REVERSE);
}
wrefresh(win);
c = wgetch(win);
switch(c) {
case KEY_UP: case 'k': highlight = (highlight == 1) ? n_options : highlight - 1; break;
case KEY_DOWN: case 'j': highlight = (highlight == n_options) ? 1 : highlight + 1; break;
case 10: case 32: choice = highlight; break;
}
if(choice != 0) break;
}
return choice;
}
void uiTextInput(WINDOW *win, int y, int x, char *input, int max_len) {
int ch, pos = strlen(input);
wattron(win, COLOR_PAIR(2));
curs_set(1);
for(int i = 0; i < max_len; i++) mvwaddch(win, y, x + i, ' ');
mvwprintw(win, y, x, "%s", input);
wmove(win, y, x + pos);
wrefresh(win);
while ((ch = wgetch(win)) != '\n') {
if (ch == KEY_BACKSPACE || ch == 127 || ch == '\b') {
if (pos > 0) {
pos--;
input[pos] = '\0';
mvwaddch(win, y, x + pos, ' ');
wmove(win, y, x + pos);
}
} else if (pos < max_len && ch >= 32 && ch <= 126) {
input[pos++] = ch;
input[pos] = '\0';
mvwaddch(win, y, x + pos - 1, ch);
}
wrefresh(win);
}
curs_set(0);
wattroff(win, COLOR_PAIR(2));
}
int uiToggleSwitch(WINDOW *win, int y, int x, int state) {
int c;
keypad(win, TRUE);
while(1) {
wattron(win, A_REVERSE);
mvwprintw(win, y, x, state ? "[ ON ] " : "[ OFF ]");
wattroff(win, A_REVERSE);
wrefresh(win);
c = wgetch(win);
if (c == 10 || c == 32) break;
if (c == KEY_LEFT || c == KEY_RIGHT || c == 'h' || c == 'j' || c == 'k' || c == 'l') state = !state;
}
mvwprintw(win, y, x, state ? "[ ON ] " : "[ OFF ]");
wrefresh(win);
return state;
}
+89
View File
@@ -0,0 +1,89 @@
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <ncurses.h>
#include "../include/windowsDeclaration.h"
#include "../include/windowsBack.h"
#include "../include/logic.h"
#include "../include/globals.h"
#define MAX_LEN 64
void example_window() {
WINDOW *win;
int height = LINES / 2 -2;
int width = COLS / 2;
int starty = LINES / 2;
int startx = COLS / 2;
win = create_newwin(height, width, starty, startx);
mvwprintw(win, 1, 2, "Esempio Interfaccia");
char *opts[] = {"Opzione 1", "Opzione 2", "Opzione 3"};
mvwprintw(win, 3, 2, "Scegli dal menu:");
int menu_res = uiSelectMenu(win, 4, 4, opts, 3);
char txt_buf[64] = "Testo base";
mvwprintw(win, 9, 2, "Inserisci testo:");
uiTextInput(win, 9, 19, txt_buf, 30);
mvwprintw(win, 11, 2, "Attiva opzione:");
int switch_res = uiToggleSwitch(win, 11, 18, 0);
mvwprintw(win, 13, 2, "Scelto: M=%d, T=%s, S=%d", menu_res, txt_buf, switch_res);
wrefresh(win);
wgetch(win);
werase(win);
destroy_win(win);
}
int runMainMenu(){
char *mainMenu[] = {
"Segna vendita",
"Vedi vendite",
"Vedi prodotti",
"Gestisci prodotti",
"Opzioni",
"Exit"
};
int n_mainMenu = sizeof(mainMenu) / sizeof(char *);
WINDOW *mainWin;
int height = LINES / 2;
int width = COLS / 2;
int starty = 0;
int startx = 0;
mainWin = create_newwin(height, width, starty, startx);
wwrap_print(mainWin, 1, 2, "Usa le FRECCETTE ed INVIO per scegliere un'opzione,\nin alternativa ci sono i movement di vim e SPAZIO.");
refresh();
int choice = uiSelectMenu(mainWin, height / 2 - 3, width / 2 - 9, mainMenu, n_mainMenu);
statusf("Avvio opzione '%s'", mainMenu[choice - 1]);
clrtoeol();
refresh();
destroy_win(mainWin);
return choice;
}
void runOptionsMenu(){
WINDOW *optionsWin;
optionsWin = create_newwin(LINES/2, COLS/2, 0, COLS/2);
char input[MAX_LEN + 1];
strcpy(input, productPricesFileLocation);
mvwprintw(optionsWin, 1, 2, "Posizione file prodotti:");
uiTextInput(optionsWin, 2, 2, input, MAX_LEN);
if (existsFile(input)) {
statusf("Il file '%s' esiste e verra' utilizzato al posto di '%s'", input, productPricesFileLocation);
strcpy(productPricesFileLocation, input);
} else {
warningf("Il file '%s' non esiste, utilizzo il vecchio file '%s'", input, productPricesFileLocation);
}
werase(optionsWin);
destroy_win(optionsWin);
}