File: bolt-term.c

package info (click to toggle)
bolt 0.7-2
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 1,396 kB
  • sloc: ansic: 21,784; python: 1,574; xml: 426; sh: 25; makefile: 13
file content (100 lines) | stat: -rw-r--r-- 2,774 bytes parent folder | download | duplicates (5)
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
/*
 * Copyright © 2017 Red Hat, Inc
 *
 * This program 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, see <http://www.gnu.org/licenses/>.
 *
 * Authors:
 *       Christian J. Kellner <christian@kellner.me>
 */

#include "config.h"

#include "bolt-str.h"
#include "bolt-term.h"

#include <string.h>
#include <unistd.h>

int
bolt_is_fancy_terminal (void)
{
  const char *val;

  if (!isatty (STDOUT_FILENO))
    return 0;

  val = g_getenv ("TERM");
  if (val && !g_ascii_strcasecmp (val, "dumb"))
    return 0;

  return 1;
}

const char *
bolt_color (const char *color)
{
  static gint on = -1;

  if (G_UNLIKELY (on == -1))
    on = bolt_is_fancy_terminal ();

  return on ? color : "";
}

/* borrowed from systemd */
static const char *const ansi_glphys[BOLT_GLYPH_LAST] = {
  [TREE_VERTICAL]        = "| ",
  [TREE_BRANCH]          = "|-",
  [TREE_RIGHT]           = "`-",
  [TREE_SPACE]           = "  ",
  [WHITE_CIRCLE]         = "o",
  [BLACK_CIRCLE]         = "*",
  [ARROW]                = "->",
  [MDASH]                = "-",
  [ELLIPSIS]             = "...",
  [WARNING_SIGN]         = "!",
};

static const char *const utf8_glphys[BOLT_GLYPH_LAST] = {
  [TREE_VERTICAL]        = "\342\224\202 ",            /* │  */
  [TREE_BRANCH]          = "\342\224\234\342\224\200", /* ├─ */
  [TREE_RIGHT]           = "\342\224\224\342\224\200", /* └─ */
  [TREE_SPACE]           = "  ",                       /*    */
  [WHITE_CIRCLE]         = "\342\227\213",             /* ○ */
  [BLACK_CIRCLE]         = "\342\227\217",             /* ● */
  [ARROW]                = "\342\206\222",             /* → */
  [MDASH]                = "\342\200\223",             /* – */
  [ELLIPSIS]             = "\342\200\246",             /* … */
  [WARNING_SIGN]         = "\342\232\240",             /* ⚠ */
};


const char *
bolt_glyph (BoltGlyph g)
{
  static const char *const *glyph_table = NULL;

  g_return_val_if_fail (g < BOLT_GLYPH_LAST, "?");

  /* TODO: check for utf-8 support */
  if (G_UNLIKELY (glyph_table == NULL))
    {
      if (bolt_is_fancy_terminal ())
        glyph_table = utf8_glphys;
      else
        glyph_table = ansi_glphys;
    }

  return glyph_table[g];
}