Libraryless. Click here for Pure Java version (620L/4K/16K).
1 | !752 |
2 | |
3 | p { |
4 | final JTextField input = consoleInputField(); |
5 | if (input != null && !isTrue(getOpt(getOpt(getJavaX(), "console"), "hasHistory"))) { |
6 | final Var<Int> historyIdx = new Var(0); |
7 | final new L<S> history; |
8 | input.addKeyListener(new KeyAdapter { |
9 | public void keyPressed(KeyEvent e) { |
10 | if (e.getKeyCode() == KeyEvent.VK_UP) { |
11 | print("up"); |
12 | listSet(history, historyIdx.get(), input.getText()); |
13 | historyIdx.set(max(0, historyIdx.get()-1)); |
14 | S text = get(history, historyIdx.get()); |
15 | if (text != null) { |
16 | input.setText(text); |
17 | input.selectAll(); |
18 | } |
19 | } |
20 | |
21 | if (e.getKeyCode() == KeyEvent.VK_DOWN) { |
22 | print("down"); |
23 | S text = input.getText(); |
24 | if (historyIdx.get() < l(history) || |
25 | neq(last(history), text)) |
26 | listSet(history, historyIdx.get(), text); |
27 | historyIdx.set(min(l(history), historyIdx.get()+1)); |
28 | text = get(history, historyIdx.get()); |
29 | input.setText(unnull(text)); |
30 | input.selectAll(); |
31 | } |
32 | } |
33 | }); |
34 | //ActionListener[] l = input.getActionListeners(); |
35 | //for (ActionListener a : l) input.removeActionListener(a); |
36 | input.addActionListener(actionListener { |
37 | S text = input.getText(); |
38 | print("Text: " + text); |
39 | if (nempty(text)) { |
40 | history.remove(text); |
41 | history.add(text); |
42 | } |
43 | historyIdx.set(l(history)); |
44 | }); |
45 | //for (ActionListener a : l) input.addActionListener(a); |
46 | } |
47 | } |
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: | 648 / 767 |
Referenced in: | [show references] |