Skip to content

Commit cfa01ad

Browse files
committed
convenience; readme
1 parent 82e9f28 commit cfa01ad

File tree

5 files changed

+66
-18
lines changed

5 files changed

+66
-18
lines changed

demos/main.cpp

Lines changed: 17 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -31,9 +31,9 @@ int main()
3131
enable_vt_mode();
3232
std::cout << oof::cursor_visibility(false) << oof::reset_formatting() << oof::clear_screen();
3333

34-
std::cout << oof::fg_color({ 255, 100, 100 }) << "This is red\n";
35-
std::cout << "Still the same - state was changed!\n";
36-
std::cout << oof::reset_formatting() << oof::hposition(10) << "All back to normal\n";
34+
//std::cout << oof::fg_color({ 255, 100, 100 }) << "This is red\n";
35+
//std::cout << "Still the same - state was changed!\n";
36+
//std::cout << oof::reset_formatting() << oof::hposition(10) << "All back to normal\n";
3737

3838
// oof::screen scr(10, 3, 0, 0, ' ');
3939
// for(uint64_t i=0; ; ++i){
@@ -60,6 +60,20 @@ int main()
6060
// fast_print(screen.get_string());
6161
// }
6262

63+
// 123 Example
64+
//for (int i = 0; i < 10; ++i) {
65+
// std::cout << oof::fg_color(oof::color{ 255 - i * 25 });
66+
// std::cout << oof::hposition(2 * i) << std::to_string(i) << "\n";
67+
//}
68+
69+
// Values example
70+
//constexpr double values[]{0.54, 0.88, 0.42, 0.21, 0.33, 0.68, 0.91};
71+
//for(int i=0; i<std::size(values); ++i){
72+
// std::cout << oof::underline(true) << std::format("value {}", i) << oof::reset_formatting() << ": ";
73+
// const oof::color color{ 255, static_cast<int>(255 - values[i] * 255), 0 };
74+
// std::cout << oof::fg_color(color) << std::format("{:.2f}\n", values[i]) << oof::reset_formatting();
75+
//}
76+
6377

6478
int demo_choice = 0;
6579
print_choice("Bars");

demos/radar_demo.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -14,9 +14,9 @@ namespace
1414
auto get_faded(const oof::color& col) -> oof::color {
1515
constexpr int fade_amount = 20;
1616
return oof::color{
17-
static_cast<uint8_t>(std::clamp(col.red - fade_amount, 0, 255)),
18-
static_cast<uint8_t>(std::clamp(col.green - fade_amount, 0, 255)),
19-
static_cast<uint8_t>(std::clamp(col.blue - fade_amount, 0, 255))
17+
std::clamp(col.red - fade_amount, 0, 255),
18+
std::clamp(col.green - fade_amount, 0, 255),
19+
std::clamp(col.blue - fade_amount, 0, 255)
2020
};
2121
};
2222

demos/tools.h

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -120,9 +120,9 @@ auto remove_from_vector(
120120
inline auto get_random_color(s9w::rng_state& rng) -> oof::color
121121
{
122122
return oof::color{
123-
static_cast<uint8_t>(rng.get_int(0, 255)),
124-
static_cast<uint8_t>(rng.get_int(0, 255)),
125-
static_cast<uint8_t>(rng.get_int(0, 255))
123+
rng.get_int(0, 255),
124+
rng.get_int(0, 255),
125+
rng.get_int(0, 255)
126126
};
127127
}
128128

oof.h

Lines changed: 23 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -11,8 +11,13 @@ namespace oof
1111
struct color {
1212
uint8_t red{}, green{}, blue{};
1313
constexpr color() = default;
14-
constexpr color(const uint8_t component);
15-
constexpr color(const uint8_t r, const uint8_t g, const uint8_t b);
14+
15+
template<std::integral component_type>
16+
constexpr color(component_type component);
17+
18+
template<std::integral component_type>
19+
constexpr color(component_type r, component_type g, component_type b);
20+
1621
friend constexpr auto operator==(const color&, const color&) -> bool = default;
1722
};
1823

@@ -318,16 +323,26 @@ namespace oof
318323

319324
} // namespace detail
320325

321-
constexpr color::color(const uint8_t component)
322-
: red{ component }, green{ component }, blue{ component }
326+
327+
template<std::integral component_type>
328+
constexpr color::color(component_type r, component_type g, component_type b)
329+
: red{ static_cast<uint8_t>(r) }
330+
, green{ static_cast<uint8_t>(g) }
331+
, blue{ static_cast<uint8_t>(b) }
323332
{
324-
333+
if (r < component_type{ 0 } || r > component_type{255})
334+
::oof::detail::error("red component not in [0, 255]");
335+
if (g < component_type{ 0 } || g > component_type{ 255 })
336+
::oof::detail::error("green component not in [0, 255]");
337+
if (b < component_type{ 0 } || b > component_type{ 255 })
338+
::oof::detail::error("blue component not in [0, 255]");
325339
}
326340

327-
constexpr color::color(const uint8_t r, const uint8_t g, const uint8_t b)
328-
: red{ r }, green{ g }, blue{ b }
341+
template<std::integral component_type>
342+
constexpr color::color(component_type component)
343+
: color(component, component, component)
329344
{
330-
345+
331346
}
332347

333348

readme.md

Lines changed: 20 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,26 @@
11
# Oof (omnipotent output friend)
22
It's common for C++ programs to write output to the console. But consoles are far more capable than what they are usually used for. The magic lies in the so-called [Virtual Terminal sequences](https://docs.microsoft.com/en-us/windows/console/console-virtual-terminal-sequences) (sometimes also confusingly called ["escape codes"](https://en.wikipedia.org/wiki/ANSI_escape_code)): These cryptic character sequences allow complete control over position, color and other properties of written characters. *Oof* is a single C++20 header that wraps these in a convenient way.
33

4-
On top of that, *oof* provides two special interfaces that heavily optimize the resulting stream of VT sequences, so that real-time outputs like those below are possible. Everything in these videos are letters in a console window:
4+
```c++
5+
for (int i = 0; i < 10; ++i){
6+
std::cout << oof::fg_color(oof::color{255 - i * 25});
7+
std::cout << oof::hposition(2*i) << std::to_string(i) << "\n";
8+
}
9+
```
10+
![123_example](https://user-images.githubusercontent.com/6044318/142816762-f1167a81-3d11-4b4a-85fc-d4edcdc06bf6.png)
11+
12+
13+
```c++
14+
constexpr double values[]{0.54, 0.88, 0.42, 0.21, 0.33, 0.68, 0.91};
15+
for(int i=0; i<std::size(values); ++i){
16+
std::cout << oof::underline(true) << std::format("value {}", i) << oof::reset_formatting() << ": ";
17+
const oof::color color{ 255, static_cast<int>(255 - values[i] * 255), 0 };
18+
std::cout << oof::fg_color(color) << std::format("{:.2f}\n", values[i]) << oof::reset_formatting();
19+
}
20+
```
21+
![values_example](https://user-images.githubusercontent.com/6044318/142819817-d1fc16fc-01e7-4a49-a908-33ed4b7a7c37.png)
22+
23+
On top of that, *oof* provides two special interfaces that heavily optimize the resulting stream of VT sequences, so that real-time outputs like those below are possible. The following videos show what's possible with that - everything in these videos are letters in a console window:
524

625
https://user-images.githubusercontent.com/6044318/142469815-ce680909-9151-4322-85aa-01dc9ba29c1d.mp4
726

0 commit comments

Comments
 (0)