Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- import random
- from colored import fg
- class RandomColorDict(dict):
- """Color storage class.
- If the value for a key is 'random', obj[key] returns a random fg(color).
- For nonexistent keys, obj[notakey] will also return a random fg(color),
- instead of throwing a KeyError. Each instance of the class stores its
- own PRNG.
- Use obj.get(key) if you need the real value (nonexistent keys will return
- None)."""
- def __init__(self, choices, *args, **kwargs):
- super(RandomColorDict, self).__init__(*args, **kwargs)
- self.random = random.Random()
- self.random.seed()
- self.choices = choices
- def __getitem__(self, key):
- """Overload to return a random fg(color) if the value is 'random'
- or the key does not exist."""
- val = None
- try:
- val = super(RandomColor, self).__getitem__(key)
- except:
- # catch nonexistent keys and return a random value
- val = 'random'
- if val == 'random':
- return fg(self.random.choice(self.choices))
- # could add other attributes or bg colors here but might be too much
- return val
- ### end RandomColorDict
- # sample use
- from colored import fg, bg, attr
- colors = list(range(19,231))
- # define the theme
- mytheme = RandomColorDict( colors, {
- 'ui_info': fg('blue'), # UI (info msg)
- 'ui_error': fg('red')+attr('bold'), # UI (error msg)
- 'user_display': 'random', # user detail
- 'spoiler': fg('red'), # spoiler text
- 'timestamp': attr('dim'), # timestamps (general use)
- 'boosttoot': 'random', # timeline (header & content)
- 'toot_name': 'random', # timeline (name line)
- 'toot_content': fg('grey_82'), # timeline (content, summary)
- 'toot_reply': fg('blue'), # timeline (reply header)
- 'vis': fg('blue'), # timeline (id line)
- 'counts': fg('cyan'), # timeline (id line)
- 'id': fg('yellow'), # timeline (id line)
- 'mention': fg('magenta_2b'), # notifications
- 'favourite': fg('sea_green_2'), # notifications
- 'reblog': fg('yellow_2'), # notifications
- 'follow': fg('red_1'), # notifications
- # ... etc ...
- })
- # now print the pieces
- # first line -- random name color, dimmed timestamp
- print( stylize(display_name, mytheme['toot_name']) + stylize(timestamp, mytheme['timestamp']) )
- # second line -- cyan, yellow, blue
- print( stylize(counts, mytheme['counts']) + stylize(id, mytheme['id']) + stylize(vis, mytheme['vis']) )
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement