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

72
LINES

< > BotCompany Repo | #1003416 // class BotChat (old)

JavaX fragment (include)

!include #1003380 // Chat

sclass BotChat {
  Chat chat;
  O dialogBot, backend;
  JTextArea thoughtsArea;

  *(O dialogBot) { this(dialogBot, null); }
  
  *(O _dialogBot, O backend) {
    if (backend is a S) backend = run((S) backend);
    this.backend = backend;
    dialogBot = _dialogBot;
    //callOpt(dialogBot, "noRewind");
    
    swingAndWait(r {
      thoughtsArea = new JTextArea;
      
      chat = new Chat;
      chat.logFile = getProgramFile("log.txt");
  
      chat.onEnter_post = r {
        call(dialogBot, "take", E.q(chat.input()));
        postIt(slurp(dialogBot, "getSingleOutput"));
      };
      
      updateThoughts();
    
      JFrame frame = showFrame(makeTitle(),
        hgrid(chat, withTitle("Thoughts", thoughtsArea)));
      setFrameWidth(frame, 800);
      chat.requestFocus();
    });
  }
  
  S makeTitle() {
    ret programTitle();
  }

  void postIt(final L<E> l) {
    for (int i = 0; i < l(l); i++) {
      E e = l.get(i);
      if (e.a())
        chatAppend("Bot: " + e.a + "\n");
      else if (e.state()) {
        chatAppend("[" + e.state + "]\n");
        final int _i = i+1;
        Runnable l8r = r { awt { postIt(subList(l, _i)); } };
        if ((bool) call(backend, "action", e.state, l8r))
          break;
      }
    }
    updateThoughts();
  }
  
  void chatAppend(S s) {
    if (s.startsWith("[") && matchStart("bot pauses", s)) ret;
    chat.append(s);
  }
  
  void updateThoughts() {
    S s;
    try {
      s = (S) callOpt(dialogBot, "thoughts");
    } catch e {
      s = getStackTrace(e);
    }
    if (empty(s)) s = "No thoughts in " + getClassName(dialogBot);   
    //print("Thoughts: " + s);
    thoughtsArea.setText(s);
  }
}

Author comment

Began life as a copy of #1003406

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1003416
Snippet name: class BotChat (old)
Eternal ID of this version: #1003416/1
Text MD5: cd9703d7b4e38d7c70b115256b18578e
Author: stefan
Category: javax / talking robots
Type: JavaX fragment (include)
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-07-19 16:48:34
Source code size: 1769 bytes / 72 lines
Pitched / IR pitched: No / No
Views / Downloads: 678 / 655
Referenced in: [show references]