1 // SemiTwist D Tools:
2 // Tests: semitwist.cmd test
3 // Written in the D programming language.
4 
5 /++
6 Author:
7 $(WEB www.semitwist.com, Nick Sabalausky)
8 
9 IGNORE THIS APP FOR NOW, IT IS NOT USABLE YET
10 +/
11 
12 module semitwist.apps.tests.cmdTest.main;
13 
14 import semitwist.cmd.all;
15 
16 void main(){}
17 
18 /+
19 // Damn, can't make templated nested func
20 void displayNodes(TElem, TColl)(TColl collection, string label)
21 {
22 	writefln("");
23 	writefln("%s:", label);
24 	foreach(TElem elem; collection)
25 		writefln(" -%s", elem.toString());
26 }
27 
28 void testVfs(string dir)
29 {
30 	auto folder = new FileFolder(dir);
31 
32 	displayNodes!(VfsFolder)(folder,      `folder`);
33 	displayNodes!(VfsFolder)(folder.self, `folder.self`);
34 	displayNodes!(VfsFolder)(folder.tree, `folder.tree`);
35 	displayNodes!(VfsFile  )(folder.self.catalog, `folder.self.catalog`);
36 	displayNodes!(VfsFile  )(folder.tree.catalog, `folder.tree.catalog`);
37 	displayNodes!(VfsFolder)(folder.tree.subset("*est"), `folder.tree.subset("*est")`);
38 	displayNodes!(VfsFile  )(folder.tree.catalog("*_r*"), `folder.tree.catalog("*_r*")`);
39 }
40 
41 //TODO: Add errLevel stuff
42 void main(string[] args)
43 {
44 	Stdout("SemiTwist Library: semitwist.cmd test");
45 
46 	Stdout.newline;
47 	mixin(traceVal!("args[0]", "Environment.cwd"));
48 
49 //	auto path = new FilePath(cmd.dir);
50 //	mixin(traceVal!("path.toString()", "path.isAbsolute()", "path.isFolder()"));
51 
52 	mixin(traceVal!("cmd.dir"));
53 	cmd.dir = "..";
54 	mixin(traceVal!("cmd.dir"));
55 	cmd.dir = "../..";
56 	mixin(traceVal!("cmd.dir"));
57 	cmd.dir = Environment.cwd;
58 	mixin(traceVal!("cmd.dir"));
59 	
60 	cmd.echo("Whee!");
61 	//testVfs(cmd.dir);
62 
63 	cmd.exec("semitwist-echo", ["I'm echoing,", "hello!"]);
64 	cmd.exec("semitwist-echo I'm echoing, hello!");
65 	
66 	cmd.echoing = false;
67 	cmd.exec("semitwist-echo Can't see this because echoing is off");
68 	cmd.echoing = true;
69 
70 /+
71 	Stdout(cmd.prompt("Enter anything:")).newline;
72 	Stdout(
73 		cmd.prompt(
74 			`Enter "yes" or "no":`,
75 			(string input) {
76 				return cast(bool)tango.core.Array.contains(["yes","no"], input);
77 			},
78 			`'%s' is not valid, must enter "yes" or "no"!`
79 		)
80 	).newline;
81 +/
82 
83 	Stdout.newline;
84 	bool done = false;
85 
86 	enum helpMsg = `
87 --Supported Commands--
88 help                 Displays this message
89 echo <text>          Echos <text>
90 pwd                  Print working directory
91 cd <dir>             Change directory
92 ls                   Displays current directory contents
93 exec <prog> <params> Runs program <prog> with paramaters <params>
94 echoing <on|off>     Chooses whether output from exec'ed programs is displayed
95 isechoing            Displays current echoing setting
96 prompt               Prompt for text entry
97 promptyn             Prompt for "yes" or "no"
98 exit                 Exits
99 `;
100 
101 	void delegate(string params)[string] cmdLookup = [
102 		""[]        : (string params) { },
103 		"help"      : (string params) { Stdout(helpMsg);         },
104 		"exit"      : (string params) { done = true;             },
105 		"echo"      : (string params) { cmd.echo(params);        },
106 		"pwd"       : (string params) { Stdout(cmd.dir).newline; },
107 		"cd"        : (string params) { cmd.dir = params;        },
108 		"exec"      : (string params) { cmd.exec(params);        },
109 		"isechoing" : (string params) { Stdout(cmd.echoing? "on" : "off").newline; },
110 		
111 		"ls":
112 		(string params) {
113 			displayNodes!(VfsFolder)(cmd.dir, "Directories");
114 			displayNodes!(VfsFile  )(cmd.dir.self.catalog, "Files");
115 		},
116 		
117 		"prompt":
118 		(string params) {
119 			writefln(
120 				"You entered: %s",
121 				cmd.prompt("Enter anything:")
122 			);
123 		},
124 		
125 		"promptyn":
126 		(string params) {
127 			writefln(
128 				"You entered: %s",
129 				cmd.prompt(
130 					`Enter "yes" or "no":`,
131 					(string input) {
132 						return cast(bool)tango.core.Array.contains(["yes","no"], input);
133 					},
134 					`'%s' is not valid. Please enter "yes" or "no".`
135 				)
136 			);
137 		},
138 		
139 		"echoing":
140 		(string params) {
141 			switch(params)
142 			{
143 			case "on":
144 				cmd.echoing = true;
145 				break;
146 			case "off":
147 				cmd.echoing = false;
148 				break;
149 			default:
150 				Stdout(`param must be either "on" or "off"`).newline;
151 				break;
152 			}
153 		},
154 	];
155 	
156 	while(!done)
157 	{
158 		Stdout("cmdTest>").flush;
159 		
160 		string input;
161 		Cin.readln(input);
162 		input = trim(input);
163 		
164 		auto splitIndex = input.locate(' ');
165 		string command = input[0..splitIndex];
166 		string params = splitIndex==input.length? "" : input[splitIndex+1..$];
167 		params = trim(params);
168 		
169 		if(command in cmdLookup)
170 		{
171 			try
172 				cmdLookup[command](params);
173 			catch(Exception e)
174 			{
175 				write("ERR: ");
176 				//write("ERR: "~e.classinfo.name~": ");
177 				e.writeOut( (string msg) {Stdout(msg);} );
178 				//writefln("Exception: %s", e.msg);
179 			}
180 		}
181 		else
182 			Stdout(`Unknown command (type "help" for list of supported commands)`).newline;
183 		
184 		if( !tango.core.Array.contains(["","exit"], command) )
185 			Stdout.newline;
186 	}
187 }
188 +/