summary refs log tree commit diff
path: root/cli/list.c
diff options
context:
space:
mode:
authorRichard Nyberg <rnyberg@murmeldjur.se>2006-10-07 10:21:01 +0000
committerRichard Nyberg <rnyberg@murmeldjur.se>2006-10-07 10:21:01 +0000
commit0eb36d3914b11b6cd7dc77de5380a191d0e6a124 (patch)
tree9098cc433ba957c42fc922a8eb036675d085d31e /cli/list.c
parente652eefa2642213b073298de5e5019486ee728ba (diff)
downloadbtpd-0eb36d3914b11b6cd7dc77de5380a191d0e6a124.tar.gz
btpd-0eb36d3914b11b6cd7dc77de5380a191d0e6a124.zip
btcli list now displays size, ratio and the percentage got of torrents.
It sorts by name instead of number (should probably add options for sorting).
It now also takes torrents to list as optional arguments. Added ratio to
btcli stat display. Changed the help text for both commands. Some code shuffle
and other tweaks.

Diffstat (limited to 'cli/list.c')
-rw-r--r--cli/list.c66
1 files changed, 54 insertions, 12 deletions
diff --git a/cli/list.c b/cli/list.c
index c0888c2..a6ed9d9 100644
--- a/cli/list.c
+++ b/cli/list.c
@@ -7,6 +7,19 @@ usage_list(void)
         "List torrents.\n"
         "\n"
         "Usage: list [-a] [-i]\n"
+        "       list torrent ...\n"
+        "\n"
+        "Arguments:\n"
+        "torrent ...\n"
+        "\tThe torrents to list. Running 'btcli list' without any arguments\n"
+        "\tor options is equivalent to running 'btcli list -ai'.\n"
+        "\n"
+        "Options:\n"
+        "-a\n"
+        "\tList active torrents.\n"
+        "\n"
+        "-i\n"
+        "\tList inactive torrents.\n"
         "\n"
         );
     exit(1);
@@ -16,11 +29,15 @@ struct item {
     unsigned num;
     char *name;
     char st;
+    long long cgot, csize, totup;
     BTPDQ_ENTRY(item) entry;
 };
 
 struct items {
     int count;
+    char **argv;
+    int ntps;
+    struct ipc_torrent *tps;
     BTPDQ_HEAD(item_tq, item) hd;
 };
 
@@ -29,7 +46,7 @@ itm_insert(struct items *itms, struct item *itm)
 {
     struct item *p;
     BTPDQ_FOREACH(p, &itms->hd, entry)
-        if (itm->num < p->num)
+        if (strcmp(itm->name, p->name) < 0)
             break;
     if (p != NULL)
         BTPDQ_INSERT_BEFORE(p, itm, entry);
@@ -42,6 +59,9 @@ list_cb(int obji, enum ipc_err objerr, struct ipc_get_res *res, void *arg)
 {
     struct items *itms = arg;
     struct item *itm = calloc(1, sizeof(*itm));
+    if (objerr != IPC_OK)
+        errx(1, "list failed for '%s' (%s)", itms->argv[obji],
+            ipc_strerror(objerr));
     itms->count++;
     itm->num = (unsigned)res[IPC_TVAL_NUM].v.num;
     itm->st = tstate_char(res[IPC_TVAL_STATE].v.num);
@@ -50,21 +70,22 @@ list_cb(int obji, enum ipc_err objerr, struct ipc_get_res *res, void *arg)
     else
         asprintf(&itm->name, "%.*s", (int)res[IPC_TVAL_NAME].v.str.l,
             res[IPC_TVAL_NAME].v.str.p);
+    itm->totup = res[IPC_TVAL_TOTUP].v.num;
+    itm->cgot = res[IPC_TVAL_CGOT].v.num;
+    itm->csize = res[IPC_TVAL_CSIZE].v.num;
     itm_insert(itms, itm);
 }
 
 void
 print_items(struct items* itms)
 {
-    int n;
     struct item *p;
     BTPDQ_FOREACH(p, &itms->hd, entry) {
-        n = printf("%u: ", p->num);
-        while (n < 7) {
-            putchar(' ');
-            n++;
-        }
-        printf("%c. %s\n", p->st, p->name);
+        printf("%-40.40s %4u %c. ", p->name, p->num, p->st);
+        print_percent(p->cgot, p->csize);
+        print_size(p->csize);
+        print_ratio(p->totup, p->csize);
+        printf("\n");
     }
 }
 
@@ -79,7 +100,9 @@ cmd_list(int argc, char **argv)
     int ch, inactive = 0, active = 0;
     enum ipc_err code;
     enum ipc_twc twc;
-    enum ipc_tval keys[] = { IPC_TVAL_NUM, IPC_TVAL_STATE, IPC_TVAL_NAME };
+    enum ipc_tval keys[] = { IPC_TVAL_NUM, IPC_TVAL_STATE, IPC_TVAL_NAME,
+       IPC_TVAL_TOTUP, IPC_TVAL_CSIZE, IPC_TVAL_CGOT };
+    size_t nkeys = sizeof(keys) / sizeof(keys[0]);
     struct items itms;
     while ((ch = getopt_long(argc, argv, "ai", list_opts, NULL)) != -1) {
         switch (ch) {
@@ -93,7 +116,22 @@ cmd_list(int argc, char **argv)
             usage_list();
         }
     }
+    argc -= optind;
+    argv += optind;
 
+    if (argc > 0) {
+        if (inactive || active)
+            usage_list();
+        itms.tps = malloc(argc * sizeof(*itms.tps));
+        for (itms.ntps = 0; itms.ntps < argc; itms.ntps++) {
+            if (!torrent_spec(argv[itms.ntps], &itms.tps[itms.ntps]))
+                exit(1);
+
+        }
+    } else {
+        itms.ntps = 0;
+        itms.tps = NULL;
+    }
     if (inactive == active)
         twc = IPC_TWC_ALL;
     else if (inactive)
@@ -102,11 +140,15 @@ cmd_list(int argc, char **argv)
         twc = IPC_TWC_ACTIVE;
 
     btpd_connect();
-    printf("NUM    ST NAME\n");
     itms.count = 0;
+    itms.argv = argv;
     BTPDQ_INIT(&itms.hd);
-    if ((code = btpd_tget_wc(ipc, twc, keys, 3, list_cb, &itms)) != IPC_OK)
+    if (itms.tps == NULL)
+        code = btpd_tget_wc(ipc, twc, keys, nkeys, list_cb, &itms);
+    else
+        code = btpd_tget(ipc, itms.tps, itms.ntps, keys, nkeys, list_cb, &itms);
+    if (code != IPC_OK)
         errx(1, "%s", ipc_strerror(code));
+    printf("%-40.40s  NUM ST   HAVE    SIZE   RATIO\n", "NAME");
     print_items(&itms);
-    printf("Listed %d torrent%s.\n", itms.count, itms.count == 1 ? "" : "s");
 }