#include <sys/ioctl.h>
#include <sstream>
#include <fcntl.h>
+#include <stdio.h>
namespace APT {
namespace Progress {
+
+/* Return a APT::Progress::PackageManager based on the global
+ * apt configuration (i.e. APT::Status-Fd and APT::Status-deb822-Fd)
+ */
+PackageManager* PackageManagerProgressFactory()
+{
+ // select the right progress
+ int status_fd = _config->FindI("APT::Status-Fd", -1);
+ int status_deb822_fd = _config->FindI("APT::Status-deb822-Fd", -1);
+
+ APT::Progress::PackageManager *progress = NULL;
+ if (status_deb822_fd > 0)
+ progress = new APT::Progress::PackageManagerProgressDeb822Fd(
+ status_deb822_fd);
+ else if (status_fd > 0)
+ progress = new APT::Progress::PackageManagerProgressFd(status_fd);
+ else if(_config->FindB("Dpkg::Progress-Fancy", false) == true)
+ progress = new APT::Progress::PackageManagerFancy();
+ else if (_config->FindB("Dpkg::Progress",
+ _config->FindB("DpkgPM::Progress", false)) == true)
+ progress = new APT::Progress::PackageManagerText();
+ else
+ progress = new APT::Progress::PackageManager();
+ return progress;
+}
+
bool PackageManager::StatusChanged(std::string PackageName,
unsigned int StepsDone,
unsigned int TotalSteps,
FileFd::Write(OutStatusFd, s.c_str(), s.size());
}
-void PackageManagerProgressFd::Start()
+void PackageManagerProgressFd::StartDpkg()
{
if(OutStatusFd <= 0)
return;
void PackageManagerProgressFd::Stop()
{
- // clear the Keep-Fd again
- _config->Clear("APT::Keep-Fds", OutStatusFd);
}
void PackageManagerProgressFd::Error(std::string PackageName,
FileFd::Write(OutStatusFd, s.c_str(), s.size());
}
-void PackageManagerProgressDeb822Fd::Start()
+void PackageManagerProgressDeb822Fd::StartDpkg()
{
// FIXME: use SetCloseExec here once it taught about throwing
// exceptions instead of doing _exit(100) on failure
void PackageManagerProgressDeb822Fd::Stop()
{
- // clear the Keep-Fd again
- _config->Clear("APT::Keep-Fds", OutStatusFd);
}
void PackageManagerProgressDeb822Fd::Error(std::string PackageName,
return true;
}
+int PackageManagerFancy::GetNumberTerminalRows()
+{
+ struct winsize win;
+ if(ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&win) != 0)
+ return -1;
+
+ return win.ws_row;
+}
void PackageManagerFancy::SetupTerminalScrollArea(int nr_rows)
{
static const char *move_cursor_up = "\033[1A";
std::cout << move_cursor_up;
+ // setup env for (hopefully!) ncurses
+ string s;
+ strprintf(s, "%i", nr_rows);
+ setenv("LINES", s.c_str(), 1);
+
std::flush(std::cout);
}
PackageManagerFancy::PackageManagerFancy()
- : nr_terminal_rows(-1)
{
- struct winsize win;
- if(ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&win) == 0)
- {
- nr_terminal_rows = win.ws_row;
- }
+ // setup terminal size
+ old_SIGWINCH = signal(SIGWINCH, HandleSIGWINCH);
+}
+
+PackageManagerFancy::~PackageManagerFancy()
+{
+ signal(SIGWINCH, old_SIGWINCH);
+}
+
+void PackageManagerFancy::HandleSIGWINCH(int)
+{
+ int nr_terminal_rows = GetNumberTerminalRows();
+ SetupTerminalScrollArea(nr_terminal_rows);
}
void PackageManagerFancy::Start()
{
+ int nr_terminal_rows = GetNumberTerminalRows();
if (nr_terminal_rows > 0)
SetupTerminalScrollArea(nr_terminal_rows);
}
void PackageManagerFancy::Stop()
{
+ int nr_terminal_rows = GetNumberTerminalRows();
if (nr_terminal_rows > 0)
{
SetupTerminalScrollArea(nr_terminal_rows + 1);
HumanReadableAction))
return false;
- int row = nr_terminal_rows;
+ int row = GetNumberTerminalRows();
static string save_cursor = "\033[s";
static string restore_cursor = "\033[u";