From 77a73af1b6c2179d06ca57edd68c06a70a1a8989 Mon Sep 17 00:00:00 2001 From: Pietro Cerutti Date: Wed, 12 Sep 2012 12:36:15 +0200 Subject: [PATCH] interactive --- libpkg/pkg.h | 6 ++++++ libpkg/pkg_jobs.c | 11 +++++++++++ pkg/autoremove.c | 49 +++++++++++++++++++++++++++++++------------------ 3 files changed, 48 insertions(+), 18 deletions(-) diff --git a/libpkg/pkg.h b/libpkg/pkg.h index ba15d0b..458b745 100644 --- a/libpkg/pkg.h +++ b/libpkg/pkg.h @@ -852,6 +852,12 @@ void pkg_jobs_free(struct pkg_jobs *jobs); int pkg_jobs_add(struct pkg_jobs *jobs, struct pkg *pkg); /** + * Remove a pkg from the jobs queue. + * @return An error code. + */ +int pkg_jobs_rem(struct pkg_jobs *jobs, struct pkg *pkg); + +/** * Returns true if there are no jobs. */ int pkg_jobs_is_empty(struct pkg_jobs *jobs); diff --git a/libpkg/pkg_jobs.c b/libpkg/pkg_jobs.c index c1c83cf..308ee70 100644 --- a/libpkg/pkg_jobs.c +++ b/libpkg/pkg_jobs.c @@ -100,6 +100,17 @@ pkg_jobs_add(struct pkg_jobs *j, struct pkg *pkg) } int +pkg_jobs_rem(struct pkg_jobs *j, struct pkg *pkg) +{ + assert(j != NULL); + assert(pkg != NULL); + + STAILQ_REMOVE(&j->jobs, pkg, pkg, next); + + return (EPKG_OK); +} + +int pkg_jobs_is_empty(struct pkg_jobs *j) { assert(j != NULL); diff --git a/pkg/autoremove.c b/pkg/autoremove.c index ec05338..2719a3f 100644 --- a/pkg/autoremove.c +++ b/pkg/autoremove.c @@ -38,7 +38,7 @@ void usage_autoremove(void) { - fprintf(stderr, "usage: pkg autoremove [-yq]\n\n"); + fprintf(stderr, "usage: pkg autoremove [-iyq]\n\n"); fprintf(stderr, "For more information see 'pkg help autoremove'.\n"); } @@ -55,9 +55,13 @@ exec_autoremove(int argc, char **argv) char size[7]; int ch; bool yes = false; + bool interactive = false; - while ((ch = getopt(argc, argv, "yq")) != -1) { + while ((ch = getopt(argc, argv, "iyq")) != -1) { switch (ch) { + case 'i': + interactive = true; + break; case 'q': quiet = true; break; @@ -99,20 +103,10 @@ exec_autoremove(int argc, char **argv) } while ((retcode = pkgdb_it_next(it, &pkg, PKG_LOAD_BASIC)) == EPKG_OK) { - pkg_get(pkg, PKG_FLATSIZE, &flatsize, PKG_NEW_FLATSIZE, &newflatsize); - oldsize += flatsize; - newsize += newflatsize; pkg_jobs_add(jobs, pkg); pkg = NULL; } - if (oldsize > newsize) { - newsize *= -1; - humanize_number(size, sizeof(size), oldsize - newsize, "B", HN_AUTOSCALE, 0); - } else { - humanize_number(size, sizeof(size), newsize - oldsize, "B", HN_AUTOSCALE, 0); - } - if (pkg_jobs_is_empty(jobs)) { printf("Nothing to do.\n"); retcode = 0; @@ -122,16 +116,35 @@ exec_autoremove(int argc, char **argv) pkg = NULL; if (!quiet) { printf("Packages to be autoremoved: \n"); + int torem = 1; while (pkg_jobs(jobs, &pkg) == EPKG_OK) { const char *name, *version; - pkg_get(pkg, PKG_NAME, &name, PKG_VERSION, &version); - printf("\t%s-%s\n", name, version); + pkg_get(pkg, PKG_NAME, &name, PKG_VERSION, &version, PKG_FLATSIZE, &flatsize, PKG_NEW_FLATSIZE, &newflatsize); + printf("\t%s-%s", name, version); + + if (interactive) { + if (!(torem = query_yesno(" [y/N]: "))) { + pkg_jobs_rem(jobs, pkg); + } + } else { + puts(""); // need a newline here + } + + + if (torem) { + oldsize += flatsize; + newsize += newflatsize; + } } - if (oldsize > newsize) - printf("\nThe autoremoval will free %s\n", size); - else - printf("\nThe autoremoval will require %s more space\n", size); + if (pkg_jobs_is_empty(jobs)) { + printf("Nothing to do.\n"); + retcode = 0; + goto cleanup; + } + + humanize_number(size, sizeof(size), newsize - oldsize, "B", HN_AUTOSCALE, 0); + printf("\nThe autoremoval will free %s\n", size); if (!yes) pkg_config_bool(PKG_CONFIG_ASSUME_ALWAYS_YES, &yes); -- 1.7.11.4