Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP

Loading…

Add Sorting Options #57

Closed
wants to merge 3 commits into from

2 participants

@baskerville

No description provided.

@baskerville

So many mistakes!

Sorry about that.

@falconindy
Owner

Please rebase your commits.

@baskerville baskerville closed this
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Commits on Apr 30, 2013
  1. @baskerville
  2. @baskerville

    Sort via a pointer to a pkg comparison function

    baskerville authored
    Applied all the changes suggested by the Author.
  3. @baskerville
This page is out of date. Refresh to see the latest.
Showing with 84 additions and 1 deletion.
  1. +8 −0 README.pod
  2. +76 −1 cower.c
View
8 README.pod
@@ -111,6 +111,14 @@ The reverse of B<--ignore-ood>.
Ignore all results marked as out of date.
+=item B<--sort=>I<KEYNAME>
+
+Sort the results according to the given key name. I<KEYNAME> is B<firstsubmitted>, B<lastmodified>, B<license>, B<maintainer>, B<name>, B<votes>, B<outofdate> or B<version>.
+
+=item B<--rsort>
+
+Reverse the sorting order.
+
=item B<-p, --from-pkgbuild>
Interpret non-option arguments to cower as paths to PKGBUILDs which will be
View
77 cower.c
@@ -119,6 +119,8 @@ typedef enum __operation_t {
enum {
OP_DEBUG = 1000,
OP_FORMAT,
+ OP_SORTBY,
+ OP_REVERSE,
OP_IGNOREPKG,
OP_IGNOREREPO,
OP_LISTDELIM,
@@ -146,6 +148,11 @@ enum {
KEY_QUERY_RESULTS,
};
+enum {
+ SORT_FORWARD = 1,
+ SORT_REVERSE = -1
+};
+
typedef enum __pkgdetail_t {
PKGDETAIL_DEPENDS = 0,
PKGDETAIL_MAKEDEPENDS,
@@ -292,6 +299,7 @@ static struct {
short color;
short ignoreood;
+ short reverse;
int extinfo:1;
int force:1;
int getdeps:1;
@@ -301,6 +309,8 @@ static struct {
int maxthreads;
long timeout;
+ int (*sortcb) (const struct aurpkg_t*, const struct aurpkg_t*);
+
alpm_list_t *targets;
struct {
alpm_list_t *pkgs;
@@ -533,12 +543,44 @@ int archive_extract_file(const struct response_t *file, char **subdir) /* {{{ */
return ret;
} /* }}} */
+int aurpkg_cmpname(const struct aurpkg_t *pkg1, const struct aurpkg_t *pkg2) {/* {{{ */
+ return strcmp(pkg1->name, pkg2->name);
+} /* }}} */
+
+int aurpkg_cmpver(const struct aurpkg_t *pkg1, const struct aurpkg_t *pkg2) {/* {{{ */
+ return alpm_pkg_vercmp(pkg1->ver, pkg2->ver);
+} /* }}} */
+
+int aurpkg_cmpmaint(const struct aurpkg_t *pkg1, const struct aurpkg_t *pkg2) {/* {{{ */
+ return strcmp(pkg1->maint, pkg2->maint);
+} /* }}} */
+
+int aurpkg_cmplic(const struct aurpkg_t *pkg1, const struct aurpkg_t *pkg2) {/* {{{ */
+ return strcmp(pkg1->lic, pkg2->lic);
+} /* }}} */
+
+int aurpkg_cmpvotes(const struct aurpkg_t *pkg1, const struct aurpkg_t *pkg2) {/* {{{ */
+ return pkg1->votes - pkg2->votes;
+} /* }}} */
+
+int aurpkg_cmpood(const struct aurpkg_t *pkg1, const struct aurpkg_t *pkg2) {/* {{{ */
+ return pkg1->ood - pkg2->ood;
+} /* }}} */
+
+int aurpkg_cmplastmod(const struct aurpkg_t *pkg1, const struct aurpkg_t *pkg2) {/* {{{ */
+ return difftime(pkg1->lastmod, pkg2->lastmod) >= 0 ? 1 : -1;
+} /* }}} */
+
+int aurpkg_cmpfirstsub(const struct aurpkg_t *pkg1, const struct aurpkg_t *pkg2) {/* {{{ */
+ return difftime(pkg1->firstsub, pkg2->firstsub) >= 0 ? 1 : -1;
+} /* }}} */
+
int aurpkg_cmp(const void *p1, const void *p2) /* {{{ */
{
const struct aurpkg_t *pkg1 = p1;
const struct aurpkg_t *pkg2 = p2;
- return strcmp(pkg1->name, pkg2->name);
+ return cfg.reverse * (*cfg.sortcb)(pkg1, pkg2);
} /* }}} */
struct aurpkg_t *aurpkg_dup(const struct aurpkg_t *pkg) /* {{{ */
@@ -1398,6 +1440,8 @@ int parse_options(int argc, char *argv[]) /* {{{ */
{"debug", no_argument, 0, OP_DEBUG},
{"force", no_argument, 0, 'f'},
{"format", required_argument, 0, OP_FORMAT},
+ {"sort", required_argument, 0, OP_SORTBY},
+ {"rsort", no_argument, 0, OP_REVERSE},
{"from-pkgbuild", no_argument, 0, 'p'},
{"help", no_argument, 0, 'h'},
{"ignore", required_argument, 0, OP_IGNOREPKG},
@@ -1487,6 +1531,33 @@ int parse_options(int argc, char *argv[]) /* {{{ */
case OP_FORMAT:
cfg.format = optarg;
break;
+ case OP_SORTBY:
+ if(!strcmp("name", optarg)) {
+ cfg.sortcb = &aurpkg_cmpname;
+ } else if(!strcmp("votes", optarg)) {
+ cfg.sortcb = &aurpkg_cmpvotes;
+ } else if(!strcmp("version", optarg)) {
+ cfg.sortcb = &aurpkg_cmpver;
+ } else if(!strcmp("maintainer", optarg)) {
+ cfg.sortcb = &aurpkg_cmpmaint;
+ } else if(!strcmp("licence", optarg)) {
+ cfg.sortcb = &aurpkg_cmplic;
+ } else if(!strcmp("votes", optarg)) {
+ cfg.sortcb = &aurpkg_cmpvotes;;
+ } else if(!strcmp("outofdate", optarg)) {
+ cfg.sortcb = &aurpkg_cmpood;;
+ } else if(!strcmp("lastmodified", optarg)) {
+ cfg.sortcb = &aurpkg_cmplastmod;
+ } else if(!strcmp("firstsubmitted", optarg)) {
+ cfg.sortcb = &aurpkg_cmpfirstsub;
+ } else {
+ fprintf(stderr, "error: invalid argument to --sort\n");
+ return 1;
+ }
+ break;
+ case OP_REVERSE:
+ cfg.reverse = SORT_REVERSE;
+ break;
case 'o':
cfg.ignoreood = 1;
break;
@@ -2364,6 +2435,8 @@ void usage(void) /* {{{ */
" --format <string> print package output according to format string\n"
" -o, --ignore-ood skip displaying out of date packages\n"
" --no-ignore-ood the opposite of --ignore-ood\n"
+ " --sort=KEYNAME sort the results according to KEYNAME\n"
+ " --rsort reverse the search results\n"
" --listdelim <delim> change list format delimeter\n"
" -q, --quiet output less\n"
" -v, --verbose output more\n\n");
@@ -2441,6 +2514,8 @@ int main(int argc, char *argv[]) {
cfg.delim = kListDelim;
cfg.logmask = LOG_ERROR|LOG_WARN|LOG_INFO;
cfg.ignoreood = kUnset;
+ cfg.sortcb = &aurpkg_cmpname;
+ cfg.reverse = SORT_FORWARD;
ret = parse_options(argc, argv);
switch(ret) {
Something went wrong with that request. Please try again.