Package daklib :: Module termcolor
[hide private]
[frames] | no frames]

Source Code for Module daklib.termcolor

 1  # vim:set et sw=4: 
 2  """ 
 3  TermColor utils for dak 
 4   
 5  @contact: Debian FTP Master <ftpmaster@debian.org> 
 6  @copyright: 2019 Mo Zhou <lumin@debian.org> 
 7  @license: GNU General Public License version 2 or later 
 8  """ 
 9   
10  # This program is free software; you can redistribute it and/or modify 
11  # it under the terms of the GNU General Public License as published by 
12  # the Free Software Foundation; either version 2 of the License, or 
13  # (at your option) any later version. 
14   
15  # This program is distributed in the hope that it will be useful, 
16  # but WITHOUT ANY WARRANTY; without even the implied warranty of 
17  # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the 
18  # GNU General Public License for more details. 
19   
20  # You should have received a copy of the GNU General Public License 
21  # along with this program; if not, write to the Free Software 
22  # Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA  02111-1307  USA 
23   
24  ############################################################################### 
25   
26  __all__ = [] 
27   
28  ############################################################################### 
29   
30  _COLORS_ = ('red', 'green', 'yellow', 'blue', 'violet', 'cyan', 'white') 
31  _COLOR_CODES_ = {k: 31 + _COLORS_.index(k) for k in _COLORS_} 
32   
33   
34 -def colorize(s, fg, bg=None, bold=False, ul=False):
35 ''' 36 s: str -- string to be colorized 37 fg: str/int -- foreground color. See _COLORS_ for choices 38 bg: str/int -- background color. See _COLORS_ for choices 39 bold: bool -- bold font? 40 ul: bool -- underline? 41 ''' 42 if fg not in _COLORS_: 43 raise ValueError("Unsupported foreground Color!") 44 if (bg is not None) or any((bold, ul)): 45 raise NotImplementedError 46 return "\x1b[{}m{}\x1b[0;m".format(_COLOR_CODES_[fg], s)
47