1#!/usr/bin/env python
2# -*- coding: utf-8 -*-
3
4#
5# Copyright 2017, Data61
6# Commonwealth Scientific and Industrial Research Organisation (CSIRO)
7# ABN 41 687 119 230.
8#
9# This software may be distributed and modified according to the terms of
10# the BSD 2-Clause license. Note that NO WARRANTY is provided.
11# See "LICENSE_BSD2.txt" for details.
12#
13# @TAG(DATA61_BSD)
14#
15
16'''
17Home for utility functions for learning things about the terminal we're running
18in.
19'''
20
21from __future__ import absolute_import, division, print_function, \
22    unicode_literals
23from camkes.internal.seven import cmp, filter, map, zip
24
25from .memoization import memoize
26import os, subprocess, sys
27
28# Various ANSI terminal control sequences. For now, we only define the ones we
29# need.
30BOLD = '\033[1m'
31RED = '\033[31m'
32RESET = '\033[0m'
33
34@memoize()
35def terminal_supports_colour():
36    if not sys.stdout.isatty():
37        return False
38    try:
39        with open(os.devnull, 'wt') as f:
40            colours = subprocess.check_output(['tput', 'colors'], stderr=f)
41    except (subprocess.CalledProcessError, OSError):
42        return False
43    try:
44        return int(colours) > 0
45    except ValueError:
46        return False
47