File: pk-progress-bar.c

package info (click to toggle)
packagekit 1.3.4-3
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 14,824 kB
  • sloc: ansic: 56,491; cpp: 15,652; xml: 5,532; python: 4,932; sh: 316; perl: 60; makefile: 56
file content (553 lines) | stat: -rw-r--r-- 14,693 bytes parent folder | download
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
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
/* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
 *
 * Copyright (C) 2008-2009 Richard Hughes <richard@hughsie.com>
 * Copyright (C) 2024-2026 Matthias Klumpp <matthias@tenstral.net>
 *
 * Licensed under the GNU Lesser General Public License Version 2.1
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2.1 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the Free Software
 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA  02110-1301 USA
 */

#include "config.h"

#include <glib.h>
#include <locale.h>
#include <langinfo.h>
#include <string.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/ioctl.h>

#include "pk-progress-bar.h"
#include "pk-console-private.h"

typedef struct {
	guint			 position;
	gboolean		 move_forward;
} PkProgressBarPulseState;

struct PkProgressBarPrivate
{
	guint			 size;
	gint			 percentage;
	guint			 timer_id;
	PkProgressBarPulseState	 pulse_state;
	gint			 tty_fd;
	gchar			*old_start_text;
	guint			 area_width;
	gboolean		 use_unicode;

	gboolean		 allow_restart;
};

#define PK_PROGRESS_BAR_PERCENTAGE_INVALID	101
#define PK_PROGRESS_BAR_PULSE_TIMEOUT		40 /* ms */
#define PK_PROGRESS_BAR_DEFAULT_SIZE		30

/* space for percentage text (e.g., " 100%") or spacing */
static const guint PK_PERCENT_TEXT_WIDTH = 5;

G_DEFINE_TYPE_WITH_PRIVATE (PkProgressBar, pk_progress_bar, G_TYPE_OBJECT)
#define GET_PRIVATE(o) (pk_progress_bar_get_instance_private (o))

/**
 * pk_progress_bar_get_draw_area_width:
 *
 * Get our drawing area width in characters.
 */
static guint
pk_progress_bar_get_draw_area_width (PkProgressBar *self)
{
	PkProgressBarPrivate *priv = GET_PRIVATE(self);
	struct winsize w;

	if (priv->tty_fd < 0)
		return 80;

	if (ioctl (priv->tty_fd, TIOCGWINSZ, &w) == 0 && w.ws_col > 0) {
		/* we may have very long terminals, so cap the area to a reasonable maximum size */
		return w.ws_col >= 110 ? 110 : w.ws_col;
	}

	return 80;
}

/**
 * pk_progress_bar_console:
 */
static void
pk_progress_bar_console (PkProgressBar *self, const gchar *tmp)
{
	PkProgressBarPrivate *priv = GET_PRIVATE(self);
	size_t count;
	ssize_t written;

	if (priv->tty_fd < 0)
		return;

	count = strlen (tmp);
	if (count == 0)
		return;

	written = write (priv->tty_fd, tmp, count);
	if (written < 0 || (size_t)written != count) {
		g_warning ("Only wrote %" G_GSSIZE_FORMAT
			   " of %" G_GSSIZE_FORMAT " bytes",
			   written, (gssize)count);
	}
}

/**
 * pk_progress_bar_set_size:
 * @progress_bar: a valid #PkProgressBar instance
 * @size: width of progress bar in characters.
 *
 * Set the width of the progress bar.
 *
 * Return value: %TRUE if changed
 */
gboolean
pk_progress_bar_set_size (PkProgressBar *progress_bar, guint size)
{
	PkProgressBarPrivate *priv = GET_PRIVATE(progress_bar);

	g_return_val_if_fail (PK_IS_PROGRESS_BAR (progress_bar), FALSE);
	g_return_val_if_fail (size > 0 && size < G_MAXINT, FALSE);

	priv->size = size;
	return TRUE;
}

/**
 * pk_progress_bar_draw:
 */
