Skip to content

Commit bd73515

Browse files
authored
Merge branch 'master' into readme
2 parents 2c10a5b + f24dfea commit bd73515

31 files changed

+291
-191
lines changed

.pre-commit-config.yaml

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -45,7 +45,7 @@ repos:
4545
name: check .editorconfig rules
4646

4747
- repo: https://github.com/pre-commit/mirrors-clang-format
48-
rev: v15.0.7
48+
rev: v16.0.3
4949
hooks:
5050
- id: clang-format
5151

@@ -58,12 +58,12 @@ repos:
5858
args: [ --config=.cmake-linter.yml ]
5959

6060
- repo: https://github.com/codespell-project/codespell
61-
rev: v2.2.2
61+
rev: v2.2.4
6262
hooks:
6363
- id: codespell
6464

6565
- repo: https://github.com/Lucas-C/pre-commit-hooks
66-
rev: v1.4.2
66+
rev: v1.5.1
6767
hooks:
6868
- id: remove-crlf
6969
- id: remove-tabs

cpp-terminal/cursor.cpp

Lines changed: 6 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,21 +1,21 @@
11
#include "cpp-terminal/cursor.hpp"
22

3-
Term::Cursor::Cursor(const std::size_t& row, const std::size_t& column) : m_position({row, column}) {}
3+
Term::Cursor::Cursor(const std::size_t& row, const std::size_t& column) : m_rows(row), m_columns(column) {}
44

5-
std::size_t Term::Cursor::row() const { return m_position.first; }
5+
std::size_t Term::Cursor::row() const { return m_rows; }
66

7-
std::size_t Term::Cursor::column() const { return m_position.second; }
7+
std::size_t Term::Cursor::column() const { return m_columns; }
88

99
bool Term::Cursor::empty() const
1010
{
11-
if(m_position.first == 0 && m_position.second == 0) return true;
11+
if(m_rows == 0 && m_columns == 0) return true;
1212
else
1313
return false;
1414
}
1515

16-
void Term::Cursor::setRow(const std::size_t& row) { m_position.first = row; }
16+
void Term::Cursor::setRow(const std::size_t& row) { m_rows = row; }
1717

18-
void Term::Cursor::setColum(const std::size_t& column) { m_position.second = column; }
18+
void Term::Cursor::setColum(const std::size_t& column) { m_columns = column; }
1919

2020
std::string Term::cursor_off() { return "\x1b[?25l"; }
2121

cpp-terminal/cursor.hpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,7 +2,6 @@
22

33
#include <cstddef>
44
#include <string>
5-
#include <utility>
65

76
namespace Term
87
{
@@ -19,7 +18,8 @@ class Cursor
1918
bool empty() const;
2019

2120
private:
22-
std::pair<std::size_t, std::size_t> m_position{0, 0};
21+
std::size_t m_rows{0};
22+
std::size_t m_columns{0};
2323
};
2424

2525
// returns the current cursor position (row, column) (Y, X)

cpp-terminal/io.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,7 @@ namespace Term
88
{
99
static char termbuf[sizeof(Term::Terminal)];
1010
Terminal& terminal = reinterpret_cast<Term::Terminal&>(termbuf);
11-
} // namespace Term
11+
} // namespace Term */
1212

1313
int Term::TerminalInitializer::m_counter{0};
1414

cpp-terminal/options.cpp

Lines changed: 2 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,12 @@
22

33
#include <algorithm>
44

5-
Term::Options::Options(const std::vector<Option>& options) : m_Options(options) {}
6-
7-
std::vector<Term::Options::Option> Term::Options::getOptions() { return m_Options; }
5+
void Term::Options::set(const Term::Option& option) { m_Options.push_back(option); }
86

97
// Return true is the option is set and not its opposite (* + No* = false)
108
bool Term::Options::has(const Option& option)
119
{
12-
if(std::find(m_Options.begin(), m_Options.end(), option) != m_Options.end() && std::find(m_Options.begin(), m_Options.end(), static_cast<Options::Option>(-1 * static_cast<std::int16_t>(option))) == m_Options.end()) return true;
10+
if(std::find(m_Options.begin(), m_Options.end(), option) != m_Options.end() && std::find(m_Options.begin(), m_Options.end(), static_cast<Option>(-1 * static_cast<std::int16_t>(option))) == m_Options.end()) return true;
1311
else
1412
return false;
1513
}

cpp-terminal/options.hpp

Lines changed: 17 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -6,32 +6,30 @@
66
namespace Term
77
{
88

9+
enum class Option : int
10+
{
11+
// Don't use 0!
12+
Default = 0,
13+
Raw = 1,
14+
Cooked = -1,
15+
ClearScreen = 2,
16+
NoClearScreen = -2,
17+
SignalKeys = 3,
18+
NoSignalKeys = -3,
19+
Cursor = 4,
20+
NoCursor = -4,
21+
};
22+
923
class Options
1024
{
1125
public:
12-
enum class Option : int
13-
{
14-
// Don't use 0!
15-
Default = 0,
16-
Raw = 1,
17-
Cooked = -1,
18-
ClearScreen = 2,
19-
NoClearScreen = -2,
20-
SignalKeys = 3,
21-
NoSignalKeys = -3,
22-
Cursor = 4,
23-
NoCursor = -4,
24-
};
2526
Options() = default;
26-
explicit Options(const std::vector<Option>& options);
27-
bool has(const Option& option);
28-
~Options() {}
29-
std::vector<Option> getOptions();
27+
bool has(const Option&);
28+
void set(const Option&);
29+
~Options() = default;
3030

3131
private:
3232
std::vector<Option> m_Options;
3333
};
3434

35-
typedef Term::Options::Option Option;
36-
3735
} // namespace Term

cpp-terminal/platforms/CMakeLists.txt

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
add_library(cpp-terminal-platforms terminal.cpp tty.cpp terminfo.cpp input.cpp screen.cpp cursor.cpp)
1+
add_library(cpp-terminal-platforms STATIC terminal.cpp tty.cpp terminfo.cpp input.cpp screen.cpp cursor.cpp file.cpp)
22
target_link_libraries(cpp-terminal-platforms PRIVATE Warnings::Warnings)
33
target_compile_options(cpp-terminal-platforms PRIVATE $<$<CXX_COMPILER_ID:MSVC>:/utf-8 /wd4668 /wd4514>)
44
target_include_directories(cpp-terminal-platforms PRIVATE $<BUILD_INTERFACE:${CMAKE_CURRENT_SOURCE_DIR}> $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}> $<BUILD_INTERFACE:${PROJECT_BINARY_DIR}> $<INSTALL_INTERFACE:include>)

cpp-terminal/platforms/cursor.cpp

Lines changed: 4 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
#include "cpp-terminal/cursor.hpp"
22

