progress.c 1.6 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  1. /*
  2. * dpkg - main program for package management
  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
  10. * published by the Free Software Foundation; either version 2,
  11. * or (at your option) any later version.
  12. *
  13. * This is distributed in the hope that it will be useful, but
  14. * 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 <http://www.gnu.org/licenses/>.
  20. */
  21. #include <config.h>
  22. #include <compat.h>
  23. #include <unistd.h>
  24. #include <stdio.h>
  25. #include <dpkg/i18n.h>
  26. #include "progress.h"
  27. void
  28. progress_init(struct progress *progress, const char *text, int max)
  29. {
  30. progress->text = text;
  31. progress->max = max;
  32. progress->cur = 0;
  33. progress->last_percent = 0;
  34. progress->on_tty = isatty(1);
  35. printf("%s", text);
  36. }
  37. void
  38. progress_step(struct progress *progress)
  39. {
  40. int cur_percent;
  41. if (!progress->on_tty)
  42. return;
  43. progress->cur++;
  44. cur_percent = (progress->cur * 100) / progress->max;
  45. if (cur_percent <= progress->last_percent)
  46. return;
  47. if (cur_percent % 5)
  48. return;
  49. progress->last_percent = cur_percent;
  50. printf("\r%s%d%%", progress->text, cur_percent);
  51. }
  52. void
  53. progress_done(struct progress *progress)
  54. {
  55. if (progress->on_tty)
  56. printf("\r%s", progress->text);
  57. }