1# DExTer : Debugging Experience Tester
2# ~~~~~~   ~         ~~         ~   ~~
3#
4# Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
5# See https://llvm.org/LICENSE.txt for license information.
6# SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
7"""Provides POSIX implementation of formatted/colored console output."""
8
9from ..PrettyOutputBase import PrettyOutputBase, _lock
10
11
12class PrettyOutput(PrettyOutputBase):
13    def _color(self, text, color, stream, lock=_lock):
14        """Use ANSI escape codes to provide color on Linux."""
15        stream = self._set_valid_stream(stream)
16        with lock:
17            if stream.color_enabled:
18                text = '\033[{}m{}\033[0m'.format(color, text)
19            self._write(text, stream)
20
21    def red_impl(self, text, stream=None, **kwargs):
22        self._color(text, 91, stream, **kwargs)
23
24    def yellow_impl(self, text, stream=None, **kwargs):
25        self._color(text, 93, stream, **kwargs)
26
27    def green_impl(self, text, stream=None, **kwargs):
28        self._color(text, 92, stream, **kwargs)
29
30    def blue_impl(self, text, stream=None, **kwargs):
31        self._color(text, 96, stream, **kwargs)
32
33    def default_impl(self, text, stream=None, **kwargs):
34        self._color(text, 0, stream, **kwargs)
35