Advertisement
Guest User

Untitled

a guest
Feb 1st, 2016
39
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.89 KB | None | 0 0
  1. def foo():
  2.     return 'abc'
  3.  
  4. def bar():
  5.     return 'hello'
  6.  
  7.  
  8.  
  9. def colorize(text, bg, fg="#ffffff", next_bg="#333333", arrow=None):
  10.     """ Format text in pango formatting and add powerline characters
  11.  
  12.    text: string
  13.        Text to format
  14.  
  15.    bg: string
  16.        Color name or html colorcode, background color
  17.  
  18.    fg: string (#ffffff)
  19.        Color name or html colorcode, foreground color
  20.  
  21.    next_bg: string (#333333)
  22.        Color name or html colorcode, background color for powerline arrow
  23.  
  24.    arrow: string
  25.        'l' or 'r': powerline arrow to append left or right
  26.        """
  27.  
  28.     out_str = "<span background=\"%s\" foreground=\"%s\"> %s </span>" % (bg, fg, text)
  29.  
  30.     arrows = {'left': '', 'right': ''}
  31.  
  32.     if arrow is not None:
  33.  
  34.         arrow_str = "<span foreground=\"%s\" background=\"%s\">%s</span>"\
  35.                      % (bg, next_bg, arrows[arrow])
  36.         if arrow == 'left':
  37.             out_str = arrow_str + out_str
  38.         else:
  39.             out_str = out_str + arrow_str
  40.  
  41.     return out_str
  42.  
  43.  
  44.  
  45. def combine_funs(funs, colors, fg_color, direction='left', bg='#333333'):
  46.  
  47.     colors.append(bg)           #  Set correct background after last arrow
  48.  
  49.     # If left, switch fore/background colors of arrow icons
  50.     offset = 1 if direction == 'right' else -1
  51.  
  52.     # Loop through items and colors, render formatted text
  53.     out_str = ''
  54.     for index, item in enumerate(funs):
  55.         try:
  56.             cmd_output = line[index]()
  57.         except:
  58.             cmd_output = '#ERROR'
  59.  
  60.         out_str = out_str + colorize(cmd_output, colors[index], fg_color,
  61.                                      colors[index+offset], direction)
  62.     return out_str
  63.  
  64. funs = [foo, bar]
  65. colors = ['#A22300', '#CD2300']     # Background
  66. fg_color = 'white'                  # Foreground
  67. direction = 'left'                  # Pointing direction of powerline arrows
  68.  
  69. combine_funs(funs, colors, text_color, direction)
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement