cpp-terminal 1.0.0
Small C++ library for writing multiplatform terminal applications
Loading...
Searching...
No Matches
Term::Terminfo Class Reference

#include <cpp-terminal/terminfo.hpp>

Public Types

enum class  ColorMode : std::uint8_t {
  Unset , NoColor , Bit3 , Bit4 ,
  Bit8 , Bit24
}
 
enum class  Bool : std::uint8_t { UTF8 = 0 , Legacy , ControlSequences }
 
enum class  String : std::uint8_t { TermEnv , TermName , TermVersion }
 
enum class  Integer : std::uint8_t
 
using Booleans = std::array<bool, BoolNumber>
 
using Strings = std::array<std::string, StringNumber>
 
using Integers = std::array<std::uint32_t, IntegerNumber>
 

Public Member Functions

 Terminfo ()
 

Static Public Member Functions

static bool get (const Term::Terminfo::Bool &key)
 
static std::uint32_t get (const Term::Terminfo::Integer &key)
 
static std::string get (const Term::Terminfo::String &key)
 
static ColorMode getColorMode ()
 

Static Private Member Functions

static void check ()
 
static void checkTermEnv ()
 
static void checkTerminalName ()
 
static void checkTerminalVersion ()
 
static void checkColorMode ()
 
static void checkUTF8 ()
 
static void checkLegacy ()
 
static void checkControlSequences ()
 
static void set (const Term::Terminfo::Bool &key, const bool &value)
 
static void set (const Term::Terminfo::Integer &key, const std::uint32_t &value)
 
static void set (const Term::Terminfo::String &key, const std::string &value)
 

Static Private Attributes

static const constexpr std::size_t BoolNumber {3}
 
static const constexpr std::size_t StringNumber {3}
 
static const constexpr std::size_t IntegerNumber {0}
 
static ColorMode m_colorMode {ColorMode::Unset}
 
static Booleans m_booleans {}
 
static Integers m_integers {}
 
static Strings m_strings {}
 

Detailed Description

Definition at line 19 of file terminfo.hpp.

Member Typedef Documentation

◆ Booleans

using Term::Terminfo::Booleans = std::array<bool, BoolNumber>

Definition at line 65 of file terminfo.hpp.

◆ Integers

using Term::Terminfo::Integers = std::array<std::uint32_t, IntegerNumber>

Definition at line 67 of file terminfo.hpp.

◆ Strings

using Term::Terminfo::Strings = std::array<std::string, StringNumber>

Definition at line 66 of file terminfo.hpp.

Member Enumeration Documentation

◆ Bool

enum class Term::Terminfo::Bool : std::uint8_t
strong
Enumerator
UTF8 

terminal has UTF-8 activated.

Legacy 

Terminal is in legacy mode (Windows only).

ControlSequences 

Terminal support control sequences.

Definition at line 38 of file terminfo.hpp.

39 {
40 UTF8 = 0,
41 Legacy,
43 };
@ Legacy
Terminal is in legacy mode (Windows only).
@ ControlSequences
Terminal support control sequences.
@ UTF8
terminal has UTF-8 activated.

◆ ColorMode

enum class Term::Terminfo::ColorMode : std::uint8_t
strong
Enumerator
Unset 
NoColor 
Bit3 
Bit4 
Bit8 
Bit24 

Definition at line 24 of file terminfo.hpp.

25 {
26 Unset,
27 // no color was used
28 NoColor,
29 // a 3bit color was used
30 Bit3,
31 // a 4bit color was used
32 Bit4,
33 // a 8bit color was used
34 Bit8,
35 // a 24bit (RGB) color was used
36 Bit24
37 };

◆ Integer

enum class Term::Terminfo::Integer : std::uint8_t
strong

Definition at line 50 of file terminfo.hpp.

51 {
52
53 };

◆ String

enum class Term::Terminfo::String : std::uint8_t
strong
Enumerator
TermEnv 

TERM environment variable value.

TermName 

Name of the terminal program if available.

TermVersion 

Terminal version.

Definition at line 44 of file terminfo.hpp.

45 {
46 TermEnv,
47 TermName,
49 };
@ TermName
Name of the terminal program if available.
@ TermVersion
Terminal version.
@ TermEnv
TERM environment variable value.

Constructor & Destructor Documentation

◆ Terminfo()

Term::Terminfo::Terminfo ( )

Definition at line 142 of file terminfo.cpp.

142{ check(); }
static void check()
Definition terminfo.cpp:120

Member Function Documentation

◆ check()

void Term::Terminfo::check ( )
staticprivate

Definition at line 120 of file terminfo.cpp.

