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

@@ -154,6 +154,11 @@
<artifactId>commons-lang3</artifactId> <artifactId>commons-lang3</artifactId>
<version>3.12.0</version> <version>3.12.0</version>
</dependency> </dependency>
<dependency>
<groupId>me.tongfei</groupId>
<artifactId>progressbar</artifactId>
<version>0.9.3</version>
</dependency>
<!-- endregion --> <!-- endregion -->
<!-- region unit-tests --> <!-- region unit-tests -->
<dependency> <dependency>

View File

@@ -8,6 +8,9 @@ import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.FileInfoDto;
import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.ResultStatistic; import at.pcgamingfreaks.mkvaudiosubtitlechanger.model.ResultStatistic;
import lombok.SneakyThrows; import lombok.SneakyThrows;
import lombok.extern.log4j.Log4j2; 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.File;
import java.io.IOException; import java.io.IOException;
@@ -33,31 +36,43 @@ public class AttributeUpdaterKernel {
public void execute() { public void execute() {
statistic.startTimer(); statistic.startTimer();
try (ProgressBar progressBar = pbBuilder().build()) {
List<File> files = collector.loadFiles(Config.getInstance().getLibraryPath()); List<File> files = collector.loadFiles(Config.getInstance().getLibraryPath());
files.forEach(file -> executor.submit(() -> process(file))); progressBar.maxHint(files.size());
files.forEach(file -> executor.submit(() -> process(file, progressBar)));
executor.shutdown(); executor.shutdown();
executor.awaitTermination(1, TimeUnit.DAYS); executor.awaitTermination(1, TimeUnit.DAYS);
}
statistic.stopTimer(); statistic.stopTimer();
System.out.println(statistic); System.out.println(statistic);
} }
private void process(File file) { private void process(File file, ProgressBar progressBar) {
statistic.total();
List<FileAttribute> attributes = processor.loadAttributes(file); List<FileAttribute> attributes = processor.loadAttributes(file);
FileInfoDto fileInfo = processor.filterAttributes(attributes); FileInfoDto fileInfo = processor.filterAttributes(attributes);
if (fileInfo.isChangeNecessary()) { if (fileInfo.isChangeNecessary()) {
statistic.shouldChange(file, fileInfo); statistic.shouldChange();
if (!Config.getInstance().isSafeMode()) { if (!Config.getInstance().isSafeMode()) {
try { try {
processor.update(file, fileInfo); processor.update(file, fileInfo);
statistic.success(file, fileInfo); statistic.success();
} catch (IOException e) { } catch (IOException e) {
statistic.failure(file, fileInfo); statistic.failure();
log.warn("File couldn't be updated: {}", file.getAbsoluteFile()); log.warn("File couldn't be updated: {}", file.getAbsoluteFile());
} }
} }
} else { } 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 @Getter
public class ResultStatistic { public class ResultStatistic {
private static final String result = "Files should change: %s%n" + private static final String result = "Total files: %s%n" +
"Files successfully changed: %s%n" + "├─ Should change: %s%n" +
"Files failed changing: %s%n" + "├─ Successfully changed: %s%n" +
"Files already fitting config: %s%n" + "├─ Already fit config: %s%n" +
"└─ Failed: %s%n" +
"Runtime: %ss"; "Runtime: %ss";
private int filesTotal = 0;
private int filesShouldChange = 0; private int filesShouldChange = 0;
private int filesSuccessfullyChanged = 0; private int filesSuccessfullyChanged = 0;
private int filesFailed = 0; private int filesFailed = 0;
@@ -21,19 +23,23 @@ public class ResultStatistic {
private long startTime = 0; private long startTime = 0;
private long runtime = 0; private long runtime = 0;
public void shouldChange(File file, FileInfoDto fileInfo) { public synchronized void total() {
filesTotal++;
}
public synchronized void shouldChange() {
filesShouldChange++; filesShouldChange++;
} }
public void success(File file, FileInfoDto fileInfo) { public synchronized void success() {
filesSuccessfullyChanged++; filesSuccessfullyChanged++;
} }
public void failure(File file, FileInfoDto fileInfo) { public synchronized void failure() {
filesFailed++; filesFailed++;
} }
public void fits(File file, FileInfoDto fileInfo) { public synchronized void fits() {
filesAlreadyFit++; filesAlreadyFit++;
} }
@@ -47,7 +53,7 @@ public class ResultStatistic {
@Override @Override
public String toString() { public String toString() {
return String.format(result, filesShouldChange, filesSuccessfullyChanged, filesFailed, filesAlreadyFit, return String.format(result, filesTotal, filesShouldChange, filesSuccessfullyChanged, filesAlreadyFit,
runtime / 1000); filesFailed, runtime / 1000);
} }
} }