Skip to content

Instantly share code, notes, and snippets.

@tcelik
Created April 11, 2019 09:55
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save tcelik/536f46104f050bbcb915b65585e3df9e to your computer and use it in GitHub Desktop.
Save tcelik/536f46104f050bbcb915b65585e3df9e to your computer and use it in GitHub Desktop.
Terminal-Reply
package org.csystem.cmdpromptapp.app;
import java.util.ArrayList;
import java.util.Scanner;
import java.util.function.Consumer;
public class Terminal {
public static void main(String[] args)
{
// Evaluate find print loop REPL App
Terminal terminal = new Terminal();
terminal.run();
}
private static final Scanner m_kb = new Scanner(System.in);
// Enum 0 ve 1 dememek için parse ederken neyin-nerede olduğu split için aslında.
private enum Info {
OPERATION, INPUT;
}
// inner class sadece biz kullanacağız dışarıya açmıyoruz.
private class Command {
public String Cmd;
public Consumer<String> Proc;
public Command(String cmd, Consumer<String> consumer)
{
Cmd = cmd;
Proc = consumer;
}
}
private ArrayList<Command> m_commands;
private void init()
{
m_commands = new ArrayList<>();
addCommandToTerminal();
}
private void addCommandToTerminal()
{
//m_commands.add( new Command("reverse", input -> System.out.println(StringUtil.reverseWithStrBuilderWay(input))) );
m_commands.add( new Command("len", input -> System.out.println(input.length())) );
// m_commands.add( new Command("isPrime", NumberUtil::isPrime) ); //kendin sınıf yaz araya gir.
m_commands.add( new Command("clear", input -> System.out.println("\n\n\n\n\n\n\n\n\n\n\n\n") ) );
}
public Terminal()
{
init();
//...
}
public Command findCommandByName(String cmd)
{
for (Command c : m_commands) {
if (c.Cmd.equalsIgnoreCase(cmd))
return c;
}
return null;
}
public void run()
{
// Loop
for (;;) {
// Read
System.out.println("operation?-input?");
String operationInput = m_kb.nextLine();
if (operationInput.equalsIgnoreCase("exit"))
return;
// Parse
String [] parts = operationInput.split("-");
// Evaluate & Find Command From Collection
Command c = findCommandByName(parts[Info.OPERATION.ordinal()]);
if (c != null)
c.Proc.accept(parts[1]); // print
else
System.out.println("command not found: " + operationInput); //...
}
}
}
@tcelik
Copy link
Author

tcelik commented Apr 11, 2019

REPL APP - Command Line Interface App

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment