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