iprogress.cc 6.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207
  1. #include <apt-pkg/configuration.h>
  2. #include <apt-pkg/fileutl.h>
  3. #include <apt-pkg/iprogress.h>
  4. #include <apt-pkg/strutl.h>
  5. #include <apti18n.h>
  6. #include <termios.h>
  7. #include <sys/ioctl.h>
  8. #include <sstream>
  9. namespace APT {
  10. namespace Progress {
  11. bool PackageManager::StatusChanged(std::string PackageName,
  12. unsigned int StepsDone,
  13. unsigned int TotalSteps,
  14. std::string HumanReadableAction)
  15. {
  16. int reporting_steps = _config->FindI("DpkgPM::Reporting-Steps", 1);
  17. percentage = StepsDone/(float)TotalSteps * 100.0;
  18. strprintf(progress_str, _("Progress: [%3i%%]"), (int)percentage);
  19. if(percentage < (last_reported_progress + reporting_steps))
  20. return false;
  21. return true;
  22. }
  23. PackageManagerProgressFd::PackageManagerProgressFd(int progress_fd)
  24. {
  25. OutStatusFd = progress_fd;
  26. }
  27. void PackageManagerProgressFd::Started()
  28. {
  29. _config->Set("APT::Keep-Fds::", OutStatusFd);
  30. // send status information that we are about to fork dpkg
  31. if(OutStatusFd > 0) {
  32. std::ostringstream status;
  33. status << "pmstatus:dpkg-exec:"
  34. << (StepsDone/float(StepsTotal)*100.0)
  35. << ":" << _("Running dpkg")
  36. << std::endl;
  37. FileFd::Write(OutStatusFd, status.str().c_str(), status.str().size());
  38. }
  39. }
  40. void PackageManagerProgressFd::Finished()
  41. {
  42. // clear the Keep-Fd again
  43. _config->Clear("APT::Keep-Fds", OutStatusFd);
  44. }
  45. void PackageManagerProgressFd::Error(std::string PackageName,
  46. unsigned int StepsDone,
  47. unsigned int TotalSteps,
  48. std::string ErrorMessage)
  49. {
  50. std::ostringstream status;
  51. status << "pmerror:" << PackageName
  52. << ":" << (StepsDone/float(TotalSteps)*100.0)
  53. << ":" << ErrorMessage
  54. << std::endl;
  55. if(OutStatusFd > 0)
  56. FileFd::Write(OutStatusFd, status.str().c_str(), status.str().size());
  57. }
  58. void PackageManagerProgressFd::ConffilePrompt(std::string PackageName,
  59. unsigned int StepsDone,
  60. unsigned int TotalSteps,
  61. std::string ConfMessage)
  62. {
  63. std::ostringstream status;
  64. status << "pmconffile:" << PackageName
  65. << ":" << (StepsDone/float(TotalSteps)*100.0)
  66. << ":" << ConfMessage
  67. << std::endl;
  68. if(OutStatusFd > 0)
  69. FileFd::Write(OutStatusFd, status.str().c_str(), status.str().size());
  70. }
  71. bool PackageManagerProgressFd::StatusChanged(std::string PackageName,
  72. unsigned int xStepsDone,
  73. unsigned int xTotalSteps,
  74. std::string pkg_action)
  75. {
  76. StepsDone = xStepsDone;
  77. StepsTotal = xTotalSteps;
  78. // build the status str
  79. std::ostringstream status;
  80. status << "pmstatus:" << PackageName
  81. << ":" << (StepsDone/float(StepsTotal)*100.0)
  82. << ":" << pkg_action
  83. << std::endl;
  84. if(OutStatusFd > 0)
  85. FileFd::Write(OutStatusFd, status.str().c_str(), status.str().size());
  86. return true;
  87. }
  88. void PackageManagerFancy::SetupTerminalScrollArea(int nr_rows)
  89. {
  90. // scroll down a bit to avoid visual glitch when the screen
  91. // area shrinks by one row
  92. std::cout << "\n";
  93. // save cursor
  94. std::cout << "\033[s";
  95. // set scroll region (this will place the cursor in the top left)
  96. std::cout << "\033[1;" << nr_rows - 1 << "r";
  97. // restore cursor but ensure its inside the scrolling area
  98. std::cout << "\033[u";
  99. static const char *move_cursor_up = "\033[1A";
  100. std::cout << move_cursor_up;
  101. std::flush(std::cout);
  102. }
  103. PackageManagerFancy::PackageManagerFancy()
  104. : nr_terminal_rows(-1)
  105. {
  106. struct winsize win;
  107. if(ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&win) == 0)
  108. {
  109. nr_terminal_rows = win.ws_row;
  110. }
  111. }
  112. void PackageManagerFancy::Started()
  113. {
  114. if (nr_terminal_rows > 0)
  115. SetupTerminalScrollArea(nr_terminal_rows);
  116. }
  117. void PackageManagerFancy::Finished()
  118. {
  119. if (nr_terminal_rows > 0)
  120. {
  121. SetupTerminalScrollArea(nr_terminal_rows + 1);
  122. // override the progress line (sledgehammer)
  123. static const char* clear_screen_below_cursor = "\033[J";
  124. std::cout << clear_screen_below_cursor;
  125. }
  126. }
  127. bool PackageManagerFancy::StatusChanged(std::string PackageName,
  128. unsigned int StepsDone,
  129. unsigned int TotalSteps,
  130. std::string HumanReadableAction)
  131. {
  132. if (!PackageManager::StatusChanged(PackageName, StepsDone, TotalSteps,
  133. HumanReadableAction))
  134. return false;
  135. int row = nr_terminal_rows;
  136. static string save_cursor = "\033[s";
  137. static string restore_cursor = "\033[u";
  138. static string set_bg_color = "\033[42m"; // green
  139. static string set_fg_color = "\033[30m"; // black
  140. static string restore_bg = "\033[49m";
  141. static string restore_fg = "\033[39m";
  142. std::cout << save_cursor
  143. // move cursor position to last row
  144. << "\033[" << row << ";0f"
  145. << set_bg_color
  146. << set_fg_color
  147. << progress_str
  148. << restore_cursor
  149. << restore_bg
  150. << restore_fg;
  151. std::flush(std::cout);
  152. last_reported_progress = percentage;
  153. return true;
  154. }
  155. bool PackageManagerText::StatusChanged(std::string PackageName,
  156. unsigned int StepsDone,
  157. unsigned int TotalSteps,
  158. std::string HumanReadableAction)
  159. {
  160. if (!PackageManager::StatusChanged(PackageName, StepsDone, TotalSteps, HumanReadableAction))
  161. return false;
  162. std::cout << progress_str << "\r\n";
  163. std::flush(std::cout);
  164. last_reported_progress = percentage;
  165. return true;
  166. }
  167. }; // namespace progress
  168. }; // namespace apt