bumped version
[rainbowstream.git] / rainbowstream / rainbow.py
index 284a6282971d54ca320c76968830f530b43f8e04..41b581ff2d68cc5f9d2bbc8464e79df93b7ae6d0 100644 (file)
@@ -27,6 +27,7 @@ from .interactive import *
 from .c_image import *
 from .py3patch import *
 from .emoji import *
+from .util import *
 
 # Global values
 g = {}
@@ -179,7 +180,10 @@ def upgrade_center():
             notice += light_green('pip install -U rainbowstream')
             printNicely(notice)
         else:
-            printNicely(yellow('You are running latest ' + current + ' version.'))
+            notice = light_yellow('You are running latest version (')
+            notice += light_green(current)
+            notice += light_yellow(')')
+            printNicely(notice)
     except:
         pass
 
@@ -198,11 +202,9 @@ def init(args):
     credential = t.account.verify_credentials()
     screen_name = '@' + credential['screen_name']
     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)
@@ -1239,8 +1241,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,
@@ -1253,8 +1259,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,
@@ -1268,7 +1276,14 @@ 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())
@@ -1405,7 +1420,10 @@ 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:
@@ -1662,6 +1680,7 @@ def help():
     usage += s * 2 + light_green('p') + ' will pause the stream.\n'
     usage += s * 2 + light_green('r') + ' will unpause the stream.\n'
     usage += s * 2 + light_green('c') + ' will clear the screen.\n'
+    usage += s * 2 + light_green('v') + ' will show version info.\n'
     usage += s * 2 + light_green('q') + ' will quit.\n'
     # End
     usage += '\n'
@@ -1780,8 +1799,8 @@ cmdset = [
     'p',
     'r',
     'c',
+    'v',
     'q',
-    'version',
 ]
 
 # Handle function set
@@ -1827,8 +1846,8 @@ funcset = [
     pause,
     replay,
     clear,
-    quit,
     upgrade_center,
+    quit,
 ]
 
 
@@ -1906,8 +1925,8 @@ def listen():
             [],  # pause
             [],  # reconnect
             [],  # clear
-            [],  # quit
             [],  # version
+            [],  # quit
         ]
     ))
     init_interactive_shell(d)
@@ -1959,7 +1978,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()
 
 
@@ -2048,10 +2067,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)
@@ -2063,10 +2082,13 @@ def stream(domain, args, name='Rainbow Stream'):
             elif tweet.get('event'):
                 c['events'].append(tweet)
                 print_event(tweet)
-    except TwitterHTTPError:
+    except TwitterHTTPError as e:
         printNicely('')
         printNicely(
-            magenta("We have maximum connection problem with twitter'stream API right now :("))
+            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():
@@ -2079,11 +2101,11 @@ def fly():
         proxy_connect(args)
         init(args)
     # Twitter API connection problem
-    except TwitterHTTPError:
+    except TwitterHTTPError as e:
         printNicely('')
         printNicely(
-            magenta("We have connection problem with twitter'stream API right now :("))
-        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