mirror of
https://gitee.com/actframework/actframework.git
synced 2024-11-29 18:37:37 +08:00
me.tongfei.progressbar
runs on Java8 only #622
This commit is contained in:
parent
b71dbf6822
commit
f3c38338a3
@ -1,6 +1,7 @@
|
||||
# ActFramework Change Log
|
||||
|
||||
**1.8.8**
|
||||
* `me.tongfei.progressbar` runs on Java8 only #622
|
||||
* Session expiration time shall not be output using `Expires` header #623
|
||||
* Param binding: allow it to ignore missing namespace when there is only one param to bind #618
|
||||
* @On(async = true) not work #611
|
||||
|
43
LICENSE
43
LICENSE
@ -1,3 +1,5 @@
|
||||
ActFramework is subject to the following license
|
||||
|
||||
Apache License
|
||||
Version 2.0, January 2004
|
||||
http://www.apache.org/licenses/
|
||||
@ -200,3 +202,44 @@ Apache License
|
||||
See the License for the specific language governing permissions and
|
||||
limitations under the License.
|
||||
|
||||
jBCrypt is subject to the following license:
|
||||
|
||||
/*
|
||||
* Copyright (c) 2006 Damien Miller <djm@mindrot.org>
|
||||
*
|
||||
* Permission to use, copy, modify, and distribute this software for any
|
||||
* purpose with or without fee is hereby granted, provided that the above
|
||||
* copyright notice and this permission notice appear in all copies.
|
||||
*
|
||||
* THE SOFTWARE IS PROVIDED "AS IS" AND THE AUTHOR DISCLAIMS ALL WARRANTIES
|
||||
* WITH REGARD TO THIS SOFTWARE INCLUDING ALL IMPLIED WARRANTIES OF
|
||||
* MERCHANTABILITY AND FITNESS. IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR
|
||||
* ANY SPECIAL, DIRECT, INDIRECT, OR CONSEQUENTIAL DAMAGES OR ANY DAMAGES
|
||||
* WHATSOEVER RESULTING FROM LOSS OF USE, DATA OR PROFITS, WHETHER IN AN
|
||||
* ACTION OF CONTRACT, NEGLIGENCE OR OTHER TORTIOUS ACTION, ARISING OUT OF
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
me.tongfei.progressbar is under MIT license
|
||||
|
||||
The MIT License (MIT)
|
||||
|
||||
Copyright (c) 2015 Tongfei Chen
|
||||
|
||||
Permission is hereby granted, free of charge, to any person obtaining a copy
|
||||
of this software and associated documentation files (the "Software"), to deal
|
||||
in the Software without restriction, including without limitation the rights
|
||||
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
|
||||
copies of the Software, and to permit persons to whom the Software is
|
||||
furnished to do so, subject to the following conditions:
|
||||
|
||||
The above copyright notice and this permission notice shall be included in all
|
||||
copies or substantial portions of the Software.
|
||||
|
||||
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
|
||||
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
|
||||
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
|
||||
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
|
||||
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
|
||||
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
|
||||
SOFTWARE.
|
10
pom.xml
10
pom.xml
@ -319,11 +319,11 @@
|
||||
<version>${jpa.version}</version>
|
||||
</dependency>
|
||||
|
||||
<dependency>
|
||||
<groupId>me.tongfei</groupId>
|
||||
<artifactId>progressbar</artifactId>
|
||||
<version>${progressbar.version}</version>
|
||||
</dependency>
|
||||
<!--<dependency>-->
|
||||
<!--<groupId>me.tongfei</groupId>-->
|
||||
<!--<artifactId>progressbar</artifactId>-->
|
||||
<!--<version>${progressbar.version}</version>-->
|
||||
<!--</dependency>-->
|
||||
|
||||
</dependencies>
|
||||
|
||||
|
@ -17,9 +17,9 @@ package fc.cron;
|
||||
|
||||
/*-
|
||||
* #%L
|
||||
* ACT Framework
|
||||
* frode-carlsen/cron
|
||||
* %%
|
||||
* Copyright (C) 2014 - 2017 ActFramework
|
||||
* Copyright (C) 2014 - 2017 frode-carlsen
|
||||
* %%
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
@ -35,6 +35,8 @@ package fc.cron;
|
||||
* #L%
|
||||
*/
|
||||
|
||||
import static org.joda.time.DateTimeConstants.DAYS_PER_WEEK;
|
||||
|
||||
import org.joda.time.DateTime;
|
||||
import org.joda.time.LocalDate;
|
||||
import org.joda.time.MutableDateTime;
|
||||
@ -46,8 +48,6 @@ import java.util.Locale;
|
||||
import java.util.regex.Matcher;
|
||||
import java.util.regex.Pattern;
|
||||
|
||||
import static org.joda.time.DateTimeConstants.DAYS_PER_WEEK;
|
||||
|
||||
/**
|
||||
* Parser for unix-like cron expressions: Cron expressions allow specifying combinations of criteria for time
|
||||
* such as: "Each Monday-Friday at 08:00" or "Every last friday of the month at 01:30"
|
||||
|
195
src/main/java/me/tongfei/progressbar/ProgressBar.java
Normal file
195
src/main/java/me/tongfei/progressbar/ProgressBar.java
Normal file
@ -0,0 +1,195 @@
|
||||
package me.tongfei.progressbar;
|
||||
|
||||
/*-
|
||||
* #%L
|
||||
* TongFei ProgressBar
|
||||
* %%
|
||||
* Copyright (C) 2014 - 2018 Tongfei Chen
|
||||
* %%
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* #L%
|
||||
*/
|
||||
|
||||
import me.tongfei.progressbar.wrapped.ProgressBarWrappedIterable;
|
||||
import me.tongfei.progressbar.wrapped.ProgressBarWrappedIterator;
|
||||
import org.joda.time.LocalDateTime;
|
||||
|
||||
import java.io.PrintStream;
|
||||
import java.util.Iterator;
|
||||
|
||||
/**
|
||||
* A simple console-based progress bar.
|
||||
* @author Tongfei Chen
|
||||
*/
|
||||
public class ProgressBar {
|
||||
|
||||
private ProgressState progress;
|
||||
private ProgressThread target;
|
||||
private Thread thread;
|
||||
|
||||
/**
|
||||
* Creates a progress bar with the specific task name and initial maximum value.
|
||||
* @param task Task name
|
||||
* @param initialMax Initial maximum value
|
||||
*/
|
||||
public ProgressBar(String task, long initialMax) {
|
||||
this(task, initialMax, 1000, System.err, ProgressBarStyle.UNICODE_BLOCK);
|
||||
}
|
||||
|
||||
public ProgressBar(String task, long initialMax, ProgressBarStyle style) {
|
||||
this(task, initialMax, 1000, System.err, style);
|
||||
}
|
||||
|
||||
public ProgressBar(String task, long initialMax, int updateIntervalMillis) {
|
||||
this(task, initialMax, updateIntervalMillis, System.err, ProgressBarStyle.UNICODE_BLOCK);
|
||||
}
|
||||
|
||||
/**
|
||||
* Creates a progress bar with the specific task name, initial maximum value,
|
||||
* customized update interval (default 1000 ms), the PrintStream to be used, and output style.
|
||||
* @param task Task name
|
||||
* @param initialMax Initial maximum value
|
||||
* @param updateIntervalMillis Update interval (default value 1000 ms)
|
||||
* @param os Print stream (default value System.err)
|
||||
* @param style Output style (default value ProgresBarStyle.UNICODE_BLOCK)
|
||||
*/
|
||||
public ProgressBar(String task, long initialMax, int updateIntervalMillis, PrintStream os, ProgressBarStyle style) {
|
||||
this.progress = new ProgressState(task, initialMax);
|
||||
this.target = new ProgressThread(progress, style, updateIntervalMillis, os);
|
||||
this.thread = new Thread(target);
|
||||
}
|
||||
|
||||
/**
|
||||
* Starts this progress bar.
|
||||
*/
|
||||
public ProgressBar start() {
|
||||
progress.startTime = LocalDateTime.now();
|
||||
thread.start();
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Advances this progress bar by a specific amount.
|
||||
* @param n Step size
|
||||
*/
|
||||
public ProgressBar stepBy(long n) {
|
||||
progress.stepBy(n);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Advances this progress bar to the specific progress value.
|
||||
* @param n New progress value
|
||||
*/
|
||||
public ProgressBar stepTo(long n) {
|
||||
progress.stepTo(n);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Advances this progress bar by one step.
|
||||
*/
|
||||
public ProgressBar step() {
|
||||
progress.stepBy(1);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Gives a hint to the maximum value of the progress bar.
|
||||
* @param n Hint of the maximum value
|
||||
*/
|
||||
public ProgressBar maxHint(long n) {
|
||||
if (n < 0)
|
||||
progress.setAsIndefinite();
|
||||
else {
|
||||
progress.setAsDefinite();
|
||||
progress.maxHint(n);
|
||||
}
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Stops this progress bar.
|
||||
*/
|
||||
public ProgressBar stop() {
|
||||
target.kill();
|
||||
try {
|
||||
thread.join();
|
||||
target.consoleStream.print("\n");
|
||||
target.consoleStream.flush();
|
||||
}
|
||||
catch (InterruptedException ex) { }
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Sets the extra message at the end of the progress bar.
|
||||
* @param msg New message
|
||||
*/
|
||||
public ProgressBar setExtraMessage(String msg) {
|
||||
progress.setExtraMessage(msg);
|
||||
return this;
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the current progress.
|
||||
*/
|
||||
public long getCurrent() {
|
||||
return progress.getCurrent();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the maximum value of this progress bar.
|
||||
*/
|
||||
public long getMax() {
|
||||
return progress.getMax();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the name of this task.
|
||||
*/
|
||||
public String getTask() {
|
||||
return progress.getTask();
|
||||
}
|
||||
|
||||
/**
|
||||
* Returns the extra message at the end of the progress bar.
|
||||
*/
|
||||
public String getExtraMessage() {
|
||||
return progress.getExtraMessage();
|
||||
}
|
||||
|
||||
/**
|
||||
* Wraps an iterator so that when iterated, a progress bar is shown to track the traversal progress.
|
||||
* @param it Underlying iterator
|
||||
* @param task Task name
|
||||
*/
|
||||
public static <T> Iterator<T> wrap(Iterator<T> it, String task) {
|
||||
return new ProgressBarWrappedIterator<>(it, task, -1); // indefinite progress bar
|
||||
}
|
||||
|
||||
/**
|
||||
* Wraps an iterable so that when iterated, a progress bar is shown to track the traversal progress.
|
||||
* <p>
|
||||
* Sample usage: {@code
|
||||
* for (T x : ProgressBar.wrap(collection, "Traversal")) { ... }
|
||||
* }
|
||||
* </p>
|
||||
* @param ts Underlying iterable
|
||||
* @param task Task name
|
||||
*/
|
||||
public static <T> Iterable<T> wrap(Iterable<T> ts, String task) {
|
||||
return new ProgressBarWrappedIterable<>(ts, task);
|
||||
}
|
||||
|
||||
}
|
50
src/main/java/me/tongfei/progressbar/ProgressBarStyle.java
Normal file
50
src/main/java/me/tongfei/progressbar/ProgressBarStyle.java
Normal file
@ -0,0 +1,50 @@
|
||||
package me.tongfei.progressbar;
|
||||
|
||||
/*-
|
||||
* #%L
|
||||
* TongFei ProgressBar
|
||||
* %%
|
||||
* Copyright (C) 2014 - 2018 Tongfei Chen
|
||||
* %%
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* #L%
|
||||
*/
|
||||
|
||||
/**
|
||||
* Represents the display style of a progress bar.
|
||||
* @author Tongfei Chen
|
||||
* @since 0.5.1
|
||||
*/
|
||||
public enum ProgressBarStyle {
|
||||
|
||||
/** Use Unicode block characters to draw the progress bar. */
|
||||
UNICODE_BLOCK("│", "│", '█', ' ', " ▏▎▍▌▋▊▉"),
|
||||
|
||||
/** Use only ASCII characters to draw the progress bar. */
|
||||
ASCII("[", "]", '=', ' ', ">");
|
||||
|
||||
String leftBracket;
|
||||
String rightBracket;
|
||||
char block;
|
||||
char space;
|
||||
String fractionSymbols;
|
||||
|
||||
ProgressBarStyle(String leftBracket, String rightBracket, char block, char space, String fractionSymbols) {
|
||||
this.leftBracket = leftBracket;
|
||||
this.rightBracket = rightBracket;
|
||||
this.block = block;
|
||||
this.space = space;
|
||||
this.fractionSymbols = fractionSymbols;
|
||||
}
|
||||
|
||||
}
|
92
src/main/java/me/tongfei/progressbar/ProgressState.java
Normal file
92
src/main/java/me/tongfei/progressbar/ProgressState.java
Normal file
@ -0,0 +1,92 @@
|
||||
package me.tongfei.progressbar;
|
||||
|
||||
/*-
|
||||
* #%L
|
||||
* TongFei ProgressBar
|
||||
* %%
|
||||
* Copyright (C) 2014 - 2018 Tongfei Chen
|
||||
* %%
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* #L%
|
||||
*/
|
||||
|
||||
|
||||
import org.joda.time.LocalDateTime;
|
||||
|
||||
/**
|
||||
* Encapsulates the internal states of a progress bar.
|
||||
* @author Tongfei Chen
|
||||
* @since 0.5.0
|
||||
*/
|
||||
class ProgressState {
|
||||
|
||||
String task;
|
||||
|
||||
boolean indefinite = false;
|
||||
|
||||
long current = 0;
|
||||
long max = 0;
|
||||
|
||||
LocalDateTime startTime = null;
|
||||
|
||||
String extraMessage = "";
|
||||
|
||||
ProgressState(String task, long initialMax) {
|
||||
this.task = task;
|
||||
this.max = initialMax;
|
||||
if (initialMax < 0) indefinite = true;
|
||||
}
|
||||
|
||||
synchronized void setAsDefinite() {
|
||||
indefinite = false;
|
||||
}
|
||||
|
||||
synchronized void setAsIndefinite() {
|
||||
indefinite = true;
|
||||
}
|
||||
|
||||
synchronized void maxHint(long n) {
|
||||
max = n;
|
||||
}
|
||||
|
||||
synchronized void stepBy(long n) {
|
||||
current += n;
|
||||
if (current > max) max = current;
|
||||
}
|
||||
|
||||
synchronized void stepTo(long n) {
|
||||
current = n;
|
||||
if (current > max) max = current;
|
||||
}
|
||||
|
||||
synchronized void setExtraMessage(String msg) {
|
||||
extraMessage = msg;
|
||||
}
|
||||
|
||||
String getTask() {
|
||||
return task;
|
||||
}
|
||||
|
||||
synchronized String getExtraMessage() {
|
||||
return extraMessage;
|
||||
}
|
||||
|
||||
synchronized long getCurrent() {
|
||||
return current;
|
||||
}
|
||||
|
||||
synchronized long getMax() {
|
||||
return max;
|
||||
}
|
||||
|
||||
}
|
152
src/main/java/me/tongfei/progressbar/ProgressThread.java
Normal file
152
src/main/java/me/tongfei/progressbar/ProgressThread.java
Normal file
@ -0,0 +1,152 @@
|
||||
package me.tongfei.progressbar;
|
||||
|
||||
/*-
|
||||
* #%L
|
||||
* TongFei ProgressBar
|
||||
* %%
|
||||
* Copyright (C) 2014 - 2018 Tongfei Chen
|
||||
* %%
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* #L%
|
||||
*/
|
||||
|
||||
import jline.Terminal;
|
||||
import jline.TerminalFactory;
|
||||
import org.joda.time.DateTimeZone;
|
||||
import org.joda.time.Duration;
|
||||
import org.joda.time.LocalDateTime;
|
||||
|
||||
import java.io.PrintStream;
|
||||
|
||||
/**
|
||||
* @author Tongfei Chen
|
||||
* @since 0.5.0
|
||||
*/
|
||||
class ProgressThread implements Runnable {
|
||||
|
||||
volatile boolean killed;
|
||||
ProgressBarStyle style;
|
||||
ProgressState progress;
|
||||
long updateInterval;
|
||||
PrintStream consoleStream;
|
||||
Terminal terminal;
|
||||
int consoleWidth = 80;
|
||||
|
||||
static int consoleRightMargin = 2;
|
||||
|
||||
int length;
|
||||
|
||||
ProgressThread(ProgressState progress, ProgressBarStyle style, long updateInterval, PrintStream consoleStream) {
|
||||
this.progress = progress;
|
||||
this.style = style;
|
||||
this.updateInterval = updateInterval;
|
||||
this.consoleStream = consoleStream;
|
||||
this.killed = false;
|
||||
|
||||
this.terminal = TerminalFactory.get();
|
||||
|
||||
if (terminal.getWidth() >= 10) // Workaround for issue #23 under IntelliJ
|
||||
consoleWidth = terminal.getWidth();
|
||||
}
|
||||
|
||||
// between 0 and 1
|
||||
double progress() {
|
||||
if (progress.max <= 0) return 0.0;
|
||||
else return ((double)progress.current) / progress.max;
|
||||
}
|
||||
|
||||
// Number of full blocks
|
||||
int progressIntegralPart() {
|
||||
return (int)(progress() * length);
|
||||
}
|
||||
|
||||
int progressFractionalPart() {
|
||||
double p = progress() * length;
|
||||
double fraction = (p - Math.floor(p)) * style.fractionSymbols.length();
|
||||
return (int) Math.floor(fraction);
|
||||
}
|
||||
|
||||
String eta(Duration elapsed) {
|
||||
if (progress.max <= 0 || progress.indefinite) return "?";
|
||||
else if (progress.current == 0) return "?";
|
||||
else return Util.formatDuration(
|
||||
elapsed.dividedBy(progress.current)
|
||||
.multipliedBy(progress.max - progress.current));
|
||||
}
|
||||
|
||||
String percentage() {
|
||||
String res;
|
||||
if (progress.max <= 0 || progress.indefinite) res = "? %";
|
||||
else res = String.valueOf((int) Math.floor(100.0 * progress.current / progress.max)) + "%";
|
||||
return Util.repeat(' ', 4 - res.length()) + res;
|
||||
}
|
||||
|
||||
String ratio() {
|
||||
String m = progress.indefinite ? "?" : String.valueOf(progress.max);
|
||||
String c = String.valueOf(progress.current);
|
||||
return Util.repeat(' ', m.length() - c.length()) + c + "/" + m;
|
||||
}
|
||||
|
||||
void refresh() {
|
||||
consoleStream.print('\r');
|
||||
|
||||
LocalDateTime currTime = LocalDateTime.now();
|
||||
Duration elapsed = new Duration(progress.startTime.toDateTime(DateTimeZone.UTC), currTime.toDateTime(DateTimeZone.UTC));
|
||||
|
||||
String prefix = progress.task + " " + percentage() + " " + style.leftBracket;
|
||||
|
||||
int maxSuffixLength = Math.max(0, consoleWidth - consoleRightMargin - prefix.length() - 10);
|
||||
String suffix = style.rightBracket + " " + ratio() + " (" + Util.formatDuration(elapsed) + " / " + eta(elapsed) + ") " + progress.extraMessage;
|
||||
if (suffix.length() > maxSuffixLength) suffix = suffix.substring(0, maxSuffixLength);
|
||||
|
||||
length = consoleWidth - consoleRightMargin - prefix.length() - suffix.length();
|
||||
|
||||
StringBuilder sb = new StringBuilder();
|
||||
sb.append(prefix);
|
||||
|
||||
// case of indefinite progress bars
|
||||
if (progress.indefinite) {
|
||||
int pos = (int)(progress.current % length);
|
||||
sb.append(Util.repeat(style.space, pos));
|
||||
sb.append(style.block);
|
||||
sb.append(Util.repeat(style.space, length - pos - 1));
|
||||
}
|
||||
// case of definite progress bars
|
||||
else {
|
||||
sb.append(Util.repeat(style.block, progressIntegralPart()));
|
||||
if (progress.current < progress.max) {
|
||||
sb.append(style.fractionSymbols.charAt(progressFractionalPart()));
|
||||
sb.append(Util.repeat(style.space, length - progressIntegralPart() - 1));
|
||||
}
|
||||
}
|
||||
sb.append(suffix);
|
||||
String line = sb.toString();
|
||||
|
||||
consoleStream.print(line);
|
||||
}
|
||||
|
||||
void kill() {
|
||||
killed = true;
|
||||
}
|
||||
|
||||
public void run() {
|
||||
try {
|
||||
while (!killed) {
|
||||
refresh();
|
||||
Thread.sleep(updateInterval);
|
||||
}
|
||||
refresh();
|
||||
// do-while loop not right: must force to refresh after stopped
|
||||
} catch (InterruptedException ex) { }
|
||||
}
|
||||
}
|
44
src/main/java/me/tongfei/progressbar/Util.java
Normal file
44
src/main/java/me/tongfei/progressbar/Util.java
Normal file
@ -0,0 +1,44 @@
|
||||
package me.tongfei.progressbar;
|
||||
|
||||
/*-
|
||||
* #%L
|
||||
* TongFei ProgressBar
|
||||
* %%
|
||||
* Copyright (C) 2014 - 2018 Tongfei Chen
|
||||
* %%
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* #L%
|
||||
*/
|
||||
|
||||
|
||||
import org.joda.time.Duration;
|
||||
|
||||
/**
|
||||
* @author Tongfei Chen
|
||||
* @since 0.5.0
|
||||
*/
|
||||
public class Util {
|
||||
|
||||
static String repeat(char c, int n) {
|
||||
if (n <= 0) return "";
|
||||
char[] s = new char[n];
|
||||
for (int i = 0; i < n; i++) s[i] = c;
|
||||
return new String(s);
|
||||
}
|
||||
|
||||
static String formatDuration(Duration d) {
|
||||
long s = d.getStandardSeconds();
|
||||
return String.format("%d:%02d:%02d", s / 3600, (s % 3600) / 60, s % 60);
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,57 @@
|
||||
package me.tongfei.progressbar.wrapped;
|
||||
|
||||
/*-
|
||||
* #%L
|
||||
* TongFei ProgressBar
|
||||
* %%
|
||||
* Copyright (C) 2014 - 2018 Tongfei Chen
|
||||
* %%
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* #L%
|
||||
*/
|
||||
|
||||
import java.util.Collection;
|
||||
import java.util.Iterator;
|
||||
|
||||
/**
|
||||
* @author Tongfei Chen
|
||||
* @since 0.6.0
|
||||
*/
|
||||
public class ProgressBarWrappedIterable<T> implements Iterable<T> {
|
||||
|
||||
Iterable<T> underlying;
|
||||
String task;
|
||||
|
||||
public ProgressBarWrappedIterable(Iterable<T> underlying, String task) {
|
||||
this.underlying = underlying;
|
||||
this.task = task;
|
||||
}
|
||||
|
||||
@Override
|
||||
public Iterator<T> iterator() {
|
||||
return new ProgressBarWrappedIterator<>(
|
||||
underlying.iterator(),
|
||||
task,
|
||||
sizeOf(underlying)
|
||||
);
|
||||
}
|
||||
|
||||
private long sizeOf(Iterable<?> iterable) {
|
||||
// if size unknown, -1, hence indefinite progress bar
|
||||
if (iterable instanceof Collection) {
|
||||
return ((Collection) iterable).size();
|
||||
}
|
||||
return -1;
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,60 @@
|
||||
package me.tongfei.progressbar.wrapped;
|
||||
|
||||
/*-
|
||||
* #%L
|
||||
* TongFei ProgressBar
|
||||
* %%
|
||||
* Copyright (C) 2014 - 2018 Tongfei Chen
|
||||
* %%
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* #L%
|
||||
*/
|
||||
|
||||
import me.tongfei.progressbar.ProgressBar;
|
||||
import org.osgl.util.E;
|
||||
|
||||
import java.util.Iterator;
|
||||
|
||||
/**
|
||||
* @author Tongfei Chen
|
||||
* @since 0.6.0
|
||||
*/
|
||||
public class ProgressBarWrappedIterator<T> implements Iterator<T> {
|
||||
|
||||
Iterator<T> underlying;
|
||||
ProgressBar pb;
|
||||
|
||||
public ProgressBarWrappedIterator(Iterator<T> underlying, String task, long size) {
|
||||
this.underlying = underlying;
|
||||
pb = new ProgressBar(task, size).start();
|
||||
}
|
||||
|
||||
@Override
|
||||
public boolean hasNext() {
|
||||
boolean r = underlying.hasNext();
|
||||
if (!r) pb.stop();
|
||||
return r;
|
||||
}
|
||||
|
||||
@Override
|
||||
public T next() {
|
||||
T r = underlying.next();
|
||||
pb.step();
|
||||
return r;
|
||||
}
|
||||
|
||||
@Override
|
||||
public void remove() {
|
||||
throw E.unsupport();
|
||||
}
|
||||
}
|
@ -23,9 +23,9 @@ package org.mindrot.jbcrypt;
|
||||
|
||||
/*-
|
||||
* #%L
|
||||
* ACT Framework
|
||||
* jeremyh/BCrypt
|
||||
* %%
|
||||
* Copyright (C) 2014 - 2017 ActFramework
|
||||
* Copyright (C) 2006 Damien Miller <djm@mindrot.org>
|
||||
* %%
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
|
63
src/test/java/me/tongfei/progressbar/Issue13Test.java
Normal file
63
src/test/java/me/tongfei/progressbar/Issue13Test.java
Normal file
@ -0,0 +1,63 @@
|
||||
package me.tongfei.progressbar;
|
||||
|
||||
/*-
|
||||
* #%L
|
||||
* ACT Framework
|
||||
* %%
|
||||
* Copyright (C) 2014 - 2018 ActFramework
|
||||
* %%
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* #L%
|
||||
*/
|
||||
|
||||
import org.junit.Test;
|
||||
|
||||
/**
|
||||
* @author bwittwer
|
||||
*/
|
||||
public class Issue13Test {
|
||||
|
||||
private static final int NBR_ELEMENTS = 100;
|
||||
private static final int PROGRESSBAR_GRACE_PERIOD = 1000;
|
||||
|
||||
@Test
|
||||
public void testOk() {
|
||||
ProgressBar pb = new ProgressBar("Test", NBR_ELEMENTS);
|
||||
pb.start();
|
||||
|
||||
try {
|
||||
Thread.sleep(PROGRESSBAR_GRACE_PERIOD);
|
||||
} catch (InterruptedException e1) {
|
||||
e1.printStackTrace();
|
||||
}
|
||||
|
||||
for (int i = 0; i < 100; i++) {
|
||||
pb.step();
|
||||
}
|
||||
|
||||
pb.stop();
|
||||
}
|
||||
|
||||
@Test
|
||||
public void testKo() {
|
||||
ProgressBar pb = new ProgressBar("Test", NBR_ELEMENTS);
|
||||
pb.start();
|
||||
|
||||
for (int i = 0; i < 100; i++) {
|
||||
pb.step();
|
||||
}
|
||||
|
||||
pb.stop();
|
||||
}
|
||||
|
||||
}
|
54
src/test/java/me/tongfei/progressbar/ProgressBarTest.java
Normal file
54
src/test/java/me/tongfei/progressbar/ProgressBarTest.java
Normal file
@ -0,0 +1,54 @@
|
||||
package me.tongfei.progressbar;
|
||||
|
||||
/*-
|
||||
* #%L
|
||||
* ACT Framework
|
||||
* %%
|
||||
* Copyright (C) 2014 - 2018 ActFramework
|
||||
* %%
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* #L%
|
||||
*/
|
||||
|
||||
import java.util.ArrayList;
|
||||
|
||||
/**
|
||||
* @author Tongfei Chen
|
||||
*/
|
||||
public class ProgressBarTest {
|
||||
|
||||
static public void main(String[] args) throws Exception {
|
||||
ProgressBar pb = new ProgressBar("Test", 5, 50, System.out, ProgressBarStyle.UNICODE_BLOCK).start();
|
||||
|
||||
double x = 1.0;
|
||||
double y = x * x;
|
||||
|
||||
ArrayList<Integer> l = new ArrayList<Integer>();
|
||||
|
||||
System.out.println("\n\n\n\n\n");
|
||||
|
||||
for (int i = 0; i < 10000; i++) {
|
||||
int sum = 0;
|
||||
for (int j = 0; j < i * 2000; j++)
|
||||
sum += j;
|
||||
l.add(sum);
|
||||
|
||||
pb.step();
|
||||
if (pb.getCurrent() > 8000) pb.maxHint(10000);
|
||||
|
||||
}
|
||||
pb.stop();
|
||||
System.out.println("Hello");
|
||||
}
|
||||
|
||||
}
|
@ -0,0 +1,45 @@
|
||||
package me.tongfei.progressbar;
|
||||
|
||||
/*-
|
||||
* #%L
|
||||
* ACT Framework
|
||||
* %%
|
||||
* Copyright (C) 2014 - 2018 ActFramework
|
||||
* %%
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
* #L%
|
||||
*/
|
||||
|
||||
import org.osgl.util.C;
|
||||
|
||||
import java.util.List;
|
||||
|
||||
/**
|
||||
* @author Tongfei Chen
|
||||
*/
|
||||
public class WrappedIterableTest {
|
||||
|
||||
static public void main(String[] args) throws Exception {
|
||||
System.out.println(System.getenv("TERM"));
|
||||
|
||||
List<Integer> sizedColl = C.newList();
|
||||
for (int i = 0; i < 10000; ++i) {
|
||||
sizedColl.add(i);
|
||||
}
|
||||
|
||||
for (Integer x : ProgressBar.wrap(sizedColl, "Traverse")) {
|
||||
Thread.sleep(2);
|
||||
}
|
||||
}
|
||||
|
||||
}
|
Loading…
Reference in New Issue
Block a user