Advertisement
Guest User

cpuspark

a guest
May 8th, 2014
281
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 1.48 KB | None | 0 0
  1. #if 0
  2. g++ -std=c++11 -o bin/cpuspark $0
  3. exit
  4. #endif
  5.  
  6. // #include <unicode/utf8.h>
  7. #include <sys/sysinfo.h>
  8. #include <sys/stat.h>
  9. #include <unistd.h>
  10. #include <cstring>
  11. #include <thread>
  12. #include <vector>
  13. #include <fstream>
  14. #include <iostream>
  15. #include <algorithm>
  16.  
  17. const std::vector<const char*> bars = { "▁", "▂", "▃", "▄", "▅", "▆", "▇", "█" };
  18.  
  19. const std::size_t HISTORY_LEN = 6;
  20.  
  21. int main()
  22. {
  23.     auto term = getenv("TERM");
  24.     if ( term && !strcmp(term, "linux") && (!getenv("DISPLAY") || !getenv("WINDOWID")) )
  25.         return 0;
  26.     const auto file = std::string("/tmp/") + std::to_string(getppid()) + ".cpuspark";
  27.     std::ifstream i{ file };
  28.     std::vector<std::string> history;
  29.     std::string l;
  30.     while ( std::getline(i, l).good() )
  31.     {
  32.         std::cout << l;
  33.         history.push_back(l);
  34.     }
  35.     i.close();
  36.     double load;
  37.     if ( getloadavg(&load, 1) == 1 )
  38.     {
  39.         std::size_t index = bars.size() * load / ( std::thread::hardware_concurrency() + 1 );
  40.         auto bar = index < bars.size() ? bars[index] : bars[bars.size()-1];
  41.         std::cout << bar;
  42.         struct stat st;
  43.         if ( stat(file.c_str(), &st) || time(NULL) - st.st_mtime >= 30 )
  44.         {
  45.             while ( history.size() > HISTORY_LEN )
  46.                 history.erase( history.begin() );
  47.             std::ofstream o{ file };
  48.             for ( auto&& h : history )
  49.                 o << h << '\n';
  50.             o << bar << '\n';
  51.         }
  52.     }
  53. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement