Blame libglnx/glnx-console.c

rpm-build c487f7
/* -*- mode: C; c-file-style: "gnu"; indent-tabs-mode: nil; -*-
rpm-build c487f7
 *
rpm-build c487f7
 * Copyright (C) 2013,2014,2015 Colin Walters <walters@verbum.org>
rpm-build c487f7
 *
rpm-build c487f7
 * This program is free software: you can redistribute it and/or modify
rpm-build c487f7
 * it under the terms of the GNU Lesser General Public License as published
rpm-build c487f7
 * by the Free Software Foundation; either version 2 of the licence or (at
rpm-build c487f7
 * your option) any later version.
rpm-build c487f7
 *
rpm-build c487f7
 * This library is distributed in the hope that it will be useful,
rpm-build c487f7
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
rpm-build c487f7
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
rpm-build c487f7
 * Lesser General Public License for more details.
rpm-build c487f7
 *
rpm-build c487f7
 * You should have received a copy of the GNU Lesser General
rpm-build c487f7
 * Public License along with this library; if not, write to the
rpm-build c487f7
 * Free Software Foundation, Inc., 59 Temple Place, Suite 330,
rpm-build c487f7
 * Boston, MA 02111-1307, USA.
rpm-build c487f7
 */
rpm-build c487f7
rpm-build c487f7
#include "config.h"
rpm-build c487f7
rpm-build c487f7
#include "glnx-console.h"
rpm-build c487f7
rpm-build c487f7
#include <unistd.h>
rpm-build c487f7
#include <string.h>
rpm-build c487f7
#include <fcntl.h>
rpm-build c487f7
#include <stdio.h>
rpm-build c487f7
#include <errno.h>
rpm-build c487f7
#include <sys/ioctl.h>
rpm-build c487f7
rpm-build c487f7
/* For people with widescreen monitors and maximized terminals, it looks pretty
rpm-build c487f7
 * bad to have an enormous progress bar. For much the same reason as web pages
rpm-build c487f7
 * tend to have a maximum width;
rpm-build c487f7
 * https://ux.stackexchange.com/questions/48982/suggest-good-max-width-for-fluid-width-design
rpm-build c487f7
 */
rpm-build c487f7
#define MAX_PROGRESSBAR_COLUMNS 20
rpm-build c487f7
rpm-build c487f7
/* Max updates output per second.  On a tty there's no point to rendering
rpm-build c487f7
 * extremely fast; and for a non-tty we're probably in a Jenkins job
rpm-build c487f7
 * or whatever and having percentages spam multiple lines there is annoying.
rpm-build c487f7
 */
