I've no idea here...
[gtkD.git] / demos / gtk / SpawnTests.d
blob92d109a3fa8f53666d0d0da0646f031787cb93b6
1 /*
2 * This file is part of duit.
4 * duit is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU Lesser General Public License as published by
6 * the Free Software Foundation; either version 2.1 of the License, or
7 * (at your option) any later version.
9 * duit is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public License
15 * along with duit; if not, write to the Free Software
16 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
19 module gtk.SpawnTests;
21 private import glib.Spawn;
22 private import gtk.Duit;
23 private import std.stdio;
25 private import gtk.TextView;
26 private import gtk.TextBuffer;
27 private import gtk.TextIter;
28 private import gtk.Box;
29 private import gtk.VBox;
30 private import gtk.ScrolledWindow;
32 private import gtk.MainWindow;
34 private import gtk.Button;
35 private import gtk.Image;
37 private import std.string;
39 class SpawnWindow : MainWindow
42 TextView viewInput;
43 TextView viewOutput;
44 TextView viewError;
46 this()
48 super("Spawn testing");
49 setupWindow();
50 setSizeRequest(400,400);
51 showAll();
54 private void setupWindow()
56 Box main = new VBox(false, 2);
58 viewInput = new TextView();
59 viewOutput = new TextView();
60 viewError = new TextView();
62 main.packStart(new ScrolledWindow(viewInput), false, false, 2);
63 Button button = new Button("exec", &execInput);
64 main.packStart(button, false, false, 4);
65 main.packStart(new ScrolledWindow(viewOutput), true, true, 2);
66 main.packStart(new ScrolledWindow(viewError), false, false, 2);
68 setBorderWidth(7);
69 add(main);
72 private void execInput(Button button)
74 char[][] args = std.string.split(viewInput.getBuffer().getText());
75 exec(args);
78 private bool exec(char[][] args)
80 foreach ( int i, char[] arg ; args)
82 writefln("[%s] >%s<", i, arg);
84 Spawn spawn = new Spawn(args[0]);
85 spawn.addChildWatch(&childEnded);
86 if (args.length > 1 )
88 for( int i=1 ; i<args.length ; i++ )
90 writefln("SpawnTests.exec adding parameter [%s] %s",i,args[i]);
91 spawn.addParm(args[i]);
94 //spawn.addParm(null);
95 return exec(spawn);
98 void childEnded(int process, int status)
100 writefln("process %s ended with status %s", process, status);
103 private bool exec(Spawn spawn)
106 viewOutput.getBuffer().setText("");
107 viewError.getBuffer().setText("");
109 int result = spawn.execAsyncWithPipes();
111 int outCount;
112 int errCount;
114 TextBuffer bufferOutput = viewOutput.getBuffer();
115 TextBuffer bufferError = viewError.getBuffer();
116 TextIter iterOut = new TextIter();
117 TextIter iterError = new TextIter();
119 while ( !spawn.endOfOutput() )
121 bufferOutput.getEndIter(iterOut);
122 viewOutput.getBuffer().insert(iterOut, spawn.readLine()~"\n");
125 while ( !spawn.endOfError() )
127 bufferError.getEndIter(iterError);
128 viewError.getBuffer().insert(iterError, spawn.readLineError()~"\n");
131 bufferError.getEndIter(iterError);
132 viewError.getBuffer().insert(iterError, spawn.getLastError()~"\n");
134 writefln("exit loop");
136 spawn.close();
137 return true;
140 public void setInput(char[][] args)
142 TextBuffer inBuffer = viewInput.getBuffer();
143 char[] t;
144 foreach ( int count, char[] arg; args)
146 if ( count > 0 ) t ~= " ";
147 t ~= arg;
149 inBuffer.setText(t);
152 public void setInput(char[] arg)
154 viewInput.getBuffer().setText(arg);
160 void main(char[][] args)
162 Duit.init(args);
164 SpawnWindow sw = new SpawnWindow();
165 if ( args.length > 1 )
167 sw.setInput(args[1..args.length]);
169 else
171 sw.setInput("/bin/ls");
174 Duit.main();