static gboolean
pk_progress_bar_draw (PkProgressBar *self, gint percentage)
{
	PkProgressBarPrivate *priv = GET_PRIVATE(self);
	g_autoptr(GString) str = NULL;
	guint available_width;
	guint bar_width;
	guint text_width;

	/* no value yet */
	if (percentage == G_MININT)
		return FALSE;

	/* clamp percentage */
	if (percentage < 0)
		percentage = 0;
	if (percentage > 100)
		percentage = 100;

	str = g_string_sized_new (256);

	/* move cursor to start of line and clear it */
	g_string_append (str, "\r\033[K");

	/* calculate available width for text + bar */
	available_width = priv->area_width > PK_PERCENT_TEXT_WIDTH ? priv->area_width - PK_PERCENT_TEXT_WIDTH : priv->area_width;

	/* determine bar width (use configured size or auto-calculate) */
	bar_width = priv->size;
	if (bar_width > available_width / 2)
		bar_width = available_width / 2;
	if (bar_width < 10)
		bar_width = 10;

	/* text width is what's left */
	if (available_width > bar_width + 3)
		text_width = available_width - bar_width - 3; /* -3 for space and brackets */
	else
		text_width = 0;

	/* truncate and pad the current text to exact width */
	if (priv->old_start_text != NULL && text_width > 0) {
		g_autofree gchar *truncated = NULL;
		g_autofree gchar *display_text = NULL;
		truncated = pk_console_text_truncate (priv->old_start_text, text_width);
		display_text = pk_console_strpad (truncated, text_width);
		g_string_append (str, display_text);
	} else {
		gsize old_len = str->len;
		g_string_set_size(str, old_len + text_width);
		memset(str->str + old_len, ' ', text_width);
		str->str[str->len] = '\0';
	}

	if (priv->use_unicode) {
		/* use Unicode block characters: █ = full, ▓ = 3/4, ▒ = 1/2, ░ = 1/4 */
		guint filled_chars = (percentage * bar_width) / 100;
		guint remainder = (percentage * bar_width) % 100;

		g_string_append (str, " [");

		/* full blocks */
		for (guint i = 0; i < filled_chars; i++)
			g_string_append (str, "█");

		/* partial block based on remainder */
		if (filled_chars < bar_width) {
			if (remainder >= 75)
				g_string_append (str, "▓");
			else if (remainder >= 50)
				g_string_append (str, "▒");
			else if (remainder >= 25)
				g_string_append (str, "░");
			else
				g_string_append (str, " ");
			filled_chars++;
		}

		/* empty space */
		for (guint i = filled_chars; i < bar_width; i++)
			g_string_append (str, " ");

		g_string_append (str, "]");
	} else {
		/* fallback to ASCII */
		guint filled = (percentage * bar_width) / 100;

		g_string_append (str, " [");
		for (guint i = 0; i < filled; i++)
			g_string_append (str, "=");
		for (guint i = filled; i < bar_width; i++)
			g_string_append (str, " ");
		g_string_append (str, "]");
	}

	/* percentage text */
	g_string_append_printf (str, " %3d%%", percentage);

	pk_progress_bar_console (self, str->str);

	return TRUE;
}

/**
 * pk_progress_bar_pulse_bar:
 */
static gboolean
pk_progress_bar_pulse_bar (PkProgressBar *self)
{
	PkProgressBarPrivate *priv = GET_PRIVATE(self);
	g_autoptr(GString) str = NULL;
	guint available;
	guint bar_width;
	guint text_width;

	/* update position */
	if (priv->pulse_state.move_forward) {
		if (priv->pulse_state.position >= priv->size - 2)
			priv->pulse_state.move_forward = FALSE;
		else
			priv->pulse_state.position++;
	} else {
		if (priv->pulse_state.position <= 1)
			priv->pulse_state.move_forward = TRUE;
		else
			priv->pulse_state.position--;
	}

	str = g_string_sized_new (256);

	/* move cursor to start of line and clear it */
	g_string_append (str, "\r\033[K");

	/* calculate dimensions */
	available = priv->area_width > PK_PERCENT_TEXT_WIDTH ? priv->area_width - PK_PERCENT_TEXT_WIDTH : priv->area_width;

	bar_width = priv->size;
	if (bar_width > available / 2)
		bar_width = available / 2;
	if (bar_width < 10)
		bar_width = 10;

	if (available > bar_width + 3)
		text_width = available - bar_width - 3;
	else
		text_width = 0;

	/* truncate and pad the current text to exact width */
	if (priv->old_start_text != NULL && text_width > 0) {
		g_autofree gchar *truncated = NULL;
		g_autofree gchar *display_text = NULL;
		truncated = pk_console_text_truncate (priv->old_start_text, text_width);
		display_text = pk_console_strpad (truncated, text_width);
		g_string_append (str, display_text);
	} else {
		gsize old_len = str->len;
		g_string_set_size(str, old_len + text_width);
		memset(str->str + old_len, ' ', text_width);
		str->str[str->len] = '\0';
	}

	g_string_append (str, " [");
	if (priv->use_unicode) {
		for (guint i = 0; i < bar_width; i++) {
			if (i == priv->pulse_state.position)
				g_string_append (str, "▓");
			else if (i == priv->pulse_state.position - 1 || i == priv->pulse_state.position + 1)
				g_string_append (str, "░");
			else
				g_string_append (str, " ");
		}
	} else {
		/* ASCII fallback */
		for (guint i = 0; i < bar_width; i++) {
			if (i == priv->pulse_state.position || i == priv->pulse_state.position + 1)
				g_string_append (str, "=");
			else
				g_string_append (str, " ");
		}
	}
	g_string_append (str, "]");

	/* show percentage if available */
	if (priv->percentage >= 0 && priv->percentage <= 100 &&
	    priv->percentage != PK_PROGRESS_BAR_PERCENTAGE_INVALID) {
		g_string_append_printf (str, " %3d%%", priv->percentage);
	} else {
		g_string_append (str, "     ");
	}

	pk_progress_bar_console (self, str->str);

	return TRUE;
}

