Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #if 0
- g++ -std=c++11 -o bin/cpuspark $0
- exit
- #endif
- // #include <unicode/utf8.h>
- #include <sys/sysinfo.h>
- #include <sys/stat.h>
- #include <unistd.h>
- #include <cstring>
- #include <thread>
- #include <vector>
- #include <fstream>
- #include <iostream>
- #include <algorithm>
- const std::vector<const char*> bars = { "▁", "▂", "▃", "▄", "▅", "▆", "▇", "█" };
- const std::size_t HISTORY_LEN = 6;
- int main()
- {
- auto term = getenv("TERM");
- if ( term && !strcmp(term, "linux") && (!getenv("DISPLAY") || !getenv("WINDOWID")) )
- return 0;
- const auto file = std::string("/tmp/") + std::to_string(getppid()) + ".cpuspark";
- std::ifstream i{ file };
- std::vector<std::string> history;
- std::string l;
- while ( std::getline(i, l).good() )
- {
- std::cout << l;
- history.push_back(l);
- }
- i.close();
- double load;
- if ( getloadavg(&load, 1) == 1 )
- {
- std::size_t index = bars.size() * load / ( std::thread::hardware_concurrency() + 1 );
- auto bar = index < bars.size() ? bars[index] : bars[bars.size()-1];
- std::cout << bar;
- struct stat st;
- if ( stat(file.c_str(), &st) || time(NULL) - st.st_mtime >= 30 )
- {
- while ( history.size() > HISTORY_LEN )
- history.erase( history.begin() );
- std::ofstream o{ file };
- for ( auto&& h : history )
- o << h << '\n';
- o << bar << '\n';
- }
- }
- }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement