Skip to content
This repository has been archived by the owner on Apr 22, 2021. It is now read-only.

Commit

Permalink
Feature: in text UI, show downloads in a single-line (#250)
Browse files Browse the repository at this point in the history
* Feature: show downloads in a single-line in text UIs
  • Loading branch information
luguina authored Jun 14, 2020
1 parent 2aa98b2 commit 324f773
Show file tree
Hide file tree
Showing 6 changed files with 82 additions and 10 deletions.
4 changes: 2 additions & 2 deletions src/com/sheepit/client/Client.java
Original file line number Diff line number Diff line change
Expand Up @@ -703,14 +703,14 @@ protected int downloadExecutable(Job ajob) throws FermeExceptionNoSpaceLeftOnDev

private int downloadFile(Job ajob, String local_path, String md5_server, String url, String download_type) throws FermeExceptionNoSpaceLeftOnDevice {
File local_path_file = new File(local_path);
String update_ui = "Downloading " + download_type + " %s %%";
String update_ui = "Downloading " + download_type;

if (local_path_file.exists() == true) {
this.gui.status("Reusing cached " + download_type);
return 0;
}

this.gui.status("Downloading " + download_type);
this.gui.status(String.format("Downloading %s", download_type), 0, 0);

// must download the archive
int ret = this.server.HTTPGetFile(url, local_path, this.gui, update_ui);
Expand Down
2 changes: 2 additions & 0 deletions src/com/sheepit/client/Gui.java
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,8 @@ public interface Gui {

public void status(String msg_, boolean overwriteSuspendedMsg);

public void status(String msg_, int progress, long size);

public void updateTrayIcon(Integer percentage_);

public void setRenderingProjectName(String name_);
Expand Down
4 changes: 2 additions & 2 deletions src/com/sheepit/client/Server.java
Original file line number Diff line number Diff line change
Expand Up @@ -439,7 +439,7 @@ public int HTTPGetFile(String url_, String destination_, Gui gui_, String status
written += len;

if ((written - lastUpd) > 1000000) { // only update the gui every 1MB
gui_.status(String.format(status_, (int) (100.0 * written / size)));
gui_.status(status_, (int) (100.0 * written / size), written);
lastUpd = written;
}
}
Expand All @@ -448,7 +448,7 @@ public int HTTPGetFile(String url_, String destination_, Gui gui_, String status
output.close();
is.close();

gui_.status(String.format(status_, 100));
gui_.status(status_, 100, size);

long end = new Date().getTime();
this.log.debug(String.format("File downloaded at %.1f kB/s, written %d B", ((float) (size / 1000)) / ((float) (end - start) / 1000), written));
Expand Down
6 changes: 6 additions & 0 deletions src/com/sheepit/client/standalone/GuiSwing.java
Original file line number Diff line number Diff line change
Expand Up @@ -191,6 +191,12 @@ else if (client.getConfiguration().getTheme().equals("dark")) {
}
}

@Override public void status(String msg, int progress, long size) {
if (activityWorking != null) {
this.activityWorking.setStatus(String.format("%s %d%%", msg, progress));
}
}

@Override public void setRenderingProjectName(String name_) {
if (activityWorking != null) {
this.activityWorking.setRenderingProjectName(name_);
Expand Down
30 changes: 30 additions & 0 deletions src/com/sheepit/client/standalone/GuiText.java
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,7 @@

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;

public class GuiText implements Gui {
Expand Down Expand Up @@ -110,6 +111,11 @@ else if (client.isRunning() && client.isSuspended() == false) {
}
}

@Override public void status(String msg, int progress, long size) {
System.out.print("\r");
System.out.print(String.format("%s %s", this.df.format(new Date()), showProgress(msg, progress, size)));
}

@Override public void error(String err_) {
System.out.println(String.format("ERROR: %s %s", this.df.format(new Date()), err_));
log.error("Error " + err_);
Expand Down Expand Up @@ -160,4 +166,28 @@ else if (client.isRunning() && client.isSuspended() == false) {
@Override public void successfulAuthenticationEvent(String publickey) {

}

private String showProgress(String message, int progress, long size) {
StringBuilder progressBar = new StringBuilder(140);
progressBar
.append(message)
.append(" ")
.append(String.join("", Collections.nCopies(progress == 0 ? 2 : 2 - (int) (Math.log10(progress)), " ")))
.append(String.format("%d%% [", progress))
.append(String.join("", Collections.nCopies((int)(progress/5), "=")))
.append('>')
.append(String.join("", Collections.nCopies(20 - (int)(progress / 5), " ")))
.append(']');

if (size > 0) {
progressBar.append(String.format(" %dMB", (size / 1024 / 1024)));
}

// Once the process has completed, show the output in a new line
if (progress == 100) {
progressBar.append("\n");
}

return progressBar.toString();
}
}
46 changes: 40 additions & 6 deletions src/com/sheepit/client/standalone/GuiTextOneLine.java
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Collections;
import java.util.Date;

public class GuiTextOneLine implements Gui {
Expand Down Expand Up @@ -122,12 +123,17 @@ else if (client.isRunning() && client.isSuspended() == false) {
}
}

@Override public void status(String msg, int progress, long size) {
status = showProgress(msg, progress, size);
updateLine();
}

@Override public void setRenderingProjectName(String name_) {
if (name_ == null || name_.isEmpty()) {
project = "";
}
else {
project = "Project \"" + name_ + "\" |";
project = name_ + " |";
}
updateLine();
}
Expand All @@ -154,7 +160,7 @@ else if (client.isRunning() && client.isSuspended() == false) {
}

@Override public void setRemainingTime(String time_) {
status = "(remaining " + time_ + ")";
status = "Rendering (remaining " + time_ + ")";
updateLine();
}

Expand Down Expand Up @@ -184,14 +190,42 @@ private void updateLine() {

System.out.print("\r");

line = String.format("%s Frames: %d Points: %s | Queued uploads: %d%s | %s %s %s", df.format(new Date()), rendered,
creditsEarned != null ? creditsEarned : "unknown", this.uploadQueueSize,
(this.uploadQueueSize > 0 ? String.format(" (%.2fMB)", (this.uploadQueueVolume / 1024.0 / 1024.0)) : ""), project, computeMethod,
status + (exiting ? " (Exiting after all frames are uploaded)" : ""));
line = String.format("%s Frames: %d Points: %s | Upload Queue: %d%s | %%s %s %s", df.format(new Date()), rendered,
creditsEarned != null ? creditsEarned : "unknown", this.uploadQueueSize,
(this.uploadQueueSize > 0 ? String.format(" (%.2fMB)", (this.uploadQueueVolume / 1024.0 / 1024.0)) : ""), computeMethod,
status + (exiting ? " (Exiting after all frames are uploaded)" : ""));

if (line.length() + project.length() > 120) {
// If the line without the project name is already >120 characters (might happen if the user has thousands of frames and millions of points in the
// session + is exiting after all frames are uploaded) then set the line to 117c to avoid a negative number exception in substring function
int lineLength = (line.length() >= 120 ? 117 : line.length());
line = String.format(line, project.substring(0, 117 - lineLength) + "...");
}
else {
line = String.format(line, project);
}

System.out.print(line);
for (int i = line.length(); i <= charToRemove; i++) {
System.out.print(" ");
}
}

private String showProgress(String message, int progress, long size) {
StringBuilder progressBar = new StringBuilder(140);
progressBar
.append(message)
.append(String.join("", Collections.nCopies(progress == 0 ? 2 : 2 - (int) (Math.log10(progress)), " ")))
.append(String.format(" %d%%%% [", progress))
.append(String.join("", Collections.nCopies((int) (progress / 10), "=")))
.append('>')
.append(String.join("", Collections.nCopies(10 - (int) (progress / 10), " ")))
.append(']');

if (size > 0) {
progressBar.append(String.format(" %dMB", (size / 1024 / 1024)));
}

return progressBar.toString();
}
}

0 comments on commit 324f773

Please sign in to comment.