diff --git a/src/console_writer.cpp b/src/console_writer.cpp index 61a8515..fa37b3f 100644 --- a/src/console_writer.cpp +++ b/src/console_writer.cpp @@ -16,10 +16,13 @@ * along with gmonitor. If not, see . */ +#include #include #include #include #include "console_writer.h" +#include +#include using namespace std; @@ -88,7 +91,7 @@ string ConsoleWriter::inBlue(const string &text) } //return the correct colour for a value (0 - 100) -Color ConsoleWriter::getColorByValue(int value) +Color ConsoleWriter::getColorByValue(unsigned int value) { if(value < 25 - resolution){ return BLUE;} else @@ -183,6 +186,8 @@ void ConsoleWriter::updateTerminalDimensions(int colms, int rows) void ConsoleWriter::clearConsole() { cout << "\x1B[2J\x1B[H"; + // On the first run, the dimensions are wrong, so let's check them + refreshTerminalDimensions(); } //print gpu identifiers @@ -190,9 +195,27 @@ void ConsoleWriter::printGpuIdentifier(const string &gpuId, const string &gpuName, int totalAvailableMemory) { - cout << "\nID: " << gpuId - << ", Name: " << gpuName - << " ("<< totalAvailableMemory << " MB)"; + char outline[BUFFER_LENGTH + 1]; + size_t len; + chrono::system_clock::time_point today = chrono::system_clock::now(); + time_t tt = chrono::system_clock::to_time_t(today); + + cout << endl; + len = snprintf(outline, BUFFER_LENGTH, "ID: %s", gpuId.c_str()); + if ((len < terminalColms) && (len < BUFFER_LENGTH)) { + len += snprintf(&outline[len], BUFFER_LENGTH - len, ", Name: %s", + gpuName.c_str()); + if ((len < terminalColms) && (len < BUFFER_LENGTH)) { + len += snprintf(&outline[len], BUFFER_LENGTH - len, " (%d MB)", + totalAvailableMemory); + if ((len < terminalColms) && (len < BUFFER_LENGTH)) { + len += snprintf(&outline[len], BUFFER_LENGTH - len, "%*s", + terminalColms - len, ctime(&tt)); + } + } + } + outline[len] = 0; // make sure it's terminated + cout << outline; } //print the current state of the gpu @@ -229,7 +252,7 @@ void ConsoleWriter::printCurrentGpuState(const GpuStates &state) //print one state, value is represented by a list //of '|', 10 -> |||||||||| (for resolution = 1) -string ConsoleWriter::printOneStateInOneLine(States state, int value) +string ConsoleWriter::printOneStateInOneLine(States state, unsigned int value) { string preLabel, postLabel; @@ -237,7 +260,7 @@ string ConsoleWriter::printOneStateInOneLine(States state, int value) ostringstream oss; - int character = 0; + unsigned int character = 0; while(character <= (terminalColms ) * resolution) { diff --git a/src/console_writer.h b/src/console_writer.h index e3e7942..ff99295 100644 --- a/src/console_writer.h +++ b/src/console_writer.h @@ -25,7 +25,7 @@ using namespace std; const int DEFAULT_ROW_N = 100; //default terminal window vertical size -const int DEFAULT_COL_N = 100; //default terminal window hotizontal size +const int DEFAULT_COL_N = 100; //default terminal window horizontal size const int DEFAULT_RES = 1; //default resolution, '|' represents one % / c /* @@ -34,10 +34,10 @@ const int DEFAULT_RES = 1; //default resolution, '|' represents one % / c class ConsoleWriter { private: - int terminalRows; //total rows of the terminal - int terminalColms; //total columns of the terminal - int resolution; //how many unites a column represents - int combinedBarSize; + unsigned int terminalRows; //total rows of the terminal + unsigned int terminalColms; //total columns of the terminal + unsigned int resolution; //how many units a column represents + unsigned int combinedBarSize; //return a coloured text string colourText(Color color, const string &text); @@ -48,7 +48,7 @@ class ConsoleWriter string inLightBlue(const string &text); //return the correct color for a value (0 - 100) - Color getColorByValue(int value); + Color getColorByValue(unsigned int value); //return the correct color for a state Color getColorByState(States toPrint); @@ -75,7 +75,7 @@ class ConsoleWriter //print one state, value is represented by a list //of '|', 10 -> |||||||||| (for resolution = 1) string printOneStateInOneLine(States state, - int value); + unsigned int value); //print one state, value is represented by a list //of '|', resolution depends on the line size @@ -98,7 +98,7 @@ class ConsoleWriter : terminalRows(DEFAULT_ROW_N), terminalColms(DEFAULT_COL_N), resolution(DEFAULT_RES), - combinedBarSize(0) {}; + combinedBarSize(0) { }; //clear the terminal void clearConsole(); diff --git a/src/constants.h b/src/constants.h index 2ca1eb7..efc6b4e 100644 --- a/src/constants.h +++ b/src/constants.h @@ -69,7 +69,8 @@ enum DisplayMode // for each gpu) const DisplayMode DEFAULT_DISPLAY_MODE = CURRENT_NEXT_TO_HISTORY; - const int DEFAULT_REFRESH_RATE = 2; //default refresh rate, 2 seconds +const int BUFFER_LENGTH = 512; + #endif diff --git a/src/stats_reader.cpp b/src/stats_reader.cpp index 90658bb..d9e0b7a 100644 --- a/src/stats_reader.cpp +++ b/src/stats_reader.cpp @@ -51,7 +51,7 @@ void StatsReader::getGpuStates(GpuStates *gpuStates, bool StatsReader::getDoubleFromSystemCall(string &command, std::vector *values) { FILE *in; - char buff[512]; + char buff[BUFFER_LENGTH]; command = checkIfSshCommand(command); @@ -75,7 +75,7 @@ bool StatsReader::getDoubleFromSystemCall(string &command, std::vector * bool StatsReader::getGpuList(vector *gpuList) { FILE *in; - char buff[512]; + char buff[BUFFER_LENGTH]; string command;