keyword None
[rainbowstream.git] / rainbowstream / rainbow.py
index f489c3f67a4a3b097baf3e4f2f4ebd1ebe21c3c5..9570fee261d8f43dcaece15dfb9e4371d35da010 100644 (file)
@@ -19,7 +19,7 @@ from .config import *
 
 g = {}
 
-def draw(t):
+def draw(t,keyword=None):
     """
     Draw the rainbow
     """
@@ -35,9 +35,15 @@ def draw(t):
     user = cycle_color(name) + grey(' ' + '@' + screen_name + ' ')
     clock = grey('[' + time + ']')
     tweet = text.split()
+    # Highlight RT
     tweet = map(lambda x: grey(x) if x == 'RT' else x, tweet)
+    # Highlight screen_name
     tweet = map(lambda x: cycle_color(x) if x[0] == '@' else x, tweet)
+    # Highlight link
     tweet = map(lambda x: cyan(x) if x[0:7] == 'http://' else x, tweet)
+    # Highlight search keyword
+    if keyword:
+        tweet = map(lambda x: on_yellow(x) if ''.join(c for c in x if c.isalnum()).lower() == keyword.lower() else x, tweet)
     tweet = ' '.join(tweet)
 
     # Draw rainbow
@@ -133,9 +139,9 @@ def search():
     t = Twitter(auth=authen())
     rel = t.search.tweets(q='#' + g['stuff'])['statuses']
     printNicely(grey('**************************************************************************************\n'))
-    print('Newest ',SEARCH_MAX_RECORD, ' tweet: \n')
+    print('Newest',SEARCH_MAX_RECORD, 'tweet: \n')
     for i in xrange(5):
-        draw(t=rel[i])
+        draw(t=rel[i],keyword=g['stuff'].strip())
     printNicely(grey('**************************************************************************************\n'))
 
 
@@ -147,8 +153,9 @@ def help():
     Hi boss! I'm ready to serve you right now!
     ----------------------------------------------------
     "!" at the beginning will start to tweet immediately
-    "/" at the beginning will search for 5 lastest twwet
-    "?" will print this help once again
+    "/" at the beginning will search for 5 newest tweet
+    "?" or "h" will print this help once again
+    "c" will clear the terminal
     "q" will exit
     ----------------------------------------------------
     Hvae fun and hang tight!
@@ -165,6 +172,13 @@ def quit():
     sys.exit()
 
 
+def clear():
+    """
+    Exit all
+    """
+    os.system('clear')
+
+
 def process(line):
     """
     Process switch by start of line
@@ -173,6 +187,8 @@ def process(line):
         '!' : tweet,
         '/' : search,
         '?' : help,
+        'h' : help,
+        'c' : clear,
         'q' : quit,
     }.get(line[0],lambda: sys.stdout.write(g['decorated_name']))
 
@@ -196,8 +212,6 @@ def stream():
 
     # The Logo
     ascii_art()
-    print("Tip: Press ENTER and type a '?' to see what command mode can do for you")
-    print('\n')
     # These arguments are optional:
     stream_args = dict(
         timeout=args.timeout,