Skip to content

Commit 2f82636

Browse files
authored
Merge branch 'master' into resizing
2 parents ce861ec + 3ff2369 commit 2f82636

File tree

14 files changed

+70
-27
lines changed

14 files changed

+70
-27
lines changed

.github/workflows/macOS.yml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -91,7 +91,7 @@ jobs:
9191
strategy:
9292
fail-fast: false
9393
matrix:
94-
version: [ 9, 10, 11 ]
94+
version: [ 10, 11, 12 ]
9595
standard: [ 11, 14, 17, 20 ]
9696

9797
steps:

cpp-terminal/CMakeLists.txt

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,13 +4,13 @@ configure_file(version.hpp.in version.hpp)
44
add_subdirectory(platforms)
55

66
# create and configure library target
7-
add_library(cpp-terminal base.cpp prompt.cpp window.cpp input.cpp terminal.cpp color.cpp key.cpp event.cpp screen.cpp)
7+
add_library(cpp-terminal base.cpp prompt.cpp window.cpp input.cpp terminal.cpp color.cpp key.cpp event.cpp screen.cpp options.cpp)
88
target_link_libraries(cpp-terminal PRIVATE Warnings::Warnings cpp-terminal::cpp-terminal-platforms)
99
target_compile_options(cpp-terminal PRIVATE $<$<CXX_COMPILER_ID:MSVC>:/utf-8>)
1010
target_include_directories(cpp-terminal PUBLIC $<BUILD_INTERFACE:${PROJECT_SOURCE_DIR}> $<BUILD_INTERFACE:${PROJECT_BINARY_DIR}> $<INSTALL_INTERFACE:include>)
1111

1212
set_target_properties(cpp-terminal PROPERTIES
13-
PUBLIC_HEADER "${CMAKE_CURRENT_SOURCE_DIR}/event.hpp;${CMAKE_CURRENT_SOURCE_DIR}/key.hpp;${CMAKE_CURRENT_SOURCE_DIR}/tty.hpp;${CMAKE_CURRENT_SOURCE_DIR}/terminal.hpp;${CMAKE_CURRENT_SOURCE_DIR}/base.hpp;${CMAKE_CURRENT_SOURCE_DIR}/prompt.hpp;${CMAKE_CURRENT_SOURCE_DIR}/window.hpp;${CMAKE_CURRENT_BINARY_DIR}/version.hpp"
13+
PUBLIC_HEADER "${CMAKE_CURRENT_SOURCE_DIR}/options.hpp;${CMAKE_CURRENT_SOURCE_DIR}/event.hpp;${CMAKE_CURRENT_SOURCE_DIR}/key.hpp;${CMAKE_CURRENT_SOURCE_DIR}/tty.hpp;${CMAKE_CURRENT_SOURCE_DIR}/terminal.hpp;${CMAKE_CURRENT_SOURCE_DIR}/base.hpp;${CMAKE_CURRENT_SOURCE_DIR}/prompt.hpp;${CMAKE_CURRENT_SOURCE_DIR}/window.hpp;${CMAKE_CURRENT_BINARY_DIR}/version.hpp"
1414
PRIVATE_HEADER "${CMAKE_CURRENT_SOURCE_DIR}/platforms/conversion.hpp;${CMAKE_CURRENT_SOURCE_DIR}/platforms/macros.hpp")
1515

1616
add_library(cpp-terminal::cpp-terminal ALIAS cpp-terminal)

cpp-terminal/options.cpp

Lines changed: 12 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,12 @@
1+
#include "cpp-terminal/options.hpp"
2+
3+
#include <algorithm>
4+
Term::Options::Options(const std::vector<Option>& options) : m_Options(options) {}
5+
6+
// Return true is the option is set and not its opposite (* + No* = false)
7+
bool Term::Options::has(const Option& option)
8+
{
9+
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+
else
11+
return false;
12+
}

cpp-terminal/options.hpp

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,32 @@
1+
#pragma once
2+
3+
#include <cstdint>
4+
#include <vector>
5+
6+
namespace Term
7+
{
8+
9+
class Options
10+
{
11+
public:
12+
enum class Option : int
13+
{
14+
// Don't use 0!
15+
ClearScreen = 1,
16+
NoClearScreen = -1,
17+
SignalKeys = 2,
18+
NoSignalKeys = -2,
19+
Cursor = 3,
20+
NoCursor = -3,
21+
};
22+
Options(const std::vector<Option>& options);
23+
bool has(const Option& option);
24+
~Options() {}
25+
26+
private:
27+
std::vector<Option> m_Options;
28+
};
29+
30+
typedef Term::Options::Option Option;
31+
32+
} // namespace Term

