1 #include <apt-pkg/iprogress.h>
2 #include <apt-pkg/strutl.h>
10 void PackageManagerFancy::SetupTerminalScrollArea(int nr_rows
)
12 // scroll down a bit to avoid visual glitch when the screen
13 // area shrinks by one row
17 std::cout
<< "\033[s";
19 // set scroll region (this will place the cursor in the top left)
20 std::cout
<< "\033[1;" << nr_rows
- 1 << "r";
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
;
27 std::flush(std::cout
);
30 PackageManagerFancy::PackageManagerFancy()
31 : nr_terminal_rows(-1)
34 if(ioctl(STDOUT_FILENO
, TIOCGWINSZ
, (char *)&win
) == 0)
36 nr_terminal_rows
= win
.ws_row
;
40 void PackageManagerFancy::Started()
42 if (nr_terminal_rows
> 0)
43 SetupTerminalScrollArea(nr_terminal_rows
);
46 void PackageManagerFancy::Finished()
48 if (nr_terminal_rows
> 0)
50 SetupTerminalScrollArea(nr_terminal_rows
+ 1);
52 // override the progress line (sledgehammer)
53 static const char* clear_screen_below_cursor
= "\033[J";
54 std::cout
<< clear_screen_below_cursor
;
58 void PackageManagerFancy::StatusChanged(std::string PackageName
,
59 unsigned int StepsDone
,
60 unsigned int TotalSteps
)
62 int reporting_steps
= _config
->FindI("DpkgPM::Reporting-Steps", 1);
63 float percentage
= StepsDone
/(float)TotalSteps
* 100.0;
65 if(percentage
< (last_reported_progress
+ reporting_steps
))
68 std::string progress_str
;
69 strprintf(progress_str
, "Progress: [%3i%%]", (int)percentage
);
71 int row
= nr_terminal_rows
;
73 static string save_cursor
= "\033[s";
74 static string restore_cursor
= "\033[u";
76 static string set_bg_color
= "\033[42m"; // green
77 static string set_fg_color
= "\033[30m"; // black
79 static string restore_bg
= "\033[49m";
80 static string restore_fg
= "\033[39m";
82 std::cout
<< save_cursor
83 // move cursor position to last row
84 << "\033[" << row
<< ";0f"
91 std::flush(std::cout
);
92 last_reported_progress
= percentage
;
95 void PackageManagerText::StatusChanged(std::string PackageName
,
96 unsigned int StepsDone
,
97 unsigned int TotalSteps
)
99 int reporting_steps
= _config
->FindI("DpkgPM::Reporting-Steps", 1);
100 float percentage
= StepsDone
/(float)TotalSteps
* 100.0;
102 if(percentage
< (last_reported_progress
+ reporting_steps
))
105 std::string progress_str
;
106 strprintf(progress_str
, "Progress: [%3i%%]", (int)percentage
);
108 std::cout
<< progress_str
<< "\r\n";
109 std::flush(std::cout
);
111 last_reported_progress
= percentage
;
115 }; // namespace progress