Skip to content

Commit

Permalink
✨ Display hash progress depending on terminal width
Browse files Browse the repository at this point in the history
  • Loading branch information
evrignaud committed May 19, 2016
1 parent 9611b16 commit 2319cec
Show file tree
Hide file tree
Showing 2 changed files with 25 additions and 1 deletion.
4 changes: 4 additions & 0 deletions fim
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,10 @@ fi

baseDir=`dirname "$(${readLinkCommand} -f "$0")"`

if hash tput 2>/dev/null; then
export TERMINAL_COLUMNS=$(tput cols)
fi

JAVA_OPTIONS="-Xmx4g -XX:MaxMetaspaceSize=4g"

JAR_FILE=`ls -1 "${baseDir}/target"/fim-*.jar | grep -v sources`
Expand Down
22 changes: 21 additions & 1 deletion src/main/java/org/fim/internal/hash/HashProgress.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
package org.fim.internal.hash;

import org.apache.commons.io.FileUtils;
import org.apache.commons.lang3.SystemUtils;
import org.apache.commons.lang3.tuple.Pair;
import org.fim.model.Constants;
import org.fim.model.Context;
Expand All @@ -44,9 +45,11 @@ public class HashProgress {
private final Context context;
private long summedFileLength;
private int fileCount;
private int hashProgressWidth;

public HashProgress(Context context) {
this.context = context;
this.hashProgressWidth = getHashProgressWidth();
}

public synchronized void outputInit() {
Expand All @@ -66,7 +69,7 @@ public synchronized void updateOutput(long fileSize) {
}
}

if (fileCount % (100 * PROGRESS_DISPLAY_FILE_COUNT) == 0) {
if (fileCount % (hashProgressWidth * PROGRESS_DISPLAY_FILE_COUNT) == 0) {
if (isProgressDisplayed()) {
Console.newLine();
}
Expand Down Expand Up @@ -120,4 +123,21 @@ public boolean isProgressDisplayed() {
public Context getContext() {
return context;
}

private int getHashProgressWidth() {
int width = 100;

if (!SystemUtils.IS_OS_WINDOWS) {
try {
String result = System.getenv("TERMINAL_COLUMNS");
int terminalColumns = Integer.parseInt(result);
if (terminalColumns > 0) {
width = (int) (terminalColumns * 0.9);
}
} catch (Exception e) {
// Never mind use the default value
}
}
return width;
}
}

0 comments on commit 2319cec

Please sign in to comment.