Skip to content

Commit f24dfea

Browse files
authored
Merge pull request #243 from flagarde/options
Simplify setOptions()
2 parents 850bcca + 6140020 commit f24dfea

File tree

13 files changed

+45
-39
lines changed

13 files changed

+45
-39
lines changed

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/prompt.cpp

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

1313
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)
1414
{
15-
Term::terminal.setOptions({Option::NoClearScreen, Option::NoSignalKeys, Option::Cursor, Term::Option::Raw});
15+
Term::terminal.setOptions(Option::NoClearScreen, Option::NoSignalKeys, Option::Cursor, Term::Option::Raw);
1616
std::cout << message << " [" << first_option << '/' << second_option << ']' << prompt_indicator << ' ' << std::flush;
1717

1818
if(!Term::is_stdin_a_tty())

cpp-terminal/terminal.cpp

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

33
#include "cpp-terminal/cursor.hpp"
4+
#include "cpp-terminal/options.hpp"
45
#include "cpp-terminal/screen.hpp"
56
#include "cpp-terminal/style.hpp"
6-
#include "options.hpp"
77

88
Term::Terminal::Terminal()
99
{
@@ -21,9 +21,10 @@ Term::Terminal::~Terminal()
2121
detachConsole();
2222
}
2323

24-
void Term::Terminal::setOptions(const std::vector<Term::Options::Option>& options)
24+
void Term::Terminal::setOptions() { applyOptions(); }
25+
26+
void Term::Terminal::applyOptions()
2527
{
26-
m_options = Options(options);
2728
if(m_options.has(Option::ClearScreen)) clog << screen_save() << clear_buffer() << style(Style::RESET) << cursor_move(1, 1);
2829
if(m_options.has(Option::NoCursor)) clog << cursor_off();
2930
if(m_options.has(Option::Raw)) setRawMode();

cpp-terminal/terminal.hpp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -21,21 +21,28 @@ class Terminal
2121
public:
2222
Terminal();
2323
~Terminal();
24-
void setOptions(const std::vector<Term::Options::Option>& options = {});
24+
25+
template<typename Arg1 = Term::Option, typename... Args> void setOptions(Arg1 arg1, Args... args)
26+
{
27+
m_options.set(arg1);
28+
setOptions(args...);
29+
}
30+
2531
template<typename T> Terminal& operator<<(const T& dt)
2632
{
2733
clog << dt;
2834
return *this;
2935
}
3036
template<typename T> std::istream& operator>>(T& dt)
3137
{
32-
if(m_options.has(Options::Option::Raw))
38+
if(m_options.has(Option::Raw))
3339
{
34-
std::vector<Term::Options::Option> option = m_options.getOptions();
40+
Options options = m_options;
3541
store_and_restore();
3642
this->cin >> dt;
3743
store_and_restore();
38-
setOptions(option);
44+
m_options = options;
45+
applyOptions();
3946
return this->cin;
4047
}
4148
else
@@ -51,6 +58,8 @@ class Terminal
5158
}
5259

5360
private:
61+
void setOptions();
62+
void applyOptions();
5463
std::ofstream cout;
5564
std::ofstream cerr;
5665
std::ofstream clog;

examples/cin.cpp

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

66
int main()
77
{
8-
Term::terminal.setOptions({Term::Option::Raw});
8+
Term::terminal.setOptions(Term::Option::Raw);
99
Term::terminal << "This should be printed" << std::flush << " only in terminal" << std::endl;
1010
std::cout << "This should be printed in terminal if stdout is not redirected" << std::endl;
1111
Term::terminal << "Now type a number and then a string !" << std::endl;

examples/keys.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,8 @@ int main()
1414
try
1515
{
1616
// check if the terminal is capable of handling input
17+
Term::terminal.setOptions(Term::Option::NoClearScreen, Term::Option::NoSignalKeys, Term::Option::Cursor, Term::Option::Raw);
1718
if(!Term::is_stdin_a_tty()) throw Term::Exception("The terminal is not attached to a TTY and therefore can't catch user input. Exiting...");
18-
Term::terminal.setOptions({Term::Option::NoClearScreen, Term::Option::NoSignalKeys, Term::Option::Cursor, Term::Option::Raw});
1919

2020
Term::Cursor cursor{Term::cursor_position()};
2121
Term::terminal << "Cursor position : " << cursor.row() << " " << cursor.column() << std::endl;

examples/kilo.cpp

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

examples/menu.cpp

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -69,8 +69,8 @@ int main()
6969
try
7070
{
7171
// check if the terminal is capable of handling input
72+
Term::terminal.setOptions(Term::Option::ClearScreen, Term::Option::NoSignalKeys, Term::Option::NoCursor, Term::Option::Raw);
7273
if(!Term::is_stdin_a_tty()) throw Term::Exception("The terminal is not attached to a TTY and therefore can't catch user input. Exiting...");
73-
Term::terminal.setOptions({Term::Option::ClearScreen, Term::Option::NoSignalKeys, Term::Option::NoCursor, Term::Option::Raw});
7474
Term::Screen term_size = Term::screen_size();
7575
int pos = 5;
7676
int h = 10;

0 commit comments

Comments
 (0)