17816Ssteve.reinhardt@amd.com# Copyright (c) 2011 Advanced Micro Devices, Inc. 27816Ssteve.reinhardt@amd.com# All rights reserved. 37816Ssteve.reinhardt@amd.com# 47816Ssteve.reinhardt@amd.com# Redistribution and use in source and binary forms, with or without 57816Ssteve.reinhardt@amd.com# modification, are permitted provided that the following conditions are 67816Ssteve.reinhardt@amd.com# met: redistributions of source code must retain the above copyright 77816Ssteve.reinhardt@amd.com# notice, this list of conditions and the following disclaimer; 87816Ssteve.reinhardt@amd.com# redistributions in binary form must reproduce the above copyright 97816Ssteve.reinhardt@amd.com# notice, this list of conditions and the following disclaimer in the 107816Ssteve.reinhardt@amd.com# documentation and/or other materials provided with the distribution; 117816Ssteve.reinhardt@amd.com# neither the name of the copyright holders nor the names of its 127816Ssteve.reinhardt@amd.com# contributors may be used to endorse or promote products derived from 137816Ssteve.reinhardt@amd.com# this software without specific prior written permission. 147816Ssteve.reinhardt@amd.com# 157816Ssteve.reinhardt@amd.com# THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 167816Ssteve.reinhardt@amd.com# "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 177816Ssteve.reinhardt@amd.com# LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR 187816Ssteve.reinhardt@amd.com# A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT 197816Ssteve.reinhardt@amd.com# OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, 207816Ssteve.reinhardt@amd.com# SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT 217816Ssteve.reinhardt@amd.com# LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, 227816Ssteve.reinhardt@amd.com# DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY 237816Ssteve.reinhardt@amd.com# THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT 247816Ssteve.reinhardt@amd.com# (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE 257816Ssteve.reinhardt@amd.com# OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE. 267816Ssteve.reinhardt@amd.com# 277816Ssteve.reinhardt@amd.com# Author: Steve Reinhardt 287816Ssteve.reinhardt@amd.com 2912563Sgabeblack@google.comfrom __future__ import print_function 3013714Sandreas.sandberg@arm.comfrom __future__ import absolute_import 3112563Sgabeblack@google.com 327816Ssteve.reinhardt@amd.comimport sys 337816Ssteve.reinhardt@amd.com 347816Ssteve.reinhardt@amd.com# Intended usage example: 357816Ssteve.reinhardt@amd.com# 367816Ssteve.reinhardt@amd.com# if force_colors: 377816Ssteve.reinhardt@amd.com# from m5.util.terminal import termcap 387816Ssteve.reinhardt@amd.com# elif no_colors: 397816Ssteve.reinhardt@amd.com# from m5.util.terminal import no_termcap as termcap 407816Ssteve.reinhardt@amd.com# else: 417816Ssteve.reinhardt@amd.com# from m5.util.terminal import tty_termcap as termcap 4212563Sgabeblack@google.com# print(termcap.Blue + "This could be blue!" + termcap.Normal) 437816Ssteve.reinhardt@amd.com 447816Ssteve.reinhardt@amd.com# ANSI color names in index order 457816Ssteve.reinhardt@amd.comcolor_names = "Black Red Green Yellow Blue Magenta Cyan".split() 467816Ssteve.reinhardt@amd.com 477816Ssteve.reinhardt@amd.com# Character attribute capabilities. Note that not all terminals 487816Ssteve.reinhardt@amd.com# support all of these capabilities, or support them 497816Ssteve.reinhardt@amd.com# differently/meaningfully. For example: 507816Ssteve.reinhardt@amd.com# 517816Ssteve.reinhardt@amd.com# - In PuTTY (with the default settings), Dim has no effect, Standout 527816Ssteve.reinhardt@amd.com# is the same as Reverse, and Blink does not blink but switches to a 537816Ssteve.reinhardt@amd.com# gray background. 547816Ssteve.reinhardt@amd.com# 557816Ssteve.reinhardt@amd.com# Please feel free to add information about other terminals here. 567816Ssteve.reinhardt@amd.com# 577816Ssteve.reinhardt@amd.comcapability_map = { 587816Ssteve.reinhardt@amd.com 'Bold': 'bold', 597816Ssteve.reinhardt@amd.com 'Dim': 'dim', 607816Ssteve.reinhardt@amd.com 'Blink': 'blink', 617816Ssteve.reinhardt@amd.com 'Underline': 'smul', 627816Ssteve.reinhardt@amd.com 'Reverse': 'rev', 637816Ssteve.reinhardt@amd.com 'Standout': 'smso', 647816Ssteve.reinhardt@amd.com 'Normal': 'sgr0' 657816Ssteve.reinhardt@amd.com} 667816Ssteve.reinhardt@amd.com 6713709Sandreas.sandberg@arm.comcapability_names = list(capability_map.keys()) 687816Ssteve.reinhardt@amd.com 697816Ssteve.reinhardt@amd.comdef null_cap_string(s, *args): 707816Ssteve.reinhardt@amd.com return '' 717816Ssteve.reinhardt@amd.com 727816Ssteve.reinhardt@amd.comtry: 737816Ssteve.reinhardt@amd.com import curses 747816Ssteve.reinhardt@amd.com curses.setupterm() 757816Ssteve.reinhardt@amd.com def cap_string(s, *args): 767816Ssteve.reinhardt@amd.com cap = curses.tigetstr(s) 777816Ssteve.reinhardt@amd.com if cap: 787816Ssteve.reinhardt@amd.com return curses.tparm(cap, *args) 797816Ssteve.reinhardt@amd.com else: 807816Ssteve.reinhardt@amd.com return '' 817816Ssteve.reinhardt@amd.comexcept: 827816Ssteve.reinhardt@amd.com cap_string = null_cap_string 837816Ssteve.reinhardt@amd.com 847816Ssteve.reinhardt@amd.comclass ColorStrings(object): 857816Ssteve.reinhardt@amd.com def __init__(self, cap_string): 867816Ssteve.reinhardt@amd.com for i, c in enumerate(color_names): 877816Ssteve.reinhardt@amd.com setattr(self, c, cap_string('setaf', i)) 8813709Sandreas.sandberg@arm.com for name, cap in capability_map.items(): 897816Ssteve.reinhardt@amd.com setattr(self, name, cap_string(cap)) 907816Ssteve.reinhardt@amd.com 917816Ssteve.reinhardt@amd.comtermcap = ColorStrings(cap_string) 927816Ssteve.reinhardt@amd.comno_termcap = ColorStrings(null_cap_string) 937816Ssteve.reinhardt@amd.com 947816Ssteve.reinhardt@amd.comif sys.stdout.isatty(): 957816Ssteve.reinhardt@amd.com tty_termcap = termcap 967816Ssteve.reinhardt@amd.comelse: 977816Ssteve.reinhardt@amd.com tty_termcap = no_termcap 987816Ssteve.reinhardt@amd.com 998947Sandreas.hansson@arm.comdef get_termcap(use_colors = None): 1008947Sandreas.hansson@arm.com if use_colors: 1018947Sandreas.hansson@arm.com return termcap 1028947Sandreas.hansson@arm.com elif use_colors is None: 1038947Sandreas.hansson@arm.com # option unspecified; default behavior is to use colors iff isatty 1048947Sandreas.hansson@arm.com return tty_termcap 1058947Sandreas.hansson@arm.com else: 1068947Sandreas.hansson@arm.com return no_termcap 1078947Sandreas.hansson@arm.com 1087816Ssteve.reinhardt@amd.comdef test_termcap(obj): 1097816Ssteve.reinhardt@amd.com for c_name in color_names: 1107816Ssteve.reinhardt@amd.com c_str = getattr(obj, c_name) 11112563Sgabeblack@google.com print(c_str + c_name + obj.Normal) 1127816Ssteve.reinhardt@amd.com for attr_name in capability_names: 1137816Ssteve.reinhardt@amd.com if attr_name == 'Normal': 1147816Ssteve.reinhardt@amd.com continue 1157816Ssteve.reinhardt@amd.com attr_str = getattr(obj, attr_name) 11612563Sgabeblack@google.com print(attr_str + c_str + attr_name + " " + c_name + obj.Normal) 11712563Sgabeblack@google.com print(obj.Bold + obj.Underline + 11812563Sgabeblack@google.com c_name + "Bold Underline " + c + obj.Normal) 1197816Ssteve.reinhardt@amd.com 1207816Ssteve.reinhardt@amd.comif __name__ == '__main__': 12112563Sgabeblack@google.com print("=== termcap enabled ===") 1227816Ssteve.reinhardt@amd.com test_termcap(termcap) 12312563Sgabeblack@google.com print(termcap.Normal) 12412563Sgabeblack@google.com print("=== termcap disabled ===") 1257816Ssteve.reinhardt@amd.com test_termcap(no_termcap) 126