Created
March 6, 2012 10:31
-
-
Save chklauser/1985592 to your computer and use it in GitHub Desktop.
Non-working example of a CSP+Text UI in Prexonite Script (bug in select?)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| build does require( | |
| @"psr\struct.pxs", | |
| @"psr\console.pxs", | |
| @"psr\csp.pxs", | |
| @"psr\queue.pxs", | |
| ); | |
| function create_tracer() { | |
| chans( | |
| var shutdown_c, | |
| var trace_c, | |
| var dump_c, | |
| ); | |
| var _log = []; | |
| function print_log { | |
| println("Trace:"); | |
| _log >> each(println("\t",?)); | |
| } | |
| function process[private] { | |
| while(true){ | |
| ::Console.Error.WriteLine("tracer ready"); | |
| select( | |
| shutdown_c: _ => { | |
| print_log; | |
| }, | |
| dump_c: _ => { | |
| print_log; | |
| _log.Clear; | |
| }, | |
| trace_c: t => { | |
| _log[] = t; | |
| ::Console.Error.WriteLine(t.ToString); | |
| } | |
| ); | |
| } | |
| } | |
| function trace(t) { | |
| trace_c.send(t); | |
| return null; | |
| } | |
| function dump() { | |
| dump_c.send(null); | |
| return null; | |
| } | |
| function dispose { | |
| if(shutdown_c is not null) { | |
| shutdown_c.send(null); | |
| shutdown_c = null; | |
| } | |
| } | |
| call\async(process(?)); | |
| return struct; | |
| } | |
| function create_ui(ref trace){ | |
| chans( | |
| var shutdown_c, | |
| var display_c, | |
| ); | |
| var console = __console; | |
| var widgets = new container; | |
| var width = ::Console.BufferWidth; | |
| var height = 30; | |
| var fields = | |
| (0.To(height-1)) | |
| >> map(new field(0,?)); | |
| fields >> each(widgets.register(?)); | |
| function dispose() { | |
| if(shutdown_c is not null){ | |
| trace("Shutdown of ui requested."); | |
| shutdown_c.send(null); | |
| shutdown_c = null; | |
| } | |
| } | |
| var _history = new mutable_queue; | |
| function display(x){ | |
| trace("Display of $x requested."); | |
| display_c.send(x); | |
| } | |
| function history = _history.snapshot; | |
| function process[private] { | |
| ::Console.Error.WriteLine("ui starting"); | |
| while(true){ | |
| trace("ui ready..."); | |
| ::Console.Error.WriteLine("ui ready..."); | |
| select( | |
| shutdown_c: _ => { | |
| trace("ui received shutdown."); | |
| break; | |
| }, | |
| display_c: x => { | |
| trace("ui received display($x)"); | |
| _history.enqueue(x); | |
| if(_history.count > height) | |
| _history.dequeue; | |
| using(var e = _history.GetEnumerator) | |
| for(var i = 0; i < height; i++){ | |
| if(not e.MoveNext) | |
| break; | |
| var x = e.Current; | |
| fields[i].text = x; | |
| } | |
| widgets.update; | |
| } | |
| ); | |
| } | |
| trace("ui exits"); | |
| } | |
| ::Console.Error.WriteLine("starting ui"); | |
| call\async(process(?)); | |
| return struct; | |
| } | |
| function main(){ | |
| using(var tracer = new tracer) | |
| using(var ui = new ui(tracer.trace(?))) | |
| { | |
| tracer.trace("Attach to console."); | |
| var oldTab = __console.Tab; | |
| try { | |
| __console.Tab = (prefix,root) => { | |
| ::Console.Error.WriteLine("prefix: $prefix"); | |
| ui.display(prefix); | |
| ::Console.Error.WriteLine("prefix displayed"); | |
| return ui.history >> where(?.StartsWith(prefix)); | |
| }; | |
| for( var line; do line = __console.ReadLineInteractive(); while line.length > 0){ | |
| tracer.trace = ui.display = ">> SELECTED: " + line; | |
| tracer.dump; | |
| } | |
| } finally { | |
| tracer.trace("Detach from console."); | |
| __console.Tab = oldTab; | |
| } | |
| } | |
| } | |
| { print("Compiling to CIL... "); CompileToCil; println("done."); } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment