[haiku-commits] haiku: hrev51583 - src/bin/pkgman

  • From: pulkomandy@xxxxxxxxxxxxx
  • To: haiku-commits@xxxxxxxxxxxxx
  • Date: Tue, 21 Nov 2017 18:26:40 +0100 (CET)

hrev51583 adds 1 changeset to branch 'master'
old head: feabff3ebbe4943f5e2c0b2abb793c690a2c36e4
new head: 6663167b598e59102c9fc5a4a62612a09b80e243
overview: 
http://cgit.haiku-os.org/haiku/log/?qt=range&q=6663167b598e+%5Efeabff3ebbe4

----------------------------------------------------------------------------

6663167b598e: pkgman: improve progress bar
  
  - Do not update more than twice a second
  - Do not use tabs, so we can reliably compute the string width
  - Cleanup and tweak other parts of the code
  
  There is still a glitch when resizing Terminal, but it isn't as bad as
  before. Ideally, using the SIGWINCH signal would solve that, but it is
  sent to the app only when you release the mouse after a release
  operation, so in our case, it is already too late.

                             [ Adrien Destugues <pulkomandy@xxxxxxxxxxxxx> ]

----------------------------------------------------------------------------

Revision:    hrev51583
Commit:      6663167b598e59102c9fc5a4a62612a09b80e243
URL:         http://cgit.haiku-os.org/haiku/commit/?id=6663167b598e
Author:      Adrien Destugues <pulkomandy@xxxxxxxxxxxxx>
Date:        Tue Nov 21 17:24:44 2017 UTC

----------------------------------------------------------------------------

1 file changed, 56 insertions(+), 34 deletions(-)
src/bin/pkgman/PackageManager.cpp | 90 ++++++++++++++++++++++-------------

----------------------------------------------------------------------------

diff --git a/src/bin/pkgman/PackageManager.cpp 
b/src/bin/pkgman/PackageManager.cpp
index 506dfea..a93e301 100644
--- a/src/bin/pkgman/PackageManager.cpp
+++ b/src/bin/pkgman/PackageManager.cpp
@@ -15,6 +15,8 @@
 
 #include "PackageManager.h"
 
+#include <InterfaceDefs.h>
+
 #include <sys/ioctl.h>
 #include <unistd.h>
 
@@ -194,48 +196,68 @@ PackageManager::ProgressPackageDownloadActive(const char* 
packageName,
        if (!fInteractive)
                return;
 
-       // Erase the line and return to the start
-       printf("\r\33[2K\r");
+       // Do not update if nothing changed in the last 500ms
+       if (bytes <= fLastBytes || (system_time() - fLastRateCalcTime) < 500000)
+               return;
 
-       if (bytes >= fLastBytes && (system_time() - fLastRateCalcTime) >= 
1000000) {
-               const bigtime_t time = system_time();
-               fDownloadRate = (bytes - fLastBytes) / ((time - 
fLastRateCalcTime) / 1000000);
-               fLastRateCalcTime = time;
-               fLastBytes = bytes;
+       const bigtime_t time = system_time();
+       if (time != fLastRateCalcTime) {
+               fDownloadRate = (bytes - fLastBytes) * 1000000
+                       / (time - fLastRateCalcTime);
        }
+       fLastRateCalcTime = time;
+       fLastBytes = bytes;
 
        // Build the current file progress percentage and size string
-       BString str;
-       char byteBuffer[32];
-       char totalBuffer[32];
-       char rateBuffer[32];
-       str.SetToFormat("%3d%% %s\t  %s/%s\t  %s",
-               (int)(completionPercentage * 100),
-               packageName,
-               string_for_size(bytes, byteBuffer, sizeof(byteBuffer)),
-               string_for_size(totalBytes, totalBuffer, sizeof(totalBuffer)),
-               fDownloadRate == 0 ? "--.-" :
-                       string_for_rate(fDownloadRate, rateBuffer, 
sizeof(rateBuffer)));
-
-       int32 width = std::max(str.CountChars(), (int32)50);
+       BString leftStr;
+       BString rightStr;
+
+       int width = 70;
        struct winsize winSize;
-       if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &winSize) == 0
-                       && winSize.ws_col < width) {
-               width = winSize.ws_col - 2;
-               str.ReplaceAll("\t  ", "  ");
-               str.ReplaceFirst(".hpkg", "");
-               str.SetToFormat("%.*s\xE2\x80\xA6%s",
-                       (int)str.CountBytes(0, width / 2 - 1), str.String(),
-                       str.String() + str.CountBytes(0, str.CountChars() - 
width / 2 - 1));
+       if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &winSize) == 0)
+               width = std::min(winSize.ws_col - 2, 78);
+
+       if (width < 30) {
+               // Not much space for anything, just draw a percentage
+               leftStr.SetToFormat("%3d%%", (int)(completionPercentage * 100));
+       } else {
+               leftStr.SetToFormat("%3d%% %s", (int)(completionPercentage * 
100),
+                               packageName);
+
+               char byteBuffer[32];
+               char totalBuffer[32];
+               char rateBuffer[32];
+               rightStr.SetToFormat("%s/%s  %s ",
+                               string_for_size(bytes, byteBuffer, 
sizeof(byteBuffer)),
+                               string_for_size(totalBytes, totalBuffer, 
sizeof(totalBuffer)),
+                               fDownloadRate == 0 ? "--.-" :
+                               string_for_rate(fDownloadRate, rateBuffer, 
sizeof(rateBuffer)));
+
+               if (leftStr.CountChars() + rightStr.CountChars() >= width)
+               {
+                       // The full string does not fit! Try to make a shorter 
one.
+                       leftStr.ReplaceLast(".hpkg", "");
+                       leftStr.TruncateChars(width - rightStr.CountChars() - 
2);
+                       leftStr.Append(B_UTF8_ELLIPSIS " ");
+               }
+
+               int extraSpace = width - leftStr.CountChars() - 
rightStr.CountChars();
+
+               leftStr.Append(' ', extraSpace);
+               leftStr.Append(rightStr);
        }
-       str.Append(' ', width - str.CountChars());
 
-       // Set bg to green, fg to white, and print progress bar
-       const int progChars = str.CountBytes(0, (int)(width * 
completionPercentage));
-       printf("\x1B[42;37m%.*s", progChars, str.String());
-       // Reset colors and print rest of text
-       printf("\x1B[0m%s", str.String() + progChars);
+       const int progChars = leftStr.CountBytes(0,
+               (int)(width * completionPercentage));
+
+       // Set bg to green, fg to white, and print progress bar.
+       // Then reset colors and print rest of text
+       // And finally remove any stray chars at the end of the line
+       printf("\r\x1B[42;37m%.*s\x1B[0m%s\x1B[K", progChars, leftStr.String(),
+               leftStr.String() + progChars);
 
+       // Force terminal to update when the line is complete, to avoid 
flickering
+       // because of updates at random times
        fflush(stdout);
 }
 


Other related posts:

  • » [haiku-commits] haiku: hrev51583 - src/bin/pkgman - pulkomandy