File: statusbar.c

package info (click to toggle)
diskscan 0.21-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid, trixie
  • size: 1,656 kB
  • sloc: ansic: 11,136; python: 338; xml: 138; sh: 41; makefile: 34
file content (107 lines) | stat: -rw-r--r-- 2,472 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
/**
* \file
* \author Trevor Fountain
* \author Johannes Buchner
* \author Erik Garrison
* \date 2010-2014
* \copyright BSD 3-Clause
*
* statusbar -- a C class (by convention) for displaying progress
* on the command line (to stderr).
*/
#include "statusbar.h"

statusbar *statusbar_new_with_format(const char *label, const char *format)
{
  statusbar *new = malloc(sizeof(statusbar));
  new->label = label;
  new->start_time = time(0);
  new->format_length = strlen(format);
  new->format = malloc( sizeof(char) * (new->format_length + 1) );
  strncpy(new->format, format, new->format_length);
  new->format_index = 0;
  new->last_printed = 0;

  return new;
}

statusbar *statusbar_new(const char *label)
{
  return statusbar_new_with_format(label, "-\\|/");
}

void statusbar_free(statusbar *bar)
{
  // We malloc'd a string, so let's be sure to free it...
  free(bar->format);
  // ...before we free the struct itself.
  free(bar);

  return;
}

void statusbar_inc(statusbar *bar)
{
  bar->format_index++;
  if (bar->format_index >= bar->format_length) {
    bar->format_index = 0;
  }
  statusbar_draw(bar);

  return;
}

void statusbar_draw(statusbar *bar)
{
  // Erase the last draw. If anything else has been printed to stderr,
  // things are going to look mighty interesting...
  for(int i=0; i < bar->last_printed; i++) {
    fprintf(stderr,"\b");
  }

  fprintf(
        stderr,
        "%s: %c%n",
        bar->label,
        bar->format[bar->format_index],
        &(bar->last_printed)
    );

  return;
}

void statusbar_finish(statusbar *bar)
{
  // Draw one more time, with the actual time to completion.
  unsigned int offset = time(0) - (bar->start_time);

  // Convert the time to display into HHH:MM:SS
  unsigned int h = offset/3600;
  offset -= h*3600;
  unsigned int m = offset/60;
  offset -= m*60;
  unsigned int s = offset;

  // Erase the last draw
  for(int i=0; i < bar->last_printed; i++) {
    fprintf(stderr,"\b");
  }

  // Calculate number of spaces for right-justified time to completion
  fprintf(stderr,"%s: %3d:%02d:%02d%n",bar->label,h,m,s,&(bar->last_printed));
  for(int i=0; i < bar->last_printed; i++) {
    fprintf(stderr,"\b");
  }

  // Print right-justified
  fprintf(stderr,"%s: ",bar->label);
  for(int i=0; i < (80 - (bar->last_printed)); i++) {
    fprintf(stderr," ");
  }
  fprintf(stderr,"%3d:%02d:%02d\n",h,m,s);

  // We've finished with this statusbar, so go ahead and free it.
  statusbar_free(bar);

  return;
}