Branch data Line data Source code
1 : : /* 2 : : * libdpkg - Debian packaging suite library routines 3 : : * progress.c - generic progress reporting 4 : : * 5 : : * Copyright © 2009 Romain Francoise <rfrancoise@debian.org> 6 : : * Copyright © 2009 Guillem Jover <guillem@debian.org> 7 : : * 8 : : * This is free software; you can redistribute it and/or modify 9 : : * it under the terms of the GNU General Public License as published by 10 : : * the Free Software Foundation; either version 2 of the License, or 11 : : * (at your option) any later version. 12 : : * 13 : : * This is distributed in the hope that it will be useful, 14 : : * but WITHOUT ANY WARRANTY; without even the implied warranty of 15 : : * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 16 : : * GNU General Public License for more details. 17 : : * 18 : : * You should have received a copy of the GNU General Public License 19 : : * along with this program. If not, see <https://www.gnu.org/licenses/>. 20 : : */ 21 : : 22 : : #include <config.h> 23 : : #include <compat.h> 24 : : 25 : : #include <unistd.h> 26 : : #include <stdio.h> 27 : : 28 : : #include <dpkg/i18n.h> 29 : : 30 : : #include "progress.h" 31 : : 32 : : void 33 : 0 : progress_init(struct progress *progress, const char *text, int max) 34 : : { 35 : 0 : progress->text = text; 36 : 0 : progress->max = max; 37 : 0 : progress->cur = 0; 38 : 0 : progress->last_percent = 0; 39 : : 40 : 0 : progress->on_tty = isatty(1); 41 : : 42 [ # # ]: 0 : if (progress->on_tty) 43 : 0 : printf("%s\r", text); 44 : : else 45 : 0 : printf("%s", text); 46 : 0 : } 47 : : 48 : : void 49 : 0 : progress_step(struct progress *progress) 50 : : { 51 : : int cur_percent; 52 : : 53 [ # # ]: 0 : if (!progress->on_tty) 54 : 0 : return; 55 : : 56 : 0 : progress->cur++; 57 : : 58 : 0 : cur_percent = (progress->cur * 100) / progress->max; 59 [ # # ]: 0 : if (cur_percent <= progress->last_percent) 60 : 0 : return; 61 [ # # ]: 0 : if (cur_percent % 5) 62 : 0 : return; 63 : : 64 : 0 : progress->last_percent = cur_percent; 65 : : 66 : 0 : printf("%s%d%%\r", progress->text, cur_percent); 67 : : } 68 : : 69 : : void 70 : 0 : progress_done(struct progress *progress) 71 : : { 72 [ # # ]: 0 : if (progress->on_tty) 73 : 0 : printf("%s", progress->text); 74 : 0 : }