Skip to content

Instantly share code, notes, and snippets.

@azenla
Created June 20, 2013 23:28
Show Gist options
  • Save azenla/5827676 to your computer and use it in GitHub Desktop.
Save azenla/5827676 to your computer and use it in GitHub Desktop.
ConsoleHandler
package com.directmyfile.gcp.client;
import com.directmyfile.gcp.client.connection.ConnectionHandler;
import com.directmyfile.gcp.client.gcp.GCPHelper;
import org.fusesource.jansi.Ansi;
import org.fusesource.jansi.AnsiConsole;
import java.io.IOException;
import java.io.PrintStream;
import java.util.NoSuchElementException;
import java.util.Scanner;
public class ConsoleHandler {
private static Scanner in = new Scanner(System.in);
private static PrintStream out = System.out;
private static boolean isConnected = false;
private static String currentChannel = "";
public static void start() throws IOException {
Thread.currentThread().setUncaughtExceptionHandler(new Thread.UncaughtExceptionHandler() {
@Override
public void uncaughtException(Thread t, Throwable e) {
if (!e.getClass().getName().equals(NoSuchElementException.class.getName())) {
e.printStackTrace();
}
}
});
ConnectionHandler connection = Main.connection;
AnsiConsole.systemInstall();
AnsiConsole.out().println(ansi().fg(Ansi.Color.RED).a("Welcome to GCP Client Console"));
AnsiConsole.out().print(ansi().reset());
while (true) {
String line = in.nextLine();
String[] full = line.trim().split(" ");
if (line.startsWith("/")) {
String command;
if (line.contains(" ")) {
command = line.substring(1, line.indexOf(' '));
} else {
command = line.substring(1);
}
if (command.equals("connect")) {
out.println("Connecting....");
connection.connect();
isConnected = true;
} else if (command.equals("join")) {
if (full.length!=2) {
out.println("Usage: /join #channel");
continue;
}
String channel = full[1];
out.println("Joining " + channel);
GCPHelper.joinChannel(channel);
} else if (command.equals("leave")) {
if (full.length!=2) {
out.println("Usage: /leave #channel");
continue;
}
String channel = full[1];
out.println("Leaving " + channel);
GCPHelper.leaveChannel(channel);
} else if (command.equals("disconnect")) {
if (isConnected) {
out.println("Quitting....");
GCPHelper.quit();
isConnected = false;
} else {
out.println("Not Connected.");
}
} else if (command.equals("exit")) {
System.exit(0);
} else if (command.equals("ping")) {
if (full.length!=2) {
out.println("Pinging Server");
GCPHelper.ping();
} else {
out.println("Pinging " + full[1]);
GCPHelper.ping(full[1]);
}
} else if (command.equals("help")) {
out.println("Commands:");
out.println(" * /connect - Connect to Server");
out.println(" * /disconnect - Disconnect the Server");
out.println(" * /join - Join a Channel");
out.println(" * /leave - Leave a Channel");
out.println(" * /exit - Exit the Client");
} else if (command.equals("debug")) {
if (Main.debug) {
Main.debug = false;
out.println("Turned off debugging.");
} else {
Main.debug = true;
out.println("Turned on debugging.");
}
} else if (command.equals("raw")) {
String raw = line.replace(full[0] + " ", "");
GCPHelper.sendRaw(raw);
} else if (command.equals("")) {
if (full.length!=2) {
out.println("Usage: /register PASSWORD");
} else {
out.println("Registering....");
GCPHelper.register(full[1]);
}
}
} else {
if (!line.equals("")) {
if (GCPHelper.getEventHandler()==null) {
out.println("Not Connected to a server!");
} else {
if (isConnected) {
GCPHelper.sendMessage(currentChannel, line);
}
}
}
}
}
}
public static void setCurrentChannel(String channel) {
if (channel==null) {
System.out.println("No Longer in a Channel");
} else {
ConsoleHandler.currentChannel = channel;
System.out.println("Now focused on " + channel);
}
}
public static String getCurrentChannel() {
return currentChannel;
}
private static Ansi ansi() {
return Ansi.ansi();
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment