restore binary compatiblity with the pkgPackageManager interface
[ntk/apt.git] / apt-private / private-progress.cc
CommitLineData
e6ad8031
MV
1#include <apt-pkg/configuration.h>
2#include <apt-pkg/fileutl.h>
31f97d7b
MV
3#include <apt-pkg/iprogress.h>
4#include <apt-pkg/strutl.h>
e6ad8031 5
6c5ae8ed 6#include <apti18n.h>
31f97d7b
MV
7
8#include <termios.h>
9#include <sys/ioctl.h>
e6ad8031 10#include <sstream>
5e9458e2 11#include <fcntl.h>
31f97d7b
MV
12
13namespace APT {
14namespace Progress {
15
6c5ae8ed 16bool PackageManager::StatusChanged(std::string PackageName,
e6ad8031
MV
17 unsigned int StepsDone,
18 unsigned int TotalSteps,
19 std::string HumanReadableAction)
6c5ae8ed
MV
20{
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);
24
25 if(percentage < (last_reported_progress + reporting_steps))
26 return false;
27
28 return true;
29}
30
e6ad8031 31PackageManagerProgressFd::PackageManagerProgressFd(int progress_fd)
65dbd5a1 32 : StepsDone(0), StepsTotal(1)
e6ad8031
MV
33{
34 OutStatusFd = progress_fd;
35}
36
f9935b1c
MV
37void PackageManagerProgressFd::WriteToStatusFd(std::string s)
38{
39 if(OutStatusFd <= 0)
40 return;
41 FileFd::Write(OutStatusFd, s.c_str(), s.size());
42}
43
a22fdebf 44void PackageManagerProgressFd::Start()
e6ad8031 45{
5e9458e2
MV
46 if(OutStatusFd <= 0)
47 return;
48
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);
e6ad8031
MV
52
53 // send status information that we are about to fork dpkg
f9935b1c
MV
54 std::ostringstream status;
55 status << "pmstatus:dpkg-exec:"
56 << (StepsDone/float(StepsTotal)*100.0)
57 << ":" << _("Running dpkg")
58 << std::endl;
59 WriteToStatusFd(status.str());
e6ad8031
MV
60}
61
a22fdebf 62void PackageManagerProgressFd::Stop()
e6ad8031
MV
63{
64 // clear the Keep-Fd again
65 _config->Clear("APT::Keep-Fds", OutStatusFd);
66}
67
68void PackageManagerProgressFd::Error(std::string PackageName,
69 unsigned int StepsDone,
70 unsigned int TotalSteps,
71 std::string ErrorMessage)
72{
73 std::ostringstream status;
74 status << "pmerror:" << PackageName
75 << ":" << (StepsDone/float(TotalSteps)*100.0)
76 << ":" << ErrorMessage
77 << std::endl;
f9935b1c 78 WriteToStatusFd(status.str());
e6ad8031
MV
79}
80
81void PackageManagerProgressFd::ConffilePrompt(std::string PackageName,
82 unsigned int StepsDone,
83 unsigned int TotalSteps,
84 std::string ConfMessage)
85{
86 std::ostringstream status;
87 status << "pmconffile:" << PackageName
88 << ":" << (StepsDone/float(TotalSteps)*100.0)
89 << ":" << ConfMessage
90 << std::endl;
f9935b1c 91 WriteToStatusFd(status.str());
e6ad8031
MV
92}
93
94
95bool PackageManagerProgressFd::StatusChanged(std::string PackageName,
96 unsigned int xStepsDone,
97 unsigned int xTotalSteps,
98 std::string pkg_action)
99{
100 StepsDone = xStepsDone;
101 StepsTotal = xTotalSteps;
102
103 // build the status str
104 std::ostringstream status;
dd640f3c 105 status << "pmstatus:" << StringSplit(PackageName, ":")[0]
e6ad8031
MV
106 << ":" << (StepsDone/float(StepsTotal)*100.0)
107 << ":" << pkg_action
108 << std::endl;
f9935b1c 109 WriteToStatusFd(status.str());
65dbd5a1
MV
110
111 if(_config->FindB("Debug::APT::Progress::PackageManagerFd", false) == true)
112 std::cerr << "progress: " << PackageName << " " << xStepsDone
113 << " " << xTotalSteps << " " << pkg_action
114 << std::endl;
115
116
e6ad8031
MV
117 return true;
118}
119
db78c60c 120void PackageManagerFancy::SetupTerminalScrollArea(int nr_rows)
31f97d7b
MV
121{
122 // scroll down a bit to avoid visual glitch when the screen
123 // area shrinks by one row
124 std::cout << "\n";
125
126 // save cursor
127 std::cout << "\033[s";
128
129 // set scroll region (this will place the cursor in the top left)
130 std::cout << "\033[1;" << nr_rows - 1 << "r";
131
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;
db78c60c 136
31f97d7b
MV
137 std::flush(std::cout);
138}
139
140PackageManagerFancy::PackageManagerFancy()
141 : nr_terminal_rows(-1)
142{
143 struct winsize win;
144 if(ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&win) == 0)
145 {
146 nr_terminal_rows = win.ws_row;
147 }
148}
149
a22fdebf 150void PackageManagerFancy::Start()
31f97d7b 151{
db78c60c
MV
152 if (nr_terminal_rows > 0)
153 SetupTerminalScrollArea(nr_terminal_rows);
31f97d7b
MV
154}
155
a22fdebf 156void PackageManagerFancy::Stop()
31f97d7b 157{
db78c60c
MV
158 if (nr_terminal_rows > 0)
159 {
160 SetupTerminalScrollArea(nr_terminal_rows + 1);
31f97d7b 161
db78c60c
MV
162 // override the progress line (sledgehammer)
163 static const char* clear_screen_below_cursor = "\033[J";
164 std::cout << clear_screen_below_cursor;
165 }
31f97d7b
MV
166}
167
6c5ae8ed 168bool PackageManagerFancy::StatusChanged(std::string PackageName,
31f97d7b 169 unsigned int StepsDone,
e6ad8031
MV
170 unsigned int TotalSteps,
171 std::string HumanReadableAction)
31f97d7b 172{
e6ad8031
MV
173 if (!PackageManager::StatusChanged(PackageName, StepsDone, TotalSteps,
174 HumanReadableAction))
6c5ae8ed 175 return false;
31f97d7b
MV
176
177 int row = nr_terminal_rows;
178
179 static string save_cursor = "\033[s";
180 static string restore_cursor = "\033[u";
181
182 static string set_bg_color = "\033[42m"; // green
183 static string set_fg_color = "\033[30m"; // black
184
185 static string restore_bg = "\033[49m";
186 static string restore_fg = "\033[39m";
187
188 std::cout << save_cursor
189 // move cursor position to last row
190 << "\033[" << row << ";0f"
191 << set_bg_color
192 << set_fg_color
193 << progress_str
194 << restore_cursor
195 << restore_bg
196 << restore_fg;
197 std::flush(std::cout);
198 last_reported_progress = percentage;
6c5ae8ed
MV
199
200 return true;
31f97d7b
MV
201}
202
6c5ae8ed 203bool PackageManagerText::StatusChanged(std::string PackageName,
31f97d7b 204 unsigned int StepsDone,
e6ad8031
MV
205 unsigned int TotalSteps,
206 std::string HumanReadableAction)
31f97d7b 207{
e6ad8031 208 if (!PackageManager::StatusChanged(PackageName, StepsDone, TotalSteps, HumanReadableAction))
6c5ae8ed 209 return false;
31f97d7b
MV
210
211 std::cout << progress_str << "\r\n";
212 std::flush(std::cout);
213
214 last_reported_progress = percentage;
6c5ae8ed
MV
215
216 return true;
31f97d7b
MV
217}
218
219
220}; // namespace progress
221}; // namespace apt