]>
Commit | Line | Data |
---|---|---|
e6ad8031 MV |
1 | #include <apt-pkg/configuration.h> |
2 | #include <apt-pkg/fileutl.h> | |
31f97d7b | 3 | #include <apt-pkg/strutl.h> |
e6ad8031 | 4 | |
c7ea1eba MV |
5 | #include <apt-private/private-progress.h> |
6 | ||
6c5ae8ed | 7 | #include <apti18n.h> |
31f97d7b MV |
8 | |
9 | #include <termios.h> | |
10 | #include <sys/ioctl.h> | |
e6ad8031 | 11 | #include <sstream> |
5e9458e2 | 12 | #include <fcntl.h> |
31f97d7b MV |
13 | |
14 | namespace APT { | |
15 | namespace Progress { | |
16 | ||
6c5ae8ed | 17 | bool PackageManager::StatusChanged(std::string PackageName, |
e6ad8031 MV |
18 | unsigned int StepsDone, |
19 | unsigned int TotalSteps, | |
20 | std::string HumanReadableAction) | |
6c5ae8ed MV |
21 | { |
22 | int reporting_steps = _config->FindI("DpkgPM::Reporting-Steps", 1); | |
23 | percentage = StepsDone/(float)TotalSteps * 100.0; | |
24 | strprintf(progress_str, _("Progress: [%3i%%]"), (int)percentage); | |
25 | ||
26 | if(percentage < (last_reported_progress + reporting_steps)) | |
27 | return false; | |
28 | ||
29 | return true; | |
30 | } | |
31 | ||
e6ad8031 | 32 | PackageManagerProgressFd::PackageManagerProgressFd(int progress_fd) |
65dbd5a1 | 33 | : StepsDone(0), StepsTotal(1) |
e6ad8031 MV |
34 | { |
35 | OutStatusFd = progress_fd; | |
36 | } | |
37 | ||
f9935b1c MV |
38 | void PackageManagerProgressFd::WriteToStatusFd(std::string s) |
39 | { | |
40 | if(OutStatusFd <= 0) | |
41 | return; | |
42 | FileFd::Write(OutStatusFd, s.c_str(), s.size()); | |
43 | } | |
44 | ||
a22fdebf | 45 | void PackageManagerProgressFd::Start() |
e6ad8031 | 46 | { |
5e9458e2 MV |
47 | if(OutStatusFd <= 0) |
48 | return; | |
49 | ||
50 | // FIXME: use SetCloseExec here once it taught about throwing | |
51 | // exceptions instead of doing _exit(100) on failure | |
52 | fcntl(OutStatusFd,F_SETFD,FD_CLOEXEC); | |
e6ad8031 MV |
53 | |
54 | // send status information that we are about to fork dpkg | |
f9935b1c MV |
55 | std::ostringstream status; |
56 | status << "pmstatus:dpkg-exec:" | |
57 | << (StepsDone/float(StepsTotal)*100.0) | |
58 | << ":" << _("Running dpkg") | |
59 | << std::endl; | |
60 | WriteToStatusFd(status.str()); | |
e6ad8031 MV |
61 | } |
62 | ||
a22fdebf | 63 | void PackageManagerProgressFd::Stop() |
e6ad8031 MV |
64 | { |
65 | // clear the Keep-Fd again | |
66 | _config->Clear("APT::Keep-Fds", OutStatusFd); | |
67 | } | |
68 | ||
69 | void PackageManagerProgressFd::Error(std::string PackageName, | |
70 | unsigned int StepsDone, | |
71 | unsigned int TotalSteps, | |
72 | std::string ErrorMessage) | |
73 | { | |
74 | std::ostringstream status; | |
75 | status << "pmerror:" << PackageName | |
76 | << ":" << (StepsDone/float(TotalSteps)*100.0) | |
77 | << ":" << ErrorMessage | |
78 | << std::endl; | |
f9935b1c | 79 | WriteToStatusFd(status.str()); |
e6ad8031 MV |
80 | } |
81 | ||
82 | void PackageManagerProgressFd::ConffilePrompt(std::string PackageName, | |
83 | unsigned int StepsDone, | |
84 | unsigned int TotalSteps, | |
85 | std::string ConfMessage) | |
86 | { | |
87 | std::ostringstream status; | |
88 | status << "pmconffile:" << PackageName | |
89 | << ":" << (StepsDone/float(TotalSteps)*100.0) | |
90 | << ":" << ConfMessage | |
91 | << std::endl; | |
f9935b1c | 92 | WriteToStatusFd(status.str()); |
e6ad8031 MV |
93 | } |
94 | ||
95 | ||
96 | bool PackageManagerProgressFd::StatusChanged(std::string PackageName, | |
97 | unsigned int xStepsDone, | |
98 | unsigned int xTotalSteps, | |
99 | std::string pkg_action) | |
100 | { | |
101 | StepsDone = xStepsDone; | |
102 | StepsTotal = xTotalSteps; | |
103 | ||
104 | // build the status str | |
105 | std::ostringstream status; | |
dd640f3c | 106 | status << "pmstatus:" << StringSplit(PackageName, ":")[0] |
e6ad8031 MV |
107 | << ":" << (StepsDone/float(StepsTotal)*100.0) |
108 | << ":" << pkg_action | |
109 | << std::endl; | |
f9935b1c | 110 | WriteToStatusFd(status.str()); |
65dbd5a1 MV |
111 | |
112 | if(_config->FindB("Debug::APT::Progress::PackageManagerFd", false) == true) | |
113 | std::cerr << "progress: " << PackageName << " " << xStepsDone | |
114 | << " " << xTotalSteps << " " << pkg_action | |
115 | << std::endl; | |
116 | ||
117 | ||
e6ad8031 MV |
118 | return true; |
119 | } | |
120 | ||
c7ea1eba MV |
121 | |
122 | PackageManagerProgressDeb822Fd::PackageManagerProgressDeb822Fd(int progress_fd) | |
123 | : StepsDone(0), StepsTotal(1) | |
124 | { | |
125 | OutStatusFd = progress_fd; | |
126 | } | |
127 | ||
128 | void PackageManagerProgressDeb822Fd::WriteToStatusFd(std::string s) | |
129 | { | |
130 | FileFd::Write(OutStatusFd, s.c_str(), s.size()); | |
131 | } | |
132 | ||
133 | void PackageManagerProgressDeb822Fd::Start() | |
134 | { | |
135 | // FIXME: use SetCloseExec here once it taught about throwing | |
136 | // exceptions instead of doing _exit(100) on failure | |
137 | fcntl(OutStatusFd,F_SETFD,FD_CLOEXEC); | |
138 | ||
139 | // send status information that we are about to fork dpkg | |
140 | std::ostringstream status; | |
141 | status << "Status: " << "progress" << std::endl | |
142 | << "Percent: " << (StepsDone/float(StepsTotal)*100.0) << std::endl | |
143 | << "Message: " << _("Running dpkg") << std::endl | |
144 | << std::endl; | |
145 | WriteToStatusFd(status.str()); | |
146 | } | |
147 | ||
148 | void PackageManagerProgressDeb822Fd::Stop() | |
149 | { | |
150 | // clear the Keep-Fd again | |
151 | _config->Clear("APT::Keep-Fds", OutStatusFd); | |
152 | } | |
153 | ||
154 | void PackageManagerProgressDeb822Fd::Error(std::string PackageName, | |
155 | unsigned int StepsDone, | |
156 | unsigned int TotalSteps, | |
157 | std::string ErrorMessage) | |
158 | { | |
159 | std::ostringstream status; | |
160 | status << "Status: " << "Error" << std::endl | |
161 | << "Package:" << PackageName << std::endl | |
162 | << "Percent: " << (StepsDone/float(TotalSteps)*100.0) << std::endl | |
163 | << "Message: " << ErrorMessage << std::endl | |
164 | << std::endl; | |
165 | WriteToStatusFd(status.str()); | |
166 | } | |
167 | ||
168 | void PackageManagerProgressDeb822Fd::ConffilePrompt(std::string PackageName, | |
169 | unsigned int StepsDone, | |
170 | unsigned int TotalSteps, | |
171 | std::string ConfMessage) | |
172 | { | |
173 | std::ostringstream status; | |
174 | status << "Status: " << "ConfFile" << std::endl | |
175 | << "Package:" << PackageName << std::endl | |
176 | << "Percent: " << (StepsDone/float(TotalSteps)*100.0) << std::endl | |
177 | << "Message: " << ConfMessage << std::endl | |
178 | << std::endl; | |
179 | WriteToStatusFd(status.str()); | |
180 | } | |
181 | ||
182 | ||
183 | bool PackageManagerProgressDeb822Fd::StatusChanged(std::string PackageName, | |
184 | unsigned int xStepsDone, | |
185 | unsigned int xTotalSteps, | |
186 | std::string message) | |
187 | { | |
188 | StepsDone = xStepsDone; | |
189 | StepsTotal = xTotalSteps; | |
190 | ||
191 | // build the status str | |
192 | std::ostringstream status; | |
193 | status << "Status: " << "progress" << std::endl | |
194 | << "Package: " << PackageName << std::endl | |
195 | << "Percent: " << (StepsDone/float(StepsTotal)*100.0) << std::endl | |
196 | << "Message: " << message << std::endl | |
197 | << std::endl; | |
198 | WriteToStatusFd(status.str()); | |
199 | ||
200 | return true; | |
201 | } | |
202 | ||
203 | ||
db78c60c | 204 | void PackageManagerFancy::SetupTerminalScrollArea(int nr_rows) |
31f97d7b MV |
205 | { |
206 | // scroll down a bit to avoid visual glitch when the screen | |
207 | // area shrinks by one row | |
208 | std::cout << "\n"; | |
209 | ||
210 | // save cursor | |
211 | std::cout << "\033[s"; | |
212 | ||
213 | // set scroll region (this will place the cursor in the top left) | |
214 | std::cout << "\033[1;" << nr_rows - 1 << "r"; | |
215 | ||
216 | // restore cursor but ensure its inside the scrolling area | |
217 | std::cout << "\033[u"; | |
218 | static const char *move_cursor_up = "\033[1A"; | |
219 | std::cout << move_cursor_up; | |
db78c60c | 220 | |
31f97d7b MV |
221 | std::flush(std::cout); |
222 | } | |
223 | ||
224 | PackageManagerFancy::PackageManagerFancy() | |
225 | : nr_terminal_rows(-1) | |
226 | { | |
227 | struct winsize win; | |
228 | if(ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&win) == 0) | |
229 | { | |
230 | nr_terminal_rows = win.ws_row; | |
231 | } | |
232 | } | |
233 | ||
a22fdebf | 234 | void PackageManagerFancy::Start() |
31f97d7b | 235 | { |
db78c60c MV |
236 | if (nr_terminal_rows > 0) |
237 | SetupTerminalScrollArea(nr_terminal_rows); | |
31f97d7b MV |
238 | } |
239 | ||
a22fdebf | 240 | void PackageManagerFancy::Stop() |
31f97d7b | 241 | { |
db78c60c MV |
242 | if (nr_terminal_rows > 0) |
243 | { | |
244 | SetupTerminalScrollArea(nr_terminal_rows + 1); | |
31f97d7b | 245 | |
db78c60c MV |
246 | // override the progress line (sledgehammer) |
247 | static const char* clear_screen_below_cursor = "\033[J"; | |
248 | std::cout << clear_screen_below_cursor; | |
249 | } | |
31f97d7b MV |
250 | } |
251 | ||
6c5ae8ed | 252 | bool PackageManagerFancy::StatusChanged(std::string PackageName, |
31f97d7b | 253 | unsigned int StepsDone, |
e6ad8031 MV |
254 | unsigned int TotalSteps, |
255 | std::string HumanReadableAction) | |
31f97d7b | 256 | { |
e6ad8031 MV |
257 | if (!PackageManager::StatusChanged(PackageName, StepsDone, TotalSteps, |
258 | HumanReadableAction)) | |
6c5ae8ed | 259 | return false; |
31f97d7b MV |
260 | |
261 | int row = nr_terminal_rows; | |
262 | ||
263 | static string save_cursor = "\033[s"; | |
264 | static string restore_cursor = "\033[u"; | |
265 | ||
266 | static string set_bg_color = "\033[42m"; // green | |
267 | static string set_fg_color = "\033[30m"; // black | |
268 | ||
269 | static string restore_bg = "\033[49m"; | |
270 | static string restore_fg = "\033[39m"; | |
271 | ||
272 | std::cout << save_cursor | |
273 | // move cursor position to last row | |
274 | << "\033[" << row << ";0f" | |
275 | << set_bg_color | |
276 | << set_fg_color | |
277 | << progress_str | |
278 | << restore_cursor | |
279 | << restore_bg | |
280 | << restore_fg; | |
281 | std::flush(std::cout); | |
282 | last_reported_progress = percentage; | |
6c5ae8ed MV |
283 | |
284 | return true; | |
31f97d7b MV |
285 | } |
286 | ||
6c5ae8ed | 287 | bool PackageManagerText::StatusChanged(std::string PackageName, |
31f97d7b | 288 | unsigned int StepsDone, |
e6ad8031 MV |
289 | unsigned int TotalSteps, |
290 | std::string HumanReadableAction) | |
31f97d7b | 291 | { |
e6ad8031 | 292 | if (!PackageManager::StatusChanged(PackageName, StepsDone, TotalSteps, HumanReadableAction)) |
6c5ae8ed | 293 | return false; |
31f97d7b MV |
294 | |
295 | std::cout << progress_str << "\r\n"; | |
296 | std::flush(std::cout); | |
297 | ||
298 | last_reported_progress = percentage; | |
6c5ae8ed MV |
299 | |
300 | return true; | |
31f97d7b MV |
301 | } |
302 | ||
303 | ||
c7ea1eba | 304 | |
31f97d7b MV |
305 | }; // namespace progress |
306 | }; // namespace apt |