/**
 * pk_progress_bar_draw_pulse_bar:
 */
static void
pk_progress_bar_draw_pulse_bar (PkProgressBar *self)
{
	PkProgressBarPrivate *priv = GET_PRIVATE(self);

	/* have we already got a pulse timer? */
	if (priv->timer_id != 0)
		return;

	priv->pulse_state.position = 1;
	priv->pulse_state.move_forward = TRUE;
	priv->timer_id = g_timeout_add (PK_PROGRESS_BAR_PULSE_TIMEOUT,
					G_SOURCE_FUNC (pk_progress_bar_pulse_bar), self);
	g_source_set_name_by_id (priv->timer_id, "[PkProgressBar] pulse");
}

/**
 * pk_progress_bar_set_percentage:
 * @progress_bar: a valid #PkProgressBar instance
 * @percentage: percentage value to set (0-100).
 *
 * Set the percentage value of the progress bar.
 *
 * Return value: %TRUE if changed
 */
gboolean
pk_progress_bar_set_percentage (PkProgressBar *progress_bar, gint percentage)
{
	PkProgressBarPrivate *priv = GET_PRIVATE(progress_bar);

	g_return_val_if_fail (PK_IS_PROGRESS_BAR (progress_bar), FALSE);
	g_return_val_if_fail (percentage <= PK_PROGRESS_BAR_PERCENTAGE_INVALID, FALSE);

	/* never called pk_progress_bar_start() */
	if (priv->percentage == G_MININT)
		pk_progress_bar_start (progress_bar, "FIXME: need to call pk_progress_bar_start() earlier!");

	/* check for old percentage */
	if (percentage == priv->percentage) {
		g_debug ("skipping as the same");
		goto out;
	}

	/* save */
	priv->percentage = percentage;

	/* either pulse or display */
	if (percentage < 0 || percentage > 100) {
		pk_progress_bar_draw_pulse_bar (progress_bar);
	} else {
		g_clear_handle_id (&priv->timer_id, g_source_remove);
		pk_progress_bar_draw (progress_bar, percentage);
	}
out:
	return TRUE;
}

/**
 * pk_progress_bar_start:
 * @progress_bar: a valid #PkProgressBar instance
 * @text: text to show in progress bar.
 *
 * Start showing progress.
 *
 * Return value: %TRUE if progress bar started
 */
gboolean
pk_progress_bar_start (PkProgressBar *progress_bar, const gchar *text)
{
	PkProgressBarPrivate *priv = GET_PRIVATE(progress_bar);
	g_return_val_if_fail (PK_IS_PROGRESS_BAR (progress_bar), FALSE);

	/* update our drawing area size, in case it has changed */
	priv->area_width = pk_progress_bar_get_draw_area_width (progress_bar);

	/* same as last time */
	if (priv->old_start_text != NULL && text != NULL) {
		if (g_strcmp0 (priv->old_start_text, text) == 0)
			return TRUE;
	}

	/* finish old progress bar if exists */
	if (priv->percentage != G_MININT) {
		pk_progress_bar_draw (progress_bar, 100);
		if (!priv->allow_restart)
			pk_progress_bar_console (progress_bar, "\n");
	}

	g_free (priv->old_start_text);
	priv->old_start_text = g_strdup (text);

	/* reset */
	priv->percentage = 0;
	pk_progress_bar_draw (progress_bar, 0);

	return TRUE;
}

