Hide keyboard shortcuts

Hot-keys on this page

r m x p   toggle line displays

j k   next/prev highlighted chunk

0   (zero) top of page

1   (one) first highlighted chunk

1""" 

2Generates a dictionary of ANSI escape codes. 

3 

4http://en.wikipedia.org/wiki/ANSI_escape_code 

5 

6Uses colorama as an optional dependency to support color on Windows 

7 

8--- 

9Copied from colorlog 

10--- 

11""" 

12 

13__all__ = ('escape_codes', 'parse_colors') 

14 

15 

16# Returns escape codes from format codes 

17def esc(*x): 

18 return '\033[' + ';'.join(x) + 'm' 

19 

20 

21# The initial list of escape codes 

22escape_codes = { 

23 'reset': esc('0'), 

24 'bold': esc('01'), 

25 'thin': esc('02') 

26} 

27 

28# The color names 

29COLORS = [ 

30 'black', 

31 'red', 

32 'green', 

33 'yellow', 

34 'blue', 

35 'purple', 

36 'cyan', 

37 'white' 

38] 

39 

40PREFIXES = [ 

41 # Foreground without prefix 

42 ('3', ''), ('01;3', 'bold_'), ('02;3', 'thin_'), 

43 

44 # Foreground with fg_ prefix 

45 ('3', 'fg_'), ('01;3', 'fg_bold_'), ('02;3', 'fg_thin_'), 

46 

47 # Background with bg_ prefix - bold/light works differently 

48 ('4', 'bg_'), ('10', 'bg_bold_'), 

49] 

50 

51for prefix, prefix_name in PREFIXES: 

52 for code, name in enumerate(COLORS): 

53 escape_codes[prefix_name + name] = esc(prefix + str(code)) 

54 

55 

56def parse_colors(sequence): 

57 """Return escape codes from a color sequence.""" 

58 return ''.join(escape_codes[n] for n in sequence.split(',') if n)