summaryrefslogtreecommitdiff
path: root/tools/eos-profile-tool/eos-profile-utils.c
blob: 2d63804fcce96308f5f4bfca2049aa70f8eb06dc (plain)
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
#include "config.h"

#include "eos-profile-utils.h"

#include <stdlib.h>
#include <stdarg.h>
#include <stdio.h>
#include <string.h>

#include <math.h>

#include <sys/types.h>
#include <sys/ioctl.h>
#include <unistd.h>
#include <termios.h>
#include <fcntl.h>

static const char *ansi_colors[] = {
  [EOS_PRINT_COLOR_GREEN] = "[1;32m",
  [EOS_PRINT_COLOR_BLUE] = "[1;34m",
  [EOS_PRINT_COLOR_YELLOW] = "[1;33m",
  [EOS_PRINT_COLOR_RED] = "[1;31m",
  [EOS_PRINT_COLOR_NONE] = "[0m",
};

static char *
gen_color_message (const char *prefix,
                   EosPrintColor color,
                   const char *fmt,
                   va_list args)
{
  g_autofree char *res = NULL;

  if (prefix != NULL)
    {
      g_autofree char *msg = g_strdup_vprintf (fmt, args);

      res = g_strdup_printf ("\033%s%s\033%s: %s",
                             ansi_colors[color],
                             prefix,
                             ansi_colors[EOS_PRINT_COLOR_NONE],
                             msg);

    }
  else
    res = g_strdup_vprintf (fmt, args);

  return g_steal_pointer (&res);
}

void
eos_profile_util_print_message (const char *prefix,
                                EosPrintColor color,
                                const char *fmt,
                                ...)
{
  g_autofree char *msg = NULL;
  va_list args;

  va_start (args, fmt);
  msg = gen_color_message (prefix, color, fmt, args);
  va_end (args);

  g_print ("%s\n", msg);
}

void
eos_profile_util_print_error (const char *fmt,
                              ...)
{
  g_autofree char *msg = NULL;
  va_list args;

  va_start (args, fmt);
  msg = gen_color_message ("ERROR", EOS_PRINT_COLOR_RED, fmt, args);
  va_end (args);

  g_printerr ("%s\n", msg);
}

void
eos_profile_util_print_warning (const char *fmt,
                                ...)
{
  g_autofree char *msg = NULL;
  va_list args;

  va_start (args, fmt);
  msg = gen_color_message ("WARNING", EOS_PRINT_COLOR_YELLOW, fmt, args);
  va_end (args);

  g_printerr ("%s\n", msg);
}