Skip to content

Commit

Permalink
Update Main.java added color in CLI text
Browse files Browse the repository at this point in the history
  • Loading branch information
J9414 authored Nov 6, 2023
1 parent 8cf4534 commit a6e490a
Showing 1 changed file with 34 additions and 29 deletions.
63 changes: 34 additions & 29 deletions src/main/java/CLI/Main.java
Original file line number Diff line number Diff line change
Expand Up @@ -42,14 +42,19 @@ public class Main {
private static String batchDownloadingFile;
private static final String MSG_FILE_EXISTS_NO_HISTORY = "\"%s\" exists in \"%s\" folder. It will be renamed to \"%s\".";
private static final String MSG_FILE_EXISTS_HAS_HISTORY = "You have previously downloaded \"%s\" and it exists in \"%s\" folder.\nDo you want to download it again? ";

public static final String RESET = "\u001B[0m";
public static final String BLUE = "\033[0;34m";
public static final String YELLOW = "\u001B[33m";
public static final String RED = "\u001B[31m";
public static final String GREEN = "\u001B[32m";

public static void main(String[] args) {
LOGGER.log(MessageType.INFO, CLI_APPLICATION_STARTED);
messageBroker = new MessageBroker(System.out);
Environment.setMessageBroker(messageBroker);
messageBroker.msgInitInfo("Initializing environment...");
messageBroker.msgInitInfo(GREEN+"Initializing environment..."+RESET);
Environment.initializeEnvironment();
messageBroker.msgInitInfo("Environment initialized successfully!");
messageBroker.msgInitInfo(GREEN+"Environment initialized successfully!"+RESET);
utility = new Utility();
jobHistory = AppSettings.GET.jobHistory();
printBanner();
Expand Down Expand Up @@ -77,7 +82,7 @@ public static void main(String[] args) {
if (isURL(args[i])) {
link = args[i];
} else {
messageBroker.msgInitError("Invalid argument(s) passed!");
messageBroker.msgInitError(RED+"Invalid argument(s) passed!"+RESET);
System.exit(1);
}
}
Expand All @@ -89,7 +94,7 @@ public static void main(String[] args) {
isUrlValid = Utility.isLinkValid(link);
} else {
isUrlValid = false;
messageBroker.msgLinkError("Link is invalid!");
messageBroker.msgLinkError(RED+"Link is invalid!"+RESET);
}
if (isUrlValid) {
isYoutubeURL = isYoutube(link);
Expand All @@ -108,7 +113,7 @@ public static void main(String[] args) {
link = link + "?utm_source=ig_embed";
}
}
messageBroker.msgFilenameInfo("Retrieving filename from link...");
messageBroker.msgFilenameInfo(GREEN+"Retrieving filename from link..."+RESET);
fileName = findFilenameInLink(link);
if (!fileName.isEmpty()) {
Job job = new Job(link, downloadsFolder, fileName, false);
Expand All @@ -133,7 +138,7 @@ public static void main(String[] args) {
batchDownloadingFile = SC.next();
SC.nextLine();
if (!(batchDownloadingFile.endsWith(".yml") || batchDownloadingFile.endsWith(".yaml"))) {
messageBroker.msgBatchError("The data file should be a YAML file!");
messageBroker.msgBatchError(RED+"The data file should be a YAML file!"+RESET);
} else {
batchDownloader();
break;
Expand All @@ -153,7 +158,7 @@ public static void main(String[] args) {
if (Utility.isURL(link)) {
Utility.isLinkValid(link);
} else {
messageBroker.msgLinkError("Link is invalid!");
messageBroker.msgLinkError(RED+"Link is invalid!"+RESET);
continue;
}
System.out.print("Download directory (\".\" for default or \"L\" for " + AppSettings.GET.lastDownloadFolder() + ") : ");
Expand All @@ -172,7 +177,7 @@ public static void main(String[] args) {
link = link + "?utm_source=ig_embed";
}
}
messageBroker.msgFilenameInfo("Retrieving filename from link...");
messageBroker.msgFilenameInfo(GREEN+"Retrieving filename from link..."+RESET);
fileName = findFilenameInLink(link);
if (!fileName.isEmpty()) {
Job job = new Job(link, downloadsFolder, fileName, false);
Expand Down Expand Up @@ -203,7 +208,7 @@ private static void printVersion() {
}

private static void handleSpotifyPlaylist() {
messageBroker.msgFilenameInfo("Retrieving the number of tracks in the playlist...");
messageBroker.msgFilenameInfo(GREEN+"Retrieving the number of tracks in the playlist..."+RESET);
LinkedList<String> linkMetadataList = Utility.getLinkMetadata(link);
String json = makePretty(linkMetadataList.get(0));
String playlistLengthRegex = "(\"list_length\": )(.+)";
Expand All @@ -212,9 +217,9 @@ private static void handleSpotifyPlaylist() {
int numberOfSongs = 0;
if (lengthMatcher.find()) {
numberOfSongs = Integer.parseInt(lengthMatcher.group(2));
messageBroker.msgFilenameInfo("Number of tracks in the playlist : " + numberOfSongs);
messageBroker.msgFilenameInfo(GREEN+"Number of tracks in the playlist : " + numberOfSongs+RESET);
} else {
messageBroker.msgFilenameError("Failed to retrieve the number of tracks in the playlist!");
messageBroker.msgFilenameError(GREEN+"Failed to retrieve the number of tracks in the playlist!"+RESET);
}
for (int i = 0; i < numberOfSongs; i++) {
messageBroker.msgStyleInfo(BANNER_BORDER);
Expand All @@ -224,20 +229,20 @@ private static void handleSpotifyPlaylist() {
if (linkMatcher.find(i)) {
link = linkMatcher.group(2);
} else {
messageBroker.msgLinkError("Failed to retrieve link from playlist!");
messageBroker.msgLinkError(RED+"Failed to retrieve link from playlist!"+RESET);
continue;
}
messageBroker.msgLinkInfo("[" + (i + 1) + "/" + numberOfSongs + "] " + "Processing link : " + link);
messageBroker.msgLinkInfo(GREEN+"[" + (i + 1) + "/" + numberOfSongs + "] " + "Processing link : " + link+RESET);
if (fileName != null) {
String filenameRegex = "(\"name\": \")(.+)(\",)";
Pattern filenamePattern = Pattern.compile(filenameRegex);
Matcher filenameMatcher = filenamePattern.matcher(json);
if (filenameMatcher.find(i)) {
fileName = cleanFilename(filenameMatcher.group(2)) + ".mp3";
messageBroker.msgFilenameInfo(FILENAME_DETECTED + "\"" + fileName + "\"");
messageBroker.msgFilenameInfo(GREEN+FILENAME_DETECTED + "\"" + fileName + "\""+RESET);
} else {
fileName = cleanFilename("Unknown_Filename_") + randomString(15) + ".mp3";
messageBroker.msgFilenameError(FILENAME_DETECTION_ERROR);
messageBroker.msgFilenameError(RED+FILENAME_DETECTION_ERROR+RESET);
}
}
Job job = new Job(link, downloadsFolder, fileName, false);
Expand All @@ -248,22 +253,22 @@ private static void handleSpotifyPlaylist() {
private static void batchDownloader() {
Yaml yamlParser = new Yaml();
try {
messageBroker.msgLogInfo("Trying to load YAML data file (" + batchDownloadingFile + ") ...");
messageBroker.msgLogInfo(GREEN+"Trying to load YAML data file (" + batchDownloadingFile + ") ..."+RESET);
InputStreamReader yamlDataFile = new InputStreamReader(new FileInputStream(batchDownloadingFile));
Map<String, List<String>> data = yamlParser.load(yamlDataFile);
messageBroker.msgLogInfo("YAML data file (" + batchDownloadingFile + ") loaded successfully");
messageBroker.msgLogInfo(GREEN+"YAML data file (" + batchDownloadingFile + ") loaded successfully"+RESET);
int numberOfLinks;
try {
numberOfLinks = data.get("links").size();
} catch (NullPointerException e) {
messageBroker.msgLinkInfo("No links specified. Exiting...");
messageBroker.msgLinkInfo(GREEN+"No links specified. Exiting..."+RESET);
return;
}
int numberOfFileNames;
if (data.containsKey("fileNames")) {
numberOfFileNames = data.get("fileNames").size();
} else {
messageBroker.msgFilenameInfo("No filename specified. Filename will be retrieved from the link.");
messageBroker.msgFilenameInfo(GREEN+"No filename specified. Filename will be retrieved from the link."+RESET);
numberOfFileNames = 0;
}
int numberOfDirectories;
Expand All @@ -277,7 +282,7 @@ private static void batchDownloader() {
} else if (data.containsKey("directories")) {
numberOfDirectories = data.get("directories").size();
} else {
messageBroker.msgDirInfo("No directory specified. Default downloads folder will be used.");
messageBroker.msgDirInfo(GREEN+"No directory specified. Default downloads folder will be used."+RESET);
numberOfDirectories = 0;
downloadsFolder = ".";
}
Expand All @@ -299,11 +304,11 @@ private static void batchDownloader() {
} else {
fileNameMessage = numberOfFileNames + " filenames";
}
messageBroker.msgBatchInfo("You have provided\n\t" + linkMessage + "\n\t" + directoryMessage + "\n\t" + fileNameMessage);
messageBroker.msgBatchInfo(GREEN+"You have provided\n\t" + linkMessage + "\n\t" + directoryMessage + "\n\t" + fileNameMessage+RESET);
for (int i = 0; i < numberOfLinks; i++) {
messageBroker.msgStyleInfo(BANNER_BORDER);
link = data.get("links").get(i);
messageBroker.msgLinkInfo("[" + (i + 1) + "/" + numberOfLinks + "] " + "Processing link : " + link);
messageBroker.msgLinkInfo(GREEN+"[" + (i + 1) + "/" + numberOfLinks + "] " + "Processing link : " + link+RESET);
isYoutubeURL = isYoutube(link);
isInstagramLink = isInstagram(link);
isSpotifyLink = isSpotify(link);
Expand Down Expand Up @@ -331,7 +336,7 @@ private static void batchDownloader() {
link = link + "?utm_source=ig_embed";
}
}
messageBroker.msgFilenameInfo("Retrieving filename from link...");
messageBroker.msgFilenameInfo(GREEN+"Retrieving filename from link..."+RESET);
fileName = findFilenameInLink(link);
if (!fileName.isEmpty()) {
renameFilenameIfRequired(false);
Expand All @@ -345,7 +350,7 @@ private static void batchDownloader() {
checkHistoryAddJobsAndDownload(job, false);
}
} catch (FileNotFoundException e) {
messageBroker.msgDownloadError("YAML Data file (" + batchDownloadingFile + ") not found ! " + e.getMessage());
messageBroker.msgDownloadError(RED+"YAML Data file (" + batchDownloadingFile + ") not found ! " + e.getMessage()+RESET);
}
}

Expand Down Expand Up @@ -384,14 +389,14 @@ private static String getProperDownloadsFolder(String downloadsFolder) {
}
}
if (new File(downloadsFolder).exists()) {
messageBroker.msgDirInfo("Download folder exists!");
messageBroker.msgDirInfo(GREEN+"Download folder exists!"+RESET);
} else {
messageBroker.msgDirError("Download folder does not exist!");
messageBroker.msgDirError(RED+"Download folder does not exist!"+RESET);
try {
Files.createDirectory(Path.of(downloadsFolder));
messageBroker.msgDirInfo("Download folder created successfully!");
messageBroker.msgDirInfo(GREEN+"Download folder created successfully!"+RESET);
} catch (IOException e) {
messageBroker.msgDirError("Failed to create download folder! " + e.getMessage());
messageBroker.msgDirError(RED+"Failed to create download folder! " + e.getMessage()+RESET);
}
}
AppSettings.SET.lastFolder(downloadsFolder);
Expand Down

1 comment on commit a6e490a

@J9414
Copy link
Contributor Author

@J9414 J9414 commented on a6e490a Nov 6, 2023

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

.

Please sign in to comment.