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

131 lines
4.0 KiB
C++
Raw Normal View History

2021-12-11 23:45:15 +01:00
#include <iostream>
#include <ctime>
2021-12-12 14:24:30 +01:00
#include <cmath>
#include <libscterm.hpp>
2021-12-11 23:45:15 +01:00
2021-12-12 14:24:30 +01:00
void show_rows_cols(const sc::term& t) {
std::cout << t.rows() << " rows, " << t.cols() << " cols\n";
}
void show_progress(const sc::term& t) {
2021-12-11 23:45:15 +01:00
struct timespec tm;
2021-12-13 13:39:18 +01:00
tm.tv_nsec = 10000000;
2021-12-11 23:45:15 +01:00
tm.tv_sec = 0;
2021-12-12 00:08:54 +01:00
sc::cursor_hider ch {STDERR_FILENO};
2021-12-11 23:45:15 +01:00
for (int i = 0; i < 1000; ++i) {
2021-12-13 13:39:18 +01:00
t.progress(12, "progress bar", i, 1000);
2021-12-11 23:45:15 +01:00
nanosleep(&tm, nullptr);
}
2021-12-12 13:13:27 +01:00
std::cerr << std::endl;
2021-12-11 23:45:15 +01:00
}
2021-12-12 14:24:30 +01:00
2021-12-13 13:39:18 +01:00
void show_hue_bar(int top) {
2021-12-12 14:24:30 +01:00
int r = top;
int g = 0;
int b = 0;
// red -> yellow
while (g <= top) {
std::cerr << sc::io::rgbb(r, g++, b) << ' ';
2021-12-12 14:24:30 +01:00
}
g = top;
// yellow -> green
while (r >= 0) {
std::cerr << sc::io::rgbb(r--, g, b) << ' ';
2021-12-12 14:24:30 +01:00
}
r = 0;
// green -> cyan
while (b < 6) {
std::cerr << sc::io::rgbb(r, g, b++) << ' ';
2021-12-12 14:24:30 +01:00
}
b = top;
// cyan -> blue
while (g >= 0) {
std::cerr << sc::io::rgbb(r, g--, b) << ' ';
2021-12-12 14:24:30 +01:00
}
g = 0;
// blue -> magenta
while (r <= top) {
std::cerr << sc::io::rgbb(r++, g, b) << ' ';
2021-12-12 14:24:30 +01:00
}
r = top;
// magenta -> red
while (b >= 0) {
std::cerr << sc::io::rgbb(r, g, b--) << ' ';
2021-12-12 14:24:30 +01:00
}
2021-12-13 13:39:18 +01:00
std::cerr << sc::io::reset << std::endl;
2021-12-12 14:24:30 +01:00
}
2021-12-13 13:39:18 +01:00
void show_hue_bars() {
2021-12-12 14:24:30 +01:00
for (int top = 1; top < 6; ++top) {
2021-12-13 13:39:18 +01:00
show_hue_bar(top);
2021-12-12 14:24:30 +01:00
}
}
2021-12-13 13:39:18 +01:00
void show_grayscale_bar() {
2021-12-12 14:24:30 +01:00
for (int i = 0; i < 23; ++i) {
std::cerr << sc::io::grayb(i) << ' ';
2021-12-12 14:24:30 +01:00
}
2021-12-13 13:39:18 +01:00
std::cerr << sc::io::reset << std::endl;
}
void show_words() {
std::cout << sc::io::redf << " red ";
std::cout << sc::io::greenf << " green ";
std::cout << sc::io::bluef << " blue ";
std::cout << sc::io::cyanf << " cyan ";
std::cout << sc::io::magentaf << " magenta ";
std::cout << sc::io::yellowf << " yellow ";
std::cout << sc::io::rgbf(5, 2, 0) << " orange ";
std::cout << sc::io::bold << std::endl;
std::cout << sc::io::redf << " red ";
std::cout << sc::io::greenf << " green ";
std::cout << sc::io::bluef << " blue ";
std::cout << sc::io::cyanf << " cyan ";
std::cout << sc::io::magentaf << " magenta ";
std::cout << sc::io::yellowf << " yellow ";
std::cout << sc::io::rgbf(5, 2, 0) << " orange ";
std::cout << sc::io::reset << std::endl;
std::cout << sc::io::blackf;
std::cout << sc::io::redb << " red ";
std::cout << sc::io::greenb << " green ";
std::cout << sc::io::blueb << " blue ";
std::cout << sc::io::cyanb << " cyan ";
std::cout << sc::io::magentab << " magenta ";
std::cout << sc::io::yellowb << " yellow ";
std::cout << sc::io::rgbb(5, 2, 0) << " orange ";
std::cout << sc::io::bold << std::endl;
std::cout << sc::io::redb << " red ";
std::cout << sc::io::greenb << " green ";
std::cout << sc::io::blueb << " blue ";
std::cout << sc::io::cyanb << " cyan ";
std::cout << sc::io::magentab << " magenta ";
std::cout << sc::io::yellowb << " yellow ";
std::cout << sc::io::rgbb(5, 2, 0) << " orange ";
std::cout << sc::io::reset << std::endl;
std::cout << sc::io::bold << "bold " << sc::io::reset;
std::cout << sc::io::italic << "italic " << sc::io::reset;
std::cout << sc::io::underline << "underline" << sc::io::reset << ' ';
std::cout << sc::io::strikethru << "strike thru" << sc::io::reset << ' ';
std::cout << sc::io::overline << "overline" << sc::io::reset << ' ';
std::cout << sc::io::reverse << "reverse" << sc::io::reset << ' ';
// std::cout << sc::io::blinkslow << "blink slowly " << sc::io::reset;
// std::cout << sc::io::blinkfast << "blink fast " << sc::io::reset;
std::cout << std::endl;
2021-12-12 14:24:30 +01:00
}
int main() {
sc::term term {STDERR_FILENO};
try {
show_rows_cols(term);
2021-12-13 13:39:18 +01:00
show_grayscale_bar();
show_hue_bars();
show_words();
2021-12-12 14:24:30 +01:00
show_progress(term);
} catch (const std::exception& ex) {
term.reset();
std::cerr << ex.what() << std::endl;
return EXIT_FAILURE;
}
return EXIT_SUCCESS;
}