|
| 1 | +/* |
| 2 | + Copyright 2018-2021 Tatsuya Yamasaki. |
| 3 | +
|
| 4 | + Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | + you may not use this file except in compliance with the License. |
| 6 | + You may obtain a copy of the License at |
| 7 | +
|
| 8 | + http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | +
|
| 10 | + Unless required by applicable law or agreed to in writing, software |
| 11 | + distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | + WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | + See the License for the specific language governing permissions and |
| 14 | + limitations under the License. |
| 15 | +*/ |
| 16 | + |
| 17 | +#ifndef INCLUDED_MEEVAX_IOSTREAM_ESCAPE_SEQUENCE_HPP |
| 18 | +#define INCLUDED_MEEVAX_IOSTREAM_ESCAPE_SEQUENCE_HPP |
| 19 | + |
| 20 | +#include <tuple> |
| 21 | + |
| 22 | +#include <meevax/iostream/is_console.hpp> |
| 23 | +#include <meevax/utility/unwrap_reference_wrapper.hpp> |
| 24 | + |
| 25 | +namespace meevax |
| 26 | +{ |
| 27 | +inline namespace iostream |
| 28 | +{ |
| 29 | + template <typename... Ts> |
| 30 | + struct escape_sequence |
| 31 | + { |
| 32 | + char const* command; |
| 33 | + |
| 34 | + std::tuple< |
| 35 | + typename std::conditional< |
| 36 | + not std::is_reference<Ts>::value or std::is_scalar<typename std::remove_reference<Ts>::type>::value, |
| 37 | + typename std::decay<Ts>::type, |
| 38 | + std::reference_wrapper<typename std::remove_reference<Ts>::type> |
| 39 | + >::type... |
| 40 | + > references; |
| 41 | + |
| 42 | + template <typename T> |
| 43 | + explicit constexpr escape_sequence(T&& x, Ts&&... xs) |
| 44 | + : command { std::forward<decltype(x)>(x) } |
| 45 | + , references { std::forward<decltype(xs)>(xs)... } |
| 46 | + {} |
| 47 | + |
| 48 | + friend auto operator <<(std::ostream & os, escape_sequence const& sequence) -> std::ostream & |
| 49 | + { |
| 50 | + auto print = [&](auto&& ... xs) -> std::ostream & |
| 51 | + { |
| 52 | + return (os << ... << unwrap_reference_wrapper(xs)); |
| 53 | + }; |
| 54 | + |
| 55 | + if (is_console(os)) |
| 56 | + { |
| 57 | + os << "\x1b[" << sequence.command; |
| 58 | + std::apply(print, sequence.references); |
| 59 | + return os << "\x1b[0m"; |
| 60 | + } |
| 61 | + else |
| 62 | + { |
| 63 | + std::apply(print, sequence.references); |
| 64 | + return os; |
| 65 | + } |
| 66 | + } |
| 67 | + }; |
| 68 | + |
| 69 | + template <typename T, typename... Ts> |
| 70 | + escape_sequence(T&&, Ts&&...) -> escape_sequence<Ts...>; |
| 71 | + |
| 72 | + #define DEFINE(COMMAND, NAME) \ |
| 73 | + auto NAME = [](auto&&... xs) \ |
| 74 | + { \ |
| 75 | + return escape_sequence(COMMAND, std::forward<decltype(xs)>(xs)...); \ |
| 76 | + } |
| 77 | + |
| 78 | + DEFINE("1m", bold); |
| 79 | + DEFINE("2m", faint); |
| 80 | + DEFINE("3m", italic); // Not widely supported. Sometimes treated as inverse. |
| 81 | + DEFINE("4m", underline); |
| 82 | + DEFINE("5m", slow_blink); // Less than 150 per minite. |
| 83 | + DEFINE("6m", rapid_blink); // More than 150 per minite. Not widely supported. |
| 84 | + DEFINE("7m", reverse); |
| 85 | + DEFINE("8m", conceal); // Not widely supported. |
| 86 | + |
| 87 | + inline namespace foreground |
| 88 | + { |
| 89 | + DEFINE("30m", black); |
| 90 | + DEFINE("31m", red); |
| 91 | + DEFINE("32m", green); |
| 92 | + DEFINE("33m", yellow); |
| 93 | + DEFINE("34m", blue); |
| 94 | + DEFINE("35m", magenta); |
| 95 | + DEFINE("36m", cyan); |
| 96 | + DEFINE("37m", white); |
| 97 | + } |
| 98 | + |
| 99 | + namespace background |
| 100 | + { |
| 101 | + DEFINE("40m", black); |
| 102 | + DEFINE("41m", red); |
| 103 | + DEFINE("42m", green); |
| 104 | + DEFINE("43m", yellow); |
| 105 | + DEFINE("44m", blue); |
| 106 | + DEFINE("45m", magenta); |
| 107 | + DEFINE("46m", cyan); |
| 108 | + DEFINE("47m", white); |
| 109 | + } |
| 110 | + |
| 111 | + #undef DEFINE |
| 112 | +} // namespace iostream |
| 113 | +} // namespace meevax |
| 114 | + |
| 115 | +#endif // INCLUDED_MEEVAX_IOSTREAM_ESCAPE_SEQUENCE_HPP |
0 commit comments