Adding Features
Adding a New Feature to the Project
This guide provides a step-by-step approach to add and integrate a new feature into the project. Each feature should be implemented as a module under the features directory.
Project Structure
The features directory contains:
- Individual
featuremodules, each in its own folder. - A
features_handlermodule for managing and dispatching features.
Steps to Add a New Feature
-
Create a New Feature Directory
- Navigate to the features directory.
- Create a new folder for the feature. For example, for a feature named new_feature, create:
features/new_feature - Inside this folder, create the following:
- A
CMakeLists.txtfile for the feature. - A
new_feature.cppfile containing the feature implementation. - A include directory containing a header file, e.g.,
new_feature.hpp.
- A
-
Define the
FeatureClass- Define a new class that inherits from the Feature base class in
features_handler.hpp. - Implement the handle method for the feature's behavior.
- Example:
features/new_feature/include/new_feature.hpp
#pragma once #include "features_handler.hpp" #include "buttons.hpp" class NewFeature : public Feature { public: void handle(const Buttons& buttons) override; };- Example:
features/new_feature/new_feature.cpp
#include "new_feature.hpp" void NewFeature::handle(const Buttons& buttons) { // Implement the feature-specific behavior here } - Define a new class that inherits from the Feature base class in
-
Update the
FeatureType_eEnum- Open
features_handler.hpp. - Add a new entry for the feature to the
FeatureType_eenum. - Example:
enum class FeatureType_e{ CtrlCV, NewFeature, // Add your feature here None, }; - Open
-
Register the Feature in
FeaturesHandler- Open
features_handler.cpp. - Update the init method to register the new feature.
- Example:
void FeaturesHandler::initialize_featuresinit() { features[FeatureType_e::CtrlCV] = std::make_unique<CtrlCVFeature>(keys_config); features[FeatureType_e::NewFeature] = std::make_unique<NewFeature>(keys_config); } - Open
-
Integrate the Feature with the
Terminal- Open
terminal.cpp. - Update the handle_feature_cmd method to parse the new feature name and map it to
FeatureType. - Example:
bool Terminal::handle_feature_cmd(const std::vector<std::string>& params) { if (params.size() != 1) { add_log("Error: feature requires exactly 1 parameter"); return false; } const std::string& feature_name = params[0]; FeatureType_efeature; if (feature_name == "ctrl_c_v") { feature = FeatureType_e::CtrlCV; } else if (feature_name == "new_feature") { // Add parsing logic feature = FeatureType_e::NewFeature; } else { add_log("Error: Unknown feature"); return false; } add_log("Feature enabled: " + feature_name); // Logic to switch to the feature features_handler.switch_to_feature(feature); return true; } - Open
-
Create the Feature’s
CMakeLists.txt- Example:
features/new_feature/CMakeLists.txt
# Define the new feature module add_library(new_feature STATIC new_feature.cpp) # Include the necessary directories target_include_directories(new_feature PUBLIC include) # Link dependencies target_link_libraries(new_feature PUBLIC features_handler) - Example:
-
Update the Parent
CMakeLists.txt- Open
features/CMakeLists.txt. - Add the new feature module.
- Example:
add_subdirectory(ctrl_c_v) add_subdirectory(new_feature) # Add your new feature here - Open
-
Test the Feature
- Build the project using the top-level CMakeLists.txt.
- Test the new feature using the terminal interface by executing the command:
feature <feature_name> - Replace
with the feature name, e.g., new_feature.
-
Future Expansion
- To add more features, repeat the steps above for each new feature.
- Use the
FeatureType_eenum andFeaturesHandlerto manage feature-specific behavior efficiently.