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

225
LINES

< > BotCompany Repo | #1001187 // Console Bot (developing)

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

Libraryless. Click here for Pure Java version (1554L/12K/40K).

1  
!747
2  
!awt {
3  
!actionListener {
4  
!pcall {
5  
6  
m {
7  
  static new L<Console> consoles;
8  
  static Object object = consoles;
9  
  
10  
  p {
11  
    makeAndroid("Console Bot.");
12  
    yo("make a console");
13  
  }
14  
  
15  
  static void yo(S s) {
16  
    pcall {
17  
      print(answer(s));
18  
    }
19  
  }
20  
  
21  
  static synchronized S answer(S s) {
22  
    if (match3("make a console", s)) {
23  
      consoles.add(new Console(new String[] {"yo"}));
24  
      return "ok";
25  
    }
26  
    if (match3("how many consoles", s))
27  
      return "Currently, " + consoles.size() + ". i can make more also.";
28  
    return talkBot(s);
29  
  }
30  
  
31  
  static S talkBot(S s) {
32  
    new Matches m;
33  
    
34  
    if (match3("are they all *", s, m))
35  
      return areTheyAll(m.m[0]);
36  
37  
    return null;
38  
  }
39  
  
40  
  static L<O> openCollection() {
41  
    if (!(object instanceof Collection))
42  
      fail(structure(object) + " is not a collection");
43  
    return new ArrayList<O>((Collection) object);
44  
  }
45  
  
46  
  static S areTheyAll(S fuckfuckfuckfuck) {
47  
    boolean yes = true;
48  
    O what = null;
49  
    int n = 0;
50  
    for (O o : openCollection()) {
51  
      ++n;
52  
      int value = is(o, fuckfuckfuckfuck);
53  
      if (value == -1)
54  
        return "no. " + structure(o) + " is not.";
55  
      else if (value == 0) {
56  
        yes = false;
57  
        what = o;
58  
      }
59  
    }
60  
    return yes ? "yes (" + n + "checked)" : "i don't know. i am not sure on " + structure(what) + ".";
61  
  }
62  
  
63  
  // -1 (no), 0 (maybe), 1 (yes) -- todo
64  
  static int is(O o, S property) {
65  
    if (o instanceof Console) {
66  
      JFrame frame = ((Console) o).frame;
67  
      return frame != null && frame.isVisible() ? 1 : -1; // XXX? is that the right method?
68  
    }
69  
    return 0;
70  
  }
71  
  
72  
  !include #1000943 // DelayedUpdate
73  
74  
  static class Console extends WindowAdapter implements WindowListener, ActionListener {
75  
    PipedInputStream pin=new PipedInputStream();
76  
    PipedInputStream pin2=new PipedInputStream();
77  
    PipedInputStream pin3=new PipedInputStream();
78  
    Thread reader, reader2;
79  
    boolean quit;
80  
  
81  
    JFrame frame;
82  
    JTextArea textArea;
83  
    JTextField tfInput;
84  
    StringBuffer buf = new StringBuffer();
85  
    JButton buttonclear, buttonkill, buttonrestart, buttonduplicate, buttonstacktrace;
86  
    String[] args;
87  
88  
    final DelayedUpdate du = new DelayedUpdate(new Runnable() {
89  
      public void run() { try {
90  
91  
        textArea.append(buf.substring(textArea.getText().length()));
92  
93  
      } catch (Exception __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}});
94  
95  
    public Console(final String[] args) ctex {
96  
      this.args = args;
97  
      // create all components and add them
98  
      frame=new JFrame(args.length == 0 ? "Console Bot Console" : "Console Bot Console - " + join(" ", args));
99  
100  
		/*Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
101  
		Dimension frameSize=new Dimension((int)(screenSize.width/2),(int)(screenSize.height/2));
102  
		int x=(int)(frameSize.width/2);
103  
		int y=(int)(frameSize.height/2);
104  
		frame.setBounds(x,y,frameSize.width,frameSize.height);*/
105  
106  
      // put in right-bottom corner
107  
      Rectangle r = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
108  
      int w = 550, h = 200;
109  
      frame.setBounds(r.x+r.width-w, r.y+r.height-h, w, h);
110  
111  
      textArea=new JTextArea();
112  
      textArea.setEditable(false);
113  
      buttonclear = new JButton("clear");
114  
      buttonkill = new JButton("kill");
115  
      buttonrestart = new JButton("restart");
116  
      buttonduplicate = new JButton("duplicate");
117  
      buttonstacktrace = new JButton("status");
118  
      buttonstacktrace.setToolTipText("Show threads & stack traces.");
119  
120  
      JPanel buttons = new JPanel(new GridLayout(1, 5));
121  
      buttons.add(buttonclear);
122  
      buttons.add(buttonkill);
123  
      buttons.add(buttonrestart);
124  
      buttons.add(buttonduplicate);
125  
      buttons.add(buttonstacktrace);
126  
127  
      final PipedOutputStream pout3=new PipedOutputStream(pin3);
128  
      tfInput = new JTextField();
129  
      tfInput.addActionListener(actionListener {
130  
        S line = tfInput.getText();
131  
        try {
132  
          pout3.write((line + "\n").getBytes("UTF-8"));
133  
          pout3.flush();
134  
        } catch (Exception e) {}
135  
        tfInput.setText("");
136  
      });
137  
      
138  
      JPanel panel = new JPanel(new BorderLayout());
139  
      panel.add(new JScrollPane(textArea), BorderLayout.CENTER);
140  
      panel.add(tfInput, BorderLayout.SOUTH);
141  
      
142  
      frame.addWindowListener(new WindowAdapter() {
143  
        public void windowActivated(WindowEvent e) {
144  
          tfInput.requestFocus();
145  
        }
146  
      });
147  
      
148  
      frame.getContentPane().setLayout(new BorderLayout());
149  
      frame.getContentPane().add(panel, BorderLayout.CENTER);
150  
      frame.getContentPane().add(buttons, BorderLayout.SOUTH);
151  
      frame.setVisible(true);
152  
      
153  
      //frame.addWindowListener(this); // disabled for now
154  
      buttonclear.addActionListener(this);
155  
      buttonkill.addActionListener(this);
156  
      buttonrestart.addActionListener(this);
157  
      buttonduplicate.addActionListener(this);
158  
      buttonstacktrace.addActionListener(this);
159  
160  
      quit=false; // signals the Threads that they should exit
161  
      
162  
      if (args.length != 0) {
163  
        print("Starting title updater");
164  
        new Thread("Console Title Updater :)") {
165  
          public void run() {
166  
            if (args.length != 0) {
167  
              int i = 0;
168  
              while (i < args.length && !isSnippetID(args[i])) ++i;
169  
              print("Getting title for " + args[i]);
170  
              String title = getSnippetTitle(args[i]);
171  
              print("Title: " + title);
172  
              if (title != null && title.length() != 0)
173  
                frame.setTitle(title + " [Output]");
174  
            }
175  
          }
176  
        }.start();
177  
      }
178  
      
179  
      System.setIn(pin3);
180  
      
181  
    }
182  
183  
    public synchronized void windowClosed(WindowEvent evt)
184  
    {
185  
      consoles.remove(this);
186  
      /*quit=true;
187  
      this.notifyAll(); // stop all threads
188  
      try { reader.join(1000);pin.close();   } catch (Exception e){}
189  
      try { reader2.join(1000);pin2.close(); } catch (Exception e){}
190  
      System.exit(0);*/
191  
    }
192  
193  
    public synchronized void windowClosing(WindowEvent evt)
194  
    {
195  
      frame.setVisible(false); // default behaviour of JFrame
196  
      frame.dispose();
197  
    }
198  
199  
    public synchronized void actionPerformed(ActionEvent evt) {
200  
      if (evt.getSource() == buttonkill) {
201  
        print("Console: Kill button pressed!");
202  
        // TODO: give threads time to finish, e.g. reportToChat?
203  
        System.exit(0);
204  
      } else if (evt.getSource() == buttonrestart) {
205  
        print("Console: Restart button pressed.");
206  
        nohupJavax(smartJoin(args));
207  
        System.exit(0);
208  
      } else if (evt.getSource() == buttonduplicate) {
209  
        print("Console: Duplicate button pressed.");
210  
        nohupJavax(smartJoin(args));
211  
      } else if (evt.getSource() == buttonstacktrace) {
212  
        listUserThreadsWithStackTraces();
213  
      } else {
214  
        textArea.setText("");
215  
        buf = new StringBuffer();
216  
      }
217  
    }
218  
219  
    public void appendText(String s, boolean outNotErr) {
220  
      //if (verbose) oldOut.println("Console appendText " + outNotErr + " " + quote(s));
221  
      buf.append(s);
222  
      du.trigger();
223  
    }
224  
  } // Console
225  
}

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: #1001187
Snippet name: Console Bot (developing)
Eternal ID of this version: #1001187/1
Text MD5: 82ea281821a4e01eed8b5f2b69da8ae9
Transpilation MD5: ed189e304352510ef616b771923f807d
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2015-10-02 14:21:37
Source code size: 7364 bytes / 225 lines
Pitched / IR pitched: No / Yes
Views / Downloads: 670 / 934
Referenced in: [show references]