-
-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy pathTitle.mpp
More file actions
68 lines (57 loc) · 1.48 KB
/
Copy pathTitle.mpp
File metadata and controls
68 lines (57 loc) · 1.48 KB
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
module;
#include <cstdio>
#include <CppUtils/System/Windows.hpp>
export module CppUtils.Terminal.Title;
import std;
import CppUtils.Terminal.RawTerminal;
import CppUtils.String.Concept;
import CppUtils.String.Utility;
import CppUtils.Terminal.TextModifier;
export namespace CppUtils::Terminal
{
inline auto getTerminalTitle() -> auto
{
#if defined(OS_WINDOWS)
auto title = std::wstring{};
constexpr auto size = 256uz;
title.resize(size);
::GetConsoleTitleW(std::data(title), size);
return title;
#elif defined(OS_LINUX) or defined(OS_MACOS)
/*
using namespace std::literals;
auto rawTerminal = RawTerminal{};
std::fwrite("\033]21;?\a", 1, 7, stdout);
std::fflush(stdout);
const auto response = rawTerminal.read('\a');
if (constexpr auto prefix = "\033]21;"sv; response.starts_with(prefix))
return std::string{std::cbegin(response) + std::size(prefix), std::cend(response)};
*/
return std::string{};
#endif
}
inline auto setTerminalTitle(const String::GenericText auto& title) -> void
{
#if defined(OS_WINDOWS)
::SetConsoleTitleW(std::data(title));
#elif defined(OS_LINUX) or defined(OS_MACOS)
std::print(Ansi::EscapeCode::SetTitle, title);
std::fflush(stdout);
#endif
}
class Title final
{
public:
inline Title(const String::GenericText auto& title):
m_oldTitle{getTerminalTitle()}
{
setTerminalTitle(title);
}
inline ~Title()
{
setTerminalTitle(m_oldTitle);
}
private:
std::invoke_result_t<decltype(getTerminalTitle)> m_oldTitle;
};
}