diff --git a/README.md b/README.md index fad4f32..4ee451c 100644 --- a/README.md +++ b/README.md @@ -1,3 +1,20 @@ # Watch-On-Windows -Since "watch" command on windows requires Cygwin and some people like me do not want to install such an package, i wrote a naive one. \ No newline at end of file +Watch command is frequently used on linux while there is no such things on windows. To use watch command on windows, one might need to install Cygwin or something similar, which is not acceptable for some users like me. To use this command on windows, I wrote a naive watch command. + +## How to compile this +Just run the following command. (GCC is required, or you can us visual studio) +```bat +g++ -Wall -O2 -static watch.cpp -o watch.exe +``` + +## How to use +Only the basic functionality is implenmented. The command line arguments are as follows: +```bat +watch +``` +Here are some examples: +```bat +watch 0.1 nvidia-smi +watch 1 nvidia-smi -q -d CLOCK +``` \ No newline at end of file diff --git a/watch.cpp b/watch.cpp new file mode 100644 index 0000000..1ede6d1 --- /dev/null +++ b/watch.cpp @@ -0,0 +1,176 @@ +#include +#include +#include +#include +#include +#include +#include +#include + +using hclock = std::chrono::high_resolution_clock; +using duration = std::chrono::duration; + +int execmd(char* cmd,char* result) { + char buffer[512]; + FILE* pipe = _popen(cmd, "r"); + if (!pipe) + return 0; + + while(!feof(pipe)) { + if(fgets(buffer, 128, pipe)){ + strcat(result,buffer); + } + } + _pclose(pipe); + return 0; +} + +std::string GetCommandOutput(std::string command) +{ + std::string result(""); + char buffer[1024]; + FILE* pipe = _popen(command.c_str(), "r"); + if (!pipe) return 0; + while (!feof(pipe)) + { + if (fgets(buffer, 1024, pipe)) { + result += buffer; + } + } + return result; +} + +std::vector split(std::string& str, std::string delemeter) +{ + std::vector strs; + size_t delsize = delemeter.size(); + while (str.size() > 0) { + size_t delpos = str.find(delemeter); + if (delpos == std::string::npos) { + strs.push_back(str); + } else { + strs.push_back(str.substr(0, delpos)); + str = str.substr(delpos + delsize); + } + } + return strs; +} + +struct ConsoleSize { + size_t w = 0; + size_t h = 0; +}; + +ConsoleSize get_console_size(std::string& content) +{ + std::vector strs = split(content, "\n"); + ConsoleSize sz; + sz.h = strs.size(); + for (size_t i = 0; i < strs.size(); i++) { + if (strs[i].size() > sz.w) { + sz.w = strs[i].size(); + } + } + return sz; +} + +void Trimmedoutput(std::string& content, size_t max_w, size_t max_h) + +{ + std::vector strs = split(content, "\n"); + size_t lines = max_h > strs.size()?strs.size():max_h; + for (size_t i = 0; i < lines; i++) { + std::string str = strs[i]; + if (str.size() > max_w) { + str = str.substr(0, max_w); + } + else if (str.size() < max_w) { + str += std::string(max_w - str.size(), ' '); + } + std::cout<(t2 - t1).count(); + double remeaning = interval - elasped; + if (remeaning > 0) { + Sleep(int(remeaning * 1000)); + } + } + + return 0; +} \ No newline at end of file