Skip to content
Snippets Groups Projects
Commit 68f7972d authored by Robert Goldmann's avatar Robert Goldmann
Browse files

implemented autocomplete feature

parent aa718dbb
No related branches found
No related tags found
No related merge requests found
No preview for this file type
File added
No preview for this file type
app.name=CommandLine
version.code=1
version.name=1.0.0
version.date=26.07.16
version.code=0
version.name=1.1.0
version.date=16.09.16
help.list=list - lists all possible commands
help.help=help - shows help for given command\nSYNTAX: help [COMMAND]
help.clear=clear - clears the history
help.shortcuts=shortcuts - lists all available shortcuts
error.general=An error occurred.
error.unknown.command=Unknown command. Use \"list\" for a list of possible commands.
error.invalid.arguments=Invalid arguments. Use \"help commandname\" for help.
error.no.help=Unknown command as parameter. Can't display help. Use \"list\" for a list of possible commands.
info.shortcuts=[ENTER] - execute command\n[UP] - scroll through recently used commands\n[ESC] - clear input\n[TAB] - autocomplete
\ No newline at end of file
File added
No preview for this file type
package commandLine;
import java.util.ArrayList;
import java.util.Comparator;
import commands.Command;
import commands.HistoryEntry;
import commands.HistoryType;
import commands.PossibleCommands;
import javafx.application.Platform;
import javafx.event.EventHandler;
import javafx.fxml.FXML;
import javafx.scene.control.TextArea;
......@@ -15,6 +17,7 @@ import javafx.scene.input.KeyEvent;
/**
* Controller for the CommandLine stage
*
* @author deadlocker8
*
*/
......@@ -54,6 +57,12 @@ public class CommandLineController
{
clearConsole();
}
if(event.getCode().equals(KeyCode.TAB))
{
autocomplete();
event.consume();
}
}
});
......@@ -176,6 +185,70 @@ public class CommandLineController
textfieldInput.setText(commandLine.globalHistory.get(commandLine.lastShownCommand - 1).getText());
commandLine.lastShownCommand--;
}
Platform.runLater(()->
{
textfieldInput.positionCaret(textfieldInput.getText().length());
});
}
}
private void autocomplete()
{
String input = textfieldInput.getText().replace("\n", "");
if(input.equals(""))
{
return;
}
ArrayList<Command> commands = PossibleCommands.possibleCommands;
//filter possible commands
ArrayList<Command> filteredCommands = new ArrayList<>();
for(Command currentCommand : commands)
{
if(currentCommand.getKeyword().startsWith(input))
{
filteredCommands.add(currentCommand);
}
}
//sort commands alphabetically
filteredCommands.sort(new Comparator<Command>()
{
@Override
public int compare(Command o1, Command o2)
{
return o1.keyword.compareTo(o2.keyword);
}
});
if(filteredCommands.size() == 1)
{
textfieldInput.setText(filteredCommands.get(0).getKeyword());
}
else
{
StringBuilder sb = new StringBuilder();
sb.append(">>> Possible commands for \"" + input + "\":\n");
for(int i = 0; i < filteredCommands.size(); i++)
{
sb.append(filteredCommands.get(i).keyword);
if(i != (filteredCommands.size()-1))
{
sb.append("\n");
}
}
print(sb.toString());
textfieldInput.setText(input);
}
Platform.runLater(()->
{
textfieldInput.positionCaret(textfieldInput.getText().length());
});
}
}
\ No newline at end of file
app.name=CommandLine
version.code=1
version.name=1.0.0
version.date=26.07.16
version.code=0
version.name=1.1.0
version.date=16.09.16
help.list=list - lists all possible commands
help.help=help - shows help for given command\nSYNTAX: help [COMMAND]
help.clear=clear - clears the history
help.shortcuts=shortcuts - lists all available shortcuts
error.general=An error occurred.
error.unknown.command=Unknown command. Use \"list\" for a list of possible commands.
error.invalid.arguments=Invalid arguments. Use \"help commandname\" for help.
error.no.help=Unknown command as parameter. Can't display help. Use \"list\" for a list of possible commands.
info.shortcuts=[ENTER] - execute command\n[UP] - scroll through recently used commands\n[ESC] - clear input\n[TAB] - autocomplete
\ No newline at end of file
package commands;
/**
* Lists all available Shortcuts
* @author deadlocker8
*
*/
public class CommandShortcuts extends Command
{
public CommandShortcuts()
{
super.keyword = "shortcuts";
super.numberOfParams = 0;
super.helptText = "help.shortcuts";
}
@Override
public void execute(String[] command, CommandBundle bundle)
{
bundle.getController().print("Available Shortcuts:");
bundle.getController().print(bundle.getLanguageBundle().getString("info.shortcuts"));
}
}
\ No newline at end of file
......@@ -13,6 +13,7 @@ public class PossibleCommands
public static final ArrayList<Command> possibleCommands = new ArrayList<>(Arrays.asList(
new CommandList(),
new CommandHelp(),
new CommandClear()
new CommandClear(),
new CommandShortcuts()
));
}
\ No newline at end of file
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment