abraunegg-onedrive/src/progress.d
abraunegg 4253318835 Squashed commit of the following:
commit 1eff2d7d67
Author: abraunegg <alex.braunegg@gmail.com>
Date:   Wed Oct 18 19:15:27 2023 +1100

    Update PR

    * Add  --source-directory 'path/as/source/' --destination-directory 'path/as/destination' functionality

commit ad3ddee0ec
Author: abraunegg <alex.braunegg@gmail.com>
Date:   Wed Oct 18 17:32:24 2023 +1100

    Update PR

    * Add --create-directory
    * Add --remove-directory

commit 7dfe6b65b7
Author: abraunegg <alex.braunegg@gmail.com>
Date:   Wed Oct 18 12:27:03 2023 +1100

    Update PR

    * Update PR

commit 75c071e56f
Author: abraunegg <alex.braunegg@gmail.com>
Date:   Wed Oct 18 10:12:05 2023 +1100

    Update PR

    * Update PR

commit 6db484cdad
Author: abraunegg <alex.braunegg@gmail.com>
Date:   Mon Oct 16 17:01:25 2023 +1100

    Update PR

    * Update PR

commit d893ea5460
Author: abraunegg <alex.braunegg@gmail.com>
Date:   Wed Oct 11 10:43:50 2023 +1100

    Update PR

    * Update PR

commit 82bd593bf4
Author: abraunegg <alex.braunegg@gmail.com>
Date:   Wed Oct 11 09:14:17 2023 +1100

    Update PR

    * Validate and document --auth-files operation

commit c551203f4c
Author: abraunegg <alex.braunegg@gmail.com>
Date:   Wed Oct 11 05:48:22 2023 +1100

    Update PR

    * Add --create-share-link

commit fbf63999ff
Author: abraunegg <alex.braunegg@gmail.com>
Date:   Tue Oct 10 18:39:21 2023 +1100

    Update PR

    * Update PR

commit 72a4680035
Author: abraunegg <alex.braunegg@gmail.com>
Date:   Tue Oct 10 17:43:15 2023 +1100

    Update PR

    * Add --get-file-link
    * Add --modified-by

commit 0d3fc3ebf2
Author: abraunegg <alex.braunegg@gmail.com>
Date:   Tue Oct 10 14:28:10 2023 +1100

    Add --display-sync-status

    * Add --display-sync-status

commit 1f183ca03e
Author: abraunegg <alex.braunegg@gmail.com>
Date:   Mon Oct 9 08:18:13 2023 +1100

    Update PR

    * Update PR with doc updates

commit b0628d7099
Author: abraunegg <alex.braunegg@gmail.com>
Date:   Sun Oct 8 10:52:52 2023 +1100

    Update PR

    * Update PR

commit 7e3df956ce
Author: abraunegg <alex.braunegg@gmail.com>
Date:   Sat Oct 7 05:31:26 2023 +1100

    Update PR

    * Update PR

commit c69f2abc4b
Author: abraunegg <alex.braunegg@gmail.com>
Date:   Sat Oct 7 05:28:28 2023 +1100

    Update PR

    * Update PR

commit ea1ca33374
Author: abraunegg <alex.braunegg@gmail.com>
Date:   Fri Oct 6 14:57:51 2023 +1100

    Update PR

    * Update PR

commit 1503f969df
Author: abraunegg <alex.braunegg@gmail.com>
Date:   Fri Oct 6 09:19:04 2023 +1100

    Update PR

    * Update PR

commit 5127464f2c
Author: abraunegg <alex.braunegg@gmail.com>
Date:   Fri Oct 6 06:48:20 2023 +1100

    Change when the integrity check is performed

    * Change when the integrity check is performed

commit c7cc45d95c
Author: abraunegg <alex.braunegg@gmail.com>
Date:   Thu Oct 5 19:40:05 2023 +1100

    Update maxInotifyWatches location

    * Update maxInotifyWatches location

commit c44ad963a6
Author: abraunegg <alex.braunegg@gmail.com>
Date:   Thu Oct 5 17:41:31 2023 +1100

    Update main.d

    * Fix --version segfault

commit 51f0ffcb1f
Author: abraunegg <alex.braunegg@gmail.com>
Date:   Thu Oct 5 17:24:30 2023 +1100

    Uplift to v2.5.0-alpha-2

    * Uplift to v2.5.0-alpha-2

commit cbe3e6ea84
Author: abraunegg <alex.braunegg@gmail.com>
Date:   Thu Oct 5 17:17:26 2023 +1100

    Clean up before onedrive-v2.5.0-alpha-2

    * Clean up before onedrive-v2.5.0-alpha-2
2023-10-19 05:31:50 +11:00

161 lines
3.5 KiB
D

// What is this module called?
module progress;
// What does this module require to function?
import std.stdio;
import std.range;
import std.format;
import std.datetime;
import core.sys.posix.unistd;
import core.sys.posix.sys.ioctl;
// What other modules that we have created do we need to import?
class Progress
{
private:
immutable static size_t default_width = 80;
size_t max_width = 40;
size_t width = default_width;
ulong start_time;
string caption = "Progress";
size_t iterations;
size_t counter;
size_t getTerminalWidth() {
size_t column = default_width;
version (CRuntime_Musl) {
} else version(Android) {
} else {
winsize ws;
if(ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws) != -1 && ws.ws_col > 0) {
column = ws.ws_col;
}
}
return column;
}
void clear() {
write("\r");
for(auto i = 0; i < width; i++) write(" ");
write("\r");
}
int calc_eta() {
immutable auto ratio = cast(double)counter / iterations;
auto current_time = Clock.currTime.toUnixTime();
auto duration = cast(int)(current_time - start_time);
int hours, minutes, seconds;
double elapsed = (current_time - start_time);
int eta_sec = cast(int)((elapsed / ratio) - elapsed);
// Return an ETA or Duration?
if (eta_sec != 0){
return eta_sec;
} else {
return duration;
}
}
string progressbarText(string header_text, string footer_text) {
immutable auto ratio = cast(double)counter / iterations;
string result = "";
double bar_length = width - header_text.length - footer_text.length;
if(bar_length > max_width && max_width > 0) {
bar_length = max_width;
}
size_t i = 0;
for(; i < ratio * bar_length; i++) result ~= "o";
for(; i < bar_length; i++) result ~= " ";
return header_text ~ result ~ footer_text;
}
void print() {
immutable auto ratio = cast(double)counter / iterations;
auto header = appender!string();
auto footer = appender!string();
header.formattedWrite("%s %3d%% |", caption, cast(int)(ratio * 100));
if(counter <= 0 || ratio == 0.0) {
footer.formattedWrite("| ETA --:--:--:");
} else {
int h, m, s;
dur!"seconds"(calc_eta())
.split!("hours", "minutes", "seconds")(h, m, s);
if (counter != iterations){
footer.formattedWrite("| ETA %02d:%02d:%02d ", h, m, s);
} else {
footer.formattedWrite("| DONE IN %02d:%02d:%02d ", h, m, s);
}
}
write(progressbarText(header.data, footer.data));
}
void update() {
width = getTerminalWidth();
clear();
print();
stdout.flush();
}
public:
this(size_t iterations) {
if(iterations <= 0) iterations = 1;
counter = -1;
this.iterations = iterations;
start_time = Clock.currTime.toUnixTime;
}
@property {
string title() { return caption; }
string title(string text) { return caption = text; }
}
@property {
size_t count() { return counter; }
size_t count(size_t val) {
if(val > iterations) val = iterations;
return counter = val;
}
}
@property {
size_t maxWidth() { return max_width; }
size_t maxWidth(size_t w) {
return max_width = w;
}
}
void reset() {
counter = -1;
start_time = Clock.currTime.toUnixTime;
}
void next() {
counter++;
if(counter > iterations) counter = iterations;
update();
}
}