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

47
LINES

< > BotCompany Repo | #1004143 // Add history to console input

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

Libraryless. Click here for Pure Java version (620L/4K/16K).

!752

p {
  final JTextField input = consoleInputField();
  if (input != null && !isTrue(getOpt(getOpt(getJavaX(), "console"), "hasHistory"))) {
    final Var<Int> historyIdx = new Var(0);
    final new L<S> history;
    input.addKeyListener(new KeyAdapter {
      public void keyPressed(KeyEvent e) {
        if (e.getKeyCode() == KeyEvent.VK_UP) {
          print("up");
          listSet(history, historyIdx.get(), input.getText());
          historyIdx.set(max(0, historyIdx.get()-1));
          S text = get(history, historyIdx.get());
          if (text != null) {
            input.setText(text);
            input.selectAll();
          }
        }
        
        if (e.getKeyCode() == KeyEvent.VK_DOWN) {
          print("down");
          S text = input.getText();
          if (historyIdx.get() < l(history) ||
            neq(last(history), text))
            listSet(history, historyIdx.get(), text);
          historyIdx.set(min(l(history), historyIdx.get()+1));
          text = get(history, historyIdx.get());
          input.setText(unnull(text));
          input.selectAll();
        }
      }
    });
    //ActionListener[] l = input.getActionListeners();
    //for (ActionListener a : l) input.removeActionListener(a);
    input.addActionListener(actionListener {
      S text = input.getText();
      print("Text: " + text);
      if (nempty(text)) {
        history.remove(text);
        history.add(text);
      }
      historyIdx.set(l(history));
    });
    //for (ActionListener a : l) input.addActionListener(a);
  }
}

download  show line numbers  debug dex  old transpilations   

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

No comments. add comment

Snippet ID: #1004143
Snippet name: Add history to console input
Eternal ID of this version: #1004143/1
Text MD5: d3685b27b16ce15d0712eb71dec3bc44
Transpilation MD5: 7f47690dfe206df72198d54420921aa7
Author: stefan
Category: javax
Type: JavaX source code
Public (visible to everyone): Yes
Archived (hidden from active list): No
Created/modified: 2016-08-08 13:46:15
Source code size: 1593 bytes / 47 lines
Pitched / IR pitched: No / No
Views / Downloads: 447 / 505
Referenced in: [show references]