multiprogress.[ch]: Save and restore `PC' in the render state.
[dvdrip] / multiprogress.c
index efad38d..4254e13 100644 (file)
@@ -69,6 +69,7 @@ int progress_init(struct progress_state *progress)
   if (setupterm(0, fileno(tty->fp), &err) != OK || err < 1) return (-1);
 
   tty->cap.cr = tigetstr("cr");
+  tty->cap.nw = tigetstr("nel");
   tty->cap.up = tigetstr("cuu1");
   tty->cap.ce = tigetstr("el");
   tty->cap.cd = tigetstr("ed");
@@ -90,14 +91,15 @@ int progress_init(struct progress_state *progress)
 
 #elif defined(USE_TERMCAP)
 
-  term = getenv("TERM"); if (!term) return (-1);
-  if (tgetent(tty->termbuf, term) < 1) return (-1);
-
   tty->termbuf = malloc(4096); if (!tty->termbuf) return (-1);
   tty->capbuf = malloc(4096); if (!tty->capbuf) return (-1);
 
+  term = getenv("TERM"); if (!term) return (-1);
+  if (tgetent(tty->termbuf, term) < 1) return (-1);
+
   capcur = tty->capbuf;
   tty->cap.cr = tgetstr("cr", &capcur);
+  tty->cap.nw = tgetstr("nw", &capcur);
   tty->cap.up = tgetstr("up", &capcur);
   tty->cap.ce = tgetstr("ce", &capcur);
   tty->cap.cd = tgetstr("cd", &capcur);
@@ -114,7 +116,7 @@ int progress_init(struct progress_state *progress)
 
   if (tgetflag("ut") > 0) tty->cap.f |= TCF_BCE;
 
-  t = tgetstr("pc", &capcur); PC = t ? *t : 0;
+  t = tgetstr("pc", &capcur); tty->cap.pc = t ? *t : 0;
 
   SETDIM(defwd, "COLUMNS", tgetnum("co"), 80);
   SETDIM(defht, "LINES", tgetnum("li"), 25);
@@ -128,7 +130,9 @@ int progress_init(struct progress_state *progress)
 
 #undef SETDIM
 
-  if (!tty->cap.cr || !tty->cap.up || !tty->cap.ce || !tty->cap.cd)
+  if (!tty->cap.cr) tty->cap.cr = "\r";
+  if (!tty->cap.nw) tty->cap.nw = "\r\n";
+  if (!tty->cap.up || !tty->cap.ce || !tty->cap.cd)
     { fclose(tty->fp); tty->fp = 0; return (-1); }
   if (!tty->cap.af || !tty->cap.ab || !tty->cap.op) tty->cap.op = 0;
   if (!tty->cap.me) tty->cap.mr = tty->cap.md = 0;
@@ -253,6 +257,7 @@ static int setup_render_state(struct progress_state *progress,
 #ifdef USE_TERMCAP
   render->old_bc = BC; BC = 0;
   render->old_up = UP; UP = 0;
+  render->old_pc = PC; PC = tty->cap.pc;
 #endif
 
   if (!ioctl(fileno(tty->fp), TIOCGWINSZ, &wsz))
@@ -273,6 +278,7 @@ static void free_render_state(struct progress_render_state *render)
 #ifdef USE_TERMCAP
   UP = render->old_up;
   BC = render->old_bc;
+  PC = render->old_pc;
 #endif
 }
 
@@ -554,6 +560,7 @@ int progress_clear(struct progress_state *progress)
 int progress_update(struct progress_state *progress)
 {
   struct progress_render_state render;
+  const struct progress_ttyinfo *tty = &progress->tty;
   struct progress_item *item;
   unsigned f = 0;
 #define f_any 1u
@@ -563,12 +570,13 @@ int progress_update(struct progress_state *progress)
   clear_progress(progress, &render, 0);
 
   for (item = progress->items; item; item = item->next) {
-    if (f&f_any) fputs("\r\n", progress->tty.fp);
+    if (f&f_any) put_sequence(tty, tty->cap.nw, 1);
     render.leftsz = render.rightsz = 0;
     render.leftwd = render.rightwd = 0;
     item->render(item, &render); progress->last_lines++; f |= f_any;
     if (progress->last_lines > render.height) break;
   }
+  if (f&f_any) put_sequence(tty, tty->cap.cr, 1);
   free_render_state(&render);
   return (0);
 }