186 lines
5.7 KiB
C++
186 lines
5.7 KiB
C++
#include <iostream>
|
|
#include <ctime>
|
|
#include <cstdlib>
|
|
#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 = 10000000;
|
|
tm.tv_sec = 0;
|
|
sc::cursor_hider ch {&std::cerr};
|
|
for (int i = 0; i < 1000; ++i) {
|
|
t.progress(12, "progress bar", i, 1000);
|
|
nanosleep(&tm, nullptr);
|
|
}
|
|
std::cerr << sc::io::clear_line(t);
|
|
std::cerr << "progress bar done.";
|
|
std::cerr << std::endl;
|
|
}
|
|
|
|
void show_hue_bar(int top) {
|
|
int r = top;
|
|
int g = 0;
|
|
int b = 0;
|
|
// red -> yellow
|
|
while (g <= top) {
|
|
std::cerr << sc::io::rgbb(r, g++, b) << ' ';
|
|
}
|
|
g = top;
|
|
// yellow -> green
|
|
while (r >= 0) {
|
|
std::cerr << sc::io::rgbb(r--, g, b) << ' ';
|
|
}
|
|
r = 0;
|
|
// green -> cyan
|
|
while (b < top) {
|
|
std::cerr << sc::io::rgbb(r, g, b++) << ' ';
|
|
}
|
|
b = top;
|
|
// cyan -> blue
|
|
while (g >= 0) {
|
|
std::cerr << sc::io::rgbb(r, g--, b) << ' ';
|
|
}
|
|
g = 0;
|
|
// blue -> magenta
|
|
while (r <= top) {
|
|
std::cerr << sc::io::rgbb(r++, g, b) << ' ';
|
|
}
|
|
r = top;
|
|
// magenta -> red
|
|
while (b >= 0) {
|
|
std::cerr << sc::io::rgbb(r, g, b--) << ' ';
|
|
}
|
|
std::cerr << sc::io::reset << std::endl;
|
|
}
|
|
|
|
void show_hue_bars() {
|
|
for (int top = 1; top < 6; ++top) {
|
|
show_hue_bar(top);
|
|
}
|
|
}
|
|
|
|
void show_grayscale_bar() {
|
|
for (int i = 0; i < 23; ++i) {
|
|
std::cerr << sc::io::grayb(i) << ' ';
|
|
}
|
|
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::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::reset << std::endl;
|
|
std::cout << sc::io::bold;
|
|
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 << sc::io::bold;
|
|
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 << "slow-blink" << sc::io::reset << ' ';
|
|
std::cout << sc::io::blinkfast << "fast-blink" << sc::io::reset;
|
|
std::cout << std::endl;
|
|
}
|
|
|
|
void show_truecolor(const sc::term& t) {
|
|
// check for true color support
|
|
char* support = getenv("COLORTERM");
|
|
if (!support || support[0] == '\0') return;
|
|
|
|
const char* lhb = u8"\u2584"; // lower half block
|
|
int inc = 6.0 * t.cols() / 256.0;
|
|
if (inc == 0) inc = 1;
|
|
for (int s = 16; s < 224; s += 32) {
|
|
int r = s;
|
|
int g = 0;
|
|
int b = 0;
|
|
// red -> yellow
|
|
while (g <= s) {
|
|
std::cerr << sc::io::truecolorb(r, g, b) << sc::io::truecolorf(r, 16 + g, b) << lhb;
|
|
g += inc;
|
|
}
|
|
g = s;
|
|
// yellow -> green
|
|
while (r >= 0) {
|
|
std::cerr << sc::io::truecolorb(r, g, b) << sc::io::truecolorf(16 + r, g, b) << lhb;
|
|
r -= inc;
|
|
}
|
|
r = 0;
|
|
// green -> cyan
|
|
while (b < s) {
|
|
std::cerr << sc::io::truecolorb(r, g, b) << sc::io::truecolorf(r, g, 16 + b) << lhb;
|
|
b += inc;
|
|
}
|
|
b = s;
|
|
// cyan -> blue
|
|
while (g >= 0) {
|
|
std::cerr << sc::io::truecolorb(r, g, b) << sc::io::truecolorf(r, 16 + g, b) << lhb;
|
|
g -= inc;
|
|
}
|
|
g = 0;
|
|
// blue -> magenta
|
|
while (r <= s) {
|
|
std::cerr << sc::io::truecolorb(r, g, b) << sc::io::truecolorf(16 + r, g, b) << lhb;
|
|
r += inc;
|
|
}
|
|
r = s;
|
|
// magenta -> red
|
|
while (b >= 0) {
|
|
std::cerr << sc::io::truecolorb(r, g, b) << sc::io::truecolorf(r, g, 16 + b) << lhb;
|
|
b -= inc;
|
|
}
|
|
std::cerr << sc::io::reset << std::endl;
|
|
}
|
|
}
|
|
|
|
int main() {
|
|
try {
|
|
sc::term term {STDERR_FILENO};
|
|
show_rows_cols(term);
|
|
show_grayscale_bar();
|
|
show_hue_bars();
|
|
show_words();
|
|
show_truecolor(term);
|
|
show_progress(term);
|
|
} catch (const std::exception& ex) {
|
|
std::cerr << sc::io::reset << ex.what() << std::endl;
|
|
return EXIT_FAILURE;
|
|
}
|
|
return EXIT_SUCCESS;
|
|
}
|