/**
 * pk_progress_bar_end:
 * @progress_bar: a valid #PkProgressBar instance
 *
 * Stop showing progress.
 *
 * Return value: %TRUE if progress bar stopped
 */
gboolean
pk_progress_bar_end (PkProgressBar *progress_bar)
{
	PkProgressBarPrivate *priv = GET_PRIVATE(progress_bar);
	g_return_val_if_fail (PK_IS_PROGRESS_BAR (progress_bar), FALSE);

	/* never drawn */
	if (priv->percentage == G_MININT)
		return FALSE;

	/* stop pulse timer if running */
	g_clear_handle_id (&priv->timer_id, g_source_remove);

	/* draw final state and newline */
	priv->percentage = G_MININT;
	pk_progress_bar_draw (progress_bar, 100);
	pk_progress_bar_console (progress_bar, "\n");

	return TRUE;
}

/**
 * pk_progress_bar_set_allow_restart:
 * @progress_bar: a valid #PkProgressBar instance
 * @allow_restart: whether restarting is allowed
 *
 * Set whether the progress bar can be restarted, changing its status text
 * instead of creating a new progress bar.
 * If set to %FALSE, calling %pk_progress_bar_start() on a running progress
 * bar will create a new one, otherwise the existing one will be overridden.
 *
 */
void
pk_progress_bar_set_allow_restart (PkProgressBar* progress_bar, gboolean allow_restart)
{
	PkProgressBarPrivate *priv = GET_PRIVATE(progress_bar);
	g_return_if_fail (PK_IS_PROGRESS_BAR (progress_bar));
	priv->allow_restart = allow_restart;
}

/**
 * pk_progress_bar_finalize:
 */
static void
pk_progress_bar_finalize (GObject *object)
{
	PkProgressBar *self = PK_PROGRESS_BAR (object);
	PkProgressBarPrivate *priv = GET_PRIVATE(self);

	g_clear_pointer (&priv->old_start_text, g_free);
	g_clear_handle_id (&priv->timer_id, g_source_remove);
	if (priv->tty_fd >= 0)
		close (priv->tty_fd);

	G_OBJECT_CLASS (pk_progress_bar_parent_class)->finalize (object);
}

/**
 * pk_progress_bar_class_init:
 */
static void
pk_progress_bar_class_init (PkProgressBarClass *klass)
{
	GObjectClass *object_class = G_OBJECT_CLASS (klass);
	object_class->finalize = pk_progress_bar_finalize;
}

/**
 * pk_progress_bar_init:
 */
static void
pk_progress_bar_init (PkProgressBar *self)
{
	PkProgressBarPrivate *priv = GET_PRIVATE(self);
	const char *codeset;

	self->priv = priv;
	priv->size = PK_PROGRESS_BAR_DEFAULT_SIZE;
	priv->percentage = G_MININT;
	priv->timer_id = 0;
	priv->area_width = 80;

	/* check if we can use Unicode */
	codeset = nl_langinfo (CODESET);
	priv->use_unicode = codeset != NULL &&
						(g_ascii_strcasecmp (codeset, "UTF-8") == 0 ||
						 g_ascii_strcasecmp (codeset, "utf8") == 0);

	/* try to open TTY */
	priv->tty_fd = open ("/dev/tty", O_RDWR, 0);
	if (priv->tty_fd < 0)
		priv->tty_fd = open ("/dev/console", O_RDWR, 0);
	if (priv->tty_fd < 0)
		priv->tty_fd = open ("/dev/stdout", O_RDWR, 0);

	/* get initial terminal width */
	if (priv->tty_fd >= 0)
		priv->area_width = pk_progress_bar_get_draw_area_width (self);
}

/**
 * pk_progress_bar_new:
 *
 * #PkProgressBar is a console text progress bar.
 *
 * Return value: A new #PkProgressBar instance
 */
PkProgressBar *
pk_progress_bar_new (void)
{
	PkProgressBar *self;
	self = g_object_new (PK_TYPE_PROGRESS_BAR, NULL);
	return PK_PROGRESS_BAR (self);
}