Skip to content

Commit

Permalink
Add watch mode menu to runner
Browse files Browse the repository at this point in the history
  • Loading branch information
cegonse committed Nov 21, 2024
1 parent 91666a9 commit f968f04
Show file tree
Hide file tree
Showing 4 changed files with 115 additions and 2 deletions.
1 change: 1 addition & 0 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ set(RUNNER_SOURCES
${PROJECT_SOURCE_DIR}/runner/helpers.cpp
${PROJECT_SOURCE_DIR}/runner/runner.cpp
${PROJECT_SOURCE_DIR}/runner/cmd-args.cpp
${PROJECT_SOURCE_DIR}/runner/watch-mode.cpp
)

SET(RUNNER_OS_SOURCES
Expand Down
38 changes: 36 additions & 2 deletions runner/main.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
#include "runner.h"
#include "cmd-args.h"
#include "directory.h"
#include "watch-mode.h"
#include <iostream>

static void showHelp()
Expand All @@ -13,13 +14,46 @@ static void showHelp()
exit(0);
}

static int runTestsInPath(const std::string& path)
{
const auto executables = Directory::findExecutableFiles(path, "test_");
return Runner::runTests(executables);
}

int main(int argc, char *argv[])
{
CmdArgs args(argc, argv);

if (args.help())
showHelp();

const auto executables = Directory::findExecutableFiles(args.path(), "test_");
return Runner::runTests(executables);
auto status = runTestsInPath(args.path());

if (args.watch())
{
while (1)
{
auto option = WatchMode::Option::None;
WatchMode::showHelp();

do
{
option = WatchMode::waitForInput();
} while (option == WatchMode::Option::None);

switch (option)
{
case WatchMode::Option::Quit:
exit(0);
break;
case WatchMode::Option::Trigger:
runTestsInPath(args.path());
break;
default:
break;
}
}
}

return status;
}
61 changes: 61 additions & 0 deletions runner/watch-mode.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
#include "watch-mode.h"
#include <iostream>
#include <termios.h>
#include <unistd.h>

#define ASCII_GRAY "\u001b[38;5;243m"
#define ASCII_BLACK "\u001b[38;5;232m"
#define ASCII_BOLD "\u001b[1m"
#define ASCII_RESET "\033[0m"

static char waitForKey()
{
struct termios old_term, new_term;
tcgetattr(STDIN_FILENO, &old_term);

new_term = old_term;
new_term.c_lflag &= ~(ICANON | ECHO);

tcsetattr(STDIN_FILENO, TCSANOW, &new_term);
char ch = getchar();
tcsetattr(STDIN_FILENO, TCSANOW, &old_term);

return ch;
}

WatchMode::Option WatchMode::waitForInput()
{
auto key = waitForKey();

switch (key)
{
case 'f':
return Option::Failed;
break;
case 'p':
return Option::FileName;
break;
case 't':
return Option::TestName;
break;
case 'q':
return Option::Quit;
break;
case '\n':
return Option::Trigger;
break;
}

return Option::None;
}

void WatchMode::showHelp()
{
std::cout << std::endl;
std::cout << ASCII_BOLD << "Watch Usage" << ASCII_RESET << std::endl;
std::cout << ASCII_GRAY << " › Press " << ASCII_BLACK << "f" << ASCII_GRAY <<" to run only failed tests." << ASCII_RESET << std::endl;
std::cout << ASCII_GRAY << " › Press " << ASCII_BLACK << "p" << ASCII_GRAY <<" to filter by a filename regex pattern." << ASCII_RESET << std::endl;
std::cout << ASCII_GRAY << " › Press " << ASCII_BLACK << "t" << ASCII_GRAY <<" to filter by a test name regex pattern." << ASCII_RESET << std::endl;
std::cout << ASCII_GRAY << " › Press " << ASCII_BLACK << "q" << ASCII_GRAY <<" to quit watch mode." << ASCII_RESET << std::endl;
std::cout << ASCII_GRAY << " › Press " << ASCII_BLACK << "Enter" ASCII_GRAY << " to trigger a test run." << ASCII_RESET << std::endl;
}
17 changes: 17 additions & 0 deletions runner/watch-mode.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

namespace WatchMode
{
enum class Option
{
Failed,
FileName,
TestName,
Trigger,
Quit,
None
};

void showHelp();
Option waitForInput();
}

0 comments on commit f968f04

Please sign in to comment.