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).

!747
!awt {
!actionListener {
!pcall {

m {
  static new L<Console> consoles;
  static Object object = consoles;
  
  p {
    makeAndroid("Console Bot.");
    yo("make a console");
  }
  
  static void yo(S s) {
    pcall {
      print(answer(s));
    }
  }
  
  static synchronized S answer(S s) {
    if (match3("make a console", s)) {
      consoles.add(new Console(new String[] {"yo"}));
      return "ok";
    }
    if (match3("how many consoles", s))
      return "Currently, " + consoles.size() + ". i can make more also.";
    return talkBot(s);
  }
  
  static S talkBot(S s) {
    new Matches m;
    
    if (match3("are they all *", s, m))
      return areTheyAll(m.m[0]);

    return null;
  }
  
  static L<O> openCollection() {
    if (!(object instanceof Collection))
      fail(structure(object) + " is not a collection");
    return new ArrayList<O>((Collection) object);
  }
  
  static S areTheyAll(S fuckfuckfuckfuck) {
    boolean yes = true;
    O what = null;
    int n = 0;
    for (O o : openCollection()) {
      ++n;
      int value = is(o, fuckfuckfuckfuck);
      if (value == -1)
        return "no. " + structure(o) + " is not.";
      else if (value == 0) {
        yes = false;
        what = o;
      }
    }
    return yes ? "yes (" + n + "checked)" : "i don't know. i am not sure on " + structure(what) + ".";
  }
  
  // -1 (no), 0 (maybe), 1 (yes) -- todo
  static int is(O o, S property) {
    if (o instanceof Console) {
      JFrame frame = ((Console) o).frame;
      return frame != null && frame.isVisible() ? 1 : -1; // XXX? is that the right method?
    }
    return 0;
  }
  
  !include #1000943 // DelayedUpdate

  static class Console extends WindowAdapter implements WindowListener, ActionListener {
    PipedInputStream pin=new PipedInputStream();
    PipedInputStream pin2=new PipedInputStream();
    PipedInputStream pin3=new PipedInputStream();
    Thread reader, reader2;
    boolean quit;
  
    JFrame frame;
    JTextArea textArea;
    JTextField tfInput;
    StringBuffer buf = new StringBuffer();
    JButton buttonclear, buttonkill, buttonrestart, buttonduplicate, buttonstacktrace;
    String[] args;

    final DelayedUpdate du = new DelayedUpdate(new Runnable() {
      public void run() { try {

        textArea.append(buf.substring(textArea.getText().length()));

      } catch (Exception __e) { throw __e instanceof RuntimeException ? (RuntimeException) __e : new RuntimeException(__e); }}});

    public Console(final String[] args) ctex {
      this.args = args;
      // create all components and add them
      frame=new JFrame(args.length == 0 ? "Console Bot Console" : "Console Bot Console - " + join(" ", args));

		/*Dimension screenSize=Toolkit.getDefaultToolkit().getScreenSize();
		Dimension frameSize=new Dimension((int)(screenSize.width/2),(int)(screenSize.height/2));
		int x=(int)(frameSize.width/2);
		int y=(int)(frameSize.height/2);
		frame.setBounds(x,y,frameSize.width,frameSize.height);*/

      // put in right-bottom corner
      Rectangle r = GraphicsEnvironment.getLocalGraphicsEnvironment().getMaximumWindowBounds();
      int w = 550, h = 200;
      frame.setBounds(r.x+r.width-w, r.y+r.height-h, w, h);

      textArea=new JTextArea();
      textArea.setEditable(false);
      buttonclear = new JButton("clear");
      buttonkill = new JButton("kill");
      buttonrestart = new JButton("restart");
      buttonduplicate = new JButton("duplicate");
      buttonstacktrace = new JButton("status");
      buttonstacktrace.setToolTipText("Show threads & stack traces.");

      JPanel buttons = new JPanel(new GridLayout(1, 5));
      buttons.add(buttonclear);
      buttons.add(buttonkill);
      buttons.add(buttonrestart);
      buttons.add(buttonduplicate);
      buttons.add(buttonstacktrace);

      final PipedOutputStream pout3=new PipedOutputStream(pin3);
      tfInput = new JTextField();
      tfInput.addActionListener(actionListener {
        S line = tfInput.getText();
        try {
          pout3.write((line + "\n").getBytes("UTF-8"));
          pout3.flush();
        } catch (Exception e) {}
        tfInput.setText("");
      });
      
      JPanel panel = new JPanel(new BorderLayout());
      panel.add(new JScrollPane(textArea), BorderLayout.CENTER);
      panel.add(tfInput, BorderLayout.SOUTH);
      
      frame.addWindowListener(new WindowAdapter() {
        public void windowActivated(WindowEvent e) {
          tfInput.requestFocus();
        }
      });
      
      frame.getContentPane().setLayout(new BorderLayout());
      frame.getContentPane().add(panel, BorderLayout.CENTER);
      frame.getContentPane().add(buttons, BorderLayout.SOUTH);
      frame.setVisible(true);
      
      //frame.addWindowListener(this); // disabled for now
      buttonclear.addActionListener(this);
      buttonkill.addActionListener(this);
      buttonrestart.addActionListener(this);
      buttonduplicate.addActionListener(this);
      buttonstacktrace.addActionListener(this);

      quit=false; // signals the Threads that they should exit
      
      if (args.length != 0) {
        print("Starting title updater");
        new Thread("Console Title Updater :)") {
          public void run() {
            if (args.length != 0) {
              int i = 0;
              while (i < args.length && !isSnippetID(args[i])) ++i;
              print("Getting title for " + args[i]);
              String title = getSnippetTitle(args[i]);
              print("Title: " + title);
              if (title != null && title.length() != 0)
                frame.setTitle(title + " [Output]");
            }
          }
        }.start();
      }
      
      System.setIn(pin3);
      
    }

    public synchronized void windowClosed(WindowEvent evt)
    {
      consoles.remove(this);
      /*quit=true;
      this.notifyAll(); // stop all threads
      try { reader.join(1000);pin.close();   } catch (Exception e){}
      try { reader2.join(1000);pin2.close(); } catch (Exception e){}
      System.exit(0);*/
    }

    public synchronized void windowClosing(WindowEvent evt)
    {
      frame.setVisible(false); // default behaviour of JFrame
      frame.dispose();
    }

    public synchronized void actionPerformed(ActionEvent evt) {
      if (evt.getSource() == buttonkill) {
        print("Console: Kill button pressed!");
        // TODO: give threads time to finish, e.g. reportToChat?
        System.exit(0);
      } else if (evt.getSource() == buttonrestart) {
        print("Console: Restart button pressed.");
        nohupJavax(smartJoin(args));
        System.exit(0);
      } else if (evt.getSource() == buttonduplicate) {
        print("Console: Duplicate button pressed.");
        nohupJavax(smartJoin(args));
      } else if (evt.getSource() == buttonstacktrace) {
        listUserThreadsWithStackTraces();
      } else {
        textArea.setText("");
        buf = new StringBuffer();
      }
    }

    public void appendText(String s, boolean outNotErr) {
      //if (verbose) oldOut.println("Console appendText " + outNotErr + " " + quote(s));
      buf.append(s);
      du.trigger();
    }
  } // Console
}

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: 597 / 841
Referenced in: [show references]