progress.c 1.7 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374
  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
  19. * License along with dpkg; if not, write to the Free Software
  20. * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
  21. */
  22. #include <config.h>
  23. #include <compat.h>
  24. #include <dpkg-i18n.h>
  25. #include <unistd.h>
  26. #include <stdio.h>
  27. #include "progress.h"
  28. void
  29. progress_init(struct progress *progress, const char *text, int max)
  30. {
  31. progress->text = text;
  32. progress->max = max;
  33. progress->cur = 0;
  34. progress->last_percent = 0;
  35. progress->on_tty = isatty(1);
  36. printf("%s", text);
  37. }
  38. void
  39. progress_step(struct progress *progress)
  40. {
  41. int cur_percent;
  42. if (!progress->on_tty)
  43. return;
  44. progress->cur++;
  45. cur_percent = (progress->cur * 100) / progress->max;
  46. if (cur_percent <= progress->last_percent)
  47. return;
  48. if (cur_percent % 5)
  49. return;
  50. progress->last_percent = cur_percent;
  51. printf("\r%s%3d%%", progress->text, cur_percent);
  52. }
  53. void
  54. progress_done(struct progress *progress)
  55. {
  56. if (progress->on_tty)
  57. printf("\r%s", progress->text);
  58. }