Merge pull request #73 from polarkac/custom_prefix
[rainbowstream.git] / rainbowstream / rainbow.py
index 2d1be923b83181ae88199ab3a53dd46f0dadfc40..fe144015df00367a566d8e0a1b5cc0abf4cd23b2 100644 (file)
@@ -27,7 +27,7 @@ from .interactive import *
 from .c_image import *
 from .py3patch import *
 from .emoji import *
-from .util import printTwitterErrors
+from .util import *
 
 # Global values
 g = {}
@@ -204,9 +204,9 @@ def init(args):
     name = credential['name']
     if not get_config('PREFIX'):
         set_config('PREFIX', screen_name)
-    c['PREFIX'] = emojize(c['PREFIX'])
-    g['PREFIX'] = u2str(c['PREFIX'])
     c['original_name'] = g['original_name'] = screen_name[1:]
+    g['listname'] = g['keyword'] = ''
+    g['PREFIX'] = u2str(emojize(format_prefix()))
     g['full_name'] = name
     g['decorated_name'] = lambda x: color_func(
         c['DECORATED_NAME'])('[' + x + ']: ', rl=True)
@@ -1243,8 +1243,12 @@ def switch():
             # Kill old thread
             g['stream_stop'] = True
             args.track_keywords = keyword
+            # Set the variable to tracked keyword
+            # and reset the listname
+            g['keyword'] = keyword
+            g['listname'] = ''
             # Reset prefix
-            g['PREFIX'] = u2str(emojize(c['PREFIX']))
+            g['PREFIX'] = u2str(emojize(format_prefix(keyword = g['keyword'])))
             # Start new thread
             th = threading.Thread(
                 target=stream,
@@ -1257,8 +1261,10 @@ def switch():
         elif target == 'mine':
             # Kill old thread
             g['stream_stop'] = True
+            # Reset the tracked keyword and listname
+            g['keyword'] = g['listname'] = ''
             # Reset prefix
-            g['PREFIX'] = u2str(emojize(c['PREFIX']))
+            g['PREFIX'] = u2str(emojize(format_prefix()))
             # Start new thread
             th = threading.Thread(
                 target=stream,
@@ -1272,7 +1278,12 @@ def switch():
         elif target == 'list':
             owner, slug = get_slug()
             # Force python 2 not redraw readline buffer
-            g['PREFIX'] = g['cmd'] = '/'.join([owner, slug])
+            listname = '/'.join([owner, slug])
+            # Set the listname variable 
+            # and reset tracked keyword
+            g['listname'] = listname
+            g['keyword'] = ''
+            g['PREFIX'] = g['cmd'] = u2str(emojize(format_prefix(listname = g['listname'])))
             printNicely(light_yellow('getting list members ...'))
             # Get members
             t = Twitter(auth=authen())
@@ -1409,7 +1420,7 @@ def config():
                 g['decorated_name'] = lambda x: color_func(
                     c['DECORATED_NAME'])('[' + x + ']: ')
             elif key == 'PREFIX':
-                g['PREFIX'] = u2str(emojize(c['PREFIX']))
+                g['PREFIX'] = u2str(emojize(format_prefix(listname = g['listname'], keyword = g['keyword'])))
             reload_config()
             printNicely(green('Updated successfully.'))
         except:
@@ -1964,7 +1975,7 @@ def reconn_notice():
         light_green("h stream") + \
         light_magenta(" for more details.")
     printNicely(guide)
-    sys.stdout.write(g['decorated_name'](c['PREFIX']))
+    sys.stdout.write(g['decorated_name'](g['PREFIX']))
     sys.stdout.flush()
 
 
@@ -2053,10 +2064,10 @@ def stream(domain, args, name='Rainbow Stream'):
                 # the 1st character of that word
                 if current_buffer and g['cmd'] != current_buffer:
                     sys.stdout.write(
-                        g['decorated_name'](c['PREFIX']) + str2u(current_buffer))
+                        g['decorated_name'](g['PREFIX']) + current_buffer)
                     sys.stdout.flush()
                 elif not c['HIDE_PROMPT']:
-                    sys.stdout.write(g['decorated_name'](c['PREFIX']))
+                    sys.stdout.write(g['decorated_name'](g['PREFIX']))
                     sys.stdout.flush()
             elif tweet.get('direct_message'):
                 # Check the semaphore pause and lock (stream process only)
@@ -2071,8 +2082,10 @@ def stream(domain, args, name='Rainbow Stream'):
     except TwitterHTTPError as e:
         printNicely('')
         printNicely(
-            magenta("We have maximum connection problem with twitter'stream API right now :("))
-        printTwitterErrors(e)
+            magenta("We have connection problem with twitter'stream API right now :("))
+        detail_twitter_error(e)
+        sys.stdout.write(g['decorated_name'](g['PREFIX']))
+        sys.stdout.flush()
 
 
 def fly():
@@ -2088,9 +2101,8 @@ def fly():
     except TwitterHTTPError as e:
         printNicely('')
         printNicely(
-            magenta("We have connection problem with twitter'stream API right now :("))
-        printTwitterErrors(e)
-        printNicely(magenta("Let's try again later."))
+            magenta("We have connection problem with twitter'REST API right now :("))
+        detail_twitter_error(e)
         save_history()
         sys.exit()
     # Proxy connection problem