title: Improving tcl/tk command prompt
body: <p>My code is below.</p>
  <ol>
  <li><p>I want that cursor should come right of <code>&gt;</code> <br>
  i.e. <code>&gt;|</code>   ( instead of <code>|&gt;</code> )</p>
  </li>
  <li><p>When someone tries to erase or press any keyboard key to upper commands,
  it should get back to current command area without erasing/modifying upper area commands
  (or their outputs)</p>
  </li>
  <li><p>In case of error, all error output should come in red color.
  Currently, only some error lines are in red color only.</p>
  </li>
  </ol>
  <p>Please suggest how to add changes to the current code to facilitate above points.</p>
  <h1 id="my-code-5aum">My code</h1>
  <pre><code>#!/usr/bin/wish
  set cur 1.0
  set ncur 1
  set prev_cmd &quot;&quot;
  
  proc printlog {msg {typ &quot;&quot;}} {
      global .f.t cur ncur
      .f.t insert end &quot;\n$msg\n&gt;&quot;
      set cur [.f.t search -count length $msg $cur end]
      set ncur [expr round($cur)]
      .f.t see end
  
      if {$typ == &quot;error&quot;} {
          .f.t tag add foo $cur $ncur.end
          .f.t tag configure foo -foreground red
      }   elseif {$typ == &quot;yellow&quot;} {
          .f.t tag add foo $cur $ncur.end
          .f.t tag configure foo -foreground yellow -background #696969
      }
  }
  
  frame .f
  pack .f -fill both -anchor n
  text .f.t -yscrollcommand {.f.s set}
  scrollbar .f.s -command {.f.t yview}
  pack .f.s -side right -fill y
  pack .f.t -fill both -expand 1
  
  .f.t insert end &quot;&gt;&quot;
  
  proc processcmd {} {
      global .f.t prev_cmd
      set prev_cmd [.f.t get 1.0 end]
      set l [string length $prev_cmd]
      set temp &quot;&quot;
      for { set a $l}  {$a &gt; -1} {incr a -1} {
          set c [string index $prev_cmd $a]
          if {$c == &quot;&gt;&quot;} {
              break
          } else {
              set temp $c$temp
          }
  }
      set prev_cmd $temp
      if {[catch {set output [eval $prev_cmd]} errmsg]} {
          if {[catch {set output [eval exec $prev_cmd]} errmsg]} {
              if {[catch {set output [exec $prev_cmd]} errmsg]} {
              printlog &quot;$errmsg&quot; &quot;error&quot;
              return
              }
          }
      }
      printlog $output
  }
  bind .f.t &lt;Return&gt; {
      processcmd
      tk::TextSetCursor %W {insert display linestart}
  }
  focus .f.t
  bind .f.t &lt;Up&gt; {
     if {$prev_cmd ne &quot;&quot;} {
        .f.t insert end $prev_cmd
        break
     }
  }
  </code></pre>
  <h1 id="output-l1t9">Output</h1>
  <p><img src="https://i.stack.imgur.com/IvW6M.png" alt="enter image description here" /></p>