File tree Expand file tree Collapse file tree 9 files changed +136
-1
lines changed Expand file tree Collapse file tree 9 files changed +136
-1
lines changed Original file line number Diff line number Diff line change 8
8
*/
9
9
public final class StringValue implements Value {
10
10
11
+ public static final StringValue EMPTY = new StringValue ("" );
12
+
11
13
private final String value ;
12
14
13
15
public StringValue (String value ) {
Original file line number Diff line number Diff line change
1
+ package com .annimon .ownlang .lib .modules .functions ;
2
+
3
+ import com .annimon .ownlang .exceptions .ArgumentsMismatchException ;
4
+ import com .annimon .ownlang .lib .*;
5
+
6
+ public final class robot_exec implements Function {
7
+
8
+ public static enum Mode { EXEC , EXEC_AND_WAIT };
9
+
10
+ private final Mode mode ;
11
+
12
+ public robot_exec (Mode mode ) {
13
+ this .mode = mode ;
14
+ }
15
+
16
+ @ Override
17
+ public Value execute (Value ... args ) {
18
+ if (args .length == 0 ) throw new ArgumentsMismatchException ("At least one argument expected" );
19
+
20
+ try {
21
+ final Process process ;
22
+ if (args .length > 1 ) {
23
+ process = Runtime .getRuntime ().exec (toStringArray (args ));
24
+ } else switch (args [0 ].type ()) {
25
+ case Types .ARRAY :
26
+ final ArrayValue array = (ArrayValue ) args [0 ];
27
+ process = Runtime .getRuntime ().exec (toStringArray (array .getCopyElements ()));
28
+ break ;
29
+
30
+ default :
31
+ process = Runtime .getRuntime ().exec (args [0 ].asString ());
32
+ }
33
+
34
+ switch (mode ) {
35
+ case EXEC_AND_WAIT :
36
+ return new NumberValue (process .waitFor ());
37
+ case EXEC :
38
+ default :
39
+ return NumberValue .ZERO ;
40
+ }
41
+ } catch (Exception ex ) {
42
+ ex .printStackTrace ();
43
+ return NumberValue .ZERO ;
44
+ }
45
+ }
46
+
47
+ private static String [] toStringArray (Value [] values ) {
48
+ final String [] strings = new String [values .length ];
49
+ for (int i = 0 ; i < values .length ; i ++) {
50
+ strings [i ] = values [i ].asString ();
51
+ }
52
+ return strings ;
53
+ }
54
+ }
Original file line number Diff line number Diff line change
1
+ package com .annimon .ownlang .lib .modules .functions ;
2
+
3
+ import com .annimon .ownlang .lib .Function ;
4
+ import com .annimon .ownlang .lib .StringValue ;
5
+ import com .annimon .ownlang .lib .Value ;
6
+ import java .awt .Toolkit ;
7
+ import java .awt .datatransfer .DataFlavor ;
8
+
9
+ public final class robot_fromclipboard implements Function {
10
+
11
+ @ Override
12
+ public Value execute (Value ... args ) {
13
+ try {
14
+ Object data = Toolkit .getDefaultToolkit ().getSystemClipboard ()
15
+ .getData (DataFlavor .stringFlavor );
16
+ return new StringValue (data .toString ());
17
+ } catch (Exception ex ) {
18
+ return StringValue .EMPTY ;
19
+ }
20
+ }
21
+ }
Original file line number Diff line number Diff line change
1
+ package com .annimon .ownlang .lib .modules .functions ;
2
+
3
+ import com .annimon .ownlang .exceptions .ArgumentsMismatchException ;
4
+ import com .annimon .ownlang .lib .Function ;
5
+ import com .annimon .ownlang .lib .NumberValue ;
6
+ import com .annimon .ownlang .lib .Value ;
7
+ import java .awt .Toolkit ;
8
+ import java .awt .datatransfer .StringSelection ;
9
+
10
+ public final class robot_toclipboard implements Function {
11
+
12
+ @ Override
13
+ public Value execute (Value ... args ) {
14
+ if (args .length != 1 ) throw new ArgumentsMismatchException ("One argument expected" );
15
+
16
+ Toolkit .getDefaultToolkit ().getSystemClipboard ()
17
+ .setContents (new StringSelection (args [0 ].asString ()), null );
18
+ return NumberValue .ZERO ;
19
+ }
20
+ }
Original file line number Diff line number Diff line change
1
+ package com .annimon .ownlang .lib .modules .functions ;
2
+
3
+ import com .annimon .ownlang .lib .*;
4
+ import java .util .Scanner ;
5
+
6
+ public final class std_readln implements Function {
7
+
8
+ @ Override
9
+ public Value execute (Value ... args ) {
10
+ try (Scanner sc = new Scanner (System .in )) {
11
+ return new StringValue (sc .nextLine ());
12
+ }
13
+ }
14
+ }
Original file line number Diff line number Diff line change
1
+ package com .annimon .ownlang .lib .modules .functions ;
2
+
3
+ import com .annimon .ownlang .lib .Function ;
4
+ import com .annimon .ownlang .lib .NumberValue ;
5
+ import com .annimon .ownlang .lib .Value ;
6
+
7
+ public final class std_time implements Function {
8
+
9
+ @ Override
10
+ public Value execute (Value ... args ) {
11
+ return new NumberValue (System .currentTimeMillis ());
12
+ }
13
+ }
Original file line number Diff line number Diff line change 2
2
3
3
import com .annimon .ownlang .exceptions .ArgumentsMismatchException ;
4
4
import com .annimon .ownlang .lib .*;
5
+ import com .annimon .ownlang .lib .modules .functions .robot_exec ;
6
+ import com .annimon .ownlang .lib .modules .functions .robot_fromclipboard ;
7
+ import com .annimon .ownlang .lib .modules .functions .robot_toclipboard ;
5
8
import java .awt .AWTException ;
6
9
import java .awt .Robot ;
7
10
import java .awt .event .InputEvent ;
@@ -53,6 +56,10 @@ public void init() {
53
56
} catch (IllegalArgumentException iae ) { }
54
57
return NumberValue .ZERO ;
55
58
});
59
+ Functions .set ("toClipboard" , new robot_toclipboard ());
60
+ Functions .set ("fromClipboard" , new robot_fromclipboard ());
61
+ Functions .set ("execProcess" , new robot_exec (robot_exec .Mode .EXEC ));
62
+ Functions .set ("execProcessAndWait" , new robot_exec (robot_exec .Mode .EXEC_AND_WAIT ));
56
63
57
64
Variables .set ("VK_DOWN" , new NumberValue (KeyEvent .VK_DOWN ));
58
65
Variables .set ("VK_LEFT" , new NumberValue (KeyEvent .VK_LEFT ));
Original file line number Diff line number Diff line change @@ -12,10 +12,12 @@ public final class std implements Module {
12
12
@ Override
13
13
public void init () {
14
14
Functions .set ("echo" , new std_echo ());
15
+ Functions .set ("readln" , new std_readln ());
15
16
Functions .set ("newarray" , new std_newarray ());
16
17
Functions .set ("sort" , new std_sort ());
17
18
Functions .set ("length" , new std_length ());
18
19
Functions .set ("rand" , new std_rand ());
20
+ Functions .set ("time" , new std_time ());
19
21
Functions .set ("sleep" , new std_sleep ());
20
22
Functions .set ("thread" , new std_thread ());
21
23
Original file line number Diff line number Diff line change @@ -22,7 +22,9 @@ public void execute() {
22
22
final String moduleName = expression .eval ().asString ();
23
23
final Module module = (Module ) Class .forName (PACKAGE + moduleName ).newInstance ();
24
24
module .init ();
25
- } catch (Exception ex ) { }
25
+ } catch (Exception ex ) {
26
+ throw new RuntimeException (ex );
27
+ }
26
28
}
27
29
28
30
@ Override
You can’t perform that action at this time.
0 commit comments