Skip to content

Commit 7fac88f

Browse files
committed
Refactor log inspection patterns into shared static table
1 parent 4717a2c commit 7fac88f

1 file changed

Lines changed: 34 additions & 23 deletions

File tree

src/MainWindow.cpp

Lines changed: 34 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -113,6 +113,32 @@ struct EventItem {
113113

114114
constexpr int ASCII_BACKSPACE = 0x08;
115115

116+
namespace {
117+
enum LogInspectionIndex {
118+
ARE_YOU_SURE_YOU_WANT_TO_CONTINUE_CONNECTING,
119+
ENTER_PASSPHRASE,
120+
ENTER_PASSPHRASE_FOR_KEY,
121+
FATAL_AUTHENTICATION_FAILED_FOR,
122+
REMOTE_HOST_IDENTIFICATION_HAS_CHANGED,
123+
};
124+
struct LogInspectionItem {
125+
const char *pattern; // regex
126+
int index;
127+
};
128+
static const LogInspectionItem log_inspection_table[] = {
129+
{"Are you sure you want to continue connecting.*\\?"
130+
, ARE_YOU_SURE_YOU_WANT_TO_CONTINUE_CONNECTING},
131+
{"Enter passphrase: "
132+
, ENTER_PASSPHRASE},
133+
{"Enter passphrase for key '"
134+
, ENTER_PASSPHRASE_FOR_KEY},
135+
{"fatal: Authentication failed for '"
136+
, FATAL_AUTHENTICATION_FAILED_FOR},
137+
{"WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!"
138+
, REMOTE_HOST_IDENTIFICATION_HAS_CHANGED},
139+
};
140+
}
141+
116142
struct MainWindow::Private {
117143

118144
QString starting_dir;
@@ -6684,26 +6710,11 @@ void MainWindow::onLogIdle()
66846710
if (!interactionEnabled()) return;
66856711
if (interactionMode() != InteractionMode::None) return;
66866712

6687-
enum PatternIndex {
6688-
ARE_YOU_SURE_YOU_WANT_TO_CONTINUE_CONNECTING,
6689-
ENTER_PASSPHRASE,
6690-
ENTER_PASSPHRASE_FOR_KEY,
6691-
FATAL_AUTHENTICATION_FAILED_FOR,
6692-
REMOTE_HOST_IDENTIFICATION_HAS_CHANGED,
6693-
};
6694-
66956713
static std::map<int, QRegularExpression> patterns;
66966714
if (patterns.empty()) {
6697-
patterns[ARE_YOU_SURE_YOU_WANT_TO_CONTINUE_CONNECTING] = QRegularExpression(
6698-
"Are you sure you want to continue connecting.*\\?");
6699-
patterns[ENTER_PASSPHRASE] = QRegularExpression(
6700-
"Enter passphrase: ");
6701-
patterns[ENTER_PASSPHRASE_FOR_KEY] = QRegularExpression(
6702-
"Enter passphrase for key '");
6703-
patterns[FATAL_AUTHENTICATION_FAILED_FOR] = QRegularExpression(
6704-
"fatal: Authentication failed for '");
6705-
patterns[REMOTE_HOST_IDENTIFICATION_HAS_CHANGED] = QRegularExpression(
6706-
"WARNING: REMOTE HOST IDENTIFICATION HAS CHANGED!");
6715+
for (const auto &t : log_inspection_table) {
6716+
patterns[t.index] = QRegularExpression(t.pattern);
6717+
}
67076718
}
67086719

67096720
std::vector<std::string> lines = getLogHistoryLines(true);
@@ -6712,17 +6723,17 @@ void MainWindow::onLogIdle()
67126723
if (lines.empty()) return;
67136724

67146725

6715-
auto RegExp = [&](PatternIndex i){
6726+
auto RegExp = [&](LogInspectionIndex i){
67166727
auto it = patterns.find(i);
67176728
return it == patterns.end() ? QRegularExpression() : it->second;
67186729
};
67196730

6720-
auto Equals = [&](std::string const &line, PatternIndex i){
6731+
auto Equals = [&](std::string const &line, LogInspectionIndex i){
67216732
std::string const &str = RegExp(i).pattern().toStdString();
67226733
return line == str;
67236734
};
67246735

6725-
auto Contains = [&](PatternIndex i){
6736+
auto Contains = [&](LogInspectionIndex i){
67266737
std::string const &str = RegExp(i).pattern().toStdString();
67276738
for (std::string const &line : lines) {
67286739
if (strstr(line.c_str(), str.c_str())) {
@@ -6732,11 +6743,11 @@ void MainWindow::onLogIdle()
67326743
return false;
67336744
};
67346745

6735-
auto Match = [&](std::string const &line, PatternIndex i){
6746+
auto Match = [&](std::string const &line, LogInspectionIndex i){
67366747
return RegExp(i).match(QString::fromStdString(line)).hasMatch();
67376748
};
67386749

6739-
auto StartsWith = [&](std::string const &line, PatternIndex i){
6750+
auto StartsWith = [&](std::string const &line, LogInspectionIndex i){
67406751
std::string const &str = RegExp(i).pattern().toStdString();
67416752
char const *p = str.c_str();
67426753
char const *s = line.c_str();

0 commit comments

Comments
 (0)