1 # SPDX-License-Identifier: Apache-2.0 2 # 3 # Copyright (C) 2015, ARM Limited and contributors. 4 # 5 # Licensed under the Apache License, Version 2.0 (the "License"); you may 6 # not use this file except in compliance with the License. 7 # You may obtain a copy of the License at 8 # 9 # http://www.apache.org/licenses/LICENSE-2.0 10 # 11 # Unless required by applicable law or agreed to in writing, software 12 # distributed under the License is distributed on an "AS IS" BASIS, WITHOUT 13 # WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 # See the License for the specific language governing permissions and 15 # limitations under the License. 16 # 17 18 import sys 19 20 class TestColors: 21 22 level = { 23 'failed' : '\033[0;31m', # Red 24 'good' : '\033[0;32m', # Green 25 'warning' : '\033[0;33m', # Yellow 26 'passed' : '\033[0;34m', # Blue 27 'purple' : '\033[0;35m', # Purple 28 'endc' : '\033[0m' # End color 29 } 30 31 @staticmethod 32 def rate(val, positive_is_good=True): 33 str_val = "{:9.2f}%".format(val) 34 35 if not sys.stdout.isatty(): 36 return str_val 37 38 if not positive_is_good: 39 val = -val 40 41 if val < -10: 42 str_color = TestColors.level['failed'] 43 elif val < 0: 44 str_color = TestColors.level['warning'] 45 elif val < 10: 46 str_color = TestColors.level['passed'] 47 else: 48 str_color = TestColors.level['good'] 49 50 return str_color + str_val + TestColors.level['endc'] 51 52 #vim :set tabstop=4 shiftwidth=4 expandtab 53