terminal.hpp 1.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566
  1. #pragma once
  2. #include <nall/string.hpp>
  3. namespace nall::terminal {
  4. inline auto escapable() -> bool {
  5. #if defined(PLATFORM_WINDOWS)
  6. //todo: colors are supported by Windows 10+ and with alternate terminals (eg msys)
  7. //disabled for now for compatibility with Windows 7 and 8.1's cmd.exe
  8. return false;
  9. #endif
  10. return true;
  11. }
  12. namespace color {
  13. template<typename... P> inline auto black(P&&... p) -> string {
  14. if(!escapable()) return string{forward<P>(p)...};
  15. return {"\e[30m", string{forward<P>(p)...}, "\e[0m"};
  16. }
  17. template<typename... P> inline auto blue(P&&... p) -> string {
  18. if(!escapable()) return string{forward<P>(p)...};
  19. return {"\e[94m", string{forward<P>(p)...}, "\e[0m"};
  20. }
  21. template<typename... P> inline auto green(P&&... p) -> string {
  22. if(!escapable()) return string{forward<P>(p)...};
  23. return {"\e[92m", string{forward<P>(p)...}, "\e[0m"};
  24. }
  25. template<typename... P> inline auto cyan(P&&... p) -> string {
  26. if(!escapable()) return string{forward<P>(p)...};
  27. return {"\e[96m", string{forward<P>(p)...}, "\e[0m"};
  28. }
  29. template<typename... P> inline auto red(P&&... p) -> string {
  30. if(!escapable()) return string{forward<P>(p)...};
  31. return {"\e[91m", string{forward<P>(p)...}, "\e[0m"};
  32. }
  33. template<typename... P> inline auto magenta(P&&... p) -> string {
  34. if(!escapable()) return string{forward<P>(p)...};
  35. return {"\e[95m", string{forward<P>(p)...}, "\e[0m"};
  36. }
  37. template<typename... P> inline auto yellow(P&&... p) -> string {
  38. if(!escapable()) return string{forward<P>(p)...};
  39. return {"\e[93m", string{forward<P>(p)...}, "\e[0m"};
  40. }
  41. template<typename... P> inline auto white(P&&... p) -> string {
  42. if(!escapable()) return string{forward<P>(p)...};
  43. return {"\e[97m", string{forward<P>(p)...}, "\e[0m"};
  44. }
  45. template<typename... P> inline auto gray(P&&... p) -> string {
  46. if(!escapable()) return string{forward<P>(p)...};
  47. return {"\e[37m", string{forward<P>(p)...}, "\e[0m"};
  48. }
  49. }
  50. }