terminal.py revision 7816
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 297816Ssteve.reinhardt@amd.comimport sys 307816Ssteve.reinhardt@amd.com 317816Ssteve.reinhardt@amd.com# Intended usage example: 327816Ssteve.reinhardt@amd.com# 337816Ssteve.reinhardt@amd.com# if force_colors: 347816Ssteve.reinhardt@amd.com# from m5.util.terminal import termcap 357816Ssteve.reinhardt@amd.com# elif no_colors: 367816Ssteve.reinhardt@amd.com# from m5.util.terminal import no_termcap as termcap 377816Ssteve.reinhardt@amd.com# else: 387816Ssteve.reinhardt@amd.com# from m5.util.terminal import tty_termcap as termcap 397816Ssteve.reinhardt@amd.com# print termcap.Blue + "This could be blue!" + termcap.Normal 407816Ssteve.reinhardt@amd.com 417816Ssteve.reinhardt@amd.com# ANSI color names in index order 427816Ssteve.reinhardt@amd.comcolor_names = "Black Red Green Yellow Blue Magenta Cyan".split() 437816Ssteve.reinhardt@amd.com 447816Ssteve.reinhardt@amd.com# Character attribute capabilities. Note that not all terminals 457816Ssteve.reinhardt@amd.com# support all of these capabilities, or support them 467816Ssteve.reinhardt@amd.com# differently/meaningfully. For example: 477816Ssteve.reinhardt@amd.com# 487816Ssteve.reinhardt@amd.com# - In PuTTY (with the default settings), Dim has no effect, Standout 497816Ssteve.reinhardt@amd.com# is the same as Reverse, and Blink does not blink but switches to a 507816Ssteve.reinhardt@amd.com# gray background. 517816Ssteve.reinhardt@amd.com# 527816Ssteve.reinhardt@amd.com# Please feel free to add information about other terminals here. 537816Ssteve.reinhardt@amd.com# 547816Ssteve.reinhardt@amd.comcapability_map = { 557816Ssteve.reinhardt@amd.com 'Bold': 'bold', 567816Ssteve.reinhardt@amd.com 'Dim': 'dim', 577816Ssteve.reinhardt@amd.com 'Blink': 'blink', 587816Ssteve.reinhardt@amd.com 'Underline': 'smul', 597816Ssteve.reinhardt@amd.com 'Reverse': 'rev', 607816Ssteve.reinhardt@amd.com 'Standout': 'smso', 617816Ssteve.reinhardt@amd.com 'Normal': 'sgr0' 627816Ssteve.reinhardt@amd.com} 637816Ssteve.reinhardt@amd.com 647816Ssteve.reinhardt@amd.comcapability_names = capability_map.keys() 657816Ssteve.reinhardt@amd.com 667816Ssteve.reinhardt@amd.comdef null_cap_string(s, *args): 677816Ssteve.reinhardt@amd.com return '' 687816Ssteve.reinhardt@amd.com 697816Ssteve.reinhardt@amd.comtry: 707816Ssteve.reinhardt@amd.com import curses 717816Ssteve.reinhardt@amd.com curses.setupterm() 727816Ssteve.reinhardt@amd.com def cap_string(s, *args): 737816Ssteve.reinhardt@amd.com cap = curses.tigetstr(s) 747816Ssteve.reinhardt@amd.com if cap: 757816Ssteve.reinhardt@amd.com return curses.tparm(cap, *args) 767816Ssteve.reinhardt@amd.com else: 777816Ssteve.reinhardt@amd.com return '' 787816Ssteve.reinhardt@amd.comexcept: 797816Ssteve.reinhardt@amd.com cap_string = null_cap_string 807816Ssteve.reinhardt@amd.com 817816Ssteve.reinhardt@amd.comclass ColorStrings(object): 827816Ssteve.reinhardt@amd.com def __init__(self, cap_string): 837816Ssteve.reinhardt@amd.com for i, c in enumerate(color_names): 847816Ssteve.reinhardt@amd.com setattr(self, c, cap_string('setaf', i)) 857816Ssteve.reinhardt@amd.com for name, cap in capability_map.iteritems(): 867816Ssteve.reinhardt@amd.com setattr(self, name, cap_string(cap)) 877816Ssteve.reinhardt@amd.com 887816Ssteve.reinhardt@amd.comtermcap = ColorStrings(cap_string) 897816Ssteve.reinhardt@amd.comno_termcap = ColorStrings(null_cap_string) 907816Ssteve.reinhardt@amd.com 917816Ssteve.reinhardt@amd.comif sys.stdout.isatty(): 927816Ssteve.reinhardt@amd.com tty_termcap = termcap 937816Ssteve.reinhardt@amd.comelse: 947816Ssteve.reinhardt@amd.com tty_termcap = no_termcap 957816Ssteve.reinhardt@amd.com 967816Ssteve.reinhardt@amd.comdef test_termcap(obj): 977816Ssteve.reinhardt@amd.com for c_name in color_names: 987816Ssteve.reinhardt@amd.com c_str = getattr(obj, c_name) 997816Ssteve.reinhardt@amd.com print c_str + c_name + obj.Normal 1007816Ssteve.reinhardt@amd.com for attr_name in capability_names: 1017816Ssteve.reinhardt@amd.com if attr_name == 'Normal': 1027816Ssteve.reinhardt@amd.com continue 1037816Ssteve.reinhardt@amd.com attr_str = getattr(obj, attr_name) 1047816Ssteve.reinhardt@amd.com print attr_str + c_str + attr_name + " " + c_name + obj.Normal 1057816Ssteve.reinhardt@amd.com print obj.Bold + obj.Underline + \ 1067816Ssteve.reinhardt@amd.com c_name + "Bold Underline " + c + obj.Normal 1077816Ssteve.reinhardt@amd.com 1087816Ssteve.reinhardt@amd.comif __name__ == '__main__': 1097816Ssteve.reinhardt@amd.com print "=== termcap enabled ===" 1107816Ssteve.reinhardt@amd.com test_termcap(termcap) 1117816Ssteve.reinhardt@amd.com print termcap.Normal 1127816Ssteve.reinhardt@amd.com print "=== termcap disabled ===" 1137816Ssteve.reinhardt@amd.com test_termcap(no_termcap) 114