diff --git a/src/main/java/algorithms/InsertionSort.java b/src/main/java/algorithms/InsertionSort.java index cf236f8..f36b032 100644 --- a/src/main/java/algorithms/InsertionSort.java +++ b/src/main/java/algorithms/InsertionSort.java @@ -25,8 +25,8 @@ public void sort() { int n = array.size(); for (int i = 1; i < n; i++) { - int sleepDuration = 40 ; // Dynamic delay for shifts and comparisons - int highlightSortedElementDelay = 20; // Delay specifically for highlighting sorted elements + int sleepDuration = 20 ; // Dynamic delay for shifts and comparisons + int highlightSortedElementDelay = 40; // Delay specifically for highlighting sorted elements int key = array.get(i); @@ -68,7 +68,6 @@ public void sort() { private static void highlightElement(ArrayList array, int index, String color, Set sortedIndices, int sleepDuration) { final String RESET = "\033[0m"; final String GREEN = "\033[32m"; - final String SQUARE = "◼"; StringBuilder output = new StringBuilder(); int maxHeight = Utils.findMax(array); @@ -78,12 +77,10 @@ private static void highlightElement(ArrayList array, int index, String int element = array.get(col); if (element >= row) { if (col == index) { - output.append(color).append(SQUARE).append(RESET); // Highlight element in specified color - } //else if (sortedIndices.contains(col)) { - //output.append(GREEN).append(SQUARE).append(RESET); - // } + output.append(color).append(Utils.getCurrentSymbol()).append(RESET); // Highlight element in specified color + } else { - output.append(SQUARE); + output.append(Utils.getCurrentSymbol()); } } else { output.append(" "); diff --git a/src/main/java/ui/UI.java b/src/main/java/ui/UI.java index d720adc..5d5a537 100644 --- a/src/main/java/ui/UI.java +++ b/src/main/java/ui/UI.java @@ -14,6 +14,7 @@ public UI(Terminal terminal, LineReader reader) { UI.terminal = terminal; UI.reader = reader; array = new ArrayList<>(); + Utils.setSymbolBasedOnOS(); } public void start() { diff --git a/src/main/java/ui/Utils.java b/src/main/java/ui/Utils.java index f80e43c..b6a436a 100644 --- a/src/main/java/ui/Utils.java +++ b/src/main/java/ui/Utils.java @@ -1,6 +1,5 @@ package ui; - import java.util.ArrayList; import java.util.Random; import java.util.Set; @@ -9,6 +8,13 @@ import static ui.UI.terminal; public abstract class Utils { + final static String RESET = "\033[0m"; + final static String RED = "\033[31m"; // comparison element + final static String BLUE = "\033[34m"; // target element + final static String GREEN = "\033[32m"; // sorted elements + final static char linuxSymbol = '◼'; + final static char windowsSymbol = '#'; + private static char currentSymbol; public static int findMax(ArrayList array) { int max = 0; @@ -33,10 +39,6 @@ public static void swapHighlighted(ArrayList arr, int currentIndex, int public static void displayVerticalArray(ArrayList arrayList, int currentIndex, int targetIndex, Set sortedIndices) { - final String RESET = "\033[0m"; - final String RED = "\033[31m"; // comparison element - final String BLUE = "\033[34m"; // target element - final String GREEN = "\033[32m"; // sorted elements StringBuilder output = new StringBuilder(); int maxHeight = findMax(arrayList); @@ -48,13 +50,13 @@ public static void displayVerticalArray(ArrayList arrayList, int curren int element = arrayList.get(col); if (element >= row) { if (sortedIndices.contains(col)) { - output.append(GREEN).append("◼").append(RESET); + output.append(GREEN).append(currentSymbol).append(RESET); } else if (col == currentIndex) { - output.append(RED).append("◼").append(RESET); + output.append(RED).append(currentSymbol).append(RESET); } else if (col == targetIndex) { - output.append(BLUE).append("◼").append(RESET); + output.append(BLUE).append(currentSymbol).append(RESET); } else { - output.append("◼"); + output.append(currentSymbol); } } else { output.append(" "); @@ -62,7 +64,6 @@ public static void displayVerticalArray(ArrayList arrayList, int curren } output.append("\n"); } - //printInCenter(output.toString(), " "); System.out.print(output); /* @@ -87,7 +88,7 @@ public static void displayVerticalArray(ArrayList arrayList) { for (int row = maxHeight; row > 0; row--) { output.append("\033[2K"); // Clear the whole line for (int element : arrayList) { - if (element >= row) output.append("◼"); + if (element >= row) output.append(currentSymbol); else output.append(" "); } output.append("\n"); @@ -140,4 +141,16 @@ public static void clearScreen() { System.out.print("\033[H\033[2J"); System.out.flush(); } + + public static void setSymbolBasedOnOS() { + String os = System.getProperty("os.name"); // Test on macOS and add it later + if (os.equals("Linux")) { + currentSymbol = linuxSymbol; + } else { + currentSymbol = windowsSymbol; + } + } + public static char getCurrentSymbol(){ + return currentSymbol; + } } \ No newline at end of file