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 |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 8 | * |
Theodore Ts'o | 0cee8a5 | 2000-04-06 21:38:34 +0000 | [diff] [blame] | 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 | |
Theodore Ts'o | d1154eb | 2011-09-18 17:34:37 -0400 | [diff] [blame] | 15 | #include "config.h" |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 16 | #include "resize2fs.h" |
| 17 | |
| 18 | struct 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 | |
| 29 | static 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 | |
| 51 | void 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'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 62 | |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 63 | 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 | |
| 78 | errcode_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'o | ca8abba | 1998-01-19 14:55:24 +0000 | [diff] [blame] | 83 | ext2_sim_progmeter prog; |
| 84 | errcode_t retval; |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 85 | |
Theodore Ts'o | c4e3d3f | 2003-08-01 09:41:07 -0400 | [diff] [blame] | 86 | retval = ext2fs_get_mem(sizeof(struct ext2_sim_progress), &prog); |
Manish Katiyar | 520a177 | 2008-10-01 19:42:57 -0400 | [diff] [blame] | 87 | if (retval) |
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 | |
Andreas Dilger | de8f3a7 | 2007-05-25 11:18:11 -0400 | [diff] [blame] | 91 | retval = ext2fs_get_mem(strlen(label)+1, &prog->label); |
Theodore Ts'o | ca8abba | 1998-01-19 14:55:24 +0000 | [diff] [blame] | 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; |
Theodore Ts'o | efc6f62 | 2008-08-27 23:07:54 -0400 | [diff] [blame] | 104 | |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 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 | c4e3d3f | 2003-08-01 09:41:07 -0400 | [diff] [blame] | 114 | ext2fs_free_mem(&prog->label); |
| 115 | ext2fs_free_mem(&prog); |
Theodore Ts'o | c762c8e | 1997-06-17 03:52:12 +0000 | [diff] [blame] | 116 | printf("\n"); |
| 117 | return; |
| 118 | } |