tests: Add interactive mode tests
[jimtcl.git] / examples / jtclsh.tcl
blob772b3273683f1d9ef2291cac027fb002cbdae609
1 # Simple example of how the history extension
2 # can be used to provide line editing and history
4 # Build jimsh with the history extension and enable line editing (the default)
5 # ./configure --with-ext=history
7 package require history
9 set histfile [env HOME]/.jtclsh
10 history load $histfile
11 # Use the standard Tcl autocompletion
12 history completion tcl::autocomplete
13 set prefix ""
14 while {1} {
15 # Read a complete line (script)
16 set prompt "${prefix}jim> "
17 set cmd {}
18 while {1} {
19 if {[history getline $prompt line] < 0} {
20 exit 0
22 if {$cmd ne ""} {
23 append cmd \n
25 append cmd $line
26 if {[info complete $cmd char]} {
27 break
29 set prompt "$char> "
32 if {$cmd eq "h"} {
33 history show
34 continue
37 # Don't bother adding single char commands to the history
38 if {[string length $cmd] > 1} {
39 history add $cmd
40 history save $histfile
43 # Evaluate the script and display the error
44 try {
45 set result [eval $cmd]
46 set prefix ""
47 } on {error return break continue signal} {result opts} {
48 set rcname [info returncodes $opts(-code)]
49 if {$rcname eq "ok" } {
50 # Note: return set -code to 0
51 set rcname return
52 } elseif {$rcname eq "error"} {
53 set result [errorInfo $result]
55 set prefix "\[$rcname\] "
57 if {$result ne {}} {
58 puts $result