Skip to content

Commit

Permalink
Update
Browse files Browse the repository at this point in the history
  • Loading branch information
sunkafei committed Jul 18, 2023
1 parent 3601feb commit 480c50d
Show file tree
Hide file tree
Showing 2 changed files with 31 additions and 13 deletions.
24 changes: 14 additions & 10 deletions src/progress_bar.h
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
#include <windows.h>
#undef max
#undef min
#else
#include <sys/ioctl.h>
#endif
Expand All @@ -27,20 +28,23 @@ class progress_bar {
this->width = size.ws_col;
#endif
this->length = std::max(width - int(hint.size()) - 8, 1);
this->length = std::min(this->length, 100);
this->temp = std::format("\r\033[1;36m{}: [{{:<{}}}]{{:3}}%\033[0m", hint, length);
update(0);
}
void update(double now) {
if (now - progress > 0.005) {
progress = now;
auto str = std::string(int(length * progress), '#');
auto args = std::make_format_args(str, int(progress * 100));
std::cerr << std::vformat(temp, args);
std::cerr.flush();
}
if (now > 0.9999) {
std::cerr << "\r" << std::string(width, ' ') << "\r";
std::cerr.flush();
if (length > 1) {
if (now - progress > 0.005) {
progress = now;
auto str = std::string(int(length * progress), '#');
auto args = std::make_format_args(str, int(progress * 100));
std::cerr << std::vformat(temp, args);
std::cerr.flush();
}
if (now > 0.9999) {
std::cerr << "\r" << std::string(width, ' ') << "\r";
std::cerr.flush();
}
}
}
};
Expand Down
20 changes: 17 additions & 3 deletions src/server.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -8,12 +8,25 @@
#include "database.h"
httplib::Server server;
void start_server() {
char buffer[1024];
std::string ip = "127.0.0.1";
#if defined(WIN32) || defined(_WIN32) || defined(__WIN32__)
FILE *output = _popen("curl ipconfig.io", "r");
FILE *stream = _popen("ipconfig", "r");
while (fgets(buffer, std::ssize(buffer), stream)) {
std::string line(buffer);
if (line.ends_with('\n')) {
line.pop_back();
}
if (line.find("IPv4") != std::string::npos && line.rfind(':') != std::string::npos) {
auto index = line.rfind(':') + 1;
while (index < line.size() && std::isspace(line[index])) {
index += 1;
}
ip = line.substr(index);
}
}
#else
FILE *output = popen("dig +short myip.opendns.com @resolver1.opendns.com", "r");
#endif
std::string ip;
for (;;) {
auto ch = fgetc(output);
if (isspace(ch) || ch == EOF) {
Expand All @@ -23,6 +36,7 @@ void start_server() {
}
if (ip.empty() || ip.find(':') != std::string::npos)
ip = "127.0.0.1";
#endif
print("Starting CoffeeDB ...");
init();
build();
Expand Down

0 comments on commit 480c50d

Please sign in to comment.