Not logged in.  Login/Logout/Register | List snippets | | Create snippet | Upload image | Upload data

140
LINES

< > BotCompany Repo | #1003814 // systemCommands - "!j" etc

JavaX fragment (include)

1  
static void systemCommands(S s, O env) {
2  
  call(env, "postSystemMessage", systemCommands_impl(s, env));
3  
}
4  
5  
static S systemCommands_impl(S s, O env) {
6  
  new Matches m;
7  
  
8  
  // SYSTEM COMMANDS
9  
  
10  
  if (matchOneOf(s, m, "start program *", "start *")
11  
    && isSnippetID(m.unq(0))) {
12  
    S progID = m.fsi(0);
13  
    S title = getSnippetTitle(progID);
14  
    // TODO: Show author!
15  
    S msg = "Run program " + progID + " - " + title + "?";
16  
    if (confirmOKCancel((JFrame) getOpt(env, "frame"), msg)) {
17  
      call(env, "postSystemMessage", "Starting program " + progID + " - " + quote(title));
18  
      nohupJavax(progID);
19  
    } else
20  
      call(env, "postSystemMessage", "Program start cancelled by user (was: " + progID + ")");
21  
  }
22  
  
23  
  if (matchOneOf(s, m, "hotwire *", "hotwire * with argument *")) {
24  
    S progID = m.fsi(0), arg = unnull(m.unq(1));
25  
    S title = getSnippetTitle(progID);
26  
    
27  
    S msg = "Hotwire & run program " + progID + " - " + quote(title) + (empty(arg) ? "" : " with argument " + quote(arg)) + "?";
28  
    if (confirmOKCancel((JFrame) getOpt(env, "frame"), msg)) {
29  
      call(env, "postSystemMessage", "Hotwiring & running program " + progID + " - " + quote(title) + (empty(arg) ? "" : " with argument " + quote(arg)));
30  
      run(progID, arg);
31  
    } else
32  
      call(env, "postSystemMessage", "Program start cancelled by user (was: " + progID + ")");
33  
  }
34  
  
35  
  if (matchOneOf(s, "jfresh", "fresh")) {
36  
    ret veryQuickJava_refresh() ? "OK, refreshed." : "Nothing to do";
37  
  }
38  
  
39  
  if (startsWithOneOf(s, "awt ")) {
40  
    S code = onlyAfter(s, ' ');
41  
    code = tok_addReturn(trim(code));
42  
    bool x = !containsReturnWithArgument(code);
43  
    S keyword = x ? "r" : "func";
44  
    ret systemCommands_evalJava("swingAndWait(" + keyword + " {\n" + code + "\n})" + (x ? ";" : ""));
45  
  }
46  
  
47  
  if (startsWithOneOf(s, "java ", "j ")) {
48  
    S code = onlyAfter(s, ' ');
49  
    ret systemCommands_evalJava(code);
50  
  }
51  
  
52  
  if (startsWith(s, "phone ")) {
53  
    S code = onlyAfter(s, ' ');
54  
    ret systemCommands_onPhone(code);
55  
  }
56  
  
57  
  if (startsWithOneOf(s, "jfresh ", "fresh ")) {
58  
    veryQuickJava_refresh();
59  
    S code = afterSpace(s);
60  
    ret systemCommands_evalJava(code);
61  
  }
62  
  
63  
  if "restart" {
64  
    call(env, "postSystemMessage", "Restarting...");
65  
    restart();
66  
  }
67  
  
68  
  if "pop" { // pop up last chat line in a window
69  
    S text = nextToLast((L<S>) get(env, "log"));
70  
    if (empty(text))
71  
      ret "Nothing to show";
72  
    else
73  
      showText(text);
74  
  }
75  
  
76  
  if (jmatch("addll *-*", s, m)) { // add to "Logic List"
77  
    int mm = m.psi(0), nn = m.psi(1);
78  
    // account for the command itself in the log
79  
    int i = l(gLog())-1;
80  
    S part = fromLines(subList(gLog(), i-nn, i-mm+1));
81  
    systemCommands_ll(env, part);
82  
  }
83  
  
84  
  if (jmatch("addll *", s, m)) { // add to "Logic List"
85  
    S part;
86  
    if (isInteger(m.get(0))) {
87  
      int mm = m.psi(0);
88  
      // account for the command itself in the log
89  
      int i = l(gLog())-1;
90  
      part = get(gLog(), i-mm);
91  
    } else
92  
      part = m.unq(0);
93  
    systemCommands_ll(env, part);
94  
  }
95  
  
96  
  if (swic(s, "care "))
97  
    systemCommands_ll(env, "!care " + afterSpace(s));
98  
99  
  null;
100  
}
101  
102  
static void systemCommands_ll(O env, S text) {
103  
  addToLogicList(text);
104  
  call(env, "postSystemMessage", "Added to Logic List: " + text);
105  
}
106  
107  
static O systemCommands_lastResult;
108  
109  
static S systemCommands_evalJava(S code) {
110  
  S main = "!include #1003911\n" + // functions for quick eval
111  
    + evalJava_prep(code, "calc");
112  
  O obj = veryQuickJava(main);
113  
  setOpt(obj, "getProgramName_cache", "User Code");
114  
  long time = now();
115  
  O result = callCalc(obj);
116  
  systemCommands_lastResult = result;
117  
  time = now()-time;
118  
  ret time + " ms\n" + systemCommands_prettyPrint(result);
119  
}
120  
121  
static S systemCommands_prettyPrint(O o) {
122  
  if (o instanceof L)
123  
    ret fromLines(map(func(O o) { systemCommands_prettyPrint(o) }, (L) o));
124  
    
125  
  if (eq(getClassName(o), "main$Snippet"))
126  
    ret formatSnippetID(getString(o, "id")) + " - " + getString(o, "title");
127  
    
128  
  if (eq(getClassName(o), "java.awt.image.BufferedImage"))
129  
    showImage("User Image", (BufferedImage) o);
130  
  
131  
  ret structureOrText(o);
132  
}
133  
134  
static S systemCommands_onPhone(S code) {
135  
  long time = now();
136  
  O result = onPhone(code);
137  
  systemCommands_lastResult = result;
138  
  time = now()-time;
139  
  ret time + " ms\n" + systemCommands_prettyPrint(result);
140  
}

download  show line numbers  debug dex  old transpilations   

Travelled to 14 computer(s): aoiabmzegqzx, bhatertpkbcr, cbybwowwnfue, cfunsshuasjs, ddnzoavkxhuk, gwrvuhgaqvyk, ishqpsrjomds, lpdgvwnxivlt, mqqgnosmbjvj, pyentgdyhuwx, pzhvpgtvlbxg, tslmcundralx, tvejysmllsmz, vouqrxazstgt

No comments. add comment

Snippet ID: #1003814
Snippet name: systemCommands - "!j" etc
Eternal ID of this version: #1003814/6
Text MD5: c68ab5d9cd8f2acd66f1b31ae5fffa9f
Author: stefan
Category: javax / talking robots
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2017-03-05 23:06:26
Source code size: 4441 bytes / 140 lines
Pitched / IR pitched: No / No
Views / Downloads: 711 / 605
Version history: 5 change(s)
Referenced in: [show references]