-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmain.cpp
186 lines (163 loc) · 5.74 KB
/
main.cpp
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
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
#define _POSIX_C_SOURCE 199309L
#include <cmath>
#include <cstring>
#include <string>
#include <algorithm>
#include <deque>
#include <cstdlib>
#include <csignal>
#include <sys/ioctl.h>
#include <termios.h>
#include <unistd.h>
#include "detail/puzzle-simulator.hpp"
#include "detail/option-builder.hpp"
#define NOT_SET nullptr
namespace detail
{
static void resize_handler( int signal)
{
if( SIGWINCH != signal)
return;
struct winsize window_size{};
ioctl(0, TIOCGWINSZ, &window_size);
auto w_instance = detail::EventDog::instance();
w_instance->setWinSize(window_size.ws_row, window_size.ws_col);
}
static struct termios orig_term_state;
static void refresh_before_exit( int signal)
{
if( orig_term_state.c_iflag == 0)
return;
fprintf( stderr, "\x1B[?25h\x1B[2J\x1B[0;0H"); // Clear screen
tcsetattr( STDIN_FILENO, TCSANOW, &orig_term_state);
// Turn-off focus control
printf( "\x1B[?1004l");
// exit() must not be called here to avoid infinite loop
_Exit( signal);
}
namespace util
{
template<typename Pred, typename First, typename... Others>
bool compareAnd( First base, Others... others)
{
return ( ... && Pred()( base, others));
}
}
}
int main( int argc, char *argv[])
{
std::string_view puzzle_file;
detail::OptionBuilder builder( argc, argv);
builder.addMismatchConsumer([ &]( std::string_view option)
{
puzzle_file = option;
});
builder.addOption("help", "h", {}, "Show this page.")
.addOption("speed", "s", "1", "Set the simulation speed for the solver.")
.addOption( "file", "f", "Set the file containing the puzzle.")
.addOption( "matches-only", "only", "no", "Display matched words only.")
.addOption( "predictable", "p", "no", "Randomize the puzzle solution on every run.")
.addOption( "wrap", "w", "yes",
"The forward and rewind button switches to first and last on reaching the end.")
.addOption( "auto-next", "a", "no", "Press `next` before next puzzle is run.")
.addOption( "reverse-solve", "r", "no", "Reverse the effect of forward and rewind button.")
.build();
if( !builder.asDefault( "help").empty())
{
builder.showHelp();
exit( EXIT_SUCCESS);
}
auto maybe_file = builder.asDefault( "file");
if( !maybe_file.empty())
puzzle_file = maybe_file.data();
if( puzzle_file.empty())
{
builder.showHelp();
exit( EXIT_FAILURE);
}
auto scope = std::ifstream( puzzle_file.data());
if( !scope)
{
fprintf( stderr, "Invalid file!");
exit( 1);
}
PuzzleFileReader reader( scope);
auto response = reader.getPuzzles();
if( response.empty())
{
fprintf( stderr, "Invalid file!");
exit( 1);
}
struct sigaction resize_action{};
resize_action.sa_handler = detail::resize_handler;
sigaction( SIGWINCH, &resize_action, NOT_SET);
raise( SIGWINCH); // Get current window size.
// If $LINES and $COLUMNS is non-zero, terminal supports cursor motion.
if( detail::EventDog::getWinLines() && detail::EventDog::getWinCols())
{
struct sigaction refresh_action{};
refresh_action.sa_handler = detail::refresh_before_exit;
sigaction( SIGINT, &refresh_action, NOT_SET);
atexit( [] { detail::refresh_before_exit( EXIT_SUCCESS);});
struct termios raw_term_state{};
tcgetattr( STDIN_FILENO, &detail::orig_term_state);
raw_term_state.c_iflag = ICRNL | IUTF8;
raw_term_state.c_oflag = OPOST | OFILL | ONLCR | NL0;
raw_term_state.c_lflag = ISIG;
raw_term_state.c_cflag = CS8 | CREAD;
raw_term_state.c_ispeed = B9600;
raw_term_state.c_ospeed = B9600;
raw_term_state.c_cc[ VINTR] = 003;
raw_term_state.c_cc[ VSUSP] = 032;
raw_term_state.c_cc[ VEOF] = 004;
raw_term_state.c_cc[ VMIN] = 1;
raw_term_state.c_cc[ VTIME] = 0;
raw_term_state.c_cc[ VERASE] = 0177;
tcsetattr( STDIN_FILENO, TCSANOW, &raw_term_state);
// Turn-on focus control
printf( "\x1B[?1004h");
auto step = builder.asBool( "reverse-solve") ? -1 : 1;
std::vector<std::unique_ptr<TerminalPuzzleSimulator>> sims( response.size());
auto g_begin = step == -1 ? --response.cend() : response.cbegin(),
g_end = step == -1 ? --response.begin() : response.cend();
for( auto begin = g_begin, end = g_end; begin != end;)
{
// Calculate the puzzle number to indicate at the top
auto puzzle_number = static_cast<size_t>( std::distance( response.cbegin(), begin)) + 1;
PuzzleSolver solver( begin->puzzle, begin->keys);
if( !sims[ puzzle_number - 1])
sims[ puzzle_number - 1] = std::make_unique<TerminalPuzzleSimulator>( solver, builder);
auto& term_simulator = sims[ puzzle_number - 1];
term_simulator->setSimulatorSpeed(( int)builder.asInt("speed"));
detail::EventDog::registerWinUpdateCallback([ &term_simulator, &puzzle_number](bool refresh)
{
term_simulator->simulate( std::cout, puzzle_number, refresh);
});
// Returns indication that this run completed.
auto status = term_simulator->simulate( std::cout, puzzle_number, false);
printf("\x1B[?25l");
char input{};
while( status == detail::Conclusion::Finished && !builder.asBool( "auto-next") &&
detail::util::compareAnd<std::not_equal_to<char>>( input = static_cast<char>( std::getchar()),
Q( KEY_QUIT), Q( KEY_RESTART), Q( KEY_NEXT), Q( KEY_REWIND)))
;
if( status == detail::Conclusion::Rewind || input == Q( KEY_REWIND))
{
begin = begin != g_begin ? begin - step
: builder.asBool( "wrap") ? g_end - step : begin;
continue;
}
else if( input == Q( KEY_RESTART))
continue;
else if( input == Q( KEY_QUIT))
exit( EXIT_SUCCESS);
begin = begin + step == g_end ? ( builder.asBool( "wrap") ? g_begin : begin) : begin + step;
}
}
else
{
fprintf( stderr, "Terminal not supported!");
std::getchar();
exit( 1);
}
}