Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- #!/usr/bin/env python3
- import sys
- import re
- urlRE = re.compile("((?:ht|f)tps?://\w+(?:\.\w+)*(?::\d+)?(?:/[a-zA-Z0-9\.\-_~]*)*(?:\?(?:\w+=\S+)+)?)")
- titleRE = re.compile("([\[\(])(#?[a-zA-Z0-9_-]+)([\]\)])")
- chanRE = re.compile(r"(\s|\A)(#\S+)(\s|\Z)")
- colorRepl = "\x03{}\x02\x02{}\x0F"
- titleRepl = "{0[border]}{1}\x0F{0[text]}{2}\x0F{0[border]}{3}\x0F"
- urlColors = (11, 10, 8, 7, 9, 3)
- titleColors = {"border": "\x033\x02", "text": "\x02"}
- def colorURLs(topic):
- currentColor = 0
- def nextColor(urlMatch):
- nonlocal currentColor
- color = urlColors[currentColor]
- currentColor = (currentColor + 1) % len(urlColors)
- return colorRepl.format(color, urlMatch.group(1))
- return urlRE.sub(nextColor, topic)
- def colorTitle(topic):
- def reTitleColor(topic):
- return titleRepl.format(titleColors, *topic.groups())
- return titleRE.sub(reTitleColor, topic)
- def colorChans(topic):
- def reChanColor(chan):
- return chan.group(1) + colorRepl.format(4, chan.group(2)) + chan.group(3)
- return chanRE.sub(reChanColor, topic)
- colorFuncs = (colorURLs, colorTitle, colorChans)
- def colorTopic(topic):
- ret = topic
- for func in colorFuncs:
- ret = func(ret)
- return ret
- if __name__ == "__main__":
- if len(sys.argv) == 1:
- try:
- while 1:
- nextTopic = input()
- print(colorTopic(nextTopic))
- except KeyboardInterrupt:
- print()
- sys.exit(1)
- except EOFError:
- sys.exit()
- else:
- print(colorTopic(" ".join(sys.argv[1:])))
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement