Add progress bar

This commit is contained in:
2022-04-02 18:13:46 +02:00
parent a5b0224d6c
commit 4e53d99c25
3 changed files with 45 additions and 19 deletions

View File

@@ -8,6 +8,9 @@ import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.FileInfoDto;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.ResultStatistic;
import lombok.SneakyThrows;
import lombok.extern.log4j.Log4j2;
import me.tongfei.progressbar.ProgressBar;
import me.tongfei.progressbar.ProgressBarBuilder;
import me.tongfei.progressbar.ProgressBarStyle;
import java.io.File;
import java.io.IOException;
@@ -33,31 +36,43 @@ public class AttributeUpdaterKernel {
public void execute() {
statistic.startTimer();
List<File> files = collector.loadFiles(Config.getInstance().getLibraryPath());
files.forEach(file -> executor.submit(() -> process(file)));
executor.shutdown();
executor.awaitTermination(1, TimeUnit.DAYS);
try (ProgressBar progressBar = pbBuilder().build()) {
List<File> files = collector.loadFiles(Config.getInstance().getLibraryPath());
progressBar.maxHint(files.size());
files.forEach(file -> executor.submit(() -> process(file, progressBar)));
executor.shutdown();
executor.awaitTermination(1, TimeUnit.DAYS);
}
statistic.stopTimer();
System.out.println(statistic);
}
private void process(File file) {
private void process(File file, ProgressBar progressBar) {
statistic.total();
List<FileAttribute> attributes = processor.loadAttributes(file);
FileInfoDto fileInfo = processor.filterAttributes(attributes);
if (fileInfo.isChangeNecessary()) {
statistic.shouldChange(file, fileInfo);
statistic.shouldChange();
if (!Config.getInstance().isSafeMode()) {
try {
processor.update(file, fileInfo);
statistic.success(file, fileInfo);
statistic.success();
} catch (IOException e) {
statistic.failure(file, fileInfo);
statistic.failure();
log.warn("File couldn't be updated: {}", file.getAbsoluteFile());
}
}
} else {
statistic.fits(file, fileInfo);
statistic.fits();
}
progressBar.step();
}
private static ProgressBarBuilder pbBuilder() {
return new ProgressBarBuilder()
.setStyle(ProgressBarStyle.ASCII)
.setUpdateIntervalMillis(250)
.setMaxRenderedLength(75);
}
}

View File

@@ -7,12 +7,14 @@ import java.io.File;
@Getter
public class ResultStatistic {
private static final String result = "Files should change: %s%n" +
"Files successfully changed: %s%n" +
"Files failed changing: %s%n" +
"Files already fitting config: %s%n" +
private static final String result = "Total files: %s%n" +
"├─ Should change: %s%n" +
"├─ Successfully changed: %s%n" +
"├─ Already fit config: %s%n" +
"└─ Failed: %s%n" +
"Runtime: %ss";
private int filesTotal = 0;
private int filesShouldChange = 0;
private int filesSuccessfullyChanged = 0;
private int filesFailed = 0;
@@ -21,19 +23,23 @@ public class ResultStatistic {
private long startTime = 0;
private long runtime = 0;
public void shouldChange(File file, FileInfoDto fileInfo) {
public synchronized void total() {
filesTotal++;
}
public synchronized void shouldChange() {
filesShouldChange++;
}
public void success(File file, FileInfoDto fileInfo) {
public synchronized void success() {
filesSuccessfullyChanged++;
}
public void failure(File file, FileInfoDto fileInfo) {
public synchronized void failure() {
filesFailed++;
}
public void fits(File file, FileInfoDto fileInfo) {
public synchronized void fits() {
filesAlreadyFit++;
}
@@ -47,7 +53,7 @@ public class ResultStatistic {
@Override
public String toString() {
return String.format(result, filesShouldChange, filesSuccessfullyChanged, filesFailed, filesAlreadyFit,
runtime / 1000);
return String.format(result, filesTotal, filesShouldChange, filesSuccessfullyChanged, filesAlreadyFit,
filesFailed, runtime / 1000);
}
}