|
| 1 | +package dev.skidfuscator.obfuscator.util.progress; |
| 2 | + |
| 3 | +import me.tongfei.progressbar.ProgressBarRenderer; |
| 4 | +import me.tongfei.progressbar.ProgressBarStyle; |
| 5 | + |
| 6 | +import java.text.DecimalFormat; |
| 7 | +import java.time.Duration; |
| 8 | +import java.time.Instant; |
| 9 | +import java.time.temporal.ChronoUnit; |
| 10 | + |
| 11 | +import static dev.skidfuscator.obfuscator.util.progress.StringDisplayUtils.getStringDisplayLength; |
| 12 | +import static dev.skidfuscator.obfuscator.util.progress.StringDisplayUtils.trimDisplayLength; |
| 13 | + |
| 14 | +/** |
| 15 | + * Default progress bar renderer (see {@link ProgressBarRenderer}). |
| 16 | + * @author Tongfei Chen |
| 17 | + * @author Muhammet Sakarya |
| 18 | + * @since 0.8.0 |
| 19 | + */ |
| 20 | +public class SkidProgressBarRenderer { |
| 21 | + |
| 22 | + private ProgressBarStyle style; |
| 23 | + private String unitName; |
| 24 | + private long unitSize; |
| 25 | + private boolean isSpeedShown; |
| 26 | + private DecimalFormat speedFormat; |
| 27 | + private ChronoUnit speedUnit; |
| 28 | + private Spinner<String> values = new Spinner<>( |
| 29 | + "⠋", |
| 30 | + "⠙", |
| 31 | + "⠹", |
| 32 | + "⠸", |
| 33 | + "⠼", |
| 34 | + "⠴", |
| 35 | + "⠦", |
| 36 | + "⠧", |
| 37 | + "⠇", |
| 38 | + "⠏" |
| 39 | + ); |
| 40 | + |
| 41 | + protected SkidProgressBarRenderer( |
| 42 | + ProgressBarStyle style, |
| 43 | + String unitName, |
| 44 | + long unitSize, |
| 45 | + boolean isSpeedShown, |
| 46 | + DecimalFormat speedFormat, |
| 47 | + ChronoUnit speedUnit |
| 48 | + ) { |
| 49 | + this.style = style; |
| 50 | + this.unitName = unitName; |
| 51 | + this.unitSize = unitSize; |
| 52 | + this.isSpeedShown = isSpeedShown; |
| 53 | + this.speedFormat = isSpeedShown && speedFormat == null ? new DecimalFormat() : speedFormat; |
| 54 | + this.speedUnit = speedUnit; |
| 55 | + } |
| 56 | + |
| 57 | + // Number of full blocks |
| 58 | + protected int progressIntegralPart(SkidProgressState progress, int length) { |
| 59 | + return (int)(progress.getNormalizedProgress() * length); |
| 60 | + } |
| 61 | + |
| 62 | + protected int progressFractionalPart(SkidProgressState progress, int length) { |
| 63 | + double p = progress.getNormalizedProgress() * length; |
| 64 | + double fraction = (p - Math.floor(p)) ;//* style.fractionSymbols.length(); |
| 65 | + return (int) Math.floor(fraction); |
| 66 | + } |
| 67 | + |
| 68 | + protected String eta(SkidProgressState progress, Duration elapsed) { |
| 69 | + if (progress.max <= 0 || progress.indefinite) return "?"; |
| 70 | + else if (progress.current - progress.start == 0) return "?"; |
| 71 | + else return Util.formatDuration( |
| 72 | + elapsed.dividedBy(progress.current - progress.start).multipliedBy(progress.max - progress.current) |
| 73 | + ); |
| 74 | + } |
| 75 | + |
| 76 | + protected String percentage(SkidProgressState progress) { |
| 77 | + String res; |
| 78 | + if (progress.max <= 0 || progress.indefinite) res = "? %"; |
| 79 | + else res = String.valueOf((int) Math.floor(100.0 * progress.current / progress.max)) + "%"; |
| 80 | + return Util.repeat(' ', 4 - res.length()) + res; |
| 81 | + } |
| 82 | + |
| 83 | + protected String ratio(SkidProgressState progress) { |
| 84 | + String m = progress.indefinite ? "?" : String.valueOf(progress.max / unitSize); |
| 85 | + String c = String.valueOf(progress.current / unitSize); |
| 86 | + return Util.repeat(' ', m.length() - c.length()) + c + "/" + m + unitName; |
| 87 | + } |
| 88 | + |
| 89 | + protected String speed(SkidProgressState progress, Duration elapsed) { |
| 90 | + String suffix = "/s"; |
| 91 | + double elapsedSeconds = elapsed.getSeconds(); |
| 92 | + double elapsedInUnit = elapsedSeconds; |
| 93 | + if (null != speedUnit) |
| 94 | + switch (speedUnit) { |
| 95 | + case MINUTES: |
| 96 | + suffix = "/min"; |
| 97 | + elapsedInUnit /= 60; |
| 98 | + break; |
| 99 | + case HOURS: |
| 100 | + suffix = "/h"; |
| 101 | + elapsedInUnit /= (60 * 60); |
| 102 | + break; |
| 103 | + case DAYS: |
| 104 | + suffix = "/d"; |
| 105 | + elapsedInUnit /= (60 * 60 * 24); |
| 106 | + break; |
| 107 | + } |
| 108 | + |
| 109 | + if (elapsedSeconds == 0) |
| 110 | + return "?" + unitName + suffix; |
| 111 | + double speed = (double) (progress.current - progress.start) / elapsedInUnit; |
| 112 | + double speedWithUnit = speed / unitSize; |
| 113 | + return speedFormat.format(speedWithUnit) + unitName + suffix; |
| 114 | + } |
| 115 | + |
| 116 | + public String render(SkidProgressState progress, int maxLength) { |
| 117 | + if (maxLength <= 0) { |
| 118 | + return ""; |
| 119 | + } |
| 120 | + /* |
| 121 | +
|
| 122 | + Instant currTime = Instant.now(); |
| 123 | + Duration elapsed = Duration.between(progress.startInstant, currTime); |
| 124 | +
|
| 125 | + String prefix = progress.taskName + " (" + percentage(progress) + ") " + style.leftBracket; |
| 126 | + int prefixLength = getStringDisplayLength(prefix); |
| 127 | +
|
| 128 | + if (prefixLength > maxLength) { |
| 129 | + prefix = trimDisplayLength(prefix, maxLength - 1); |
| 130 | + prefixLength = maxLength - 1; |
| 131 | + } |
| 132 | +
|
| 133 | + // length of progress should be at least 1 |
| 134 | + int maxSuffixLength = Math.max(maxLength - prefixLength - 1, 0); |
| 135 | +
|
| 136 | + String speedString = isSpeedShown ? speed(progress, elapsed) : ""; |
| 137 | + String suffix = style.rightBracket + " " + ratio(progress) + " (" |
| 138 | + + Util.formatDuration(elapsed) + " / " + eta(progress, elapsed) + ") " |
| 139 | + + speedString + progress.extraMessage; |
| 140 | + int suffixLength = getStringDisplayLength(suffix); |
| 141 | + // trim excessive suffix |
| 142 | + if (suffixLength > maxSuffixLength) { |
| 143 | + suffix = trimDisplayLength(suffix, maxSuffixLength); |
| 144 | + suffixLength = maxSuffixLength; |
| 145 | + } |
| 146 | +
|
| 147 | + int length = maxLength - prefixLength - suffixLength; |
| 148 | +
|
| 149 | + StringBuilder sb = new StringBuilder(maxLength); |
| 150 | + sb.append(prefix); |
| 151 | +
|
| 152 | + // case of indefinite progress bars |
| 153 | + if (progress.indefinite) { |
| 154 | + int pos = (int)(progress.current % length); |
| 155 | + sb.append(Util.repeat(' ', pos)); |
| 156 | + sb.append(values.next()); |
| 157 | + sb.append(Util.repeat(' ', length - pos - 1)); |
| 158 | + } |
| 159 | + // case of definite progress bars |
| 160 | + else { |
| 161 | + sb.append(Util.repeat(style.block, progressIntegralPart(progress, length))); |
| 162 | + if (progress.current < progress.max) { |
| 163 | + sb.append(style.fractionSymbols.charAt(progressFractionalPart(progress, length))); |
| 164 | + sb.append(Util.repeat(style.space, length - progressIntegralPart(progress, length) - 1)); |
| 165 | + } |
| 166 | + } |
| 167 | +
|
| 168 | + sb.append(suffix);*/ |
| 169 | + return null; //sb.toString(); |
| 170 | + } |
| 171 | + |
| 172 | +} |
0 commit comments