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