Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
119 changes: 111 additions & 8 deletions src/App.java
Original file line number Diff line number Diff line change
@@ -1,13 +1,19 @@
import java.time.LocalDateTime;
import java.io.*;
import java.sql.*;
import java.time.LocalDateTime;
import java.util.*;

class MessageFetch extends Thread {
private Connection conn;
private static String SELECT_QUERY = "SELECT * FROM messages WHERE id > ? ORDER BY id ASC";
private String CurrentUser;
private String CurrentColor;

public MessageFetch(Connection conn) {
public MessageFetch(Connection conn,String CurrentColor,String CurrentUser) {
this.conn = conn;
this.CurrentColor=CurrentColor;
this.CurrentUser=CurrentUser;

}

public void run() {
Expand All @@ -22,8 +28,11 @@ public void run() {
LocalDateTime time = rs.getObject("timestamp", LocalDateTime.class);
String color = rs.getString("color_code");
String text = rs.getString("message_text");

message msg = new message(username, text, color, time);
msg.fetchmessage();
msg.fetchmessage(CurrentColor,CurrentUser);



App.id = rs.getInt("id");
}
Expand All @@ -44,6 +53,7 @@ public void run() {
}

public class App {

public static int id = 0;
private static final String[] COLORS = {
"\u001B[31m", // Red
Expand All @@ -65,26 +75,119 @@ public static String getRandomColor() {
return COLORS[rand.nextInt(COLORS.length)];
}

public static void main(String[] args) throws InterruptedException {
// ANSI CODES
private static final String SAVE = "\u001B[s";
private static final String REST = "\u001B[u";
private static final String CLR = "\u001B[K";

private static StringBuilder input = new StringBuilder();

public static synchronized void printIncoming(String msg) {
System.out.print(SAVE); // save cursor
System.out.print("\r\n"); // move to new line
System.out.println(msg); // print message
System.out.print(REST); // restore cursor
System.out.print(CLR); // clear junk
System.out.print("> " + input); // redraw input
System.out.flush();
}


public static void main(String[] args) throws Exception {
Scanner sc = new Scanner(System.in);
System.out.print("Enter username: ");
String uname = sc.nextLine();
String CurrentUser =uname;
String ucolor = getRandomColor();
String CurrentColor = ucolor;
Console console = System.console();

try (Connection conn = DBConnection.getConnection();) {
System.out.println("--- Chat room (Live) ---");
MessageFetch reciever = new MessageFetch(conn);
MessageFetch reciever = new MessageFetch(conn, CurrentColor,CurrentUser);
reciever.start();

// String text;
while (true) {

// if (System.in.available()>0){
// char c = (char) System.in.read();

// if(c=='\n'){
// text = input.toString();
// input.setLength(0);

// if (text.equalsIgnoreCase("exit")){
// break; // allow quitting
// }

// message msg = new message(uname, text, ucolor);
// msg.writemessage(conn);
// System.out.println();
// continue;
// }

// if(c==127 || c==8){
// if (input.length()>0){
// input.setLength(input.length()-1);
// System.out.print("\b \b");

// }
// continue;
// }
// input.append(c);
// System.out.print(c);


// }

// Thread.sleep(10);

String text = sc.nextLine();
//String text = console.readLine("msg:");

if (text.equalsIgnoreCase("exit"))
if (text.equalsIgnoreCase("exit")){
break; // allow quitting
}else if (text.equalsIgnoreCase("/list")){
System.out.println("/red /butterfly");
}
else if (text.equalsIgnoreCase("/butterfly")) {
text = "\n⣠⣤⣤⡤⠤⢤⣤⣀⡀⠀⠐⠒⡄⠀⡠⠒⠀⠀⢀⣀⣤⠤⠤⣤⣤⣤⡄\n" + //
"⠈⠻⣿⡤⠤⡏⠀⠉⠙⠲⣄⠀⢰⢠⠃⢀⡤⠞⠋⠉⠈⢹⠤⢼⣿⠏⠀\n" + //
"⠀⠀⠘⣿⡅⠓⢒⡤⠤⠀⡈⠱⣄⣼⡴⠋⡀⠀⠤⢤⡒⠓⢬⣿⠃⠀⠀\n" + //
"⠀⠀⠀⠹⣿⣯⣐⢷⣀⣀⢤⡥⢾⣿⠷⢥⠤⣀⣀⣞⣢⣽⡿⠃⠀⠀⠀\n" + //
"⠀⠀⠀⠀⠈⢙⣿⠝⠀⢁⠔⡨⡺⡿⡕⢔⠀⡈⠐⠹⣟⠋⠀⠀⠀⠀⠀\n" + //
"⠀⠀⠀⠀⠀⢼⣟⢦⢶⢅⠜⢰⠃⠀⢹⡌⢢⣸⠦⠴⣿⡇⠀⠀⠀⠀⠀\n" + //
"⠀⠀⠀⠀⠀⠘⣿⣇⡬⡌⢀⡟⠀⠀⠀⢷⠀⣧⢧⣵⣿⠂⠀⠀⠀⠀⠀\n" + //
"⠀⠀⠀⠀⠀⠀⠈⢻⠛⠋⠉⠀⠀⠀⠀⠈⠉⠙⢻⡏⠀⠀⠀⠀⠀⠀⠀\n" + //
"⠀⠀⠀⠀⠀⠀⢰⡿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⠄⠀⠀⠀⠀⠀⠀";
System.out.println(text);

}else if(text.equalsIgnoreCase("/red")){
text = "\n⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀\n" + //
"⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣼⢻⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀\n" + //
"⠀⠀⠀⠀⠀⠀⠀⠀⢤⣄⣀⣀⣀⣰⡇⠈⣧⣀⣀⣀⣀⣀⠀⠀⠀⠀⠀⠀⠀⠀\n" + //
"⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠻⢦⡀⠀⠀⠀⠀⢀⣠⠾⠋⠀⠀⠀⠀⠀⠀⠀⠀⠀\n" + //
"⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣹⠃⠀⡀⠀⢿⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀\n" + //
"⠀⠀⠀⠀⠀⣠⠔⠂⠀⠀⠀⢠⣏⣴⠞⠛⢦⣜⣧⠀⠀⠀⠀⠢⣄⡀⠀⠀⠀⠀\n" + //
"⠀⠀⢠⣖⡿⡋⠀⠀⠀⠀⠀⠾⠋⠀⠀⠀⠀⠉⠻⡄⠀⠀⠀⠀⢝⢿⣱⣄⠀⠀\n" + //
"⠀⡜⣿⣨⡾⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠲⣤⡀⠀⠀⠀⠀⠀⠈⢳⣇⣿⢡⠀\n" + //
"⢰⣇⣟⣵⠃⠀⠀⠀⠀⠀⠀⢀⣴⣦⡤⠀⠀⠈⠻⣷⡀⠀⠀⠀⠀⠈⣯⡻⢸⡆\n" + //
"⡆⣿⡾⡅⠀⠀⠀⠀⠀⢀⣴⣿⣿⣏⠀⠀⠀⠀⠀⠹⣿⡆⠀⠀⠀⠀⢨⢻⣾⢱\n" + //
"⣷⡘⣱⠇⠀⠀⠀⠀⠀⠀⠹⠋⠈⠻⣷⣄⠀⠀⠀⠀⣿⣿⠀⠀⠀⠀⠘⣧⢋⣾\n" + //
"⡼⣷⡿⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠻⣷⣄⠀⢀⣿⣿⠀⠀⠀⠀⢰⢻⣾⢇\n" + //
"⢳⣌⠇⣿⠀⠀⠀⠀⠀⠀⣴⢶⣤⣀⡀⠀⠈⢻⣷⣾⣿⠏⠀⠀⠀⠀⣿⠸⣡⡞\n" + //
"⠀⡿⢷⣿⡸⣄⠀⢀⣴⡾⠉⠀⠈⠛⠿⢿⣿⣿⡿⠿⣷⣄⠀⠀⢠⡇⣿⡾⢛⠀\n" + //
"⠀⠘⢦⣝⡣⢿⡦⡈⠋⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⠋⢀⣴⡿⣘⣭⡶⠃⠀\n" + //
"⠀⠀⠀⠹⣛⠿⠷⡹⣦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣴⢟⠾⠟⣛⠝⠀⠀⠀\n" + //
"⠀⠀⠀⠀⠈⠛⡿⠿⠶⢛⣫⣤⡶⣒⡶⠶⣖⠶⣶⣍⣛⠚⠿⣟⠛⠁⠀⠀⠀⠀\n" + //
"⠀⠀⠀⠀⠀⠀⠈⠙⠛⠛⠋⢡⠞⠁⠀⠀⠈⠻⣮⠙⠛⠛⠋⠁⠀⠀⠀⠀⠀⠀";
System.out.println(text);
}

message msg = new message(uname, text, ucolor);
message msg = new message(uname, text, ucolor);
msg.writemessage(conn);


}

} catch (SQLException e) {
Expand Down
4 changes: 2 additions & 2 deletions src/DBConnection.java
Original file line number Diff line number Diff line change
Expand Up @@ -4,9 +4,9 @@

public class DBConnection {

private static final String DB_URL = "jdbc:mysql://localhost:3306/Chatapp";
private static final String DB_URL = "jdbc:mysql://0.0.0.0:3306/chat";
private static final String USER = "root";
private static final String PASS = "devdat";
private static final String PASS = "1234";

public static Connection getConnection() throws SQLException {

Expand Down
3 changes: 3 additions & 0 deletions src/extra.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
public class extra {

}
35 changes: 30 additions & 5 deletions src/message.java
Original file line number Diff line number Diff line change
@@ -1,38 +1,63 @@
import java.sql.*;
import java.time.LocalDateTime;
import java.time.format.DateTimeFormatter;
import java.util.*;
import java.sql.*;

public class message {
private String username;
private String messageText;
private String colorCode;
private LocalDateTime timestamp;

public message(String username, String message, String colorCode) {
public message(String username,String message, String colorCode) {
this.username = username;

this.messageText = message;
this.colorCode = colorCode;

}

public message(String username, String messageText, String colorCode, LocalDateTime timestamp) {
this.username = username;

this.messageText = messageText;
this.colorCode = colorCode;
this.timestamp = (timestamp != null) ? timestamp : LocalDateTime.now();
}

public void fetchmessage() {
//public String buff(){

// }

public String format(String currentColor, String currentUser) {
final String BOLD = "\u001B[1m";
final String RESET = "\u001B[0m";
String time = timestamp.format(DateTimeFormatter.ofPattern("HH:mm:ss"));

if(colorCode.equals(currentColor) && username.equals(currentUser)) {
return null;
}

return "[" + time + "] " + BOLD + colorCode + username + RESET + " : " + messageText;
}


public void fetchmessage(String CurrentColor,String CurrentUser) {

final String BOLD = "\u001B[1m";
final String RESET = "\u001B[0m";
System.out.print("\r"); // move cursor to line start
System.out.print("\033[K"); // clear line
String time = timestamp.format(DateTimeFormatter.ofPattern("HH:mm:ss"));


if(! ( (colorCode.equals(CurrentColor)) && (username.equals(CurrentUser)) )){
String output = "[" + time + "] " + BOLD + colorCode + username + RESET + " : " + messageText;

System.out.println(output);
}
System.out.print("[" + time + "] " + BOLD + CurrentColor +"YOU"+ RESET + " : ");
// buff();
System.out.flush();
System.out.print("> ");

}

Expand Down
36 changes: 36 additions & 0 deletions test/test.java
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
else if (text.equalsIgnoreCase("/list")){
System.out.println("/red /butterfly");
}
else if (text.equalsIgnoreCase("/butterfly")) {
text = "\n⣠⣤⣤⡤⠤⢤⣤⣀⡀⠀⠐⠒⡄⠀⡠⠒⠀⠀⢀⣀⣤⠤⠤⣤⣤⣤⡄\n" + //
"⠈⠻⣿⡤⠤⡏⠀⠉⠙⠲⣄⠀⢰⢠⠃⢀⡤⠞⠋⠉⠈⢹⠤⢼⣿⠏⠀\n" + //
"⠀⠀⠘⣿⡅⠓⢒⡤⠤⠀⡈⠱⣄⣼⡴⠋⡀⠀⠤⢤⡒⠓⢬⣿⠃⠀⠀\n" + //
"⠀⠀⠀⠹⣿⣯⣐⢷⣀⣀⢤⡥⢾⣿⠷⢥⠤⣀⣀⣞⣢⣽⡿⠃⠀⠀⠀\n" + //
"⠀⠀⠀⠀⠈⢙⣿⠝⠀⢁⠔⡨⡺⡿⡕⢔⠀⡈⠐⠹⣟⠋⠀⠀⠀⠀⠀\n" + //
"⠀⠀⠀⠀⠀⢼⣟⢦⢶⢅⠜⢰⠃⠀⢹⡌⢢⣸⠦⠴⣿⡇⠀⠀⠀⠀⠀\n" + //
"⠀⠀⠀⠀⠀⠘⣿⣇⡬⡌⢀⡟⠀⠀⠀⢷⠀⣧⢧⣵⣿⠂⠀⠀⠀⠀⠀\n" + //
"⠀⠀⠀⠀⠀⠀⠈⢻⠛⠋⠉⠀⠀⠀⠀⠈⠉⠙⢻⡏⠀⠀⠀⠀⠀⠀⠀\n" + //
"⠀⠀⠀⠀⠀⠀⢰⡿⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣿⠄⠀⠀⠀⠀⠀⠀";
System.out.println(text);

}else if(text.equalsIgnoreCase("/red")){
text = "\n⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣆⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀\n" + //
"⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣼⢻⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀\n" + //
"⠀⠀⠀⠀⠀⠀⠀⠀⢤⣄⣀⣀⣀⣰⡇⠈⣧⣀⣀⣀⣀⣀⠀⠀⠀⠀⠀⠀⠀⠀\n" + //
"⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠻⢦⡀⠀⠀⠀⠀⢀⣠⠾⠋⠀⠀⠀⠀⠀⠀⠀⠀⠀\n" + //
"⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⣹⠃⠀⡀⠀⢿⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀\n" + //
"⠀⠀⠀⠀⠀⣠⠔⠂⠀⠀⠀⢠⣏⣴⠞⠛⢦⣜⣧⠀⠀⠀⠀⠢⣄⡀⠀⠀⠀⠀\n" + //
"⠀⠀⢠⣖⡿⡋⠀⠀⠀⠀⠀⠾⠋⠀⠀⠀⠀⠉⠻⡄⠀⠀⠀⠀⢝⢿⣱⣄⠀⠀\n" + //
"⠀⡜⣿⣨⡾⠁⠀⠀⠀⠀⠀⠀⠀⠀⠀⠉⠲⣤⡀⠀⠀⠀⠀⠀⠈⢳⣇⣿⢡⠀\n" + //
"⢰⣇⣟⣵⠃⠀⠀⠀⠀⠀⠀⢀⣴⣦⡤⠀⠀⠈⠻⣷⡀⠀⠀⠀⠀⠈⣯⡻⢸⡆\n" + //
"⡆⣿⡾⡅⠀⠀⠀⠀⠀⢀⣴⣿⣿⣏⠀⠀⠀⠀⠀⠹⣿⡆⠀⠀⠀⠀⢨⢻⣾⢱\n" + //
"⣷⡘⣱⠇⠀⠀⠀⠀⠀⠀⠹⠋⠈⠻⣷⣄⠀⠀⠀⠀⣿⣿⠀⠀⠀⠀⠘⣧⢋⣾\n" + //
"⡼⣷⡿⣀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠈⠻⣷⣄⠀⢀⣿⣿⠀⠀⠀⠀⢰⢻⣾⢇\n" + //
"⢳⣌⠇⣿⠀⠀⠀⠀⠀⠀⣴⢶⣤⣀⡀⠀⠈⢻⣷⣾⣿⠏⠀⠀⠀⠀⣿⠸⣡⡞\n" + //
"⠀⡿⢷⣿⡸⣄⠀⢀⣴⡾⠉⠀⠈⠛⠿⢿⣿⣿⡿⠿⣷⣄⠀⠀⢠⡇⣿⡾⢛⠀\n" + //
"⠀⠘⢦⣝⡣⢿⡦⡈⠋⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠙⠋⢀⣴⡿⣘⣭⡶⠃⠀\n" + //
"⠀⠀⠀⠹⣛⠿⠷⡹⣦⡀⠀⠀⠀⠀⠀⠀⠀⠀⠀⠀⢀⣴⢟⠾⠟⣛⠝⠀⠀⠀\n" + //
"⠀⠀⠀⠀⠈⠛⡿⠿⠶⢛⣫⣤⡶⣒⡶⠶⣖⠶⣶⣍⣛⠚⠿⣟⠛⠁⠀⠀⠀⠀\n" + //
"⠀⠀⠀⠀⠀⠀⠈⠙⠛⠛⠋⢡⠞⠁⠀⠀⠈⠻⣮⠙⠛⠛⠋⠁⠀⠀⠀⠀⠀⠀";
System.out.println(text);
}