121{
122 static bool checked{false};
123 if(!checked)
124 {
125 checkTermEnv();
129 checkLegacy();
131 checkUTF8();
132 checked = true;
133 }
134}
static void checkTerminalName()
Definition terminfo.cpp:109
static void checkControlSequences()
Definition terminfo.cpp:162
static void checkLegacy()
Definition terminfo.cpp:84
static void checkColorMode()
Definition terminfo.cpp:144
static void checkTermEnv()
Definition terminfo.cpp:107
static void checkUTF8()
Definition terminfo.cpp:172
static void checkTerminalVersion()
Definition terminfo.cpp:118

◆ checkColorMode()

void Term::Terminfo::checkColorMode ( )
staticprivate

Definition at line 144 of file terminfo.cpp.

145{
146 std::string name{m_strings[static_cast<std::size_t>(Terminfo::String::TermName)]};
147 if(name == "Apple_Terminal") { m_colorMode = Term::Terminfo::ColorMode::Bit8; }
148 else if(name == "JetBrains-JediTerm") { m_colorMode = Term::Terminfo::ColorMode::Bit24; }
149 else if(name == "vscode") { m_colorMode = Term::Terminfo::ColorMode::Bit24; }
150 else if(name == "linux") { m_colorMode = Term::Terminfo::ColorMode::Bit4; }
151 else if(name == "ansicon") { m_colorMode = Term::Terminfo::ColorMode::Bit4; }
152 else if(m_strings[static_cast<std::size_t>(Terminfo::String::TermEnv)] == "linux") { m_colorMode = Term::Terminfo::ColorMode::Bit4; }
153#if defined(_WIN32)
154 else if(WindowsVersionGreater(10, 0, 10586) && !m_booleans[static_cast<std::size_t>(Terminfo::Bool::Legacy)]) { m_colorMode = Term::Terminfo::ColorMode::Bit24; }
155 else if(m_booleans[static_cast<std::size_t>(Terminfo::Bool::Legacy)]) { m_colorMode = Term::Terminfo::ColorMode::Bit4; }
156#endif
158 std::string colorterm = Private::getenv("COLORTERM").second;
159 if((colorterm == "truecolor" || colorterm == "24bit") && m_colorMode != ColorMode::Unset) { m_colorMode = Term::Terminfo::ColorMode::Bit24; }
160}
static Strings m_strings
Definition terminfo.hpp:28
static ColorMode m_colorMode
Definition terminfo.hpp:25
static Booleans m_booleans
Definition terminfo.hpp:26
std::pair< bool, std::string > getenv(const std::string &env)
Value of an environment variables.
Definition env.cpp:14
bool WindowsVersionGreater(const DWORD &major, const DWORD &minor, const DWORD &patch)
Definition terminfo.cpp:53

◆ checkControlSequences()

void Term::Terminfo::checkControlSequences ( )
staticprivate

Definition at line 162 of file terminfo.cpp.

163{
164#ifdef _WIN32
167#else
169#endif
170}
static void set(const Term::Terminfo::Bool &key, const bool &value)
Definition terminfo.cpp:48

◆ checkLegacy()

void Term::Terminfo::checkLegacy ( )
staticprivate

Definition at line 84 of file terminfo.cpp.

85{
86#if defined(_WIN32)
87 #ifndef ENABLE_VIRTUAL_TERMINAL_PROCESSING
88 #define ENABLE_VIRTUAL_TERMINAL_PROCESSING 0x0004
89 #endif
90 if(!m_booleans[static_cast<std::size_t>(Terminfo::Bool::ControlSequences)]) { set(Terminfo::Bool::Legacy, true); }
91 else
92 {
93 DWORD dwOriginalOutMode{0};
94 GetConsoleMode(Private::out.handle(), &dwOriginalOutMode);
95 if(!SetConsoleMode(Private::out.handle(), dwOriginalOutMode | ENABLE_VIRTUAL_TERMINAL_PROCESSING)) { set(Terminfo::Bool::Legacy, true); }
96 else
97 {
98 SetConsoleMode(Private::out.handle(), dwOriginalOutMode);
100 }
101 }
102#else
104#endif
105}
OutputFileHandler & out
Definition file.cpp:44
#define ENABLE_VIRTUAL_TERMINAL_PROCESSING

◆ checkTermEnv()

void Term::Terminfo::checkTermEnv ( )
staticprivate

Definition at line 107 of file terminfo.cpp.

◆ checkTerminalName()

void Term::Terminfo::checkTerminalName ( )
staticprivate

Definition at line 109 of file terminfo.cpp.

110{
111 std::string name;
112 name = Private::getenv("TERM_PROGRAM").second;
113 if(name.empty()) { name = Private::getenv("TERMINAL_EMULATOR").second; }
114 if(Private::getenv("ANSICON").first) { name = "ansicon"; }
116}

