{{extend 'layout.html'}}
{{block sectionclass}}debug{{end}}
<!-- begin "debug" block -->
<div class="row-fluid">
    <div class="applist f60 span7" style="padding-bottom:20px;">
      <div class="applist_inner">
        <h2>{{=T("web2py online debugger")}}</h2>
        {{if filename:}}
            <h3 style="word-wrap:break-word;">{{=T("Interaction at %s line %s") % (filename, lineno)}}</h3>
            {{if exception:}}
                  <h3 class="exception">{{=T("Exception %s", exception['title'])}}</h3>
            {{pass}}
                <h5>{{=T("Code listing")}}</h5>
                {{if lines:}}<div class="inspect" style="width:100%;overflow:auto;background-color:whiteSmoke;margin-bottom:20px;">
                <pre>{{=CODE('\n'.join([x[1] for x in sorted(lines.items(),key=lambda x: x[0])]),
                        language='python', link=None, counter=min(lines.keys()), 
                        highlight_line=lineno, context_lines=10)}}</pre></div>
                {{pass}}
            <div class="row-fluid">
                <div class="help span7 alert alert-block alert-info">
                  <ul class="unstyled">
                    <li>{{=T("Your application will be blocked until you click an action button (next, step, continue, etc.)")}}</li>
                    <li>{{=T("You can inspect variables using the console below")}}</li>
                  </ul>
                </div>
            </div>
            <h3>{{=T("Interactive console")}}</h3>
            <div class="shell"><div id="wrapper">
                <div class="row-fluid">
                    <div class="output-wrapper span11">
                        <textarea id="output" readonly="readonly">{{=data}}</textarea>
                    </div>
                </div>
                <div class="row-fluid">
                    <form id="form" action="{{=URL(r=request, f='callback', args=app)}}" method="get" class="span11">
                        <div id="shellwrapper">
                            <div class="prompt-wrapper">
                                <div class="prompt-container">
                                    <textarea class="prompt" name="statement" id="statement"></textarea>
                                </div>
                                <a href="#" rel="tooltip" data-placement="right" data-original-title="{{=T('Type some Python code in here and hit Return (Enter) to execute it.')}}">
                                    {{=helpicon()}}
                                    <span>Type some Python code in here and hit Return (Enter) to execute it.</span>
                                </a>
                            </div>
                            <div id="caret"><span>&gt;&gt;&gt;</span></div>
                            <div id="autoscroll">autoscroll</div>
                        </div>
                    </form>
                </div>
            </div></div>
        {{elif request.env.get('wsgi_multiprocess') or not request.env.get('wsgi_multithread'):}}
              <h3 class="not_paused">{{=T("Unsupported webserver working mode: %s", request.env.get('server_software', ''))}}</h3>
                <div class="help span7 alert alert-block alert-info">
                  <ul class="unstyled">
                    <li><b>{{=T("WARNING:")}} </b>{{=T("This debugger may not work properly if you don't have a threaded webserver or you're using multiple daemon processes.")}}</li>
                    <li>{{=T("In development, use the default Rocket webserver that is currently supported by this debugger.")}}</li>
                    <li>{{=T("On production, you'll have to configure your webserver to use one process and multiple threads to use this debugger.")}}</li>
                  </ul>
                </div>
                {{#=BEAUTIFY(request.env)}}
        {{else:}}
              <h3 class="not_paused">{{=T("No Interaction yet")}}</h3>
                <div class="help span7 alert alert-block alert-info">
                  <ul class="unstyled">
                    <li>{{=T("You need to set up and reach a")}} {{=A(T("breakpoint"), _href=URL('breakpoints'))}} {{=T('to use the debugger!')}}</li>
                    <li>{{=T('To emulate a breakpoint programatically, write:')}}
                    {{=CODE("from gluon.debug import dbg\n"
                            "dbg.set_trace() # stop here!\n",
                            counter=None)}}</li>
                    <li>{{=T('Please')}} {{=A(T("refresh"), _href=URL('interact'))}} {{=T('this page to see if a breakpoint was hit and debug interaction is required.')}}</li>
                  </ul>
                </div>
        {{pass}}
      </div>
    </div> <!-- /applist -->
    <div class="sidebar fl60 span5">
      <div class="sidebar_inner controls" style="margin:0;">
        <span class="pwdchange pull-right">
            {{if filename:}}
                {{=sp_button(URL('step'), T("step"))}}
                {{=sp_button(URL('next'), T("next"))}}
                {{=sp_button(URL('ret'), T("return"))}}
                {{=sp_button(URL('cont'), T("continue"))}}
                {{=sp_button(URL('stop'), T("stop"))}}
            {{pass}}
                {{=button(URL('breakpoints'), T("breakpoints"))}}
        </span>

        {{if exception:}}
            <div class="box">
              <h3>{{=T('Exception %(extype)s: %(exvalue)s', dict(extype=exception['extype'], exvalue=exception['exvalue']))}}</h3>
              <div class="formfield">
                   {{=CODE((exception['request']), counter=None)}}
              </div>
            </div>
        {{pass}}

        <div class="box">
          <h3>{{=T('Locals##debug')}}</h3>
          <div class="formfield">
        {{=BEAUTIFY(f_locals)}}
          </div>
        </div>
        <div class="box">
          <h3>{{=T('Globals##debug')}}</h3>
          <div class="formfield">
        {{=BEAUTIFY(f_globals)}}
          </div>
        </div>
      </div>
    </div>

    <script src="{{=URL('static', 'js/autoscroll.js')}}"></script>

    <script type="text/javascript">
    var bShellScrolling=0
    jQuery(document).ready(function(){
      // jQuery('#statement').focus();  // not usefull most times... 

      jQuery('#statement').keyup(function(event){
        var t=jQuery(this),
        s=t.val(),
        o=jQuery('#output'),
        RETURN = 38;
        if(s=='\n') t.val('');
        if(s.length>1 && s.substr(s.length-1,1)=='\n' && s.substr(s.length-2,1)!=':' &&
        (s.indexOf(':\n ')<0 || s.substr(s.length-2,1)=='\n')) {
          t.val('');
          jQuery.post("{{=URL(r=request,f='execute',args=app)}}",
          {statement:s},function(data){o.html(o.html()+data).attr('scrollTop',o.attr('scrollHeight'));});
        } else { };
        if(event.keyCode==RETURN){
          var i=s.length
          if(i==0){
            var s=o.find('table:last pre:first').text();
            bShellScrolling=o.find('table').length;
          }else if(bShellScrolling){
            var i=bShellScrolling
            if(i<1){
              return
            }else{
              i--
              var s=o.find('table:nth-child('+(i)+') pre:first').text();
              bShellScrolling=i
            }
          }else if(s.indexOf('\n')<0){
            var oo=o.find('tr:first-child pre:contains("'+s+'")')
            if(oo.length==0){
              return
            }else if(oo.length==1){
              s=oo.text();
            }else{
              sVar=oo.text();
              o.html(o.html()+'<dd>'+s+' ?</dd><dt>'+sVar+'</dt>').attr('scrollTop',o.attr('scrollHeight'))
              return
            }
          }else{
            //multistring expr
            return;
          }
          //  if(s.slice(s.length-1)=='\n'){
          s=s.slice(0,s.length-1)
          //	}
          t.val(s);
        }
        if(bShellScrolling && event.keyCode==40){
          var i=bShellScrolling
          i++
          var s=o.find('table:nth-child('+i+') tr:first-child pre').text();
          if(s){
            s=s.slice(0,s.length-1)
            t.val(s);
            bShellScrolling=i
          }else{
            bShellScrolling=0
            t.val('')
          }
        };
        if(bShellScrolling && (event.keyCode==37 || event.keyCode==39)){
          bShellScrolling=0;
        };
        if(event.keyCode==27){
          bShellScrolling=0;
          t.val('');
        };
      });
    });
    </script>
</div>
<!-- end "debug" block -->
