Merge pull request #73 from polarkac/custom_prefix
authorOrakaro <DTVD@users.noreply.github.com>
Thu, 30 Oct 2014 04:17:48 +0000 (13:17 +0900)
committerOrakaro <DTVD@users.noreply.github.com>
Thu, 30 Oct 2014 04:17:48 +0000 (13:17 +0900)
Custom formatted prefix

docs/index.rst
rainbowstream/rainbow.py
rainbowstream/util.py

index db6cc91..9dd732e 100644 (file)
@@ -323,7 +323,12 @@ You also can view or set a new value of every config key by ``config`` command (
 \r
 -  ``HIDE_PROMPT``: hide prompt after receiving a tweet or not.\r
 \r
--  ``PREFIX``: display string of prompt.\r
+-  ``PREFIX``: display formated string of prompt.\r
+\r
+    + ``#me``: Your username with '@'\r
+    + ``#place``: Current list name or public for public stream\r
+    + ``#owner``: Owner of the current list\r
+    + ``#keyword``: Keyword for a public stream\r
 \r
 -  ``SEARCH_TYPE``: search type in 'search' command ('mixed','recent','popular').\r
 \r
index 236c1b8..fe14401 100644 (file)
@@ -204,8 +204,9 @@ def init(args):
     name = credential['name']
     if not get_config('PREFIX'):
         set_config('PREFIX', screen_name)
-    g['PREFIX'] = u2str(emojize(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)
@@ -1242,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,
@@ -1256,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,
@@ -1271,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())
@@ -1408,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:
index e8d3c74..8a33711 100644 (file)
@@ -2,6 +2,7 @@ import json
 
 from twitter.util import printNicely
 from .colors import *
+from .config import *
 
 
 def detail_twitter_error(twitterException):
@@ -18,3 +19,26 @@ def detail_twitter_error(twitterException):
     except:
         info = "Error: " + twitterException.response_data.decode('utf8')
         printNicely(yellow(info))
+
+
+def format_prefix(listname = '', keyword = ''):
+    """
+    Format the custom prefix
+    """
+    formattedPrefix = c['PREFIX']
+    owner = '@' + c['original_name']
+    place = ''
+    if keyword:
+        place = '/public'
+        keyword = '#' + keyword
+
+    if listname:
+        owner, place = listname.split('/')
+        place = "/" + place
+
+    formattedPrefix = formattedPrefix.replace("#owner", owner)
+    formattedPrefix = formattedPrefix.replace("#place", place)
+    formattedPrefix = formattedPrefix.replace("#keyword", keyword)
+    formattedPrefix = formattedPrefix.replace("#me", '@' + c['original_name'])
+
+    return formattedPrefix