Skip to content
Snippets Groups Projects
Commit c231a3b8 authored by Colin Walters's avatar Colin Walters
Browse files

console: Do basic output if we're not on a tty

Doing nothing isn't super useful; if you're using e.g. rpm-ostree in
Jenkins you want to see *something* from the "live tail".

This is a basic line-per-change implementation.

Closes: https://github.com/GNOME/libglnx/pull/6
parent 371172bc
Branches
Tags
No related merge requests found
...@@ -140,25 +140,27 @@ glnx_console_lock (GLnxConsoleRef *console) ...@@ -140,25 +140,27 @@ glnx_console_lock (GLnxConsoleRef *console)
{ {
static gsize sigwinch_initialized = 0; static gsize sigwinch_initialized = 0;
if (!stdout_is_tty ())
return;
g_return_if_fail (!locked); g_return_if_fail (!locked);
g_return_if_fail (!console->locked); g_return_if_fail (!console->locked);
console->is_tty = stdout_is_tty ();
locked = console->locked = TRUE; locked = console->locked = TRUE;
current_percent = 0; current_percent = 0;
if (g_once_init_enter (&sigwinch_initialized)) if (console->is_tty)
{ {
signal (SIGWINCH, on_sigwinch); if (g_once_init_enter (&sigwinch_initialized))
g_once_init_leave (&sigwinch_initialized, 1); {
signal (SIGWINCH, on_sigwinch);
g_once_init_leave (&sigwinch_initialized, 1);
}
{ static const char initbuf[] = { '\n', 0x1B, 0x37 };
(void) fwrite (initbuf, 1, sizeof (initbuf), stdout);
}
} }
{ static const char initbuf[] = { '\n', 0x1B, 0x37 };
(void) fwrite (initbuf, 1, sizeof (initbuf), stdout);
}
} }
static void static void
...@@ -180,13 +182,13 @@ printpad (const char *padbuf, ...@@ -180,13 +182,13 @@ printpad (const char *padbuf,
* @text: Show this text before the progress bar * @text: Show this text before the progress bar
* @percentage: An integer in the range of 0 to 100 * @percentage: An integer in the range of 0 to 100
* *
* Print to the console @text followed by an ASCII art progress bar * On a tty, print to the console @text followed by an ASCII art
* whose percentage is @percentage. * progress bar whose percentage is @percentage. If stdout is not a
* tty, a more basic line by line change will be printed.
* *
* You must have called glnx_console_lock() before invoking this * You must have called glnx_console_lock() before invoking this
* function. * function.
* *
* Currently, if stdout is not a tty, this function does nothing.
*/ */
void void
glnx_console_progress_text_percent (const char *text, glnx_console_progress_text_percent (const char *text,
...@@ -202,9 +204,6 @@ glnx_console_progress_text_percent (const char *text, ...@@ -202,9 +204,6 @@ glnx_console_progress_text_percent (const char *text,
guint textlen; guint textlen;
guint barlen; guint barlen;
if (!stdout_is_tty ())
return;
g_return_if_fail (percentage >= 0 && percentage <= 100); g_return_if_fail (percentage >= 0 && percentage <= 100);
if (text && !*text) if (text && !*text)
...@@ -214,6 +213,21 @@ glnx_console_progress_text_percent (const char *text, ...@@ -214,6 +213,21 @@ glnx_console_progress_text_percent (const char *text,
&& g_strcmp0 (text, current_text) == 0) && g_strcmp0 (text, current_text) == 0)
return; return;
if (!stdout_is_tty ())
{
if (text)
fprintf (stdout, "%s", text);
if (percentage != -1)
{
if (text)
fputc (' ', stdout);
fprintf (stdout, "%u%%", percentage);
}
fputc ('\n', stdout);
fflush (stdout);
return;
}
if (ncolumns < bar_min) if (ncolumns < bar_min)
return; /* TODO: spinner */ return; /* TODO: spinner */
...@@ -262,15 +276,14 @@ glnx_console_progress_text_percent (const char *text, ...@@ -262,15 +276,14 @@ glnx_console_progress_text_percent (const char *text,
void void
glnx_console_unlock (GLnxConsoleRef *console) glnx_console_unlock (GLnxConsoleRef *console)
{ {
if (!stdout_is_tty ())
return;
g_return_if_fail (locked); g_return_if_fail (locked);
g_return_if_fail (console->locked); g_return_if_fail (console->locked);
current_percent = -1; current_percent = -1;
g_clear_pointer (&current_text, g_free); g_clear_pointer (&current_text, g_free);
fputc ('\n', stdout);
if (console->is_tty)
fputc ('\n', stdout);
locked = FALSE; locked = FALSE;
} }
...@@ -26,6 +26,7 @@ G_BEGIN_DECLS ...@@ -26,6 +26,7 @@ G_BEGIN_DECLS
struct GLnxConsoleRef { struct GLnxConsoleRef {
gboolean locked; gboolean locked;
gboolean is_tty;
}; };
typedef struct GLnxConsoleRef GLnxConsoleRef; typedef struct GLnxConsoleRef GLnxConsoleRef;
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment