So then I says to Mabel, "Mabel," I says. "Mabel, I was just telling Mabel..."


SUBMITTED BY: Guest

DATE: July 15, 2014, 3:30 a.m.

FORMAT: Text only

SIZE: 1.9 kB

HITS: 656

  1. #include <iostream>
  2. #include <string>
  3. #include <chrono>
  4. #include <thread>
  5. #include <cstdlib>
  6. #include <sstream>
  7. enum Color
  8. {
  9. NONE = 0,
  10. BLACK, RED, GREEN,
  11. YELLOW, BLUE, MAGENTA,
  12. CYAN, WHITE
  13. };
  14. static std::string set_color(Color foreground = NONE, Color background = NONE)
  15. {
  16. std::stringstream s;
  17. s << "\033[";
  18. if (!foreground && ! background){
  19. s << "0"; // reset colors if no params
  20. }
  21. if (foreground) {
  22. s << 29 + foreground;
  23. if (background) s << ";";
  24. }
  25. if (background) {
  26. s << 39 + background;
  27. }
  28. s << "m";
  29. return s.str();
  30. }
  31. const int width = 158; // Width of terminal window
  32. const int flipsPerLine = 5; // No. of columns changed per line
  33. const int millisecondsOfSleep = 50; // Delay between lines in millisecond
  34. int main() {
  35. std::cout << set_color(GREEN);
  36. system("color 2");
  37. srand(time_t(NULL));
  38. bool switches[width] = { true };
  39. const std::string garbage = "1234567890/*-+.,./;[]\\=_~`!@#$%^&*()";
  40. const auto glen = garbage.size();
  41. while (true) { // Waiting for Ctrl-C
  42. for (int i = 0; i != width; ++i) {
  43. if (switches[i]) {
  44. std::cout << garbage[rand() % glen];
  45. } else {
  46. std::cout << ' ';
  47. }
  48. }
  49. std::cout << std::endl;
  50. for (int i = 0; i != flipsPerLine; ++i) {
  51. int x = rand() % width;
  52. switches[x] = !switches[x];
  53. // Was switches[x] = (switches[x]) ? false : true; thanks to Adrian Petrescu
  54. // Flipping switches
  55. }
  56. std::this_thread::sleep_for(std::chrono::milliseconds(millisecondsOfSleep));
  57. }
  58. return 0;
  59. }

comments powered by Disqus