!759 !include #1003797 // Thinker static JTable table; static JTextArea chat; static JTextField input; static File logFile; static new L log; static L recommendations; static int listDelay = 2000; static Bool thinking; static bool updateOnce = true; static new Thinker thinker; static S systemPrefix = "[system]"; p-awt { //substanceLAF("EmeraldDusk"); // Too dark! substanceLAF("ChallengerDeep"); loadLog(); thinker.startUp(log); table = tableWithTooltips(); chat = autoScroll(wordWrapTextArea()); chat.setText(joinLines(log)); input = new JTextField; JFrame frame = showFrame(vgrid(centerAndSouth(chat, input), table)); //setFrameIconLater(frame, "#1003593"); onEnter(input, r { post(); }); onDoubleClick(table, voidfunc(int row) { chooseSuggestion(row); }); for (int i = 1; i <= 12; i++) { final int _i = i; registerFunctionKey(frame, i, r { chooseSuggestion(_i-1); }); } fillList(); input.requestFocus(); } static S getInput() { ret joinLines(" # ", input.getText().trim()); } static void post() { S i = getInput(); if (inputAllowedByUser(i)) post(i); } svoid chooseSuggestion(int row) { L line = getTableLine(table, row); if (line == null) ret; S s = line.get(0); if (empty(s)) ret; input.setText(s); post(); } static bool inputAllowedByUser(S i) { ret !swic(i, systemPrefix); } static void postSystemMessage(S msg) { post(systemPrefix + " " + msg); } static void post(S i) { S s = i + "\n"; chat.append(s); appendToFile(logFile, "[" + chatTime() + "] " + s); synchronized(mc()) { log.add(i); } input.selectAll(); updateOnce = true; try { action(i); } catch e { printStackTrace(e); postSystemMessage(exceptionToStringShort(e)); } } static void action(S s) { s = dropBracketPrefix(s); // e.g. "[bot]" if (!s.startsWith("!")) ret; s = dropPrefix("!", s); new Matches m; // SYSTEM COMMANDS if "start program *" { S progID = m.fsi(0); S title = getSnippetTitle(progID); // TODO: Show author! S msg = "Run program " + progID + " - " + title + "?"; if (confirmOKCancel(chat, msg)) { postSystemMessage("Starting program " + progID + " - " + quote(title)); nohupJavax(progID); } else postSystemMessage("Program start cancelled by user (was: " + progID + ")"); } if "hotwire * with argument *" { S progID = m.fsi(0), arg = m.unq(1); S title = getSnippetTitle(progID); S msg = "Hotwire & run program " + progID + " - " + quote(title) + " with argument " + quote(arg) + "?"; if (confirmOKCancel(chat, msg)) { postSystemMessage("Hotwiring & running program " + progID + " - " + quote(title) + " with argument " + quote(arg)); run(progID, arg); } else postSystemMessage("Program start cancelled by user (was: " + progID + ")"); } } static void fillList() { bool t = shouldUpdateList() || updateOnce; updateOnce = false; if (neq(t, thinking)) { thinking = t; setFrameIcon(table, t ? "#1003603" : "#1003593"); } if (!t) againl8r(); else thread "Fill List" { final new L data; thinker.makeListData(cloneList(log), data); dataToTable_uneditable(table, data); againl8r(); } } static void againl8r() { swingAfter(table, listDelay, r { fillList(); }); } static bool shouldUpdateList() { ret getFrame(table).isFocused() && !mouseInComponent(table); } // also called from outside static L loadLog() { if (logFile == null) logFile = getProgramFile("log.txt"); for (S s : toLines(loadTextFile(logFile))) pcall { log.add(substring(s, s.indexOf(']')+1).trim()); } ret log; } // CREATIVE PART! // put func {}'s returning a string in there static void makeGenerators(L l, final L log) { gen(l, "hello random", func { "Hello " + randomID(10) }); gen(l, quine(func { last(log) })); gen(l, quine(func { oneOf(log) })); gen(l, "1 assoc", oneAssoc(log)); gen(l, "most popular", mostPopular(log)); addLoadedSolvers(l); } svoid addLoadedSolvers(L l) { if (recommendations == null) { recommendations = new L; for (S s : scanLog("recommendations.txt")) if (isSnippetID(s)) recommendations.add(s); } for (final S solverID : recommendations) gen(l, solverID, func { O c = hotwireCached(solverID); ret call(c, "calc", log); }); } static O oneAssoc(final L log) { ret func { for (int i = l(log)-2; i >= 0; i--) if (eqic(log.get(i), last(log))) ret log.get(i+1); null; }; } static O mostPopular(final L log) { ret func { ret new MultiHashSet(log).getMostPopularEntry(); }; } synchronized static L getLastFromLog(int n) { ret cloneList(getLast(log, n)); }