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
)
32 : StepsDone(0), StepsTotal(1)
34 OutStatusFd
= progress_fd
;
37 void PackageManagerProgressFd::WriteToStatusFd(std::string s
)
41 FileFd::Write(OutStatusFd
, s
.c_str(), s
.size());
44 void PackageManagerProgressFd::Start()
49 // FIXME: use SetCloseExec here once it taught about throwing
50 // exceptions instead of doing _exit(100) on failure
51 fcntl(OutStatusFd
,F_SETFD
,FD_CLOEXEC
);
53 // send status information that we are about to fork dpkg
54 std::ostringstream status
;
55 status
<< "pmstatus:dpkg-exec:"
56 << (StepsDone
/float(StepsTotal
)*100.0)
57 << ":" << _("Running dpkg")
59 WriteToStatusFd(status
.str());
62 void PackageManagerProgressFd::Stop()
64 // clear the Keep-Fd again
65 _config
->Clear("APT::Keep-Fds", OutStatusFd
);
68 void PackageManagerProgressFd::Error(std::string PackageName
,
69 unsigned int StepsDone
,
70 unsigned int TotalSteps
,
71 std::string ErrorMessage
)
73 std::ostringstream status
;
74 status
<< "pmerror:" << PackageName
75 << ":" << (StepsDone
/float(TotalSteps
)*100.0)
76 << ":" << ErrorMessage
78 WriteToStatusFd(status
.str());
81 void PackageManagerProgressFd::ConffilePrompt(std::string PackageName
,
82 unsigned int StepsDone
,
83 unsigned int TotalSteps
,
84 std::string ConfMessage
)
86 std::ostringstream status
;
87 status
<< "pmconffile:" << PackageName
88 << ":" << (StepsDone
/float(TotalSteps
)*100.0)
91 WriteToStatusFd(status
.str());
95 bool PackageManagerProgressFd::StatusChanged(std::string PackageName
,
96 unsigned int xStepsDone
,
97 unsigned int xTotalSteps
,
98 std::string pkg_action
)
100 StepsDone
= xStepsDone
;
101 StepsTotal
= xTotalSteps
;
103 // build the status str
104 std::ostringstream status
;
105 status
<< "pmstatus:" << StringSplit(PackageName
, ":")[0]
106 << ":" << (StepsDone
/float(StepsTotal
)*100.0)
109 WriteToStatusFd(status
.str());
111 if(_config
->FindB("Debug::APT::Progress::PackageManagerFd", false) == true)
112 std::cerr
<< "progress: " << PackageName
<< " " << xStepsDone
113 << " " << xTotalSteps
<< " " << pkg_action
120 void PackageManagerFancy::SetupTerminalScrollArea(int nr_rows
)
122 // scroll down a bit to avoid visual glitch when the screen
123 // area shrinks by one row
127 std::cout
<< "\033[s";
129 // set scroll region (this will place the cursor in the top left)
130 std::cout
<< "\033[1;" << nr_rows
- 1 << "r";
132 // restore cursor but ensure its inside the scrolling area
133 std::cout
<< "\033[u";
134 static const char *move_cursor_up
= "\033[1A";
135 std::cout
<< move_cursor_up
;
137 std::flush(std::cout
);
140 PackageManagerFancy::PackageManagerFancy()
141 : nr_terminal_rows(-1)
144 if(ioctl(STDOUT_FILENO
, TIOCGWINSZ
, (char *)&win
) == 0)
146 nr_terminal_rows
= win
.ws_row
;
150 void PackageManagerFancy::Start()
152 if (nr_terminal_rows
> 0)
153 SetupTerminalScrollArea(nr_terminal_rows
);
156 void PackageManagerFancy::Stop()
158 if (nr_terminal_rows
> 0)
160 SetupTerminalScrollArea(nr_terminal_rows
+ 1);
162 // override the progress line (sledgehammer)
163 static const char* clear_screen_below_cursor
= "\033[J";
164 std::cout
<< clear_screen_below_cursor
;
168 bool PackageManagerFancy::StatusChanged(std::string PackageName
,
169 unsigned int StepsDone
,
170 unsigned int TotalSteps
,
171 std::string HumanReadableAction
)
173 if (!PackageManager::StatusChanged(PackageName
, StepsDone
, TotalSteps
,
174 HumanReadableAction
))
177 int row
= nr_terminal_rows
;
179 static string save_cursor
= "\033[s";
180 static string restore_cursor
= "\033[u";
182 static string set_bg_color
= "\033[42m"; // green
183 static string set_fg_color
= "\033[30m"; // black
185 static string restore_bg
= "\033[49m";
186 static string restore_fg
= "\033[39m";
188 std::cout
<< save_cursor
189 // move cursor position to last row
190 << "\033[" << row
<< ";0f"
197 std::flush(std::cout
);
198 last_reported_progress
= percentage
;
203 bool PackageManagerText::StatusChanged(std::string PackageName
,
204 unsigned int StepsDone
,
205 unsigned int TotalSteps
,
206 std::string HumanReadableAction
)
208 if (!PackageManager::StatusChanged(PackageName
, StepsDone
, TotalSteps
, HumanReadableAction
))
211 std::cout
<< progress_str
<< "\r\n";
212 std::flush(std::cout
);
214 last_reported_progress
= percentage
;
220 }; // namespace progress