Skip to content

Commit

Permalink
feat: impl dpkg ProgressBar
Browse files Browse the repository at this point in the history
  • Loading branch information
eatradish committed Aug 18, 2024
1 parent 44c3aa7 commit 81047f6
Show file tree
Hide file tree
Showing 7 changed files with 37 additions and 24 deletions.
1 change: 0 additions & 1 deletion oma-pm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,6 @@ cxx = "1.0.121"
nix = { version = "0.29.0", features = ["fs", "process", "term", "ioctl", "signal"] }
mio = { version = "1.0.1", features = ["os-ext"] }


[dev-dependencies]
dashmap = "6"
indicatif = "0.17"
Expand Down
18 changes: 1 addition & 17 deletions oma-pm/src/dpkg.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,7 @@ impl Pty {
// dprog!(config, progress, "pty", "{line:?}");
debug!("pty: {line}");

if line.trim().is_empty() || check_spam(line) {
continue;
}

// Occasionally there is a line which comes through
if line.ends_with('\r') {
if line.trim().is_empty() {
continue;
}

Expand Down Expand Up @@ -253,17 +248,6 @@ impl Pty {
}
}

fn check_spam(line: &str) -> bool {
[
"Nothing to fetch",
"(Reading database",
"Selecting previously unselected package",
"Preparing to unpack",
]
.iter()
.any(|spam| line.contains(spam))
}

fn read_fd<'a>(file: &mut File, buffer: &'a mut [u8]) -> Result<PtyStr<'a>, ReadDpkgStatusError> {
let sized_buf = match file.read(buffer) {
Ok(0) => return Ok(PtyStr::Eof),
Expand Down
3 changes: 2 additions & 1 deletion oma-pm/src/progress.rs
Original file line number Diff line number Diff line change
Expand Up @@ -96,9 +96,10 @@ impl OmaAptInstallProgress {
debug!("Dpkg::Options:: is set to --force-all");
}

config.set("Dpkg::Use-Pty", "false");

if !is_terminal() || no_progress {
std::env::set_var("DEBIAN_FRONTEND", "noninteractive");
config.set("Dpkg::Use-Pty", "false");
}

if yes || force_yes || dpkg_force_all {
Expand Down
2 changes: 1 addition & 1 deletion src/subcommand/download.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,7 +44,7 @@ pub fn execute(
|count, event, total| {
let event = InstallPackageEvent::from(event);
if !no_progress {
pb!(event, mb, pb_map, count, total, global_is_set)
pb!(event, mb, pb_map, count, total, global_is_set);
} else {
handle_event_without_progressbar(event);
}
Expand Down
9 changes: 9 additions & 0 deletions src/subcommand/upgrade.rs
Original file line number Diff line number Diff line change
Expand Up @@ -151,6 +151,9 @@ pub fn execute(
op,
) {
Ok(()) => {
if let Some(gpb) = pb_map.get(&0) {
gpb.finish_and_clear();
}
write_history_entry(
op_after,
typ,
Expand All @@ -171,6 +174,9 @@ pub fn execute(
OmaAptError::AptErrors(_)
| OmaAptError::AptError(_)
| OmaAptError::AptCxxException(_) => {
if let Some(gpb) = pb_map.get(&0) {
gpb.finish_and_clear();
}
if retry_times == 3 {
write_history_entry(
op_after,
Expand All @@ -196,6 +202,9 @@ pub fn execute(
retry_times += 1;
}
_ => {
if let Some(gpb) = pb_map.get(&0) {
gpb.finish_and_clear();
}
drop(fds);
return Err(OutputError::from(e));
}
Expand Down
8 changes: 6 additions & 2 deletions src/subcommand/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -266,14 +266,18 @@ pub(crate) fn normal_commit(args: NormalCommitArgs, client: &Client) -> Result<(
&apt_args,
|count, event, total| {
if !no_progress {
pb!(event, mb, pb_map, count, total, global_is_set)
pb!(event, mb, pb_map, count, total, global_is_set);
} else {
handle_event_without_progressbar(event);
}
},
op,
);

if let Some(gpb) = pb_map.get(&0) {
gpb.finish_and_clear();
}

match res {
Ok(_) => {
success!("{}", fl!("history-tips-1"));
Expand Down Expand Up @@ -328,7 +332,7 @@ pub(crate) fn handle_event_without_progressbar(event: InstallPackageEvent) {
},
InstallPackageEvent::DpkgEvent(_) => {}
InstallPackageEvent::DpkgLine(line) => {
info!("{line}")
println!("{line}")
}
}
}
Expand Down
20 changes: 18 additions & 2 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -100,14 +100,30 @@ macro_rules! pb {
if let Some(gpb) = $pb_map.get(&0) {
gpb.finish_and_clear();
}
$pb_map.remove(&0);
}
}
}
oma_pm::apt::InstallPackageEvent::DpkgEvent(event) => {
dbg!(event);
let gpb = $pb_map.get(&0);
if gpb.is_none() {
$global_is_set.store(true, std::sync::atomic::Ordering::SeqCst);
let sty =
oma_console::pb::oma_style_pb(oma_console::writer::Writer::default(), true);
let gpb = $mb.insert(
0,
oma_console::indicatif::ProgressBar::new(100).with_style(sty),
);
gpb.set_prefix("Progress");
gpb.set_position(event.percent);
$pb_map.insert(0, gpb);
return;
}
let gpb = gpb.unwrap();
gpb.set_position(event.percent);
}
oma_pm::apt::InstallPackageEvent::DpkgLine(line) => {
dbg!(line);
$mb.println(line).ok();
}
}

Expand Down

0 comments on commit 81047f6

Please sign in to comment.