Skip to content
This repository

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse code

Add some javascript to make terminal listing pretty, specifically, to…

… turn escaped terminal commands (colors) into html. Fancy regexps!
  • Loading branch information...
commit d5a34906abdd7c6c612bb4261d1318de4358dd53 1 parent 9dce0dc
James Gao authored

Showing 1 changed file with 30 additions and 2 deletions. Show diff stats Hide diff stats

  1. 32  IPython/frontend/html/notebook.html
32  IPython/frontend/html/notebook.html
@@ -4,6 +4,12 @@
4 4
     <head>
5 5
         <meta http-equiv="content-type" content="text/html; charset=utf-8" /> 
6 6
         <title>AJAX test</title>
  7
+        <style type="text/css">
  8
+            .cblue { color: blue }
  9
+            .cgreen { color: green }
  10
+            .cred { color: red }
  11
+            .cbold { font-weight:bold }
  12
+        </style>
7 13
         <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
8 14
         <script type="text/javascript">
9 15
             client_id = "/$client_id";
@@ -11,12 +17,34 @@
11 17
                 url: client_id,
12 18
                 dataType: "json"
13 19
             })
  20
+            function fixConsole(txt) {
  21
+                //Fixes escaped console commands, IE colors. Turns them into HTML
  22
+                //Unfortunately, the "semantics" of html and console are very 
  23
+                //different, so fancy things *will* break
  24
+                var attrib = {"30":"cblack", "31":"cred","32":"cgreen", "34":"cblue", "01":"cbold"}
  25
+                //Must remove first \033[0m to eliminate pairing errors
  26
+                txt = txt.replace(/\033\[0m/, "")
  27
+                //Phase 1: substitute <span> for html
  28
+                var re = /\033\[([\d;]+?)m(.*?)\033\[0m/g
  29
+                txt = txt.replace(re, "<span class='[[$1]]'>$2</span>")
  30
+                //Phase 2: substitute class in span with correct attributes
  31
+                re = /\[\[([\d;]+?)\]\]/
  32
+                while (re.test(txt)) {
  33
+                    var match = re.exec(txt)
  34
+                    var cmds = match[1].split(";")
  35
+                    var reps = []
  36
+                    for (var j in cmds)
  37
+                        reps.push(attrib[cmds[j]])
  38
+                    txt = txt.replace( match[0], reps.join(" ") )
  39
+                }
  40
+                return txt
  41
+            }
14 42
             function comet() {
15 43
                 $.ajax({
16 44
                     success: function(json, status, request) {
17  
-                        msg = ""
  45
+                        msg = json.msg_type+": "
18 46
                         if (json.msg_type == "stream")
19  
-                            msg += json.content.data
  47
+                            msg += fixConsole(json.content.data)
20 48
                         else if (json.msg_type == "pyin")
21 49
                             msg += json.content.code
22 50
                         else if (json.msg_type == "pyout")

0 notes on commit d5a3490

Please sign in to comment.
Something went wrong with that request. Please try again.