1 #include <apt-pkg/configuration.h>
2 #include <apt-pkg/fileutl.h>
3 #include <apt-pkg/iprogress.h>
4 #include <apt-pkg/strutl.h>
16 bool PackageManager::StatusChanged(std::string PackageName
,
17 unsigned int StepsDone
,
18 unsigned int TotalSteps
,
19 std::string HumanReadableAction
)
21 int reporting_steps
= _config
->FindI("DpkgPM::Reporting-Steps", 1);
22 percentage
= StepsDone
/(float)TotalSteps
* 100.0;
23 strprintf(progress_str
, _("Progress: [%3i%%]"), (int)percentage
);
25 if(percentage
< (last_reported_progress
+ reporting_steps
))
31 PackageManagerProgressFd::PackageManagerProgressFd(int progress_fd
)
33 OutStatusFd
= progress_fd
;
36 void PackageManagerProgressFd::WriteToStatusFd(std::string s
)
40 FileFd::Write(OutStatusFd
, s
.c_str(), s
.size());
43 void PackageManagerProgressFd::Started()
45 _config
->Set("APT::Keep-Fds::", OutStatusFd
);
47 // send status information that we are about to fork dpkg
48 std::ostringstream status
;
49 status
<< "pmstatus:dpkg-exec:"
50 << (StepsDone
/float(StepsTotal
)*100.0)
51 << ":" << _("Running dpkg")
53 WriteToStatusFd(status
.str());
56 void PackageManagerProgressFd::Finished()
58 // clear the Keep-Fd again
59 _config
->Clear("APT::Keep-Fds", OutStatusFd
);
62 void PackageManagerProgressFd::Error(std::string PackageName
,
63 unsigned int StepsDone
,
64 unsigned int TotalSteps
,
65 std::string ErrorMessage
)
67 std::ostringstream status
;
68 status
<< "pmerror:" << PackageName
69 << ":" << (StepsDone
/float(TotalSteps
)*100.0)
70 << ":" << ErrorMessage
72 WriteToStatusFd(status
.str());
75 void PackageManagerProgressFd::ConffilePrompt(std::string PackageName
,
76 unsigned int StepsDone
,
77 unsigned int TotalSteps
,
78 std::string ConfMessage
)
80 std::ostringstream status
;
81 status
<< "pmconffile:" << PackageName
82 << ":" << (StepsDone
/float(TotalSteps
)*100.0)
85 WriteToStatusFd(status
.str());
89 bool PackageManagerProgressFd::StatusChanged(std::string PackageName
,
90 unsigned int xStepsDone
,
91 unsigned int xTotalSteps
,
92 std::string pkg_action
)
94 StepsDone
= xStepsDone
;
95 StepsTotal
= xTotalSteps
;
97 // build the status str
98 std::ostringstream status
;
99 status
<< "pmstatus:" << PackageName
100 << ":" << (StepsDone
/float(StepsTotal
)*100.0)
103 WriteToStatusFd(status
.str());
107 void PackageManagerFancy::SetupTerminalScrollArea(int nr_rows
)
109 // scroll down a bit to avoid visual glitch when the screen
110 // area shrinks by one row
114 std::cout
<< "\033[s";
116 // set scroll region (this will place the cursor in the top left)
117 std::cout
<< "\033[1;" << nr_rows
- 1 << "r";
119 // restore cursor but ensure its inside the scrolling area
120 std::cout
<< "\033[u";
121 static const char *move_cursor_up
= "\033[1A";
122 std::cout
<< move_cursor_up
;
124 std::flush(std::cout
);
127 PackageManagerFancy::PackageManagerFancy()
128 : nr_terminal_rows(-1)
131 if(ioctl(STDOUT_FILENO
, TIOCGWINSZ
, (char *)&win
) == 0)
133 nr_terminal_rows
= win
.ws_row
;
137 void PackageManagerFancy::Started()
139 if (nr_terminal_rows
> 0)
140 SetupTerminalScrollArea(nr_terminal_rows
);
143 void PackageManagerFancy::Finished()
145 if (nr_terminal_rows
> 0)
147 SetupTerminalScrollArea(nr_terminal_rows
+ 1);
149 // override the progress line (sledgehammer)
150 static const char* clear_screen_below_cursor
= "\033[J";
151 std::cout
<< clear_screen_below_cursor
;
155 bool PackageManagerFancy::StatusChanged(std::string PackageName
,
156 unsigned int StepsDone
,
157 unsigned int TotalSteps
,
158 std::string HumanReadableAction
)
160 if (!PackageManager::StatusChanged(PackageName
, StepsDone
, TotalSteps
,
161 HumanReadableAction
))
164 int row
= nr_terminal_rows
;
166 static string save_cursor
= "\033[s";
167 static string restore_cursor
= "\033[u";
169 static string set_bg_color
= "\033[42m"; // green
170 static string set_fg_color
= "\033[30m"; // black
172 static string restore_bg
= "\033[49m";
173 static string restore_fg
= "\033[39m";
175 std::cout
<< save_cursor
176 // move cursor position to last row
177 << "\033[" << row
<< ";0f"
184 std::flush(std::cout
);
185 last_reported_progress
= percentage
;
190 bool PackageManagerText::StatusChanged(std::string PackageName
,
191 unsigned int StepsDone
,
192 unsigned int TotalSteps
,
193 std::string HumanReadableAction
)
195 if (!PackageManager::StatusChanged(PackageName
, StepsDone
, TotalSteps
, HumanReadableAction
))
198 std::cout
<< progress_str
<< "\r\n";
199 std::flush(std::cout
);
201 last_reported_progress
= percentage
;
207 }; // namespace progress