33
#if defined(_WIN32)
4+
#include "cpp-terminal/platforms/file.hpp"
45
#include "windows.h"
56
#else
67
#include "cpp-terminal/input.hpp"
@@ -10,12 +11,10 @@
1011
Term::Cursor Term::cursor_position()
1112
{
1213
#if defined(_WIN32)
13-
Term::Cursor ret;
14-
HANDLE hConOut{CreateFile("CONOUT$", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)};
1514
CONSOLE_SCREEN_BUFFER_INFO inf;
16-
if(GetConsoleScreenBufferInfo(hConOut, &inf)) { ret = {static_cast<std::size_t>(inf.dwCursorPosition.Y + 1), static_cast<std::size_t>(inf.dwCursorPosition.X + 1)}; }
17-
CloseHandle(hConOut);
18-
return ret;
15+
if(GetConsoleScreenBufferInfo(Private::std_cout.getHandler(), &inf)) return Term::Cursor(inf.dwCursorPosition.Y + 1, inf.dwCursorPosition.X + 1);
16+
else
17+
return Term::Cursor(0, 0);
1918
#else
2019
Term::terminal << Term::cursor_position_report();
2120
Term::Cursor c;

cpp-terminal/platforms/file.cpp

Lines changed: 90 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,90 @@
1+
#include "cpp-terminal/platforms/file.hpp"
2+
3+
#include <new>
4+
5+
#if defined(_WIN32)
6+
#include <windows.h>
7+
typedef void* Handle;
8+
#else
9+
#include <cstdio>
10+
typedef FILE* Handle;
11+
#endif
12+
13+
namespace Term
14+
{
15+
16+
namespace Private
17+
{
18+
static char stdin_buf[sizeof(Term::Private::FileHandler)];
19+
Term::Private::FileHandler& std_cin = reinterpret_cast<Term::Private::FileHandler&>(stdin_buf);
20+
static char stdout_buf[sizeof(Term::Private::FileHandler)];
21+
Term::Private::FileHandler& std_cout = reinterpret_cast<Term::Private::FileHandler&>(stdout_buf);
22+
} // namespace Private
23+
24+
} // namespace Term
25+
26+
Term::Private::FileHandler::FileHandler(const char* filename, const char* mode)
27+
{
28+
#if defined(_WIN32)
29+
m_file = {CreateFile(filename, GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)};
30+
if(m_file == INVALID_HANDLE_VALUE)
31+
{
32+
m_file = {CreateFile("NUL", GENERIC_READ | GENERIC_WRITE, FILE_SHARE_READ | FILE_SHARE_WRITE, NULL, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, NULL)};
33+
m_isNull = true;
34+
}
35+
#else
36+
m_file = {std::fopen(filename, mode)};
37+
if(m_file == nullptr)
38+
{
39+
m_file = {std::fopen("/dev/null", mode)};
40+
m_isNull = true;
41+
}
42+
#endif
43+
}
44+
45+
Term::Private::FileHandler::~FileHandler()
46+
{
47+
#if defined(_WIN32)
48+
CloseHandle(reinterpret_cast<HANDLE>(m_file));
49+
#else
50+
std::fclose(reinterpret_cast<FILE*>(m_file));
51+
#endif
52+
}
53+
54+
bool Term::Private::FileHandler::isNull() { return m_isNull; }
55+
56+
Term::Private::consoleFileHandler Term::Private::FileHandler::getHandler()
57+
{
58+
#if defined(_WIN32)
59+
return m_file;
60+
#else
61+
return fileno(reinterpret_cast<FILE*>(m_file));
62+
#endif
63+
}
64+
65+
int Term::Private::FileInitializer::m_counter = {0};
66+
67+
void Term::Private::FileInitializer::initialize()
68+
{
69+
if(m_counter++ == 0)
70+
{
71+
#if defined(_WIN32)
72+
new(&Term::Private::std_cin) FileHandler("CONIN$", "");
73+
new(&Term::Private::std_cout) FileHandler("CONOUT$", "");
74+
#else
75+
new(&Term::Private::std_cin) FileHandler("/dev/tty", "r");
76+
new(&Term::Private::std_cout) FileHandler("/dev/tty", "w");
77+
#endif
78+
}
79+
}
80+
81+
Term::Private::FileInitializer::FileInitializer() { initialize(); }
82+
83+
Term::Private::FileInitializer::~FileInitializer()
84+
{
85+
if(--m_counter == 0)
86+
{
87+
(&Term::Private::std_cin)->~FileHandler();
88+
(&Term::Private::std_cout)->~FileHandler();
89+
}
90+
}

cpp-terminal/platforms/file.hpp

Lines changed: 49 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,49 @@
1+
// Open file for stdin, stdout, stderr without redirection. A kind of FILE class for consoles
2+
// PRIVATE !!!
3+
#pragma once
4+
5+
namespace Term
6+
{
7+
8+
namespace Private
9+
{
10+
11+
#if defined(_WIN32)
12+
typedef void* consoleFileHandler;
13+
#else
14+
typedef int consoleFileHandler;
15+
#endif
16+
17+
class FileInitializer
18+
{
19+
public:
20+
FileInitializer();
21+
void initialize();
22+
~FileInitializer();
23+
24+
private:
25+
static int m_counter;
26+
};
27+
28+
static FileInitializer m_fileInitializer;
29+
30+
class FileHandler
31+
{
32+
public:
33+
FileHandler(const char*, const char*);
34+
~FileHandler();
35+
consoleFileHandler getHandler();
36+
bool isNull();
37+
38+
private:
39+
bool m_isNull{false};
40+
void* m_file{nullptr};
41+
};
42+
43+
// Even in namespace it can't be called stdin because stdin can be a Macro :(
44+
extern FileHandler& std_cin;
45+
extern FileHandler& std_cout;
46+
47+
} // namespace Private
48+
49+
} // namespace Term

0 commit comments

Comments
 (0)