]> git.saurik.com Git - apt.git/blame - apt-pkg/iprogress.cc
deal with nr_terminal_rows unavailable
[apt.git] / apt-pkg / iprogress.cc
CommitLineData
31f97d7b
MV
1#include <apt-pkg/iprogress.h>
2#include <apt-pkg/strutl.h>
3
4#include <termios.h>
5#include <sys/ioctl.h>
6
7namespace APT {
8namespace Progress {
9
db78c60c 10void PackageManagerFancy::SetupTerminalScrollArea(int nr_rows)
31f97d7b
MV
11{
12 // scroll down a bit to avoid visual glitch when the screen
13 // area shrinks by one row
14 std::cout << "\n";
15
16 // save cursor
17 std::cout << "\033[s";
18
19 // set scroll region (this will place the cursor in the top left)
20 std::cout << "\033[1;" << nr_rows - 1 << "r";
21
22 // restore cursor but ensure its inside the scrolling area
23 std::cout << "\033[u";
24 static const char *move_cursor_up = "\033[1A";
25 std::cout << move_cursor_up;
db78c60c 26
31f97d7b
MV
27 std::flush(std::cout);
28}
29
30PackageManagerFancy::PackageManagerFancy()
31 : nr_terminal_rows(-1)
32{
33 struct winsize win;
34 if(ioctl(STDOUT_FILENO, TIOCGWINSZ, (char *)&win) == 0)
35 {
36 nr_terminal_rows = win.ws_row;
37 }
38}
39
40void PackageManagerFancy::Started()
41{
db78c60c
MV
42 if (nr_terminal_rows > 0)
43 SetupTerminalScrollArea(nr_terminal_rows);
31f97d7b
MV
44}
45
46void PackageManagerFancy::Finished()
47{
db78c60c
MV
48 if (nr_terminal_rows > 0)
49 {
50 SetupTerminalScrollArea(nr_terminal_rows + 1);
31f97d7b 51
db78c60c
MV
52 // override the progress line (sledgehammer)
53 static const char* clear_screen_below_cursor = "\033[J";
54 std::cout << clear_screen_below_cursor;
55 }
31f97d7b
MV
56}
57
58void PackageManagerFancy::StatusChanged(std::string PackageName,
59 unsigned int StepsDone,
60 unsigned int TotalSteps)
61{
62 int reporting_steps = _config->FindI("DpkgPM::Reporting-Steps", 1);
63 float percentage = StepsDone/(float)TotalSteps * 100.0;
64
65 if(percentage < (last_reported_progress + reporting_steps))
66 return;
67
68 std::string progress_str;
69 strprintf(progress_str, "Progress: [%3i%%]", (int)percentage);
70
71 int row = nr_terminal_rows;
72
73 static string save_cursor = "\033[s";
74 static string restore_cursor = "\033[u";
75
76 static string set_bg_color = "\033[42m"; // green
77 static string set_fg_color = "\033[30m"; // black
78
79 static string restore_bg = "\033[49m";
80 static string restore_fg = "\033[39m";
81
82 std::cout << save_cursor
83 // move cursor position to last row
84 << "\033[" << row << ";0f"
85 << set_bg_color
86 << set_fg_color
87 << progress_str
88 << restore_cursor
89 << restore_bg
90 << restore_fg;
91 std::flush(std::cout);
92 last_reported_progress = percentage;
93}
94
95void PackageManagerText::StatusChanged(std::string PackageName,
96 unsigned int StepsDone,
97 unsigned int TotalSteps)
98{
99 int reporting_steps = _config->FindI("DpkgPM::Reporting-Steps", 1);
100 float percentage = StepsDone/(float)TotalSteps * 100.0;
101
102 if(percentage < (last_reported_progress + reporting_steps))
103 return;
104
105 std::string progress_str;
106 strprintf(progress_str, "Progress: [%3i%%]", (int)percentage);
107
108 std::cout << progress_str << "\r\n";
109 std::flush(std::cout);
110
111 last_reported_progress = percentage;
112}
113
114
115}; // namespace progress
116}; // namespace apt