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

97
LINES

< > BotCompany Repo | #1000976 // Dialog 3 (sucking chat for answers)

JavaX source code [tags: use-pretranspiled] - run with: x30.jar

Libraryless. Click here for Pure Java version (837L/7K/22K).

1  
!747
2  
!awt {
3  
4  
m {
5  
  static JTextArea textArea;
6  
  static float fontSize = 24f;
7  
  static S fontSnippet =
8  
    //"#1000970"
9  
    "#1000973" // Classic Trash 1
10  
    ;
11  
  static S thinking = "[...]";
12  
  static S chatID;
13  
  
14  
  p {
15  
    chatID = (S) get(getJavaX(), "chatSend_id");
16  
    print("Chat ID: " + chatID);
17  
    
18  
    textArea = new JTextArea();
19  
    textArea.setLineWrap(true);
20  
    textArea.setWrapStyleWord(true);
21  
    textArea.setFont(loadFont());
22  
    textArea.setMargin(new Insets(10, 10, 10, 10));
23  
    
24  
    textArea.addKeyListener(new KeyAdapter() {
25  
      public void keyPressed(KeyEvent e) {
26  
        if (e.getKeyCode() == KeyEvent.VK_ENTER && e.getModifiers() == 0) {
27  
          enterPressed();
28  
        }
29  
      }
30  
    });
31  
    
32  
    makeFrame("Dialog 3", new JScrollPane(textArea));
33  
    
34  
    daemon {
35  
      while (true) {
36  
        S line = suckFromChatNoHistory();
37  
        if (line == null) break;
38  
        L<S> tok = javaTok(line);
39  
        S text = unquote(tok.get(tok.size()-2));
40  
        S guy = tok.get(1);
41  
        S prefix = chatID + ":";
42  
        if (text.startsWith(prefix))
43  
          answer(text.substring(prefix.length()).trim());
44  
      }
45  
    }
46  
    
47  
    sleep(); // important for auto-logging as of now
48  
  }
49  
50  
  static void enterPressed() {
51  
    S text = textArea.getText();
52  
    int idx = textArea.getCaretPosition();
53  
    
54  
    // move to end of line first
55  
    while (idx < text.length() && text.charAt(idx) != '\n') ++idx;
56  
    textArea.setCaretPosition(idx);
57  
    
58  
    //print(quote(text.substring(0, idx)));
59  
    int lineStart = text.lastIndexOf("\n", idx-1);
60  
    //print(lineStart + " " + idx);
61  
    final S line = text.substring(lineStart+1, idx);
62  
    print("?? " + line);
63  
    type(thinking);
64  
    thread {
65  
      processLine(line);
66  
    }
67  
  }
68  
  
69  
  static Font loadFont() ctex {
70  
    return Font.createFont(Font.TRUETYPE_FONT, loadLibrary(fontSnippet)).deriveFont(fontSize);
71  
  }
72  
73  
  static void processLine(S line) {
74  
    // nothing to do, just wait for answers
75  
  }
76  
  
77  
  static void answer(final S s) {
78  
    awt {
79  
      print("!! " + s);
80  
      S text = textArea.getText();
81  
      int idx = text.indexOf(thinking);
82  
      if (idx < 0) {
83  
        print("Weird! Maybe user edited?");
84  
        return;
85  
      }
86  
      int cursor = textArea.getCaretPosition();
87  
      textArea.replaceRange(s, idx, idx+thinking.length());
88  
      if (cursor > idx+thinking.length())
89  
        textArea.setCaretPosition(cursor+s.length()-thinking.length());
90  
    }
91  
  }
92  
  
93  
  static void type(S bla) {
94  
    print("!! " + bla);
95  
    textArea.insert("\n" + bla, textArea.getCaretPosition());
96  
  }
97  
}

Author comment

Began life as a copy of #1000974

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1000976
Snippet name: Dialog 3 (sucking chat for answers)
Eternal ID of this version: #1000976/1
Text MD5: 83c67f88d2e6c03f75bc41dc9163a8fc
Transpilation MD5: 7e40aea5f033275810f247cef5fc0293
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-09-13 03:33:22
Source code size: 2638 bytes / 97 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 600 / 645
Referenced in: [show references]