This repository has been archived on 2024-12-13. You can view files and clone it, but cannot push or open issues or pull requests.
progress/progress.cpp
2021-12-13 10:41:45 +01:00

94 lines
1.9 KiB
C++

#include <iostream>
#include <ctime>
#include <cmath>
#include <libscterm.hpp>
void show_rows_cols(const sc::term& t) {
std::cout << t.rows() << " rows, " << t.cols() << " cols\n";
}
void show_progress(const sc::term& t) {
struct timespec tm;
tm.tv_nsec = 20000000;
tm.tv_sec = 0;
sc::cursor_hider ch {STDERR_FILENO};
for (int i = 0; i < 1000; ++i) {
t.progress(8, "progress", i, 1000);
nanosleep(&tm, nullptr);
}
std::cerr << std::endl;
}
void show_hue_bar(const sc::term& t, int top) {
int r = top;
int g = 0;
int b = 0;
// red -> yellow
while (g <= top) {
t.rgbb(r, g++, b);
std::cerr << ' ';
}
g = top;
// yellow -> green
while (r >= 0) {
t.rgbb(r--, g, b);
std::cerr << ' ';
}
r = 0;
// green -> cyan
while (b < 6) {
t.rgbb(r, g, b++);
std::cerr << ' ';
}
b = top;
// cyan -> blue
while (g >= 0) {
t.rgbb(r, g--, b);
std::cerr << ' ';
}
g = 0;
// blue -> magenta
while (r <= top) {
t.rgbb(r++, g, b);
std::cerr << ' ';
}
r = top;
// magenta -> red
while (b >= 0) {
t.rgbb(r, g, b--);
std::cerr << ' ';
}
t.reset();
std::cerr << std::endl;
}
void show_hue_bars(const sc::term& t) {
for (int top = 1; top < 6; ++top) {
show_hue_bar(t, top);
}
}
void show_grayscale_bar(const sc::term& t) {
for (int i = 0; i < 23; ++i) {
t.grayb(i);
std::cerr << ' ';
}
t.reset();
std::cerr << std::endl;
}
int main() {
sc::term term {STDERR_FILENO};
try {
show_rows_cols(term);
show_grayscale_bar(term);
show_hue_bars(term);
show_progress(term);
} catch (const std::exception& ex) {
term.reset();
std::cerr << ex.what() << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}