You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 
 
 
 
 

112 lines
3.0 KiB

  1. #include <apt-pkg/iprogress.h>
  2. #include <apt-pkg/strutl.h>
  3. #include <termios.h>
  4. #include <sys/ioctl.h>
  5. namespace APT {
  6. namespace Progress {
  7. static void SetupTerminalScrollArea(int nr_rows)
  8. {
  9. // scroll down a bit to avoid visual glitch when the screen
  10. // area shrinks by one row
  11. std::cout << "\n";
  12. // save cursor
  13. std::cout << "\033[s";
  14. // set scroll region (this will place the cursor in the top left)
  15. std::cout << "\033[1;" << nr_rows - 1 << "r";
  16. // restore cursor but ensure its inside the scrolling area
  17. std::cout << "\033[u";
  18. static const char *move_cursor_up = "\033[1A";
  19. std::cout << move_cursor_up;
  20. std::flush(std::cout);
  21. }
  22. PackageManagerFancy::PackageManagerFancy()
  23. : nr_terminal_rows(-1)
  24. {
  25. struct winsize win;
  26. if(ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&win) == 0)
  27. {
  28. nr_terminal_rows = win.ws_row;
  29. }
  30. }
  31. void PackageManagerFancy::Started()
  32. {
  33. SetupTerminalScrollArea(nr_terminal_rows);
  34. }
  35. void PackageManagerFancy::Finished()
  36. {
  37. SetupTerminalScrollArea(nr_terminal_rows + 1);
  38. // override the progress line (sledgehammer)
  39. static const char* clear_screen_below_cursor = "\033[J";
  40. std::cout << clear_screen_below_cursor;
  41. }
  42. void PackageManagerFancy::StatusChanged(std::string PackageName,
  43. unsigned int StepsDone,
  44. unsigned int TotalSteps)
  45. {
  46. int reporting_steps = _config->FindI("DpkgPM::Reporting-Steps", 1);
  47. float percentage = StepsDone/(float)TotalSteps * 100.0;
  48. if(percentage < (last_reported_progress + reporting_steps))
  49. return;
  50. std::string progress_str;
  51. strprintf(progress_str, "Progress: [%3i%%]", (int)percentage);
  52. int row = nr_terminal_rows;
  53. static string save_cursor = "\033[s";
  54. static string restore_cursor = "\033[u";
  55. static string set_bg_color = "\033[42m"; // green
  56. static string set_fg_color = "\033[30m"; // black
  57. static string restore_bg = "\033[49m";
  58. static string restore_fg = "\033[39m";
  59. std::cout << save_cursor
  60. // move cursor position to last row
  61. << "\033[" << row << ";0f"
  62. << set_bg_color
  63. << set_fg_color
  64. << progress_str
  65. << restore_cursor
  66. << restore_bg
  67. << restore_fg;
  68. std::flush(std::cout);
  69. last_reported_progress = percentage;
  70. }
  71. void PackageManagerText::StatusChanged(std::string PackageName,
  72. unsigned int StepsDone,
  73. unsigned int TotalSteps)
  74. {
  75. int reporting_steps = _config->FindI("DpkgPM::Reporting-Steps", 1);
  76. float percentage = StepsDone/(float)TotalSteps * 100.0;
  77. if(percentage < (last_reported_progress + reporting_steps))
  78. return;
  79. std::string progress_str;
  80. strprintf(progress_str, "Progress: [%3i%%]", (int)percentage);
  81. std::cout << progress_str << "\r\n";
  82. std::flush(std::cout);
  83. last_reported_progress = percentage;
  84. }
  85. }; // namespace progress
  86. }; // namespace apt