Welcome to little lamb

Code » anopa » commit b667484

start/stop: Tweak "waiting" message

author Olivier Brunel
2015-03-23 17:52:33 UTC
committer Olivier Brunel
2015-04-04 12:47:36 UTC
parent 772b6aea22733e8b90d3ebdd585da2fd33a79899

start/stop: Tweak "waiting" message

Put how long since the service was exec-d and its name for 3s, then
moves on to the next one (if any).

src/anopa/start-stop.c +84 -18
src/anopa/start-stop.h +1 -1
src/include/anopa/service.h +2 -0
src/libanopa/service.c +1 -0

diff --git a/src/anopa/start-stop.c b/src/anopa/start-stop.c
index 8795fd7..3aecceb 100644
--- a/src/anopa/start-stop.c
+++ b/src/anopa/start-stop.c
@@ -107,25 +107,96 @@ draw_password ()
 void
 draw_waiting (int already_drawn)
 {
-    static int n = 0;
-    const char busy[] = "-\\|/";
-    int l_b = (sizeof (busy) / sizeof (*busy)) - 1;
+    static int n = 1;
+    static int tick = -1;
     char buf[UINT_FMT];
     int nb;
+    int si;
+    tain_t ts;
+    int secs;
 
     if (already_drawn)
         aa_is_noflush (AA_OUT, ANSI_CLEAR_BEFORE ANSI_START_LINE);
 
-    nb = nb_wait_longrun + genalloc_len (pid_t, &ga_pid);
-    aa_is_noflush (AA_OUT, "Waiting on ");
-    buf[uint_fmt (buf, nb)] = '\0';
-    aa_is_noflush (AA_OUT, buf);
-    aa_is_noflush (AA_OUT, " service");
-    aa_is_noflush (AA_OUT, ("s... " + ((nb > 1) ? 0 : 1)));
+    nb = genalloc_len (pid_t, &ga_pid) + nb_wait_longrun;
+    if (nb <= 0)
+        return;
+    else if (n > nb)
+    {
+        n = 1;
+        tick = 0;
+    }
+    else if (tick > 1)
+    {
+        tick = 0;
+        if (++n > nb)
+            n = 1;
+    }
+    else
+        ++tick;
+
+    if (n <= genalloc_len (pid_t, &ga_pid))
+        si = list_get (&aa_tmp_list, n - 1);
+    else
+    {
+        int l = genalloc_len (int, &aa_main_list);
+        int i;
+        int j;
+
+        j = n - genalloc_len (pid_t, &ga_pid);
+        for (i = 0; i < l && j < n; ++i)
+            if (aa_service (list_get (&aa_main_list, i))->ft_id > 0)
+                ++j;
+        if (j < n)
+            return;
+        si = list_get (&aa_main_list, i);
+    }
+
+    if (!tain_sub (&ts, &STAMP, &aa_service (si)->ts_exec))
+        secs = -1;
+    else
+    {
+        secs = tain_to_millisecs (&ts);
+        if (secs > 0)
+            secs /= 1000;
+    }
 
-    aa_ib_flush (AA_OUT, busy + n, 1);
-    if (++n >= l_b)
-        n = 0;
+    if (nb > 1 || secs >= 0)
+        aa_is_noflush (AA_OUT, "[");
+
+    if (nb > 1)
+    {
+        buf[uint_fmt (buf, n)] = '\0';
+        aa_is_noflush (AA_OUT, buf);
+        aa_is_noflush (AA_OUT, "/");
+        buf[uint_fmt (buf, nb)] = '\0';
+        aa_is_noflush (AA_OUT, buf);
+        if (secs >= 0)
+            aa_is_noflush (AA_OUT, "; ");
+    }
+
+    if (secs >= 0)
+    {
+        int mins;
+
+        mins = secs / 60;
+        secs -= 60 * mins;
+
+        if (mins > 0)
+        {
+            buf[uint_fmt (buf, mins)] = '\0';
+            aa_is_noflush (AA_OUT, buf);
+            aa_is_noflush (AA_OUT, "m");
+        }
+        buf[uint_fmt (buf, secs)] = '\0';
+        aa_is_noflush (AA_OUT, buf);
+        aa_is_noflush (AA_OUT, "s");
+    }
+
+    if (nb > 1 || secs >= 0)
+        aa_is_noflush (AA_OUT, "] ");
+
+    aa_is_flush (AA_OUT, aa_service_name (aa_service (si)));
 
     draw |= DRAW_CUR_WAITING;
 }
@@ -230,12 +301,7 @@ refresh_draw ()
         draw_waiting ((old_draw & DRAW_CUR_WAITING) && !(draw & DRAW_CUR_PROGRESS));
 
     if (draw & DRAW_CUR_WAITING)
-    {
-        tain_t t;
-
-        tain_from_millisecs (&t, 108);
-        tain_add_g (&iol_deadline, &t);
-    }
+        iol_deadline_addsec (1);
     else
         iol_deadline_addsec (TIMEOUT_SECS);
 }
diff --git a/src/anopa/start-stop.h b/src/anopa/start-stop.h
index ebd73bd..05d4163 100644
--- a/src/anopa/start-stop.h
+++ b/src/anopa/start-stop.h
@@ -9,7 +9,7 @@
 #include <anopa/progress.h>
 #include <anopa/output.h>
 
-#define TIMEOUT_SECS                2
+#define TIMEOUT_SECS                3
 
 #define ANSI_PREV_LINE              "\x1B[F"
 #define ANSI_CLEAR_AFTER            "\x1B[K"
diff --git a/src/include/anopa/service.h b/src/include/anopa/service.h
index 38503c0..e6d4a22 100644
--- a/src/include/anopa/service.h
+++ b/src/include/anopa/service.h
@@ -6,6 +6,7 @@
 #include <skalibs/stralloc.h>
 #include <skalibs/genalloc.h>
 #include <skalibs/uint16.h>
+#include <skalibs/tai.h>
 #include <s6/ftrigr.h>
 #include <anopa/service_status.h>
 
@@ -59,6 +60,7 @@ typedef struct
     genalloc after;
     aa_ls ls;
     aa_service_status st;
+    tain_t ts_exec;
     /* longrun */
     uint16 ft_id;
     int gets_ready;
diff --git a/src/libanopa/service.c b/src/libanopa/service.c
index 491395d..fb7889d 100644
--- a/src/libanopa/service.c
+++ b/src/libanopa/service.c
@@ -510,6 +510,7 @@ aa_exec_service (int si, aa_mode mode)
          * we use common code for aa-start & aa-stop, so... yeah */
         _exec_cb (si, 0, (pid_t) mode);
 
+    tain_copynow (&aa_service (si)->ts_exec);
     if (aa_service (si)->st.type == AA_TYPE_ONESHOT)
         r = _exec_oneshot (si, mode);
     else