more compact progress display

Each progress can be on a single line instead of two.

[sp: Changed "Checking files out" to "Checking out files" at
     Johannes Sixt's suggestion as it better explains the
	 action that is taking place]

Signed-off-by: Nicolas Pitre <nico@cam.org>
Signed-off-by: Shawn O. Pearce <spearce@spearce.org>
This commit is contained in:
Nicolas Pitre 2007-10-16 21:55:45 -04:00 committed by Shawn O. Pearce
parent 207f1a75e7
commit 42e18fbf5f
6 changed files with 39 additions and 50 deletions

View File

@ -606,7 +606,7 @@ static void write_pack_file(void)
uint32_t nr_remaining = nr_result; uint32_t nr_remaining = nr_result;
if (do_progress) if (do_progress)
start_progress(&progress_state, "Writing %u objects...", "", nr_result); start_progress(&progress_state, "Writing objects", nr_result);
written_list = xmalloc(nr_objects * sizeof(struct object_entry *)); written_list = xmalloc(nr_objects * sizeof(struct object_entry *));
do { do {
@ -1717,9 +1717,8 @@ static void prepare_pack(int window, int depth)
if (nr_deltas) { if (nr_deltas) {
unsigned nr_done = 0; unsigned nr_done = 0;
if (progress) if (progress)
start_progress(&progress_state, start_progress(&progress_state, "Deltifying objects",
"Deltifying %u objects...", "", nr_deltas);
nr_deltas);
qsort(delta_list, n, sizeof(*delta_list), type_size_sort); qsort(delta_list, n, sizeof(*delta_list), type_size_sort);
ll_find_deltas(delta_list, n, window+1, depth, &nr_done); ll_find_deltas(delta_list, n, window+1, depth, &nr_done);
if (progress) if (progress)
@ -2135,23 +2134,18 @@ int cmd_pack_objects(int argc, const char **argv, const char *prefix)
prepare_packed_git(); prepare_packed_git();
if (progress) if (progress)
start_progress(&progress_state, "Generating pack...", start_progress(&progress_state, "Counting objects", 0);
"Counting objects: ", 0);
if (!use_internal_rev_list) if (!use_internal_rev_list)
read_object_list_from_stdin(); read_object_list_from_stdin();
else { else {
rp_av[rp_ac] = NULL; rp_av[rp_ac] = NULL;
get_object_list(rp_ac, rp_av); get_object_list(rp_ac, rp_av);
} }
if (progress) { if (progress)
stop_progress(&progress_state); stop_progress(&progress_state);
fprintf(stderr, "Done counting %u objects.\n", nr_objects);
}
if (non_empty && !nr_result) if (non_empty && !nr_result)
return 0; return 0;
if (progress && (nr_objects != nr_result))
fprintf(stderr, "Result has %u objects.\n", nr_result);
if (nr_result) if (nr_result)
prepare_pack(window, depth); prepare_pack(window, depth);
write_pack_file(); write_pack_file();

View File

@ -322,7 +322,7 @@ static void unpack_all(void)
use(sizeof(struct pack_header)); use(sizeof(struct pack_header));
if (!quiet) if (!quiet)
start_progress(&progress, "Unpacking %u objects...", "", nr_objects); start_progress(&progress, "Unpacking objects", nr_objects);
obj_list = xmalloc(nr_objects * sizeof(*obj_list)); obj_list = xmalloc(nr_objects * sizeof(*obj_list));
for (i = 0; i < nr_objects; i++) { for (i = 0; i < nr_objects; i++) {
unpack_one(i); unpack_one(i);

View File

@ -406,7 +406,7 @@ static void parse_pack_objects(unsigned char *sha1)
* - remember base (SHA1 or offset) for all deltas. * - remember base (SHA1 or offset) for all deltas.
*/ */
if (verbose) if (verbose)
start_progress(&progress, "Indexing %u objects...", "", nr_objects); start_progress(&progress, "Indexing objects", nr_objects);
for (i = 0; i < nr_objects; i++) { for (i = 0; i < nr_objects; i++) {
struct object_entry *obj = &objects[i]; struct object_entry *obj = &objects[i];
data = unpack_raw_entry(obj, &delta->base); data = unpack_raw_entry(obj, &delta->base);
@ -455,7 +455,7 @@ static void parse_pack_objects(unsigned char *sha1)
* for some more deltas. * for some more deltas.
*/ */
if (verbose) if (verbose)
start_progress(&progress, "Resolving %u deltas...", "", nr_deltas); start_progress(&progress, "Resolving deltas", nr_deltas);
for (i = 0; i < nr_objects; i++) { for (i = 0; i < nr_objects; i++) {
struct object_entry *obj = &objects[i]; struct object_entry *obj = &objects[i];
union delta_base base; union delta_base base;

View File

@ -35,10 +35,11 @@ static void clear_progress_signal(void)
progress_update = 0; progress_update = 0;
} }
int display_progress(struct progress *progress, unsigned n) static int display(struct progress *progress, unsigned n, int done)
{ {
char *eol;
if (progress->delay) { if (progress->delay) {
char buf[80];
if (!progress_update || --progress->delay) if (!progress_update || --progress->delay)
return 0; return 0;
if (progress->total) { if (progress->total) {
@ -51,60 +52,56 @@ int display_progress(struct progress *progress, unsigned n)
return 0; return 0;
} }
} }
if (snprintf(buf, sizeof(buf),
progress->delayed_title, progress->total))
fprintf(stderr, "%s\n", buf);
} }
progress->last_value = n;
eol = done ? ", done. \n" : " \r";
if (progress->total) { if (progress->total) {
unsigned percent = n * 100 / progress->total; unsigned percent = n * 100 / progress->total;
if (percent != progress->last_percent || progress_update) { if (percent != progress->last_percent || progress_update) {
progress->last_percent = percent; progress->last_percent = percent;
fprintf(stderr, "%s%4u%% (%u/%u) done\r", fprintf(stderr, "%s: %3u%% (%u/%u)%s", progress->title,
progress->prefix, percent, n, progress->total); percent, n, progress->total, eol);
progress_update = 0; progress_update = 0;
progress->need_lf = 1;
return 1; return 1;
} }
} else if (progress_update) { } else if (progress_update) {
fprintf(stderr, "%s%u\r", progress->prefix, n); fprintf(stderr, "%s: %u%s", progress->title, n, eol);
progress_update = 0; progress_update = 0;
progress->need_lf = 1;
return 1; return 1;
} }
return 0; return 0;
} }
void start_progress(struct progress *progress, const char *title, int display_progress(struct progress *progress, unsigned n)
const char *prefix, unsigned total)
{ {
char buf[80]; return display(progress, n, 0);
progress->prefix = prefix;
progress->total = total;
progress->last_percent = -1;
progress->delay = 0;
progress->need_lf = 0;
if (snprintf(buf, sizeof(buf), title, total))
fprintf(stderr, "%s\n", buf);
set_progress_signal();
} }
void start_progress_delay(struct progress *progress, const char *title, void start_progress_delay(struct progress *progress, const char *title,
const char *prefix, unsigned total, unsigned total, unsigned percent_treshold, unsigned delay)
unsigned percent_treshold, unsigned delay)
{ {
progress->prefix = prefix; progress->title = title;
progress->total = total; progress->total = total;
progress->last_value = -1;
progress->last_percent = -1; progress->last_percent = -1;
progress->delayed_percent_treshold = percent_treshold; progress->delayed_percent_treshold = percent_treshold;
progress->delayed_title = title;
progress->delay = delay; progress->delay = delay;
progress->need_lf = 0;
set_progress_signal(); set_progress_signal();
} }
void start_progress(struct progress *progress, const char *title, unsigned total)
{
start_progress_delay(progress, title, total, 0, 0);
}
void stop_progress(struct progress *progress) void stop_progress(struct progress *progress)
{ {
if (progress->last_value != -1) {
/* Force the last update */
progress_update = 1;
display(progress, progress->last_value, 1);
}
clear_progress_signal(); clear_progress_signal();
if (progress->need_lf)
fputc('\n', stderr);
} }

View File

@ -2,21 +2,19 @@
#define PROGRESS_H #define PROGRESS_H
struct progress { struct progress {
const char *prefix; const char *title;
int last_value;
unsigned total; unsigned total;
unsigned last_percent; unsigned last_percent;
unsigned delay; unsigned delay;
unsigned delayed_percent_treshold; unsigned delayed_percent_treshold;
const char *delayed_title;
int need_lf;
}; };
int display_progress(struct progress *progress, unsigned n); int display_progress(struct progress *progress, unsigned n);
void start_progress(struct progress *progress, const char *title, void start_progress(struct progress *progress, const char *title,
const char *prefix, unsigned total); unsigned total);
void start_progress_delay(struct progress *progress, const char *title, void start_progress_delay(struct progress *progress, const char *title,
const char *prefix, unsigned total, unsigned total, unsigned percent_treshold, unsigned delay);
unsigned percent_treshold, unsigned delay);
void stop_progress(struct progress *progress); void stop_progress(struct progress *progress);
#endif #endif

View File

@ -307,8 +307,8 @@ static void check_updates(struct cache_entry **src, int nr,
total++; total++;
} }
start_progress_delay(&progress, "Checking %u files out...", start_progress_delay(&progress, "Checking out files",
"", total, 50, 2); total, 50, 2);
cnt = 0; cnt = 0;
} }