Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * sim_progress.c --- simple progress meter |
| 3 | */ |
| 4 | |
| 5 | #include "resize2fs.h" |
| 6 | |
| 7 | struct ext2_sim_progress { |
| 8 | FILE *f; |
| 9 | char *label; |
| 10 | int labelwidth; |
| 11 | int barwidth; |
| 12 | __u32 maxdone; |
| 13 | __u32 current; |
| 14 | int shown; |
| 15 | int flags; |
| 16 | }; |
| 17 | |
| 18 | static errcode_t ext2fs_progress_display(ext2_sim_progmeter prog) |
| 19 | { |
| 20 | int i, width; |
| 21 | |
| 22 | fputs(prog->label, prog->f); |
| 23 | width = prog->labelwidth - strlen(prog->label); |
| 24 | while (width-- > 0) |
| 25 | putc(' ', prog->f); |
| 26 | if (prog->labelwidth + prog->barwidth > 80) { |
| 27 | fputs("\n", prog->f); |
| 28 | for (width = prog->labelwidth; width > 0; width--) |
| 29 | putc(' ', prog->f); |
| 30 | } |
| 31 | for (i=0; i < prog->barwidth; i++) |
| 32 | putc('-', prog->f); |
| 33 | for (i=0; i < prog->barwidth; i++) |
| 34 | putc('\b', prog->f); |
| 35 | fflush(prog->f); |
| 36 | return 0; |
| 37 | } |
| 38 | |
| 39 | |
| 40 | void ext2fs_progress_update(ext2_sim_progmeter prog, __u32 current) |
| 41 | { |
| 42 | int old_level, level, num, i; |
| 43 | |
| 44 | level = prog->barwidth * current / prog->maxdone; |
| 45 | old_level = prog->barwidth * prog->current / prog->maxdone; |
| 46 | prog->current = current; |
| 47 | |
| 48 | num = level - old_level; |
| 49 | if (num == 0) |
| 50 | return; |
| 51 | |
| 52 | if (num > 0) { |
| 53 | for (i=0; i < num; i++) |
| 54 | putc('X', prog->f); |
| 55 | } else { |
| 56 | num = -num; |
| 57 | for (i=0; i < num; i++) |
| 58 | putc('\b', prog->f); |
| 59 | for (i=0; i < num; i++) |
| 60 | putc('-', prog->f); |
| 61 | for (i=0; i < num; i++) |
| 62 | putc('\b', prog->f); |
| 63 | } |
| 64 | fflush(prog->f); |
| 65 | } |
| 66 | |
| 67 | errcode_t ext2fs_progress_init(ext2_sim_progmeter *ret_prog, |
| 68 | const char *label, |
| 69 | int labelwidth, int barwidth, |
| 70 | __u32 maxdone, int flags) |
| 71 | { |
Theodore Ts'o | ca8abba | 1998-01-19 14:55:24 +0000 | [diff] [blame^] | 72 | ext2_sim_progmeter prog; |
| 73 | errcode_t retval; |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 74 | |
Theodore Ts'o | ca8abba | 1998-01-19 14:55:24 +0000 | [diff] [blame^] | 75 | retval = ext2fs_get_mem(sizeof(struct ext2_sim_progress), |
| 76 | (void **) &prog); |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 77 | if (!prog) |
Theodore Ts'o | ca8abba | 1998-01-19 14:55:24 +0000 | [diff] [blame^] | 78 | return retval; |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 79 | memset(prog, 0, sizeof(struct ext2_sim_progress)); |
| 80 | |
Theodore Ts'o | ca8abba | 1998-01-19 14:55:24 +0000 | [diff] [blame^] | 81 | retval = ext2fs_get_mem(strlen(label)+1, (void **) &prog->label); |
| 82 | if (retval) { |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 83 | free(prog); |
Theodore Ts'o | ca8abba | 1998-01-19 14:55:24 +0000 | [diff] [blame^] | 84 | return retval; |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 85 | } |
| 86 | strcpy(prog->label, label); |
| 87 | prog->labelwidth = labelwidth; |
| 88 | prog->barwidth = barwidth; |
| 89 | prog->flags = flags; |
| 90 | prog->maxdone = maxdone; |
| 91 | prog->current = 0; |
| 92 | prog->shown = 0; |
| 93 | prog->f = stdout; |
| 94 | |
| 95 | *ret_prog = prog; |
| 96 | |
| 97 | return ext2fs_progress_display(prog); |
| 98 | } |
| 99 | |
| 100 | void ext2fs_progress_close(ext2_sim_progmeter prog) |
| 101 | { |
| 102 | |
| 103 | if (prog->label) |
Theodore Ts'o | ca8abba | 1998-01-19 14:55:24 +0000 | [diff] [blame^] | 104 | ext2fs_free_mem((void **) &prog->label); |
| 105 | ext2fs_free_mem((void **) &prog); |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 106 | printf("\n"); |
| 107 | return; |
| 108 | } |