Debian Codegraph @

lib/packagekit-glib2/pk-progress-bar.c

raw
1 /* -*- Mode: C; tab-width: 8; indent-tabs-mode: t; c-basic-offset: 8 -*-
2 *
3 * Copyright (C) 2008-2009 Richard Hughes <[email protected]>
4 * Copyright (C) 2024-2026 Matthias Klumpp <[email protected]>
5 *
6 * Licensed under the GNU Lesser General Public License Version 2.1
7 *
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
12 *
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
17 *
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA
21 */
22
23 #include "config.h"
24
25 #include <glib.h>
26 #include <locale.h>
27 #include <langinfo.h>
28 #include <string.h>
29 #include <unistd.h>
30 #include <fcntl.h>
31 #include <sys/ioctl.h>
32
33 #include "pk-progress-bar.h"
34 #include "pk-console-private.h"
35
36 typedef struct {
37 guint position;
38 gboolean move_forward;
39 } PkProgressBarPulseState;
40
41 struct PkProgressBarPrivate
42 {
43 guint size;
44 gint percentage;
45 guint timer_id;
46 PkProgressBarPulseState pulse_state;
47 gint tty_fd;
48 gchar *old_start_text;
49 guint area_width;
50 gboolean use_unicode;
51
52 gboolean allow_restart;
53 };
54
55 #define PK_PROGRESS_BAR_PERCENTAGE_INVALID 101
56 #define PK_PROGRESS_BAR_PULSE_TIMEOUT 40 /* ms */
57 #define PK_PROGRESS_BAR_DEFAULT_SIZE 30
58
59 /* space for percentage text (e.g., " 100%") or spacing */
60 static const guint PK_PERCENT_TEXT_WIDTH = 5;
61
62 G_DEFINE_TYPE_WITH_PRIVATE (PkProgressBar, pk_progress_bar, G_TYPE_OBJECT)
63 #define GET_PRIVATE(o) (pk_progress_bar_get_instance_private (o))
64
65 /**
66 * pk_progress_bar_get_draw_area_width:
67 *
68 * Get our drawing area width in characters.
69 */
70 static guint
71 pk_progress_bar_get_draw_area_width (PkProgressBar *self)
72 {
73 PkProgressBarPrivate *priv = GET_PRIVATE(self);
74 struct winsize w;
75
76 if (priv->tty_fd < 0)
77 return 80;
78
79 if (ioctl (priv->tty_fd, TIOCGWINSZ, &w) == 0 && w.ws_col > 0) {
80 /* we may have very long terminals, so cap the area to a reasonable maximum size */
81 return w.ws_col >= 110 ? 110 : w.ws_col;
82 }
83
84 return 80;
85 }
86
87 /**
88 * pk_progress_bar_console:
89 */
90 static void
91 pk_progress_bar_console (PkProgressBar *self, const gchar *tmp)
92 {
93 PkProgressBarPrivate *priv = GET_PRIVATE(self);
94 size_t count;
95 ssize_t written;
96
97 if (priv->tty_fd < 0)
98 return;
99
100 count = strlen (tmp);
101 if (count == 0)
102 return;
103
104 written = write (priv->tty_fd, tmp, count);
105 if (written < 0 || (size_t)written != count) {
106 g_warning ("Only wrote %" G_GSSIZE_FORMAT
107 " of %" G_GSSIZE_FORMAT " bytes",
108 written, (gssize)count);
109 }
110 }
111
112 /**
113 * pk_progress_bar_set_size:
114 * @progress_bar: a valid #PkProgressBar instance
115 * @size: width of progress bar in characters.
116 *
117 * Set the width of the progress bar.
118 *
119 * Return value: %TRUE if changed
120 */
121 gboolean
122 pk_progress_bar_set_size (PkProgressBar *progress_bar, guint size)
123 {
124 PkProgressBarPrivate *priv = GET_PRIVATE(progress_bar);
125
126 g_return_val_if_fail (PK_IS_PROGRESS_BAR (progress_bar), FALSE);
127 g_return_val_if_fail (size > 0 && size < G_MAXINT, FALSE);
128
129 priv->size = size;
130 return TRUE;
131 }
132
133 /**
134 * pk_progress_bar_draw:
135 */
136 static gboolean
137 pk_progress_bar_draw (PkProgressBar *self, gint percentage)
138 {
139 PkProgressBarPrivate *priv = GET_PRIVATE(self);
140 g_autoptr(GString) str = NULL;
141 guint available_width;
142 guint bar_width;
143 guint text_width;
144
145 /* no value yet */
146 if (percentage == G_MININT)
147 return FALSE;
148
149 /* clamp percentage */
150 if (percentage < 0)
151 percentage = 0;
152 if (percentage > 100)
153 percentage = 100;
154
155 str = g_string_sized_new (256);
156
157 /* move cursor to start of line and clear it */
158 g_string_append (str, "\r\033[K");
159
160 /* calculate available width for text + bar */
161 available_width = priv->area_width > PK_PERCENT_TEXT_WIDTH ? priv->area_width - PK_PERCENT_TEXT_WIDTH : priv->area_width;
162
163 /* determine bar width (use configured size or auto-calculate) */
164 bar_width = priv->size;
165 if (bar_width > available_width / 2)
166 bar_width = available_width / 2;
167 if (bar_width < 10)
168 bar_width = 10;
169
170 /* text width is what's left */
171 if (available_width > bar_width + 3)
172 text_width = available_width - bar_width - 3; /* -3 for space and brackets */
173 else
174 text_width = 0;
175
176 /* truncate and pad the current text to exact width */
177 if (priv->old_start_text != NULL && text_width > 0) {
178 g_autofree gchar *truncated = NULL;
179 g_autofree gchar *display_text = NULL;
180 truncated = pk_console_text_truncate (priv->old_start_text, text_width);
181 display_text = pk_console_strpad (truncated, text_width);
182 g_string_append (str, display_text);
183 } else {
184 gsize old_len = str->len;
185 g_string_set_size(str, old_len + text_width);
186 memset(str->str + old_len, ' ', text_width);
187 str->str[str->len] = '\0';
188 }
189
190 if (priv->use_unicode) {
191 /* use Unicode block characters: █ = full, ▓ = 3/4, ▒ = 1/2, ░ = 1/4 */
192 guint filled_chars = (percentage * bar_width) / 100;
193 guint remainder = (percentage * bar_width) % 100;
194
195 g_string_append (str, " [");
196
197 /* full blocks */
198 for (guint i = 0; i < filled_chars; i++)
199 g_string_append (str, "█");
200
201 /* partial block based on remainder */
202 if (filled_chars < bar_width) {
203 if (remainder >= 75)
204 g_string_append (str, "▓");
205 else if (remainder >= 50)
206 g_string_append (str, "▒");
207 else if (remainder >= 25)
208 g_string_append (str, "░");
209 else
210 g_string_append (str, " ");
211 filled_chars++;
212 }
213
214 /* empty space */
215 for (guint i = filled_chars; i < bar_width; i++)
216 g_string_append (str, " ");
217
218 g_string_append (str, "]");
219 } else {
220 /* fallback to ASCII */
221 guint filled = (percentage * bar_width) / 100;
222
223 g_string_append (str, " [");
224 for (guint i = 0; i < filled; i++)
225 g_string_append (str, "=");
226 for (guint i = filled; i < bar_width; i++)
227 g_string_append (str, " ");
228 g_string_append (str, "]");
229 }
230
231 /* percentage text */
232 g_string_append_printf (str, " %3d%%", percentage);
233
234 pk_progress_bar_console (self, str->str);
235
236 return TRUE;
237 }
238
239 /**
240 * pk_progress_bar_pulse_bar:
241 */
242 static gboolean
243 pk_progress_bar_pulse_bar (PkProgressBar *self)
244 {
245 PkProgressBarPrivate *priv = GET_PRIVATE(self);
246 g_autoptr(GString) str = NULL;
247 guint available;
248 guint bar_width;
249 guint text_width;
250
251 /* update position */
252 if (priv->pulse_state.move_forward) {
253 if (priv->pulse_state.position >= priv->size - 2)
254 priv->pulse_state.move_forward = FALSE;
255 else
256 priv->pulse_state.position++;
257 } else {
258 if (priv->pulse_state.position <= 1)
259 priv->pulse_state.move_forward = TRUE;
260 else
261 priv->pulse_state.position--;
262 }
263
264 str = g_string_sized_new (256);
265
266 /* move cursor to start of line and clear it */
267 g_string_append (str, "\r\033[K");
268
269 /* calculate dimensions */
270 available = priv->area_width > PK_PERCENT_TEXT_WIDTH ? priv->area_width - PK_PERCENT_TEXT_WIDTH : priv->area_width;
271
272 bar_width = priv->size;
273 if (bar_width > available / 2)
274 bar_width = available / 2;
275 if (bar_width < 10)
276 bar_width = 10;
277
278 if (available > bar_width + 3)
279 text_width = available - bar_width - 3;
280 else
281 text_width = 0;
282
283 /* truncate and pad the current text to exact width */
284 if (priv->old_start_text != NULL && text_width > 0) {
285 g_autofree gchar *truncated = NULL;
286 g_autofree gchar *display_text = NULL;
287 truncated = pk_console_text_truncate (priv->old_start_text, text_width);
288 display_text = pk_console_strpad (truncated, text_width);
289 g_string_append (str, display_text);
290 } else {
291 gsize old_len = str->len;
292 g_string_set_size(str, old_len + text_width);
293 memset(str->str + old_len, ' ', text_width);
294 str->str[str->len] = '\0';
295 }
296
297 g_string_append (str, " [");
298 if (priv->use_unicode) {
299 for (guint i = 0; i < bar_width; i++) {
300 if (i == priv->pulse_state.position)
301 g_string_append (str, "▓");
302 else if (i == priv->pulse_state.position - 1 || i == priv->pulse_state.position + 1)
303 g_string_append (str, "░");
304 else
305 g_string_append (str, " ");
306 }
307 } else {
308 /* ASCII fallback */
309 for (guint i = 0; i < bar_width; i++) {
310 if (i == priv->pulse_state.position || i == priv->pulse_state.position + 1)
311 g_string_append (str, "=");
312 else
313 g_string_append (str, " ");
314 }
315 }
316 g_string_append (str, "]");
317
318 /* show percentage if available */
319 if (priv->percentage >= 0 && priv->percentage <= 100 &&
320 priv->percentage != PK_PROGRESS_BAR_PERCENTAGE_INVALID) {
321 g_string_append_printf (str, " %3d%%", priv->percentage);
322 } else {
323 g_string_append (str, " ");
324 }
325
326 pk_progress_bar_console (self, str->str);
327
328 return TRUE;
329 }
330
331 /**
332 * pk_progress_bar_draw_pulse_bar:
333 */
334 static void
335 pk_progress_bar_draw_pulse_bar (PkProgressBar *self)
336 {
337 PkProgressBarPrivate *priv = GET_PRIVATE(self);
338
339 /* have we already got a pulse timer? */
340 if (priv->timer_id != 0)
341 return;
342
343 priv->pulse_state.position = 1;
344 priv->pulse_state.move_forward = TRUE;
345 priv->timer_id = g_timeout_add (PK_PROGRESS_BAR_PULSE_TIMEOUT,
346 G_SOURCE_FUNC (pk_progress_bar_pulse_bar), self);
347 g_source_set_name_by_id (priv->timer_id, "[PkProgressBar] pulse");
348 }
349
350 /**
351 * pk_progress_bar_set_percentage:
352 * @progress_bar: a valid #PkProgressBar instance
353 * @percentage: percentage value to set (0-100).
354 *
355 * Set the percentage value of the progress bar.
356 *
357 * Return value: %TRUE if changed
358 */
359 gboolean
360 pk_progress_bar_set_percentage (PkProgressBar *progress_bar, gint percentage)
361 {
362 PkProgressBarPrivate *priv = GET_PRIVATE(progress_bar);
363
364 g_return_val_if_fail (PK_IS_PROGRESS_BAR (progress_bar), FALSE);
365 g_return_val_if_fail (percentage <= PK_PROGRESS_BAR_PERCENTAGE_INVALID, FALSE);
366
367 /* never called pk_progress_bar_start() */
368 if (priv->percentage == G_MININT)
369 pk_progress_bar_start (progress_bar, "FIXME: need to call pk_progress_bar_start() earlier!");
370
371 /* check for old percentage */
372 if (percentage == priv->percentage) {
373 g_debug ("skipping as the same");
374 goto out;
375 }
376
377 /* save */
378 priv->percentage = percentage;
379
380 /* either pulse or display */
381 if (percentage < 0 || percentage > 100) {
382 pk_progress_bar_draw_pulse_bar (progress_bar);
383 } else {
384 g_clear_handle_id (&priv->timer_id, g_source_remove);
385 pk_progress_bar_draw (progress_bar, percentage);
386 }
387 out:
388 return TRUE;
389 }
390
391 /**
392 * pk_progress_bar_start:
393 * @progress_bar: a valid #PkProgressBar instance
394 * @text: text to show in progress bar.
395 *
396 * Start showing progress.
397 *
398 * Return value: %TRUE if progress bar started
399 */
400 gboolean
401 pk_progress_bar_start (PkProgressBar *progress_bar, const gchar *text)
402 {
403 PkProgressBarPrivate *priv = GET_PRIVATE(progress_bar);
404 g_return_val_if_fail (PK_IS_PROGRESS_BAR (progress_bar), FALSE);
405
406 /* update our drawing area size, in case it has changed */
407 priv->area_width = pk_progress_bar_get_draw_area_width (progress_bar);
408
409 /* same as last time */
410 if (priv->old_start_text != NULL && text != NULL) {
411 if (g_strcmp0 (priv->old_start_text, text) == 0)
412 return TRUE;
413 }
414
415 /* finish old progress bar if exists */
416 if (priv->percentage != G_MININT) {
417 pk_progress_bar_draw (progress_bar, 100);
418 if (!priv->allow_restart)
419 pk_progress_bar_console (progress_bar, "\n");
420 }
421
422 g_free (priv->old_start_text);
423 priv->old_start_text = g_strdup (text);
424
425 /* reset */
426 priv->percentage = 0;
427 pk_progress_bar_draw (progress_bar, 0);
428
429 return TRUE;
430 }
431
432 /**
433 * pk_progress_bar_end:
434 * @progress_bar: a valid #PkProgressBar instance
435 *
436 * Stop showing progress.
437 *
438 * Return value: %TRUE if progress bar stopped
439 */
440 gboolean
441 pk_progress_bar_end (PkProgressBar *progress_bar)
442 {
443 PkProgressBarPrivate *priv = GET_PRIVATE(progress_bar);
444 g_return_val_if_fail (PK_IS_PROGRESS_BAR (progress_bar), FALSE);
445
446 /* never drawn */
447 if (priv->percentage == G_MININT)
448 return FALSE;
449
450 /* stop pulse timer if running */
451 g_clear_handle_id (&priv->timer_id, g_source_remove);
452
453 /* draw final state and newline */
454 priv->percentage = G_MININT;
455 pk_progress_bar_draw (progress_bar, 100);
456 pk_progress_bar_console (progress_bar, "\n");
457
458 return TRUE;
459 }
460
461 /**
462 * pk_progress_bar_set_allow_restart:
463 * @progress_bar: a valid #PkProgressBar instance
464 * @allow_restart: whether restarting is allowed
465 *
466 * Set whether the progress bar can be restarted, changing its status text
467 * instead of creating a new progress bar.
468 * If set to %FALSE, calling %pk_progress_bar_start() on a running progress
469 * bar will create a new one, otherwise the existing one will be overridden.
470 *
471 */
472 void
473 pk_progress_bar_set_allow_restart (PkProgressBar* progress_bar, gboolean allow_restart)
474 {
475 PkProgressBarPrivate *priv = GET_PRIVATE(progress_bar);
476 g_return_if_fail (PK_IS_PROGRESS_BAR (progress_bar));
477 priv->allow_restart = allow_restart;
478 }
479
480 /**
481 * pk_progress_bar_finalize:
482 */
483 static void
484 pk_progress_bar_finalize (GObject *object)
485 {
486 PkProgressBar *self = PK_PROGRESS_BAR (object);
487 PkProgressBarPrivate *priv = GET_PRIVATE(self);
488
489 g_clear_pointer (&priv->old_start_text, g_free);
490 g_clear_handle_id (&priv->timer_id, g_source_remove);
491 if (priv->tty_fd >= 0)
492 close (priv->tty_fd);
493
494 G_OBJECT_CLASS (pk_progress_bar_parent_class)->finalize (object);
495 }
496
497 /**
498 * pk_progress_bar_class_init:
499 */
500 static void
501 pk_progress_bar_class_init (PkProgressBarClass *klass)
502 {
503 GObjectClass *object_class = G_OBJECT_CLASS (klass);
504 object_class->finalize = pk_progress_bar_finalize;
505 }
506
507 /**
508 * pk_progress_bar_init:
509 */
510 static void
511 pk_progress_bar_init (PkProgressBar *self)
512 {
513 PkProgressBarPrivate *priv = GET_PRIVATE(self);
514 const char *codeset;
515
516 self->priv = priv;
517 priv->size = PK_PROGRESS_BAR_DEFAULT_SIZE;
518 priv->percentage = G_MININT;
519 priv->timer_id = 0;
520 priv->area_width = 80;
521
522 /* check if we can use Unicode */
523 codeset = nl_langinfo (CODESET);
524 priv->use_unicode = codeset != NULL &&
525 (g_ascii_strcasecmp (codeset, "UTF-8") == 0 ||
526 g_ascii_strcasecmp (codeset, "utf8") == 0);
527
528 /* try to open TTY */
529 priv->tty_fd = open ("/dev/tty", O_RDWR, 0);
530 if (priv->tty_fd < 0)
531 priv->tty_fd = open ("/dev/console", O_RDWR, 0);
532 if (priv->tty_fd < 0)
533 priv->tty_fd = open ("/dev/stdout", O_RDWR, 0);
534
535 /* get initial terminal width */
536 if (priv->tty_fd >= 0)
537 priv->area_width = pk_progress_bar_get_draw_area_width (self);
538 }
539
540 /**
541 * pk_progress_bar_new:
542 *
543 * #PkProgressBar is a console text progress bar.
544 *
545 * Return value: A new #PkProgressBar instance
546 */
547 PkProgressBar *
548 pk_progress_bar_new (void)
549 {
550 PkProgressBar *self;
551 self = g_object_new (PK_TYPE_PROGRESS_BAR, NULL);
552 return PK_PROGRESS_BAR (self);
553 }