blob: d4206ba2814afee7717aed116ffd5eaaa7e38d0b [file] [log] [blame]
Theodore Ts'oc762c8e1997-06-17 03:52:12 +00001/*
2 * sim_progress.c --- simple progress meter
Theodore Ts'o0cee8a52000-04-06 21:38:34 +00003 *
4 * Copyright (C) 1997, 1998 by Theodore Ts'o and
5 * PowerQuest, Inc.
6 *
7 * Copyright (C) 1999, 2000 by Theosore Ts'o
Theodore Ts'oefc6f622008-08-27 23:07:54 -04008 *
Theodore Ts'o0cee8a52000-04-06 21:38:34 +00009 * %Begin-Header%
10 * This file may be redistributed under the terms of the GNU Public
11 * License.
12 * %End-Header%
Theodore Ts'oc762c8e1997-06-17 03:52:12 +000013 */
14
Theodore Ts'od1154eb2011-09-18 17:34:37 -040015#include "config.h"
Theodore Ts'oc762c8e1997-06-17 03:52:12 +000016#include "resize2fs.h"
17
18struct ext2_sim_progress {
19 FILE *f;
20 char *label;
21 int labelwidth;
22 int barwidth;
23 __u32 maxdone;
24 __u32 current;
25 int shown;
26 int flags;
27};
28
29static errcode_t ext2fs_progress_display(ext2_sim_progmeter prog)
30{
31 int i, width;
32
33 fputs(prog->label, prog->f);
34 width = prog->labelwidth - strlen(prog->label);
35 while (width-- > 0)
36 putc(' ', prog->f);
37 if (prog->labelwidth + prog->barwidth > 80) {
38 fputs("\n", prog->f);
39 for (width = prog->labelwidth; width > 0; width--)
40 putc(' ', prog->f);
41 }
42 for (i=0; i < prog->barwidth; i++)
43 putc('-', prog->f);
44 for (i=0; i < prog->barwidth; i++)
45 putc('\b', prog->f);
46 fflush(prog->f);
47 return 0;
48}
49
50
51void ext2fs_progress_update(ext2_sim_progmeter prog, __u32 current)
52{
53 int old_level, level, num, i;
54
55 level = prog->barwidth * current / prog->maxdone;
56 old_level = prog->barwidth * prog->current / prog->maxdone;
57 prog->current = current;
58
59 num = level - old_level;
60 if (num == 0)
61 return;
Theodore Ts'oefc6f622008-08-27 23:07:54 -040062
Theodore Ts'oc762c8e1997-06-17 03:52:12 +000063 if (num > 0) {
64 for (i=0; i < num; i++)
65 putc('X', prog->f);
66 } else {
67 num = -num;
68 for (i=0; i < num; i++)
69 putc('\b', prog->f);
70 for (i=0; i < num; i++)
71 putc('-', prog->f);
72 for (i=0; i < num; i++)
73 putc('\b', prog->f);
74 }
75 fflush(prog->f);
76}
77
78errcode_t ext2fs_progress_init(ext2_sim_progmeter *ret_prog,
79 const char *label,
80 int labelwidth, int barwidth,
81 __u32 maxdone, int flags)
82{
Theodore Ts'oca8abba1998-01-19 14:55:24 +000083 ext2_sim_progmeter prog;
84 errcode_t retval;
Theodore Ts'oc762c8e1997-06-17 03:52:12 +000085
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -040086 retval = ext2fs_get_mem(sizeof(struct ext2_sim_progress), &prog);
Manish Katiyar520a1772008-10-01 19:42:57 -040087 if (retval)
Theodore Ts'oca8abba1998-01-19 14:55:24 +000088 return retval;
Theodore Ts'oc762c8e1997-06-17 03:52:12 +000089 memset(prog, 0, sizeof(struct ext2_sim_progress));
90
Andreas Dilgerde8f3a72007-05-25 11:18:11 -040091 retval = ext2fs_get_mem(strlen(label)+1, &prog->label);
Theodore Ts'oca8abba1998-01-19 14:55:24 +000092 if (retval) {
Theodore Ts'oc762c8e1997-06-17 03:52:12 +000093 free(prog);
Theodore Ts'oca8abba1998-01-19 14:55:24 +000094 return retval;
Theodore Ts'oc762c8e1997-06-17 03:52:12 +000095 }
96 strcpy(prog->label, label);
97 prog->labelwidth = labelwidth;
98 prog->barwidth = barwidth;
99 prog->flags = flags;
100 prog->maxdone = maxdone;
101 prog->current = 0;
102 prog->shown = 0;
103 prog->f = stdout;
Theodore Ts'oefc6f622008-08-27 23:07:54 -0400104
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000105 *ret_prog = prog;
106
107 return ext2fs_progress_display(prog);
108}
109
110void ext2fs_progress_close(ext2_sim_progmeter prog)
111{
112
113 if (prog->label)
Theodore Ts'oc4e3d3f2003-08-01 09:41:07 -0400114 ext2fs_free_mem(&prog->label);
115 ext2fs_free_mem(&prog);
Theodore Ts'oc762c8e1997-06-17 03:52:12 +0000116 printf("\n");
117 return;
118}