first commit
This commit is contained in:
57
src/utils.cpp
Normal file
57
src/utils.cpp
Normal file
@ -0,0 +1,57 @@
|
||||
#include "utils.hpp"
|
||||
#include <libscerror.hpp>
|
||||
#include <cmath>
|
||||
#include <iostream>
|
||||
#include <iomanip>
|
||||
#include <string>
|
||||
|
||||
using namespace sc;
|
||||
|
||||
term::term() {
|
||||
if (isatty(STDOUT_FILENO)) {
|
||||
throw_if_min1(ioctl(STDOUT_FILENO, TIOCGWINSZ, &ws));
|
||||
}
|
||||
}
|
||||
|
||||
int term::rows() const {
|
||||
return ws.ws_row;
|
||||
}
|
||||
|
||||
int term::cols() const {
|
||||
return ws.ws_col;
|
||||
}
|
||||
|
||||
void term::progress(double cur, double total) const {
|
||||
// use unicode to make nice bar
|
||||
const char* bar[8] = {
|
||||
u8"\u258F", u8"\u258E", u8"\u258D", u8"\u258C",
|
||||
u8"\u258B", u8"\u258A", u8"\u2589", u8"\u2588"
|
||||
};
|
||||
int barwidth = cols() - 5;
|
||||
int maxsteps = barwidth * 8;
|
||||
int steps = round(cur * maxsteps / total);
|
||||
int perc = round(100 * cur / total);
|
||||
std::cerr << '\r' << std::setw(3) << perc << "% ";
|
||||
for (int i = 0; i < steps / 8; ++i) {
|
||||
std::cerr << bar[7];
|
||||
}
|
||||
if (steps % 8) {
|
||||
std::cerr << bar[steps % 8 - 1];
|
||||
}
|
||||
}
|
||||
|
||||
void term::hide_cursor() const {
|
||||
std::cerr << "\x1b[?25l";
|
||||
}
|
||||
|
||||
void term::show_cursor() const {
|
||||
std::cerr << "\x1b[?25h";
|
||||
}
|
||||
|
||||
cursor_hider::cursor_hider() {
|
||||
t.hide_cursor();
|
||||
}
|
||||
|
||||
cursor_hider::~cursor_hider() {
|
||||
t.show_cursor();
|
||||
}
|
35
src/utils.hpp
Normal file
35
src/utils.hpp
Normal file
@ -0,0 +1,35 @@
|
||||
#ifndef UTILS_H_
|
||||
#define UTILS_H_
|
||||
|
||||
#include <unistd.h>
|
||||
#include <termios.h>
|
||||
#include <sys/ioctl.h>
|
||||
|
||||
namespace sc {
|
||||
|
||||
class term {
|
||||
struct winsize ws;
|
||||
|
||||
public:
|
||||
term();
|
||||
|
||||
int rows() const;
|
||||
int cols() const;
|
||||
|
||||
void progress(double cur, double total) const;
|
||||
|
||||
void hide_cursor() const;
|
||||
void show_cursor() const;
|
||||
};
|
||||
|
||||
class cursor_hider {
|
||||
term t;
|
||||
|
||||
public:
|
||||
cursor_hider();
|
||||
~cursor_hider();
|
||||
};
|
||||
|
||||
}
|
||||
|
||||
#endif // UTILS_H_
|
Reference in New Issue
Block a user