From a8bb0008a24086c8c04fbb9b6e1510c8fac51705 Mon Sep 17 00:00:00 2001 From: xad1 Date: Tue, 2 May 2023 18:41:54 +0100 Subject: [PATCH] Small refactor, reducing global variables and redundant includes --- include/global.h | 13 ------------- include/menu.h | 12 +++++++----- src/main.cpp | 1 - src/manage.cpp | 20 +++++++------------- src/menu.cpp | 22 ++++++++++++---------- src/statistics.cpp | 15 ++++++--------- src/utils.cpp | 1 - 7 files changed, 32 insertions(+), 52 deletions(-) delete mode 100644 include/global.h diff --git a/include/global.h b/include/global.h deleted file mode 100644 index feef71f..0000000 --- a/include/global.h +++ /dev/null @@ -1,13 +0,0 @@ -#include -#ifndef GLOBAL_H_ -#define GLOBAL_H_ - -extern String selectedNode; -extern int selectedItem; -extern int selectedLXC; -extern int selectedVM; -extern String selectedDisk; -extern String selectedPool; -// TODO remove later - -#endif \ No newline at end of file diff --git a/include/menu.h b/include/menu.h index f368ee1..2be2786 100644 --- a/include/menu.h +++ b/include/menu.h @@ -1,6 +1,6 @@ #ifndef MENU_H_ #define MENU_H_ -#include + #include #include // Struct for static menu items such as on the main menu. Has the name of the menu item and a function to run when it is selected. @@ -10,10 +10,10 @@ typedef struct { } MenuItem; void listNodes(Node* nodes, const int &numItems); -void listContainers(Container* containers, const int &numItems); -void listVMs(VM* vms, const int &numItems); -void listDisks(Disk* disks, const int &numItems); -void listPools(Pool* pools, const int &numItems); +int listContainers(Container* containers, const int &numItems); +int listVMs(VM* vms, const int &numItems); +String listDisks(Disk* disks, const int &numItems); +String listPools(Pool* pools, const int &numItems); void mainMenu(); void manageContainerMenu(); void manageVMMenu(); @@ -23,4 +23,6 @@ typedef void (*MenuPrintCallback)(void*); extern int selectedItem; extern int selectedPage; +extern String selectedNode; + #endif \ No newline at end of file diff --git a/src/main.cpp b/src/main.cpp index 8c2fdc6..9574a9b 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,5 +1,4 @@ #include -#include #include #include #include diff --git a/src/manage.cpp b/src/manage.cpp index 4adcc1d..3609efb 100644 --- a/src/manage.cpp +++ b/src/manage.cpp @@ -1,7 +1,6 @@ #include #include #include -#include #include #include #include @@ -11,10 +10,10 @@ void vmRestart() { Serial.println("vm restart"); selectedItem = 0; - selectedVM = 0; + int numVMs; VM *vms = getVMInfo(numVMs, selectedNode); - listVMs(vms, numVMs); + int selectedVM = listVMs(vms, numVMs); delete[] vms; if (selectedVM > 0) { @@ -32,10 +31,9 @@ void containerRestart() { Serial.println("lxc restart"); selectedItem = 0; - selectedLXC = 0; int numContainers; Container *containers = getContainerInfo(numContainers, selectedNode); - listContainers(containers, numContainers); + int selectedLXC = listContainers(containers, numContainers); delete[] containers; if (selectedLXC > 0) { @@ -54,10 +52,9 @@ void vmStart() { Serial.println("vm start"); selectedItem = 0; - selectedVM = 0; int numVMs; VM *vms = getVMInfo(numVMs, selectedNode); - listVMs(vms, numVMs); + int selectedVM = listVMs(vms, numVMs); delete[] vms; if (selectedVM > 0) { @@ -78,10 +75,9 @@ void containerStart() { Serial.println("lxc start"); selectedItem = 0; - selectedLXC = 0; int numContainers; Container *containers = getContainerInfo(numContainers, selectedNode); - listContainers(containers, numContainers); + int selectedLXC = listContainers(containers, numContainers); delete[] containers; if (selectedLXC > 0) { @@ -100,10 +96,9 @@ void vmStop() { Serial.println("vm stop"); selectedItem = 0; - selectedVM = 0; int numVMs; VM *vms = getVMInfo(numVMs, selectedNode); - listVMs(vms, numVMs); + int selectedVM = listVMs(vms, numVMs); delete[] vms; if (selectedVM > 0) { @@ -124,10 +119,9 @@ void containerStop() { Serial.println("lxc stop"); selectedItem = 0; - selectedLXC = 0; int numContainers; Container *containers = getContainerInfo(numContainers, selectedNode); - listContainers(containers, numContainers); + int selectedLXC = listContainers(containers, numContainers); delete[] containers; if (selectedLXC > 0) { diff --git a/src/menu.cpp b/src/menu.cpp index 2de0353..cdd5764 100644 --- a/src/menu.cpp +++ b/src/menu.cpp @@ -1,9 +1,7 @@ #include #include #include -#include #include -#include #include // UI constants for the menu system. @@ -366,7 +364,7 @@ void listNodes(Node *nodes, const int &numItems) * @param containers The array of containers to list. * @param numItems The number of containers to list. */ -void listContainers(Container *containers, const int &numItems) +int listContainers(Container *containers, const int &numItems) { selectedItem = 0; void *item; @@ -375,8 +373,9 @@ void listContainers(Container *containers, const int &numItems) if (item != nullptr) { Container *container = static_cast(item); - selectedLXC = (*container).id; + return (*container).id; } + return 0; } /** @@ -391,7 +390,7 @@ void listContainers(Container *containers, const int &numItems) * @param vms The array of VMs to list. * @param numItems The number of VMs to list. */ -void listVMs(VM *vms, const int &numItems) +int listVMs(VM *vms, const int &numItems) { selectedItem = 0; void *item; @@ -399,8 +398,9 @@ void listVMs(VM *vms, const int &numItems) if (item != nullptr) { VM *vm = static_cast(item); - selectedVM = (*vm).id; + return (*vm).id; } + return 0; } /** @@ -415,7 +415,7 @@ void listVMs(VM *vms, const int &numItems) * @param disks The array of disks to list. * @param numItems The number of disks to list. */ -void listDisks(Disk *disks, const int &numItems) +String listDisks(Disk *disks, const int &numItems) { selectedItem = 0; void *item; @@ -424,8 +424,9 @@ void listDisks(Disk *disks, const int &numItems) if (item != nullptr) { Disk *disk = static_cast(item); - selectedDisk = (*disk).devpath; + return (*disk).devpath; } + return ""; } /** @@ -440,7 +441,7 @@ void listDisks(Disk *disks, const int &numItems) * @param pools The array of pools to list. * @param numItems The number of pools to list. */ -void listPools(Pool *pools, const int &numItems) +String listPools(Pool *pools, const int &numItems) { selectedItem = 0; void *item; @@ -448,6 +449,7 @@ void listPools(Pool *pools, const int &numItems) if (item != nullptr) { Pool *pool = static_cast(item); - selectedPool = (*pool).name; + return (*pool).name; } + return ""; } diff --git a/src/statistics.cpp b/src/statistics.cpp index 7324d3a..3b328d9 100644 --- a/src/statistics.cpp +++ b/src/statistics.cpp @@ -1,6 +1,5 @@ #include #include -#include #include #include #include @@ -211,12 +210,11 @@ void containerInfo() Serial.println("container info"); selectedItem = 0; selectedPage = 0; - selectedLXC = 0; int numContainers; Container *containers = getContainerInfo(numContainers, selectedNode); - listContainers(containers, numContainers); + int selectedLXC = listContainers(containers, numContainers); delete[] containers; unsigned long lastUpdate = 0; @@ -251,10 +249,9 @@ void vmInfo() Serial.println("vm info"); selectedItem = 0; selectedPage = 0; - selectedVM = 0; int numVMs; VM *vms = getVMInfo(numVMs, selectedNode); - listVMs(vms, numVMs); + int selectedVM = listVMs(vms, numVMs); delete[] vms; unsigned long lastUpdate = 0; @@ -287,11 +284,11 @@ void diskInfo() Serial.println("disk info"); selectedItem = 0; selectedPage = 0; - selectedDisk = ""; + int numDisks; Disk *disks = getDiskInfo(numDisks, selectedNode); - listDisks(disks, numDisks); + String selectedDisk = listDisks(disks, numDisks); delete[] disks; if (selectedDisk != "") @@ -323,11 +320,11 @@ void poolInfo() Serial.println("pool info"); selectedItem = 0; selectedPage = 0; - selectedPool = ""; + int numPools; Pool *pools = getPoolInfo(numPools, selectedNode); - listPools(pools, numPools); + String selectedPool = listPools(pools, numPools); delete[] pools; if (selectedPool != "") diff --git a/src/utils.cpp b/src/utils.cpp index e2ccf96..3b8abd6 100644 --- a/src/utils.cpp +++ b/src/utils.cpp @@ -2,7 +2,6 @@ #include #include #include -#include /**