Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 1 | /* |
| 2 | * sim_progress.c --- simple progress meter |
Theodore Ts'o | 0cee8a5 | 2000-04-06 21:38:34 +0000 | [diff] [blame] | 3 | * |
| 4 | * Copyright (C) 1997, 1998 by Theodore Ts'o and |
| 5 | * PowerQuest, Inc. |
| 6 | * |
| 7 | * Copyright (C) 1999, 2000 by Theosore Ts'o |
| 8 | * |
| 9 | * %Begin-Header% |
| 10 | * This file may be redistributed under the terms of the GNU Public |
| 11 | * License. |
| 12 | * %End-Header% |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 13 | */ |
| 14 | |
| 15 | #include "resize2fs.h" |
| 16 | |
| 17 | struct ext2_sim_progress { |
| 18 | FILE *f; |
| 19 | char *label; |
| 20 | int labelwidth; |
| 21 | int barwidth; |
| 22 | __u32 maxdone; |
| 23 | __u32 current; |
| 24 | int shown; |
| 25 | int flags; |
| 26 | }; |
| 27 | |
| 28 | static errcode_t ext2fs_progress_display(ext2_sim_progmeter prog) |
| 29 | { |
| 30 | int i, width; |
| 31 | |
| 32 | fputs(prog->label, prog->f); |
| 33 | width = prog->labelwidth - strlen(prog->label); |
| 34 | while (width-- > 0) |
| 35 | putc(' ', prog->f); |
| 36 | if (prog->labelwidth + prog->barwidth > 80) { |
| 37 | fputs("\n", prog->f); |
| 38 | for (width = prog->labelwidth; width > 0; width--) |
| 39 | putc(' ', prog->f); |
| 40 | } |
| 41 | for (i=0; i < prog->barwidth; i++) |
| 42 | putc('-', prog->f); |
| 43 | for (i=0; i < prog->barwidth; i++) |
| 44 | putc('\b', prog->f); |
| 45 | fflush(prog->f); |
| 46 | return 0; |
| 47 | } |
| 48 | |
| 49 | |
| 50 | void ext2fs_progress_update(ext2_sim_progmeter prog, __u32 current) |
| 51 | { |
| 52 | int old_level, level, num, i; |
| 53 | |
| 54 | level = prog->barwidth * current / prog->maxdone; |
| 55 | old_level = prog->barwidth * prog->current / prog->maxdone; |
| 56 | prog->current = current; |
| 57 | |
| 58 | num = level - old_level; |
| 59 | if (num == 0) |
| 60 | return; |
| 61 | |
| 62 | if (num > 0) { |
| 63 | for (i=0; i < num; i++) |
| 64 | putc('X', prog->f); |
| 65 | } else { |
| 66 | num = -num; |
| 67 | for (i=0; i < num; i++) |
| 68 | putc('\b', prog->f); |
| 69 | for (i=0; i < num; i++) |
| 70 | putc('-', prog->f); |
| 71 | for (i=0; i < num; i++) |
| 72 | putc('\b', prog->f); |
| 73 | } |
| 74 | fflush(prog->f); |
| 75 | } |
| 76 | |
| 77 | errcode_t ext2fs_progress_init(ext2_sim_progmeter *ret_prog, |
| 78 | const char *label, |
| 79 | int labelwidth, int barwidth, |
| 80 | __u32 maxdone, int flags) |
| 81 | { |
Theodore Ts'o | ca8abba | 1998-01-19 14:55:24 +0000 | [diff] [blame] | 82 | ext2_sim_progmeter prog; |
| 83 | errcode_t retval; |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 84 | |
Theodore Ts'o | ca8abba | 1998-01-19 14:55:24 +0000 | [diff] [blame] | 85 | retval = ext2fs_get_mem(sizeof(struct ext2_sim_progress), |
| 86 | (void **) &prog); |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 87 | if (!prog) |
Theodore Ts'o | ca8abba | 1998-01-19 14:55:24 +0000 | [diff] [blame] | 88 | return retval; |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 89 | memset(prog, 0, sizeof(struct ext2_sim_progress)); |
| 90 | |
Theodore Ts'o | ca8abba | 1998-01-19 14:55:24 +0000 | [diff] [blame] | 91 | retval = ext2fs_get_mem(strlen(label)+1, (void **) &prog->label); |
| 92 | if (retval) { |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 93 | free(prog); |
Theodore Ts'o | ca8abba | 1998-01-19 14:55:24 +0000 | [diff] [blame] | 94 | return retval; |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 95 | } |
| 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; |
| 104 | |
| 105 | *ret_prog = prog; |
| 106 | |
| 107 | return ext2fs_progress_display(prog); |
| 108 | } |
| 109 | |
| 110 | void ext2fs_progress_close(ext2_sim_progmeter prog) |
| 111 | { |
| 112 | |
| 113 | if (prog->label) |
Theodore Ts'o | ca8abba | 1998-01-19 14:55:24 +0000 | [diff] [blame] | 114 | ext2fs_free_mem((void **) &prog->label); |
| 115 | ext2fs_free_mem((void **) &prog); |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 116 | printf("\n"); |
| 117 | return; |
| 118 | } |