Echo Writes Code

console.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
#include "crucible/core/console.hpp"

#include "crucible/core/environment.hpp"

#include <cstdio>

namespace crucible::core
{
	namespace
	{

#ifdef CRUCIBLE_PLATFORM_IS_POSIX_LIKE
		auto color_red(FILE *stream) -> void
		{
			if (!environment().query<Environment_Variable::Colors>()) {
				return;
			}

			std::fputs("\x1b[31m", stream);
		}

		auto color_reset(FILE *stream) -> void
		{
			if (!environment().query<Environment_Variable::Colors>()) {
				return;
			}

			std::fputs("\x1b[0m", stream);
		}
#endif // CRUCIBLE_PLATFORM_IS_POSIX_LIKE

#ifdef CRUCIBLE_PLATFORM_IS_WINDOWS_LIKE
		auto color_red(FILE *stream) -> void
		{}

		auto color_reset(FILE *stream) -> void
		{}
#endif // CRUCIBLE_PLATFORM_IS_WINDOWS_LIKE

	}

  auto Standard_Output_Formatter::write(char const *data, std::size_t const size) -> void
  {
    std::fwrite(data, sizeof(char), size, stdout);
  }

  auto Standard_Output_Formatter::write(char const data) -> void
  {
    std::fwrite(&data, sizeof(char), 1, stdout);
  }

  auto Standard_Error_Formatter::write(char const *data, std::size_t const size) -> void
  {
		color_red(stderr);
    std::fwrite(data, sizeof(char), size, stderr);
		color_reset(stderr);
  }

  auto Standard_Error_Formatter::write(char const data) -> void
  {
		color_red(stderr);
    std::fwrite(&data, sizeof(char), 1, stderr);
		color_reset(stderr);
  }

  auto print(String_View const message) -> void
  {
    std::fwrite(message.data(), sizeof(char), message.size(), stdout);
  }

  auto print_line(String_View const message) -> void
  {
    std::fwrite(message.data(), sizeof(char), message.size(), stdout);
    std::fputs(CRUCIBLE_PLATFORM_NEWLINE_SEQUENCE, stdout);
  }

  auto print_line() -> void
  {
    std::fputs(CRUCIBLE_PLATFORM_NEWLINE_SEQUENCE, stdout);
  }

  auto print_error(String_View const message) -> void
  {
		color_red(stderr);
    std::fwrite(message.data(), sizeof(char), message.size(), stderr);
		color_reset(stderr);
  }

  auto print_error_line(String_View const message) -> void
  {
		color_red(stderr);
    std::fwrite(message.data(), sizeof(char), message.size(), stderr);
    std::fputs(CRUCIBLE_PLATFORM_NEWLINE_SEQUENCE, stderr);
		color_reset(stderr);
  }

  auto print_error_line() -> void
  {
		color_red(stderr);
    std::fputs(CRUCIBLE_PLATFORM_NEWLINE_SEQUENCE, stderr);
		color_reset(stderr);
  }
}