cpp-terminal/platforms/terminal.cpp

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ void Term::Terminal::store_and_restore()
5151
CloseHandle(hConIn);
5252
}
5353
#else
54-
static termios orig_termios;
54+
static termios orig_termios{};
5555
static int fd{open("/dev/tty", O_RDWR, O_NOCTTY)};
5656
if(!enabled)
5757
{
@@ -91,7 +91,7 @@ void Term::Terminal::setRawMode()
9191
flags = {0};
9292
if(!GetConsoleMode(hConIn, &flags)) { throw Term::Exception("GetConsoleMode() failed"); }
9393
if(m_terminfo.hasANSIEscapeCode()) { flags |= ENABLE_VIRTUAL_TERMINAL_INPUT; }
94-
if(disable_signal_keys) { flags &= ~ENABLE_PROCESSED_INPUT; }
94+
if(m_options.has(Option::NoSignalKeys)) { flags &= ~ENABLE_PROCESSED_INPUT; }
9595
flags &= ~(ENABLE_LINE_INPUT | ENABLE_ECHO_INPUT);
9696
if(!SetConsoleMode(hConIn, flags)) { throw Term::Exception("SetConsoleMode() failed"); }
9797
CloseHandle(hConOut);
@@ -110,7 +110,7 @@ void Term::Terminal::setRawMode()
110110
// raw.c_oflag &= ~(OPOST);
111111
raw.c_cflag |= CS8;
112112
raw.c_lflag &= ~(ECHO | ICANON | IEXTEN);
113-
if(disable_signal_keys) { raw.c_lflag &= ~ISIG; }
113+
if(m_options.has(Option::NoSignalKeys)) { raw.c_lflag &= ~ISIG; }
114114
raw.c_cc[VMIN] = 0;
115115
raw.c_cc[VTIME] = 0;
116116
if(tcsetattr(fd, TCSAFLUSH, &raw) == -1) { throw Term::Exception("tcsetattr() failed"); }

cpp-terminal/prompt.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,7 +10,7 @@
1010

1111
Term::Result Term::prompt(const std::string& message, const std::string& first_option, const std::string& second_option, const std::string& prompt_indicator, bool immediate)
1212
{
13-
Terminal term(false, true, false);
13+
Terminal term({Option::NoClearScreen, Option::NoSignalKeys, Option::Cursor});
1414
std::cout << message << " [" << first_option << '/' << second_option << ']' << prompt_indicator << ' ' << std::flush;
1515

1616
if(!Term::is_stdin_a_tty())

cpp-terminal/terminal.cpp

Lines changed: 6 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -4,29 +4,28 @@
44

55
#include <iostream>
66

7-
Term::Terminal::Terminal(const bool& _clear_screen, const bool& _disable_signal_keys, const bool& _hide_cursor) : clear_screen{_clear_screen}, disable_signal_keys{_disable_signal_keys}, hide_cursor{_hide_cursor}
7+
Term::Terminal::Terminal(const std::vector<Term::Options::Option>& options) : m_options(options)
88
{
99
attachConsole();
1010
store_and_restore();
1111
setRawMode();
12-
if(clear_screen)
12+
if(m_options.has(Option::ClearScreen))
1313
{
1414
// Fix consoles that ignore save_screen()
1515
std::cout << screen_save() << clear_buffer() << style(Style::RESET) << cursor_move(1, 1);
1616
}
17-
if(hide_cursor) std::cout << cursor_off();
18-
// flush stdout
17+
if(m_options.has(Option::NoCursor)) std::cout << cursor_off();
1918
std::cout << std::flush;
2019
}
2120

2221
Term::Terminal::~Terminal()
2322
{
24-
if(clear_screen)
23+
if(m_options.has(Option::ClearScreen))
2524
{
2625
// Fix consoles that ignore save_screen()
27-
std::cout << clear_buffer() << style(Style::RESET) << cursor_move(1, 1) << screen_load();
26+
std::cout << clear_buffer() << style(Style::RESET) << cursor_move(1, 1) << screen_load() << std::flush;
2827
}
29-
if(hide_cursor) std::cout << cursor_on();
28+
if(m_options.has(Option::NoCursor)) std::cout << cursor_on() << std::flush;
3029
// flush the output stream
3130
std::cout << std::flush;
3231
detachConsole();

cpp-terminal/terminal.hpp

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#pragma once
22

3+
#include "cpp-terminal/options.hpp"
34
#include "cpp-terminal/terminfo.hpp"
45

56
#include <cstdio>
@@ -12,10 +13,11 @@ namespace Term
1213
* terminal will not be left in a good state. Terminal uses exceptions when
1314
* something goes wrong.
1415
*/
16+
1517
class Terminal
1618
{
1719
public:
18-
Terminal(const bool& _clear_screen = false, const bool& _disable_signal_keys = true, const bool& _hide_cursor = false);
20+
Terminal(const std::vector<Term::Options::Option>& options = {});
1921
~Terminal();
2022

2123
private:
@@ -26,11 +28,9 @@ class Terminal
2628
void setRawMode();
2729
void attachConsole();
2830
void detachConsole();
29-
bool clear_screen{};
30-
bool disable_signal_keys{true};
31-
bool hide_cursor{};
3231
bool has_allocated_console{false};
3332
Term::Terminfo m_terminfo;
33+
Term::Options m_options;
3434
};
3535

3636
} // namespace Term

examples/keys.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ int main()
2222
std::cout << "The terminal is not attached to a TTY and therefore can't catch user input. Exiting...\n";
2323
return 1;
2424
}
25-
Term::Terminal term(true, true, false);
25+
Term::Terminal term({Term::Option::NoClearScreen, Term::Option::NoSignalKeys, Term::Option::Cursor});
2626
std::pair<std::size_t, std::size_t> term_size{Term::get_size()};
2727
std::cout << Term::cursor_move(1, 1);
2828
std::cout << "Dimension:" << std::get<1>(term_size) << " " << std::get<0>(term_size) << std::endl;

examples/kilo.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -924,7 +924,7 @@ int main(int argc, char* argv[])
924924
std::cout << "The terminal is not attached to a TTY and therefore can't catch user input. Exiting...\n";
925925
return 1;
926926
}
927-
Term::Terminal term(true, true, false);
927+
Term::Terminal term({Term::Option::ClearScreen, Term::Option::NoSignalKeys, Term::Option::NoCursor});
928928
initEditor();
929929
if(argc >= 2) { editorOpen(argv[1]); }
930930

0 commit comments

Comments
 (0)