iprogress.cc 6.0 KB

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