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

213
LINES

< > BotCompany Repo | #1003695 // "Random" v6 (with !hotwire)

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

Libraryless. Click here for Pure Java version (4236L/30K/102K).

1  
!759
2  
3  
!include #1003797 // Thinker
4  
5  
static JTable table;
6  
static JTextArea chat;
7  
static JTextField input;
8  
static File logFile;
9  
static new L<S> log;
10  
static L<S> recommendations;
11  
12  
static int listDelay = 2000;
13  
14  
static Bool thinking;
15  
static bool updateOnce = true;
16  
static new Thinker thinker;
17  
18  
static S systemPrefix = "[system]";
19  
20  
p-awt {
21  
  //substanceLAF("EmeraldDusk"); // Too dark!
22  
  substanceLAF("ChallengerDeep");
23  
  loadLog();
24  
25  
  thinker.startUp(log);
26  
  
27  
  table = tableWithTooltips();
28  
  chat = autoScroll(wordWrapTextArea());
29  
  chat.setText(joinLines(log));
30  
  input = new JTextField;
31  
  JFrame frame = showFrame(vgrid(centerAndSouth(chat, input), table));
32  
  //setFrameIconLater(frame, "#1003593");
33  
  
34  
  onEnter(input, r {
35  
    post();
36  
  });
37  
  
38  
  onDoubleClick(table, voidfunc(int row) {
39  
    chooseSuggestion(row);
40  
  });
41  
  
42  
  for (int i = 1; i <= 12; i++) {
43  
    final int _i = i;
44  
    registerFunctionKey(frame, i, r {
45  
      chooseSuggestion(_i-1);
46  
    });
47  
  }
48  
  
49  
  fillList();
50  
  
51  
  input.requestFocus();
52  
}
53  
54  
static S getInput() {
55  
  ret joinLines(" # ", input.getText().trim());
56  
}
57  
58  
static void post() {
59  
  S i = getInput();
60  
  if (inputAllowedByUser(i))
61  
    post(i);
62  
}
63  
64  
svoid chooseSuggestion(int row) {
65  
  L<S> line = getTableLine(table, row);
66  
  if (line == null) ret;
67  
  S s = line.get(0);
68  
  if (empty(s)) ret;
69  
  input.setText(s);
70  
  post();
71  
}
72  
73  
static bool inputAllowedByUser(S i) {
74  
  ret !swic(i, systemPrefix);
75  
}
76  
77  
static void postSystemMessage(S msg) {
78  
  post(systemPrefix + " " + msg);
79  
}
80  
81  
static void post(S i) {
82  
  S s = i + "\n";
83  
  chat.append(s);
84  
  appendToFile(logFile, "[" + chatTime() + "] " + s);
85  
  synchronized(mc()) {
86  
    log.add(i);
87  
  }
88  
  input.selectAll();
89  
  updateOnce = true;
90  
  try {
91  
    action(i);
92  
  } catch e {
93  
    printStackTrace(e);
94  
    postSystemMessage(exceptionToStringShort(e));
95  
  }
96  
}
97  
98  
static void action(S s) {
99  
  s = dropBracketPrefix(s); // e.g. "[bot]"
100  
  if (!s.startsWith("!")) ret;
101  
  s = dropPrefix("!", s);
102  
  new Matches m;
103  
  
104  
  // SYSTEM COMMANDS
105  
  
106  
  if "start program *" {
107  
    S progID = m.fsi(0);
108  
    S title = getSnippetTitle(progID);
109  
    // TODO: Show author!
110  
    S msg = "Run program " + progID + " - " + title + "?";
111  
    if (confirmOKCancel(chat, msg)) {
112  
      postSystemMessage("Starting program " + progID + " - " + quote(title));
113  
      nohupJavax(progID);
114  
    } else
115  
      postSystemMessage("Program start cancelled by user (was: " + progID + ")");
116  
  }
117  
  
118  
  if "hotwire * with argument *" {
119  
    S progID = m.fsi(0), arg = m.unq(1);
120  
    S title = getSnippetTitle(progID);
121  
    
122  
    S msg = "Hotwire & run program " + progID + " - " + quote(title) + " with argument " + quote(arg) + "?";
123  
    if (confirmOKCancel(chat, msg)) {
124  
      postSystemMessage("Hotwiring & running program " + progID + " - " + quote(title) + " with argument " + quote(arg));
125  
      run(progID, arg);
126  
    } else
127  
      postSystemMessage("Program start cancelled by user (was: " + progID + ")");
128  
  }
129  
}
130  
131  
static void fillList() {
132  
  bool t = shouldUpdateList() || updateOnce;
133  
  updateOnce = false;
134  
  if (neq(t, thinking)) {
135  
    thinking = t;
136  
    setFrameIcon(table, t ? "#1003603" : "#1003593");
137  
  }
138  
  
139  
  if (!t)
140  
    againl8r();
141  
  else thread "Fill List" {
142  
    final new L<S> data;
143  
    thinker.makeListData(cloneList(log), data);
144  
    
145  
    dataToTable_uneditable(table, data);
146  
    againl8r();
147  
  }
148  
}
149  
150  
static void againl8r() {
151  
  swingAfter(table, listDelay, r { fillList(); });
152  
}
153  
154  
static bool shouldUpdateList() {
155  
  ret getFrame(table).isFocused()
156  
    && !mouseInComponent(table);
157  
}
158  
159  
// also called from outside
160  
static L<S> loadLog() {
161  
  if (logFile == null)
162  
    logFile = getProgramFile("log.txt");
163  
  for (S s : toLines(loadTextFile(logFile))) pcall {
164  
    log.add(substring(s, s.indexOf(']')+1).trim());
165  
  }
166  
  ret log;
167  
}
168  
169  
// CREATIVE PART!
170  
171  
// put func {}'s returning a string in there
172  
static void makeGenerators(L<Gen> l, final L<S> log) {
173  
  gen(l, "hello random", func { "Hello " + randomID(10) });
174  
  gen(l, quine(func { last(log) }));
175  
  gen(l, quine(func { oneOf(log) }));
176  
  gen(l, "1 assoc", oneAssoc(log));
177  
  gen(l, "most popular", mostPopular(log));
178  
  addLoadedSolvers(l);
179  
}
180  
181  
svoid addLoadedSolvers(L<Gen> l) {
182  
  if (recommendations == null) { 
183  
    recommendations = new L;
184  
    for (S s : scanLog("recommendations.txt"))
185  
      if (isSnippetID(s))
186  
        recommendations.add(s);
187  
  }
188  
    
189  
  for (final S solverID : recommendations)
190  
    gen(l, solverID, func {
191  
      O c = hotwireCached(solverID);
192  
      ret call(c, "calc", log);
193  
    });
194  
}
195  
196  
static O oneAssoc(final L<S> log) {
197  
  ret func {
198  
    for (int i = l(log)-2; i >= 0; i--)
199  
      if (eqic(log.get(i), last(log)))
200  
        ret log.get(i+1);
201  
    null;
202  
  };
203  
}
204  
205  
static O mostPopular(final L<S> log) {
206  
  ret func {
207  
    ret new MultiHashSet<S>(log).getMostPopularEntry();
208  
  };
209  
}
210  
211  
synchronized static L<S> getLastFromLog(int n) {
212  
  ret cloneList(getLast(log, n));
213  
}

Author comment

Began life as a copy of #1003684

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: #1003695
Snippet name: "Random" v6 (with !hotwire)
Eternal ID of this version: #1003695/1
Text MD5: 8a41ac109c6877d5f22b2060b3f3385c
Transpilation MD5: 82b804970adcd1b411a98a5d654b0841
Author: stefan
Category: javax / talking robots
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-07-27 17:05:06
Source code size: 5023 bytes / 213 lines
Pitched / IR pitched: No / No
Views / Downloads: 738 / 968
Referenced in: [show references]