Commits

Miki Tebeka committed 033be08

ansiprint

  • Participants
  • Parent commits d812f19

Comments (0)

Files changed (3)

File ansiprint

-#!/usr/bin/env python
-'''Print message using ANSI terminal codes'''
-
-__author__ = "Miki Tebeka <miki@mikitebeka.com>"
-
-from sys import stdout, stderr
-
-# Format
-bright = 1
-dim = 2
-underline = 4
-blink = 5
-reverse = 7
-hidden = 8
-
-# Forground
-black = 30
-red = 31
-green = 32
-yellow = 33
-blue = 34
-magenta = 35
-cyan = 36
-white = 37
-
-# Background
-on_black = 40
-on_red = 41
-on_green = 42
-on_yellow = 43
-on_blue = 44
-on_magenta = 45
-on_cyan = 46
-on_white = 47
-
-def ansiformat(msg, *args):
-    '''Format msg according to args.
-
-    See http://www.termsys.demon.co.uk/vtansi.htm for more details/
-    '''
-    return "\033[%sm%s\033[0m" % (";".join(["%s" % f for f in args]), msg)
-
-def ansiprint(msg, *args, **kw):
-    '''Print formatted message.
-
-    Should work on ANSI compatible terminal.
-    '''
-
-    if kw.get("stderr", 0):
-        outfo = stderr
-    else:
-        outfo = stdout
-
-    outfo.write(ansiformat(msg, *args))
-    outfo.flush()
-
-if __name__ == "__main__":
-    from sys import argv, exit
-    from os.path import basename
-
-    h = {
-        "bright" : bright,
-        "dim" : dim,
-        "underline" : underline,
-        "blink" : blink,
-        "reverse" : reverse,
-        "hidden" : hidden,
-        "black" : black,
-        "red" : red,
-        "green" : green,
-        "yellow" : yellow,
-        "blue" : blue,
-        "magenta" : magenta,
-        "cyan" : cyan,
-        "white" : white,
-        "on_black" : on_black,
-        "on_red" : on_red,
-        "on_green" : on_green,
-        "on_yellow" : on_yellow,
-        "on_blue" : on_blue,
-        "on_magenta" : on_magenta,
-        "on_cyan" : on_cyan,
-        "on_white" : on_white
-    }
-
-    eg = "e.g. ansiprint hello red on_green underline -> %s" % \
-        ansiformat("hello", red, on_green, underline)
-
-    if len(argv) < 2:
-        print >> stderr, "usage: %s message [format ...]" % basename(argv[0])
-        print >> stderr, eg
-        exit(1)
-
-    for i in argv[2:]:
-        if i not in h:
-            ansiprint("%s: Unknown format\n" % i, red, bright, stderr=True)
-            print >> stderr, "Formats can be:",
-            msg = ", ".join([ansiformat(f, h[f]) for f in h.keys()])
-            print msg
-            print >> stderr, eg
-            exit(1)
-
-    ansiprint(argv[1], *[h[i] for i in argv[2:]])
-    print

File ansiprint.py

