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)

static void systemCommands(S s, O env) {
  call(env, "postSystemMessage", systemCommands_impl(s, env));
}

static S systemCommands_impl(S s, O env) {
  new Matches m;
  
  // SYSTEM COMMANDS
  
  if (matchOneOf(s, m, "start program *", "start *")
    && isSnippetID(m.unq(0))) {
    S progID = m.fsi(0);
    S title = getSnippetTitle(progID);
    // TODO: Show author!
    S msg = "Run program " + progID + " - " + title + "?";
    if (confirmOKCancel((JFrame) getOpt(env, "frame"), msg)) {
      call(env, "postSystemMessage", "Starting program " + progID + " - " + quote(title));
      nohupJavax(progID);
    } else
      call(env, "postSystemMessage", "Program start cancelled by user (was: " + progID + ")");
  }
  
  if (matchOneOf(s, m, "hotwire *", "hotwire * with argument *")) {
    S progID = m.fsi(0), arg = unnull(m.unq(1));
    S title = getSnippetTitle(progID);
    
    S msg = "Hotwire & run program " + progID + " - " + quote(title) + (empty(arg) ? "" : " with argument " + quote(arg)) + "?";
    if (confirmOKCancel((JFrame) getOpt(env, "frame"), msg)) {
      call(env, "postSystemMessage", "Hotwiring & running program " + progID + " - " + quote(title) + (empty(arg) ? "" : " with argument " + quote(arg)));
      run(progID, arg);
    } else
      call(env, "postSystemMessage", "Program start cancelled by user (was: " + progID + ")");
  }
  
  if (matchOneOf(s, "jfresh", "fresh")) {
    ret veryQuickJava_refresh() ? "OK, refreshed." : "Nothing to do";
  }
  
  if (startsWithOneOf(s, "awt ")) {
    S code = onlyAfter(s, ' ');
    code = tok_addReturn(trim(code));
    bool x = !containsReturnWithArgument(code);
    S keyword = x ? "r" : "func";
    ret systemCommands_evalJava("swingAndWait(" + keyword + " {\n" + code + "\n})" + (x ? ";" : ""));
  }
  
  if (startsWithOneOf(s, "java ", "j ")) {
    S code = onlyAfter(s, ' ');
    ret systemCommands_evalJava(code);
  }
  
  if (startsWith(s, "phone ")) {
    S code = onlyAfter(s, ' ');
    ret systemCommands_onPhone(code);
  }
  
  if (startsWithOneOf(s, "jfresh ", "fresh ")) {
    veryQuickJava_refresh();
    S code = afterSpace(s);
    ret systemCommands_evalJava(code);
  }
  
  if "restart" {
    call(env, "postSystemMessage", "Restarting...");
    restart();
  }
  
  if "pop" { // pop up last chat line in a window
    S text = nextToLast((L<S>) get(env, "log"));
    if (empty(text))
      ret "Nothing to show";
    else
      showText(text);
  }
  
  if (jmatch("addll *-*", s, m)) { // add to "Logic List"
    int mm = m.psi(0), nn = m.psi(1);
    // account for the command itself in the log
    int i = l(gLog())-1;
    S part = fromLines(subList(gLog(), i-nn, i-mm+1));
    systemCommands_ll(env, part);
  }
  
  if (jmatch("addll *", s, m)) { // add to "Logic List"
    S part;
    if (isInteger(m.get(0))) {
      int mm = m.psi(0);
      // account for the command itself in the log
      int i = l(gLog())-1;
      part = get(gLog(), i-mm);
    } else
      part = m.unq(0);
    systemCommands_ll(env, part);
  }
  
  if (swic(s, "care "))
    systemCommands_ll(env, "!care " + afterSpace(s));

  null;
}

static void systemCommands_ll(O env, S text) {
  addToLogicList(text);
  call(env, "postSystemMessage", "Added to Logic List: " + text);
}

static O systemCommands_lastResult;

static S systemCommands_evalJava(S code) {
  S main = "!include #1003911\n" + // functions for quick eval
    + evalJava_prep(code, "calc");
  O obj = veryQuickJava(main);
  setOpt(obj, "getProgramName_cache", "User Code");
  long time = now();
  O result = callCalc(obj);
  systemCommands_lastResult = result;
  time = now()-time;
  ret time + " ms\n" + systemCommands_prettyPrint(result);
}

static S systemCommands_prettyPrint(O o) {
  if (o instanceof L)
    ret fromLines(map(func(O o) { systemCommands_prettyPrint(o) }, (L) o));
    
  if (eq(getClassName(o), "main$Snippet"))
    ret formatSnippetID(getString(o, "id")) + " - " + getString(o, "title");
    
  if (eq(getClassName(o), "java.awt.image.BufferedImage"))
    showImage("User Image", (BufferedImage) o);
  
  ret structureOrText(o);
}

static S systemCommands_onPhone(S code) {
  long time = now();
  O result = onPhone(code);
  systemCommands_lastResult = result;
  time = now()-time;
  ret time + " ms\n" + systemCommands_prettyPrint(result);
}

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: 712 / 605
Version history: 5 change(s)
Referenced in: #1006654 - Standard functions list 2 (LIVE, continuation of #761)