rpm-build c487f7
#define MAX_TTY_UPDATE_HZ (5)
rpm-build c487f7
#define MAX_NONTTY_UPDATE_HZ (1)
rpm-build c487f7
rpm-build c487f7
static gboolean locked;
rpm-build c487f7
static guint64 last_update_ms; /* monotonic time in millis we last updated */
rpm-build c487f7
rpm-build c487f7
gboolean
rpm-build c487f7
glnx_stdout_is_tty (void)
rpm-build c487f7
{
rpm-build c487f7
  static gsize initialized = 0;
rpm-build c487f7
  static gboolean stdout_is_tty_v;
rpm-build c487f7
rpm-build c487f7
  if (g_once_init_enter (&initialized))
rpm-build c487f7
    {
rpm-build c487f7
      stdout_is_tty_v = isatty (1);
rpm-build c487f7
      g_once_init_leave (&initialized, 1);
rpm-build c487f7
    }
rpm-build c487f7
rpm-build c487f7
  return stdout_is_tty_v;
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
static volatile guint cached_columns = 0;
rpm-build c487f7
static volatile guint cached_lines = 0;
rpm-build c487f7
rpm-build c487f7
static int
rpm-build c487f7
fd_columns (int fd)
rpm-build c487f7
{
rpm-build c487f7
  struct winsize ws = {};
rpm-build c487f7
rpm-build c487f7
  if (ioctl (fd, TIOCGWINSZ, &ws) < 0)
rpm-build c487f7
    return -errno;
rpm-build c487f7
rpm-build c487f7
  if (ws.ws_col <= 0)
rpm-build c487f7
    return -EIO;
rpm-build c487f7
rpm-build c487f7
  return ws.ws_col;
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
/**
rpm-build c487f7
 * glnx_console_columns:
rpm-build c487f7
 * 
rpm-build c487f7
 * Returns: The number of columns for terminal output
rpm-build c487f7
 */
rpm-build c487f7
guint
rpm-build c487f7
glnx_console_columns (void)
rpm-build c487f7
{
rpm-build c487f7
  if (G_UNLIKELY (cached_columns == 0))
rpm-build c487f7
    {
rpm-build c487f7
      int c;
rpm-build c487f7
rpm-build c487f7
      c = fd_columns (STDOUT_FILENO);
rpm-build c487f7
rpm-build c487f7
      if (c <= 0)
rpm-build c487f7
        c = 80;
rpm-build c487f7
rpm-build c487f7
      if (c > 256)
rpm-build c487f7
        c = 256;
rpm-build c487f7
rpm-build c487f7
      cached_columns = c;
rpm-build c487f7
    }
rpm-build c487f7
rpm-build c487f7
  return cached_columns;
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
static int
rpm-build c487f7
fd_lines (int fd)
rpm-build c487f7
{
rpm-build c487f7
  struct winsize ws = {};
rpm-build c487f7
rpm-build c487f7
  if (ioctl (fd, TIOCGWINSZ, &ws) < 0)
rpm-build c487f7
    return -errno;
rpm-build c487f7
rpm-build c487f7
  if (ws.ws_row <= 0)
rpm-build c487f7
    return -EIO;
rpm-build c487f7
rpm-build c487f7
  return ws.ws_row;
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
/**
rpm-build c487f7
 * glnx_console_lines:
rpm-build c487f7
 * 
rpm-build c487f7
 * Returns: The number of lines for terminal output
rpm-build c487f7
 */
rpm-build c487f7
guint
rpm-build c487f7
glnx_console_lines (void)
rpm-build c487f7
{
rpm-build c487f7
  if (G_UNLIKELY (cached_lines == 0))
rpm-build c487f7
    {
rpm-build c487f7
      int l;
rpm-build c487f7
rpm-build c487f7
      l = fd_lines (STDOUT_FILENO);
rpm-build c487f7
rpm-build c487f7
      if (l <= 0)
rpm-build c487f7
        l = 24;
rpm-build c487f7
rpm-build c487f7
      cached_lines = l;
rpm-build c487f7
    }
rpm-build c487f7
rpm-build c487f7
  return cached_lines;
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
static void
rpm-build c487f7
on_sigwinch (int signum)
rpm-build c487f7
{
rpm-build c487f7
  cached_columns = 0;
rpm-build c487f7
  cached_lines = 0;
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
void
rpm-build c487f7
glnx_console_lock (GLnxConsoleRef *console)
rpm-build c487f7
{
rpm-build c487f7
  static gsize sigwinch_initialized = 0;
rpm-build c487f7
rpm-build c487f7
  g_return_if_fail (!locked);
rpm-build c487f7
  g_return_if_fail (!console->locked);
rpm-build c487f7
rpm-build c487f7
  console->is_tty = glnx_stdout_is_tty ();
rpm-build c487f7
rpm-build c487f7
  locked = console->locked = TRUE;
rpm-build c487f7
rpm-build c487f7
  if (console->is_tty)
rpm-build c487f7
    {
rpm-build c487f7
      if (g_once_init_enter (&sigwinch_initialized))
rpm-build c487f7
        {
rpm-build c487f7
          signal (SIGWINCH, on_sigwinch);
rpm-build c487f7
          g_once_init_leave (&sigwinch_initialized, 1);
rpm-build c487f7
        }
rpm-build c487f7
      
rpm-build c487f7
      { static const char initbuf[] = { 0x1B, 0x37 };
rpm-build c487f7
        (void) fwrite (initbuf, 1, sizeof (initbuf), stdout);
rpm-build c487f7
      }
rpm-build c487f7
    }
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
static void
rpm-build c487f7
printpad (const char *padbuf,
rpm-build c487f7
          guint       padbuf_len,
rpm-build c487f7
          guint       n)
rpm-build c487f7
{
rpm-build c487f7
  const guint d = n / padbuf_len;
rpm-build c487f7
  const guint r = n % padbuf_len;
rpm-build c487f7
  guint i;
rpm-build c487f7
rpm-build c487f7
  for (i = 0; i < d; i++)
rpm-build c487f7
    fwrite (padbuf, 1, padbuf_len, stdout);
rpm-build c487f7
  fwrite (padbuf, 1, r, stdout);
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
static void
rpm-build c487f7
text_percent_internal (const char *text,
rpm-build c487f7
                       int percentage)
rpm-build c487f7
{
rpm-build c487f7
  /* Check whether we're trying to render too fast; unless percentage is 100, in
rpm-build c487f7
   * which case we assume this is the last call, so we always render it.
rpm-build c487f7
   */
rpm-build c487f7
  const guint64 current_ms = g_get_monotonic_time () / 1000;
rpm-build c487f7
  if (percentage != 100)
rpm-build c487f7
    {
rpm-build c487f7
      const guint64 diff_ms = current_ms - last_update_ms;
rpm-build c487f7
      if (glnx_stdout_is_tty ())
rpm-build c487f7
        {
rpm-build c487f7
          if (diff_ms < (1000/MAX_TTY_UPDATE_HZ))
rpm-build c487f7
            return;
rpm-build c487f7
        }
rpm-build c487f7
      else
rpm-build c487f7
        {
rpm-build c487f7
          if (diff_ms < (1000/MAX_NONTTY_UPDATE_HZ))
rpm-build c487f7
            return;
rpm-build c487f7
        }
rpm-build c487f7
    }
rpm-build c487f7
  last_update_ms = current_ms;
rpm-build c487f7
rpm-build c487f7
  static const char equals[] = "====================";
rpm-build c487f7
  const guint n_equals = sizeof (equals) - 1;
rpm-build c487f7
  static const char spaces[] = "                    ";
rpm-build c487f7
  const guint n_spaces = sizeof (spaces) - 1;
rpm-build c487f7
  const guint ncolumns = glnx_console_columns ();
rpm-build c487f7
  const guint bar_min = 10;
rpm-build c487f7
rpm-build c487f7
  if (text && !*text)
rpm-build c487f7
    text = NULL;
rpm-build c487f7
rpm-build c487f7
  const guint input_textlen = text ? strlen (text) : 0;
rpm-build c487f7
rpm-build c487f7
  if (!glnx_stdout_is_tty ())
rpm-build c487f7
    {
rpm-build c487f7
      if (text)
rpm-build c487f7
        fprintf (stdout, "%s", text);
rpm-build c487f7
      if (percentage != -1)
rpm-build c487f7
        {
rpm-build c487f7
          if (text)
rpm-build c487f7
            fputc (' ', stdout);
rpm-build c487f7
          fprintf (stdout, "%u%%", percentage);
rpm-build c487f7
        }
rpm-build c487f7
      fputc ('\n', stdout);
rpm-build c487f7
      fflush (stdout);
rpm-build c487f7
      return;
rpm-build c487f7
    }
rpm-build c487f7
rpm-build c487f7
  if (ncolumns < bar_min)
rpm-build c487f7
    return; /* TODO: spinner */
rpm-build c487f7
rpm-build c487f7
  /* Restore cursor */
rpm-build c487f7
  { const char beginbuf[2] = { 0x1B, 0x38 };
rpm-build c487f7
    (void) fwrite (beginbuf, 1, sizeof (beginbuf), stdout);
rpm-build c487f7
  }
rpm-build c487f7
rpm-build c487f7
  if (percentage == -1)
rpm-build c487f7
    {
rpm-build c487f7
      if (text != NULL)
rpm-build c487f7
        fwrite (text, 1, input_textlen, stdout);
rpm-build c487f7
rpm-build c487f7
      /* Overwrite remaining space, if any */
rpm-build c487f7
      if (ncolumns > input_textlen)
rpm-build c487f7
        printpad (spaces, n_spaces, ncolumns - input_textlen);
rpm-build c487f7
    }
rpm-build c487f7
  else
rpm-build c487f7
    {
rpm-build c487f7
      const guint textlen = MIN (input_textlen, ncolumns - bar_min);
rpm-build c487f7
      const guint barlen = MIN (MAX_PROGRESSBAR_COLUMNS, ncolumns - (textlen + 1));
rpm-build c487f7
rpm-build c487f7
      if (textlen > 0)
rpm-build c487f7
        {
rpm-build c487f7
          fwrite (text, 1, textlen, stdout);
rpm-build c487f7
          fputc (' ', stdout);
rpm-build c487f7
        }
rpm-build c487f7
rpm-build c487f7
      {
rpm-build c487f7
        const guint nbraces = 2;
rpm-build c487f7
        const guint textpercent_len = 5;
rpm-build c487f7
        const guint bar_internal_len = barlen - nbraces - textpercent_len;
rpm-build c487f7
        const guint eqlen = bar_internal_len * (percentage / 100.0);
rpm-build c487f7
        const guint spacelen = bar_internal_len - eqlen;
rpm-build c487f7
rpm-build c487f7
        fputc ('[', stdout);
rpm-build c487f7
        printpad (equals, n_equals, eqlen);
rpm-build c487f7
        printpad (spaces, n_spaces, spacelen);
rpm-build c487f7
        fputc (']', stdout);
rpm-build c487f7
        fprintf (stdout, " %3d%%", percentage);
rpm-build c487f7
      }
rpm-build c487f7
    }
rpm-build c487f7
rpm-build c487f7
  fflush (stdout);
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
/**
rpm-build c487f7
 * glnx_console_progress_text_percent:
rpm-build c487f7
 * @text: Show this text before the progress bar
rpm-build c487f7
 * @percentage: An integer in the range of 0 to 100
rpm-build c487f7
 *
rpm-build c487f7
 * On a tty, print to the console @text followed by an ASCII art
rpm-build c487f7
 * progress bar whose percentage is @percentage.  If stdout is not a
rpm-build c487f7
 * tty, a more basic line by line change will be printed.
rpm-build c487f7
 *
rpm-build c487f7
 * You must have called glnx_console_lock() before invoking this
rpm-build c487f7
 * function.
rpm-build c487f7
 *
rpm-build c487f7
 */
rpm-build c487f7
void
rpm-build c487f7
glnx_console_progress_text_percent (const char *text,
rpm-build c487f7
                                    guint percentage)
rpm-build c487f7
{
rpm-build c487f7
  g_return_if_fail (percentage <= 100);
rpm-build c487f7
rpm-build c487f7
  text_percent_internal (text, percentage);
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
/**
rpm-build c487f7
 * glnx_console_progress_n_items:
rpm-build c487f7
 * @text: Show this text before the progress bar
rpm-build c487f7
 * @current: An integer for how many items have been processed
rpm-build c487f7
 * @total: An integer for how many items there are total
rpm-build c487f7
 *
rpm-build c487f7
 * On a tty, print to the console @text followed by [@current/@total],
rpm-build c487f7
 * then an ASCII art progress bar, like glnx_console_progress_text_percent().
rpm-build c487f7
 *
rpm-build c487f7
 * You must have called glnx_console_lock() before invoking this
rpm-build c487f7
 * function.
rpm-build c487f7
 */
rpm-build c487f7
void
rpm-build c487f7
glnx_console_progress_n_items (const char     *text,
rpm-build c487f7
                               guint           current,
rpm-build c487f7
                               guint           total)
rpm-build c487f7
{
rpm-build c487f7
  g_return_if_fail (current <= total);
rpm-build c487f7
  g_return_if_fail (total > 0);
rpm-build c487f7
rpm-build c487f7
  g_autofree char *newtext = g_strdup_printf ("%s (%u/%u)", text, current, total);
rpm-build c487f7
  /* Special case current == total to ensure we end at 100% */
rpm-build c487f7
  int percentage = (current == total) ? 100 : (((double)current) / total * 100);
rpm-build c487f7
  glnx_console_progress_text_percent (newtext, percentage);
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
void
rpm-build c487f7
glnx_console_text (const char *text)
rpm-build c487f7
{
rpm-build c487f7
  text_percent_internal (text, -1);
rpm-build c487f7
}
rpm-build c487f7
rpm-build c487f7
/**
rpm-build c487f7
 * glnx_console_unlock:
rpm-build c487f7
 *
rpm-build c487f7
 * Print a newline, and reset all cached console progress state.
rpm-build c487f7
 *
rpm-build c487f7
 * This function does nothing if stdout is not a tty.
rpm-build c487f7
 */
rpm-build c487f7
void
rpm-build c487f7
glnx_console_unlock (GLnxConsoleRef *console)
rpm-build c487f7
{
rpm-build c487f7
  g_return_if_fail (locked);
rpm-build c487f7
  g_return_if_fail (console->locked);
rpm-build c487f7
rpm-build c487f7
  if (console->is_tty)
rpm-build c487f7
    fputc ('\n', stdout);
rpm-build c487f7
      
rpm-build c487f7
  locked = console->locked = FALSE;
rpm-build c487f7
}