Back to home page

EIC code displayed by LXR

 
 

    


File indexing completed on 2026-04-09 07:48:47

0001 #!/usr/bin/env python
0002 #
0003 # Copyright (c) 2019 Opticks Team. All Rights Reserved.
0004 #
0005 # This file is part of Opticks
0006 # (see https://bitbucket.org/simoncblyth/opticks).
0007 #
0008 # Licensed under the Apache License, Version 2.0 (the "License"); 
0009 # you may not use this file except in compliance with the License.  
0010 # You may obtain a copy of the License at
0011 #
0012 #   http://www.apache.org/licenses/LICENSE-2.0
0013 #
0014 # Unless required by applicable law or agreed to in writing, software 
0015 # distributed under the License is distributed on an "AS IS" BASIS, 
0016 # WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  
0017 # See the License for the specific language governing permissions and 
0018 # limitations under the License.
0019 #
0020 
0021 
0022 
0023 def example():
0024     """
0025     http://ozzmaker.com/add-colour-to-text-in-python/
0026     """
0027     print("\033[0;37;40m Normal text\n")
0028     print("\033[2;37;40m Underlined text\033[0;37;40m \n")
0029     print("\033[1;37;40m Bright Colour\033[0;37;40m \n")
0030     print("\033[3;37;40m Negative Colour\033[0;37;40m \n")
0031     print("\033[5;37;40m Negative Colour\033[0;37;40m\n")
0032      
0033     print("\033[1;37;40m \033[2;37:40m TextColour BlackBackground          TextColour GreyBackground                WhiteText ColouredBackground\033[0;37;40m\n")
0034     print("\033[1;30;40m Dark Gray      \033[0m 1;30;40m            \033[0;30;47m Black      \033[0m 0;30;47m               \033[0;37;41m Black      \033[0m 0;37;41m")
0035     print("\033[1;31;40m Bright Red     \033[0m 1;31;40m            \033[0;31;47m Red        \033[0m 0;31;47m               \033[0;37;42m Black      \033[0m 0;37;42m")
0036     print("\033[1;32;40m Bright Green   \033[0m 1;32;40m            \033[0;32;47m Green      \033[0m 0;32;47m               \033[0;37;43m Black      \033[0m 0;37;43m")
0037     print("\033[1;33;40m Yellow         \033[0m 1;33;40m            \033[0;33;47m Brown      \033[0m 0;33;47m               \033[0;37;44m Black      \033[0m 0;37;44m")
0038     print("\033[1;34;40m Bright Blue    \033[0m 1;34;40m            \033[0;34;47m Blue       \033[0m 0;34;47m               \033[0;37;45m Black      \033[0m 0;37;45m")
0039     print("\033[1;35;40m Bright Magenta \033[0m 1;35;40m            \033[0;35;47m Magenta    \033[0m 0;35;47m               \033[0;37;46m Black      \033[0m 0;37;46m")
0040     print("\033[1;36;40m Bright Cyan    \033[0m 1;36;40m            \033[0;36;47m Cyan       \033[0m 0;36;47m               \033[0;37;47m Black      \033[0m 0;37;47m")
0041     print("\033[1;37;40m White          \033[0m 1;37;40m            \033[0;37;40m Light Grey \033[0m 0;37;40m               \033[0;37;48m Black      \033[0m 0;37;48m")
0042 
0043 
0044 
0045 
0046 def print_format_table():
0047     """
0048     https://stackoverflow.com/questions/287871/how-to-print-colored-text-in-terminal-in-python
0049 
0050     prints table of formatted text format options
0051     """
0052     for style in range(8):
0053         print(" style: %d " % style ) 
0054         for fg in range(30,38):
0055             s1 = ''
0056             for bg in range(40,48):
0057                 format = ';'.join([str(style), str(fg), str(bg)])
0058                 s1 += '\x1b[%sm %s \x1b[0m' % (format, format)
0059             pass
0060             print(s1)
0061         print('\n')
0062     pass
0063 
0064 
0065 def prTest():
0066     for fn in [prRed,prGreen,prYellow,prLightPurple,prCyan,prLightGray,prBlack]:
0067         fn("Hello world %d " % 42 )
0068     pass
0069 
0070 def prRed(prt): print("\033[91m {}\033[00m" .format(prt))
0071 def prGreen(prt): print("\033[92m {}\033[00m" .format(prt))
0072 def prYellow(prt): print("\033[93m {}\033[00m" .format(prt))
0073 def prLightPurple(prt): print("\033[94m {}\033[00m" .format(prt))
0074 def prPurple(prt): print("\033[95m {}\033[00m" .format(prt))
0075 def prCyan(prt): print("\033[96m {}\033[00m" .format(prt))
0076 def prLightGray(prt): print("\033[97m {}\033[00m" .format(prt))
0077 def prBlack(prt): print("\033[98m {}\033[00m" .format(prt))
0078 
0079 
0080 ansi_ = lambda msg, codes:unicode("\x1b[%sm%s\x1b[0m" % (";".join(map(str, codes)), msg))
0081 
0082 
0083 def ansiTest():
0084     """ 
0085     http://www.lihaoyi.com/post/BuildyourownCommandLinewithANSIescapecodes.html
0086     """
0087     print(ansi_("hello",(7,31,47)) + " world")
0088     print(ansi_("hello",(47,)) )
0089     print(ansi_("hello",(31,47,)) )
0090     print(ansi_("hello",(5,31,47,)) )   # blinking 
0091     print(ansi_("hello",(5,47,31)) )    # blinking red on gray : seems order of fg and bg makes no difference
0092 
0093     print("\n".join(map(lambda c:ansi_("hello",(c,)), range(30,38))))   # foreground
0094 
0095     print("\n".join(map(lambda c:ansi_("hello",(c,1)), range(30,38))))  # foreground brighter
0096 
0097     print("\n".join(map(lambda c:ansi_("hello",(c,)), range(40,48))))   # background
0098 
0099 
0100 if __name__ == '__main__':
0101     #example()
0102     #print_format_table()
0103     #prTest()
0104 
0105     ansiTest()
0106