diff --git a/src/main/java/cli/Command.java b/src/main/java/cli/Command.java index 45cba24f81723bc18f6a2cb9e69c64d2f89d7475..cc88608eea966e824f65813200e9a5cd8af3c2c1 100644 --- a/src/main/java/cli/Command.java +++ b/src/main/java/cli/Command.java @@ -145,8 +145,31 @@ public class Command implements CommandInterface { level --; } return ret; - } + public Vector<Command> findCommands(String[] split, int index) { + if (split == null) { + return null; + } + + if (index >= split.length) { + return null; + } + + String s = split[index]; + Vector<Command> couldBe = new Vector<>(); + + // Search of all compatible commands starting with s + for (Command c: subcommands) { + if (c.getShortCommand().startsWith(s) || c.getCommand().startsWith(s)) { + Vector<Command> others = c.findCommands(split, index+1); + if (others != null) { + couldBe.addAll(others); + } + } + } + + return couldBe; + } } diff --git a/src/main/java/cli/Interpreter.java b/src/main/java/cli/Interpreter.java index 08721af6502342173dfa21e5418a92ca0cc396f9..31b7826df7aa0db74377edc074ef707851969319 100644 --- a/src/main/java/cli/Interpreter.java +++ b/src/main/java/cli/Interpreter.java @@ -60,6 +60,7 @@ import java.util.*; * @author Ludovic APVRILLE */ public class Interpreter implements Runnable, TerminalProviderInterface { + public final static Command[] commands = {new Help(), new Quit(), new Action(), new Set(), new Wait(), new Print(), new History(), new TestSpecific()}; @@ -94,8 +95,6 @@ public class Interpreter implements Runnable, TerminalProviderInterface { variables = new HashMap<>(); this.show = show; formerCommands = new Vector<>(); - term = new Terminal(); - term.setTerminalProvider(this); } @Override @@ -317,8 +316,50 @@ public class Interpreter implements Runnable, TerminalProviderInterface { return "> "; } - public void tabAction(String buffer) { + public boolean tabAction(String buffer) { + // Print all possibilities from current buffer + String buf = Conversion.replaceAllString(buffer, " ", " "); + String[] split = buf.split(" "); + + // From the split, determine commands already entered and completes it + Vector<Command> listOfCommands = findCommands(split, 0); + + if (listOfCommands.size()== 0) { + return false; + } + + for(Command c: listOfCommands) { + System.out.println(""+c.getCommand()); + return true; + } + + return true; + + } + + public Vector<Command> findCommands(String[] split, int index) { + if (split == null) { + return null; + } + + if (index >= split.length) { + return null; + } + + String s = split[index]; + Vector<Command> couldBe = new Vector<>(); + + // Search of all compatible commands starting with s + for (Command c: commands) { + if (c.getShortCommand().startsWith(s) || c.getCommand().startsWith(s)) { + Vector<Command> others = c.findCommands(split, index+1); + if (others != null) { + couldBe.addAll(others); + } + } + } + return couldBe; } diff --git a/src/main/java/myutil/RawConsoleInput.java b/src/main/java/myutil/RawConsoleInput.java index 90355b43ef3e66ec415ea468ef5596218e256098..7a304084ed7e1fd1db15e9c1324ece2fb5a4ead9 100644 --- a/src/main/java/myutil/RawConsoleInput.java +++ b/src/main/java/myutil/RawConsoleInput.java @@ -201,6 +201,7 @@ private static int readUnix (boolean wait) throws IOException { setTerminalAttrs(stdinFd, rawTermios); // switch off canonical mode, echo and signals try { if (!wait && System.in.available() == 0) { + System.out.println("Not ready"); return -2; } // no input available return readSingleCharFromByteStream(System.in); } finally { @@ -220,8 +221,10 @@ private static void setTerminalAttrs (int fd, Termios termios) throws IOExceptio try { int rc = libc.tcsetattr(fd, LibcDefs.TCSANOW, termios); if (rc != 0) { + System.out.println("Exception in term tcset"); throw new RuntimeException("tcsetattr() failed."); }} catch (LastErrorException e) { + System.out.println("Exception in term tcset"); throw new IOException("tcsetattr() failed.", e); }} private static int readSingleCharFromByteStream (InputStream inputStream) throws IOException { @@ -257,13 +260,17 @@ private static synchronized void initUnix() throws IOException { stdinIsConsole = libc.isatty(stdinFd) == 1; charsetDecoder = Charset.defaultCharset().newDecoder(); if (stdinIsConsole) { + //System.out.println("IsConsole=" + stdinIsConsole); originalTermios = getTerminalAttrs(stdinFd); rawTermios = new Termios(originalTermios); rawTermios.c_lflag &= ~(LibcDefs.ICANON | LibcDefs.ECHO | LibcDefs.ECHONL | LibcDefs.ISIG); + //rawTermios.c_lflag &= ~(LibcDefs.ICANON | LibcDefs.ECHO | LibcDefs.ECHONL); intermediateTermios = new Termios(rawTermios); - intermediateTermios.c_lflag |= LibcDefs.ICANON; + //intermediateTermios.c_lflag |= LibcDefs.ICANON; // Canonical mode can be switched off between the read() calls, but echo must remain disabled. - registerShutdownHook(); } + registerShutdownHook(); + System.out.println("New console"); + } initDone = true; } private static void resetConsoleModeUnix() throws IOException { diff --git a/src/main/java/myutil/Terminal.java b/src/main/java/myutil/Terminal.java index 5700aff48bc8fc06e611de5fc875622a1588b3b5..837d97f95e93eec8a9eec5928c31546d2abb318c 100644 --- a/src/main/java/myutil/Terminal.java +++ b/src/main/java/myutil/Terminal.java @@ -60,17 +60,23 @@ public class Terminal { private final static int ESC = 27; + private final static int TAB = 9; + private Vector<String> buffer; private int maxbufferSize = MAX_BUFFER_SIZE; private TerminalProviderInterface terminalProvider; private int cpt; private String sequence; + private String os; public Terminal() { buffer = new Vector<>(); cpt = 0; + os = System.getProperty("os.name").toLowerCase(); + System.out.println("Detected OS:" + os); + os = os.split(" ")[0]; } public void setTerminalProvider(TerminalProviderInterface tp) { @@ -79,7 +85,7 @@ public class Terminal { public String getNextCommand() { - char x = 110; + char x; int val = 0; @@ -123,6 +129,7 @@ public class Terminal { } + // Usual CHAR if ((sequence == null) && (val != -1)) { if (val == CR) { if (currentBuf.length() == 0) { @@ -135,8 +142,18 @@ public class Terminal { } } + //BACKSPACE if ((val == BACKSPACE) || (val == DEL)) { currentBuf = del(currentBuf); + //TAB + } else if (val == TAB) { + System.out.println("TAB"); + if (terminalProvider != null) { + boolean b =terminalProvider.tabAction(currentBuf); + if (b) { + printPrompt(cpt); + } + } } else if (val >= 32) { //System.out.print("" + x + "(val=" + val + ");"); myPrint("" + x); @@ -164,7 +181,10 @@ public class Terminal { } public void myPrint(String s) { - System.out.print(s); + if (os.compareTo("mac") != 0) { + System.out.print(s); + } + //System.out.flush(); } public void printHistory() { diff --git a/src/main/java/myutil/TerminalProviderInterface.java b/src/main/java/myutil/TerminalProviderInterface.java index 7bde18bf3ea15f055e9ec9f1ab7af05ef3902b28..1298906b132aa8b08fcb11395b2df3713dd0c053 100644 --- a/src/main/java/myutil/TerminalProviderInterface.java +++ b/src/main/java/myutil/TerminalProviderInterface.java @@ -54,7 +54,7 @@ public interface TerminalProviderInterface { public String getMidPrompt(); - public void tabAction(String buffer); + public boolean tabAction(String buffer);