+#!/usr/bin/env python
+'''Print message using ANSI terminal codes'''
+
+__author__ = "Miki Tebeka <miki@mikitebeka.com>"
+
+from sys import stdout, stderr
+
+# Format
+bright = 1
+dim = 2
+underline = 4
+blink = 5
+reverse = 7
+hidden = 8
+
+# Forground
+black = 30
+red = 31
+green = 32
+yellow = 33
+blue = 34
+magenta = 35
+cyan = 36
+white = 37
+
+# Background
+on_black = 40
+on_red = 41
+on_green = 42
+on_yellow = 43
+on_blue = 44
+on_magenta = 45
+on_cyan = 46
+on_white = 47
+
+def ansiformat(msg, *args):
+    '''Format msg according to args.
+
+    See http://www.termsys.demon.co.uk/vtansi.htm for more details/
+    '''
+    return "\033[%sm%s\033[0m" % (";".join(["%s" % f for f in args]), msg)
+
+def ansiprint(msg, *args, **kw):
+    '''Print formatted message.
+
+    Should work on ANSI compatible terminal.
+    '''
+
+    if kw.get("stderr", 0):
+        outfo = stderr
+    else:
+        outfo = stdout
+
+    outfo.write(ansiformat(msg, *args))
+    outfo.flush()
+
+if __name__ == "__main__":
+    from sys import argv, exit
+    from os.path import basename
+
+    h = {
+        "bright" : bright,
+        "dim" : dim,
+        "underline" : underline,
+        "blink" : blink,
+        "reverse" : reverse,
+        "hidden" : hidden,
+        "black" : black,
+        "red" : red,
+        "green" : green,
+        "yellow" : yellow,
+        "blue" : blue,
+        "magenta" : magenta,
+        "cyan" : cyan,
+        "white" : white,
+        "on_black" : on_black,
+        "on_red" : on_red,
+        "on_green" : on_green,
+        "on_yellow" : on_yellow,
+        "on_blue" : on_blue,
+        "on_magenta" : on_magenta,
+        "on_cyan" : on_cyan,
+        "on_white" : on_white
+    }
+
+    eg = "e.g. ansiprint hello red on_green underline -> %s" % \
+        ansiformat("hello", red, on_green, underline)
+
+    if len(argv) < 2:
+        print >> stderr, "usage: %s message [format ...]" % basename(argv[0])
+        print >> stderr, eg
+        exit(1)
+
+    for i in argv[2:]:
+        if i not in h:
+            ansiprint("%s: Unknown format\n" % i, red, bright, stderr=True)
+            print >> stderr, "Formats can be:",
+            msg = ", ".join([ansiformat(f, h[f]) for f in h.keys()])
+            print msg
+            print >> stderr, eg
+            exit(1)
+
+    ansiprint(argv[1], *[h[i] for i in argv[2:]])
+    print
 #!/usr/bin/env python
 
 from BaseHTTPServer import HTTPServer, BaseHTTPRequestHandler
-from urlparse import urlparse
-from urllib2 import urlopen
-from operator import itemgetter
+from functools import partial
 from rfc822 import parsedate_tz
 from time import strftime
+from urllib2 import urlopen
+from urlparse import urlparse
+import json
 import re
-from functools import partial
-
-try:
-    import json
-except ImportError:
-    import simplejson as json
 
 HTML = '''
 <html>
         <hr />
         By <a href="mailto:miki@mikitebeka.com">Miki</a>
     </body>
-    <script src="jquery.js"></script>
+    <script
+        src="http://ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js">
+    </script>
     <script>
         var running = 0;
 
             var button = $('#run');
             if (button.text() == "Go") {
                 var query = $.trim($('#query').val());
-                /* FIXME: The best way will be to disable the button until there
-                  is text
+                /* FIXME: The best way will be to disable the button until
+                  there is text
                 */
                 if (query.length == 0) {
                     alert("Please enter *something*");
 </html>
 '''
 
+
 def format_time(time):
     # Wed, 11 Feb 2009 00:10:36 +0000
     time = parsedate_tz(time)
     return strftime("%m/%d/%Y %H:%M", time[:9])
 
-# 'http://mikitebeka.com' -> 
+# 'http://mikitebeka.com' ->
 # '<a href="http://mikitebeka.com">http://mikitebeka.com</a>'
 inject_links = partial(
-    re.compile("(http://[^ ]+)").sub, 
+    re.compile("(http://[^ ]+)").sub,
     "<a target=\"_new\" href=\"\\1\">\\1</a>")
 
+
 def format_result(result):
     time = format_time(result["created_at"])
     text = inject_links(result["text"])
     return "[%s] %s" % (time, text)
 
+
 class RequestHandler(BaseHTTPRequestHandler):
     def do_GET(self):
         if self.path == "/":
 if __name__ == "__main__":
     server = HTTPServer(("", 8888), RequestHandler)
     server.serve_forever()
-