private-progress.cc 6.3 KB

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