Last active
December 31, 2022 03:44
-
-
Save matheusmv/2f1fb395a76ff863aaa9fd8c4ea9bd0f to your computer and use it in GitHub Desktop.
dummy progress bar in java
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import java.io.IOException; | |
import java.time.Duration; | |
import java.util.Arrays; | |
import java.util.logging.Level; | |
import java.util.logging.Logger; | |
import java.util.stream.Collectors; | |
public class ConsoleLoadBar { | |
private static final Logger LOGGER = Logger.getLogger(ConsoleLoadBar.class.getSimpleName()); | |
public static void main(String[] args) throws InterruptedException { | |
LoadBar download = new LoadBar("download", 49); | |
LoadBar torrent = new LoadBar("torrent", 49); | |
for (int i = 0; i <= 49; i += 1) { | |
renderAllBars(i, download, torrent); | |
Thread.sleep(Duration.ofSeconds(1).toMillis()); | |
} | |
} | |
static void renderAllBars(int progress, LoadBar... bars) { | |
clearConsole(); | |
for (LoadBar bar : bars) { | |
System.out.print(bar.getBar(progress)); | |
} | |
} | |
static void clearConsole() { | |
String os = System.getProperty("os.name").toLowerCase(); | |
switch (os) { | |
case "darwin": | |
runCmd("clear"); | |
break; | |
case "linux": | |
runCmd("clear"); | |
break; | |
case "windows", "windows 10": | |
runCmd("cmd", "/c", "cls"); | |
break; | |
default: | |
LOGGER.log(Level.SEVERE, () -> "unsupported os " + os); | |
} | |
} | |
static void runCmd(String... command) { | |
try { | |
ProcessBuilder pb = new ProcessBuilder(command); | |
Process startProcess = pb.inheritIO().start(); | |
startProcess.waitFor(); | |
} catch (IOException | InterruptedException e) { | |
LOGGER.log(Level.SEVERE, e, e::getMessage); | |
} | |
} | |
static class LoadBar { | |
private static final String COMPLETED = "🗸"; | |
private final Character[] bar = new Character[100]; | |
private final RotateSymbols<Character> bars; | |
private final String processName; | |
private int currentProgress; | |
private int currentBytes; | |
private int maxBytes; | |
public LoadBar(String processName, int maxBytes) { | |
fillBar(bar, '-', 0, bar.length); | |
this.bars = getProgressSymbols(); | |
this.processName = processName; | |
this.currentProgress = 0; | |
this.currentBytes = 0; | |
this.maxBytes = maxBytes; | |
} | |
private RotateSymbols<Character> getProgressSymbols() { | |
return new RotateSymbols<>(new Character[] { '⋮', '⋰', '⋯', '⋱' }); | |
} | |
private void fillBar(Character[] bar, Character token, int start, int end) { | |
for (int i = start; i < end; i++) { | |
bar[i] = token; | |
} | |
} | |
private String convertBarToString() { | |
return Arrays.asList(bar).stream() | |
.map(String::valueOf) | |
.collect(Collectors.joining()); | |
} | |
private int getActualProgess(int bytesLoaded) { | |
int next = bytesLoaded - currentBytes; | |
int newProgress = ((currentBytes + next) * 100) / maxBytes; | |
boolean reachedMaximumCapacity = newProgress >= bar.length; | |
if (!reachedMaximumCapacity) { | |
return newProgress; | |
} | |
return bar.length; | |
} | |
private int updateProgress(int bytesLoaded) { | |
currentBytes += bytesLoaded - currentBytes; | |
int newProgress = getActualProgess(bytesLoaded); | |
fillBar(bar, '#', currentProgress, newProgress); | |
currentProgress = newProgress; | |
return currentProgress; | |
} | |
private String getNextSymbol() { | |
return currentBytes == maxBytes ? COMPLETED : bars.next().toString(); | |
} | |
public String getBar(int bytesLoaded) { | |
final String inProgress = "%s %s\t[%s](%d%c)\n"; | |
int progress = updateProgress(bytesLoaded); | |
String nextSymbol = getNextSymbol(); | |
String strBar = convertBarToString(); | |
return String.format(inProgress, nextSymbol, processName, strBar, progress, '%'); | |
} | |
} | |
static class RotateSymbols<T> { | |
private final T[] symbols; | |
private int nextPosition; | |
public RotateSymbols(T[] symbols) { | |
this.symbols = symbols; | |
this.nextPosition = 0; | |
} | |
public T next() { | |
T symbol = symbols[nextPosition]; | |
nextPosition = (nextPosition + 1) % symbols.length; | |
return symbol; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment