added progress bar
This commit is contained in:
parent
0037292a63
commit
4ade94e285
|
@ -5,6 +5,34 @@
|
||||||
#include <ostream>
|
#include <ostream>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <unistd.h>
|
#include <unistd.h>
|
||||||
|
#include <sys/ioctl.h>
|
||||||
|
|
||||||
|
const char CLEAR_LINE[] = "\x1b[2K";
|
||||||
|
|
||||||
|
static void display_progress(unsigned long at, unsigned long size) {
|
||||||
|
|
||||||
|
struct winsize w;
|
||||||
|
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
|
||||||
|
unsigned cols = w.ws_col - 8;
|
||||||
|
|
||||||
|
std::cout << "[";
|
||||||
|
|
||||||
|
unsigned long j = at * cols / size;
|
||||||
|
|
||||||
|
for(unsigned long i = 0; i < cols; i++) {
|
||||||
|
char ch;
|
||||||
|
if(i > j) {
|
||||||
|
ch = ' ';
|
||||||
|
} else if(i == j) {
|
||||||
|
ch = '>';
|
||||||
|
} else {
|
||||||
|
ch = '=';
|
||||||
|
}
|
||||||
|
std::cout << ch;
|
||||||
|
}
|
||||||
|
|
||||||
|
std::cout << "] " << (at * 100 / size) << "%\r" << std::flush;
|
||||||
|
}
|
||||||
|
|
||||||
int main(int argc, char** argv) {
|
int main(int argc, char** argv) {
|
||||||
std::string data;
|
std::string data;
|
||||||
|
@ -14,6 +42,8 @@ int main(int argc, char** argv) {
|
||||||
return 1;
|
return 1;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
std::cout << "loaded: " << data.length() << " bytes" << std::endl;
|
||||||
|
|
||||||
unsigned at = 0;
|
unsigned at = 0;
|
||||||
Device device(argv[1]);
|
Device device(argv[1]);
|
||||||
device.put(Device::Mode::e_serial);
|
device.put(Device::Mode::e_serial);
|
||||||
|
@ -22,12 +52,12 @@ int main(int argc, char** argv) {
|
||||||
Device::Mode mode = (Device::Mode)device.get();
|
Device::Mode mode = (Device::Mode)device.get();
|
||||||
switch(mode) {
|
switch(mode) {
|
||||||
default: {
|
default: {
|
||||||
std::cout << "device: unknown (" << mode << ")" << std::endl;
|
std::cout << CLEAR_LINE << "device: unknown (" << mode << ")" << std::endl;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case Device::Mode::e_ready: {
|
case Device::Mode::e_ready: {
|
||||||
device.put(Device::Mode::e_serial);
|
device.put(Device::Mode::e_serial);
|
||||||
std::cout << "device: ready" << std::endl;
|
std::cout << CLEAR_LINE << "device: ready" << std::endl;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case Device::Mode::e_jump: {
|
case Device::Mode::e_jump: {
|
||||||
|
@ -35,7 +65,6 @@ int main(int argc, char** argv) {
|
||||||
at = (at << 8) | (device.get() & 0xff);
|
at = (at << 8) | (device.get() & 0xff);
|
||||||
at = (at << 8) | (device.get() & 0xff);
|
at = (at << 8) | (device.get() & 0xff);
|
||||||
at = (at << 8) | (device.get() & 0xff);
|
at = (at << 8) | (device.get() & 0xff);
|
||||||
std::cout << "device: jump (" << at << ")" << std::endl;
|
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
case Device::Mode::e_read: {
|
case Device::Mode::e_read: {
|
||||||
|
@ -53,6 +82,8 @@ int main(int argc, char** argv) {
|
||||||
}
|
}
|
||||||
break;
|
break;
|
||||||
}}
|
}}
|
||||||
|
|
||||||
|
display_progress(at, data.length());
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
Loading…
Reference in New Issue