◆ checkTerminalVersion()

void Term::Terminfo::checkTerminalVersion ( )
staticprivate

Definition at line 118 of file terminfo.cpp.

118{ set(Terminfo::String::TermVersion, Private::getenv("TERM_PROGRAM_VERSION").second); }

◆ checkUTF8()

void Term::Terminfo::checkUTF8 ( )
staticprivate

Definition at line 172 of file terminfo.cpp.

173{
174#if defined(_WIN32)
175 (GetConsoleOutputCP() == CP_UTF8 && GetConsoleCP() == CP_UTF8) ? set(Terminfo::Bool::UTF8, true) : set(Terminfo::Bool::UTF8, false);
176#else
177 Term::Cursor cursor_before{Term::cursor_position()};
178 Term::Private::out.write("\xe2\x82\xac"); // € 3bits in utf8 one character
179 std::string read{Term::Private::in.read()};
180 Term::Cursor cursor_after{Term::cursor_position()};
181 std::size_t moved{cursor_after.column() - cursor_before.column()};
182 if(moved == 1) { set(Terminfo::Bool::UTF8, true); }
183 else { set(Terminfo::Bool::UTF8, false); }
184 for(std::size_t i = 0; i != moved; ++i) { Term::Private::out.write("\b \b"); }
185#endif
186}
std::size_t column() const
Definition cursor.cpp:16
std::string read() const
Definition file.cpp:127
std::size_t write(const std::string &str) const
Definition file.cpp:101
InputFileHandler & in
Definition file.cpp:43
Term::Cursor cursor_position()
Definition cursor.cpp:26

◆ get() [1/3]

bool Term::Terminfo::get ( const Term::Terminfo::Bool & key)
static

Definition at line 30 of file terminfo.cpp.

31{
32 check();
33 return m_booleans[static_cast<std::size_t>(key)];
34}

◆ get() [2/3]

std::uint32_t Term::Terminfo::get ( const Term::Terminfo::Integer & key)
static

Definition at line 36 of file terminfo.cpp.

37{
38 check();
39 return m_integers[static_cast<std::size_t>(key)];
40}
static Integers m_integers
Definition terminfo.hpp:27

◆ get() [3/3]

std::string Term::Terminfo::get ( const Term::Terminfo::String & key)
static

Definition at line 42 of file terminfo.cpp.

43{
44 check();
45 return m_strings[static_cast<std::size_t>(key)];
46}

◆ getColorMode()

Term::Terminfo::ColorMode Term::Terminfo::getColorMode ( )
static

Definition at line 136 of file terminfo.cpp.

137{
139 return m_colorMode;
140}

◆ set() [1/3]

void Term::Terminfo::set ( const Term::Terminfo::Bool & key,
const bool & value )
staticprivate

Definition at line 48 of file terminfo.cpp.

48{ m_booleans[static_cast<std::size_t>(key)] = value; }

◆ set() [2/3]

void Term::Terminfo::set ( const Term::Terminfo::Integer & key,
const std::uint32_t & value )
staticprivate

Definition at line 49 of file terminfo.cpp.

49{ m_integers[static_cast<std::size_t>(key)] = value; }

◆ set() [3/3]

void Term::Terminfo::set ( const Term::Terminfo::String & key,
const std::string & value )
staticprivate

Definition at line 50 of file terminfo.cpp.

50{ m_strings[static_cast<std::size_t>(key)] = value; }

Member Data Documentation

◆ BoolNumber

const constexpr std::size_t Term::Terminfo::BoolNumber {3}
staticconstexprprivate

Definition at line 60 of file terminfo.hpp.

60{3};

◆ IntegerNumber

const constexpr std::size_t Term::Terminfo::IntegerNumber {0}
staticconstexprprivate

Definition at line 62 of file terminfo.hpp.

62{0};

◆ m_booleans

Term::Terminfo::Booleans Term::Terminfo::m_booleans {}
staticprivate

Definition at line 26 of file terminfo.hpp.

◆ m_colorMode

Term::Terminfo::ColorMode Term::Terminfo::m_colorMode {ColorMode::Unset}
staticprivate

Definition at line 25 of file terminfo.hpp.

25{

◆ m_integers

Term::Terminfo::Integers Term::Terminfo::m_integers {}
staticprivate

Definition at line 27 of file terminfo.hpp.

◆ m_strings

Term::Terminfo::Strings Term::Terminfo::m_strings {}
staticprivate

Definition at line 28 of file terminfo.hpp.

◆ StringNumber

const constexpr std::size_t Term::Terminfo::StringNumber {3}
staticconstexprprivate

Definition at line 61 of file terminfo.hpp.

61{3};

The documentation for this class was generated from the following files: