diff --git a/.clang-format b/.clang-format index 41e0670..faaff93 100644 --- a/.clang-format +++ b/.clang-format @@ -1,2 +1,3 @@ Language: Cpp -BasedOnStyle: Google \ No newline at end of file +BasedOnStyle: Google +IndentWidth: 4 \ No newline at end of file diff --git a/Makefile b/Makefile index 9c6acde..2167bc7 100644 --- a/Makefile +++ b/Makefile @@ -35,4 +35,7 @@ deploy-linux: @$(MAKE) -C $(scripts_folder) deploy-linux build_options="$(build_options)" tests-ffi: - @$(MAKE) -C tests-ffi tests \ No newline at end of file + @$(MAKE) -C tests-ffi tests + +fmt: + @$(MAKE) -C tools fmt \ No newline at end of file diff --git a/src/front_end/about_widget.cxx b/src/front_end/about_widget.cxx index 0a22ace..412e1b5 100644 --- a/src/front_end/about_widget.cxx +++ b/src/front_end/about_widget.cxx @@ -17,19 +17,19 @@ using Self = AboutWidget; AboutWidget::AboutWidget() { show_about = make_unique(); } void Self::setup_event() { - const auto function = [this]() { - if (!about_window) { - about_window = make_unique(); - } + const auto function = [this]() { + if (!about_window) { + about_window = make_unique(); + } - about_window->show_window(); - }; + about_window->show_window(); + }; - QObject::connect(show_about.get(), &QPushButton::clicked, function); + QObject::connect(show_about.get(), &QPushButton::clicked, function); } void Self::setup_widget(QGridLayout *grid_layout) { - show_about->setText("About"); - this->setup_event(); - grid_layout->addWidget(show_about.get(), 1, 1); + show_about->setText("About"); + this->setup_event(); + grid_layout->addWidget(show_about.get(), 1, 1); } \ No newline at end of file diff --git a/src/front_end/about_window.cxx b/src/front_end/about_window.cxx index b73cad3..4cbd3ae 100644 --- a/src/front_end/about_window.cxx +++ b/src/front_end/about_window.cxx @@ -16,66 +16,67 @@ using std::make_unique; using Self = AboutWindow; AboutWindow::AboutWindow() { - about_window = make_unique(); - grid_layout = make_unique(); - github_button = make_unique(); - github_issue_button = make_unique(); - github_pull_button = make_unique(); + about_window = make_unique(); + grid_layout = make_unique(); + github_button = make_unique(); + github_issue_button = make_unique(); + github_pull_button = make_unique(); } void Self::is_open_browser_ok(OpenBrowserStatus status) { - switch (status) { - case OpenBrowserStatus::OK: - break; - - case OpenBrowserStatus::OPEN_BROWSER_FAILED: - QMessageBox::critical(about_window.get(), "Error", - "Could not open your browser"); - break; - - case OpenBrowserStatus::WRAP_RAW_C_FAILED: - QMessageBox::critical(about_window.get(), "Error", - "Could not wrap C raw to safety string"); - - break; - case OpenBrowserStatus::URL_IS_EMPTY: - QMessageBox::critical(about_window.get(), "Error", "Url is empty"); - break; - } + switch (status) { + case OpenBrowserStatus::OK: + break; + + case OpenBrowserStatus::OPEN_BROWSER_FAILED: + QMessageBox::critical(about_window.get(), "Error", + "Could not open your browser"); + break; + + case OpenBrowserStatus::WRAP_RAW_C_FAILED: + QMessageBox::critical(about_window.get(), "Error", + "Could not wrap C raw to safety string"); + + break; + case OpenBrowserStatus::URL_IS_EMPTY: + QMessageBox::critical(about_window.get(), "Error", "Url is empty"); + break; + } } void Self::open_browser_when_clicked(QPushButton *button, const char *url) { - const auto open_browser = [this, url] { - auto status = Lazyboard::ffi::open_browser(url); - is_open_browser_ok(status); - }; + const auto open_browser = [this, url] { + auto status = Lazyboard::ffi::open_browser(url); + is_open_browser_ok(status); + }; - QObject::connect(button, &QPushButton::clicked, open_browser); + QObject::connect(button, &QPushButton::clicked, open_browser); } void Self::setup_buttons() { - github_button->setText("GitHub | Source Code"); - github_issue_button->setText("Issue | Bug Report"); - github_pull_button->setText("Pull Request | Contribute"); - - this->open_browser_when_clicked(github_button.get(), GITHUB_URL); - this->open_browser_when_clicked(github_issue_button.get(), GITHUB_ISSUE_URL); - this->open_browser_when_clicked(github_pull_button.get(), GITHUB_PULL_URL); - - grid_layout->addWidget(github_button.get(), 0, 0); - grid_layout->addWidget(github_issue_button.get(), 0, 1); - grid_layout->addWidget(github_pull_button.get(), 1, 0); + github_button->setText("GitHub | Source Code"); + github_issue_button->setText("Issue | Bug Report"); + github_pull_button->setText("Pull Request | Contribute"); + + this->open_browser_when_clicked(github_button.get(), GITHUB_URL); + this->open_browser_when_clicked(github_issue_button.get(), + GITHUB_ISSUE_URL); + this->open_browser_when_clicked(github_pull_button.get(), GITHUB_PULL_URL); + + grid_layout->addWidget(github_button.get(), 0, 0); + grid_layout->addWidget(github_issue_button.get(), 0, 1); + grid_layout->addWidget(github_pull_button.get(), 1, 0); } void Self::setup_front_end() { - about_window->setLayout(this->grid_layout.get()); - this->setup_buttons(); + about_window->setLayout(this->grid_layout.get()); + this->setup_buttons(); } void Self::show_window() { - about_window->setMinimumSize(MIN_WIDTH, MIN_HEIGHT); - about_window->setWindowTitle("Lazyboard About"); + about_window->setMinimumSize(MIN_WIDTH, MIN_HEIGHT); + about_window->setWindowTitle("Lazyboard About"); - this->setup_front_end(); - about_window->exec(); + this->setup_front_end(); + about_window->exec(); } \ No newline at end of file diff --git a/src/front_end/main_window.cxx b/src/front_end/main_window.cxx index 177fe5f..18a023a 100644 --- a/src/front_end/main_window.cxx +++ b/src/front_end/main_window.cxx @@ -17,33 +17,33 @@ using std::make_unique; using Self = MainWindow; Self::MainWindow() { - main_window = make_unique(); - main_window_preload = make_unique(); - auto central_w = make_unique(); - auto layout_w = make_unique(central_w.get()); + main_window = make_unique(); + main_window_preload = make_unique(); + auto central_w = make_unique(); + auto layout_w = make_unique(central_w.get()); - main_window->setCentralWidget(central_w.get()); + main_window->setCentralWidget(central_w.get()); - central_widget = central_w.release(); - grid_layout = layout_w.release(); + central_widget = central_w.release(); + grid_layout = layout_w.release(); } Self *Self::init_main_window() { - main_window->setMinimumSize(MIN_WIDTH, MIN_HEIGHT); - main_window->setWindowTitle("Lazyboard"); - main_window_preload->create_default_config(main_window.get()); - this->front_end_show(); - main_window->show(); + main_window->setMinimumSize(MIN_WIDTH, MIN_HEIGHT); + main_window->setWindowTitle("Lazyboard"); + main_window_preload->create_default_config(main_window.get()); + this->front_end_show(); + main_window->show(); - return this; + return this; } void Self::front_end_show() { - table_widget = make_unique(); - setting_widget = make_unique(); - about_widget = make_unique(); + table_widget = make_unique(); + setting_widget = make_unique(); + about_widget = make_unique(); - table_widget->setup_widget(this->grid_layout); - setting_widget->setup_widget(this->grid_layout); - about_widget->setup_widget(this->grid_layout); + table_widget->setup_widget(this->grid_layout); + setting_widget->setup_widget(this->grid_layout); + about_widget->setup_widget(this->grid_layout); } \ No newline at end of file diff --git a/src/front_end/main_window_preload.cxx b/src/front_end/main_window_preload.cxx index 1c52cc5..9b92dc5 100644 --- a/src/front_end/main_window_preload.cxx +++ b/src/front_end/main_window_preload.cxx @@ -22,65 +22,65 @@ using std::stringstream; using namespace Lazyboard::ffi; void Self::on_error(WriteConfigStatus status, QMainWindow* main_window) { - switch (status) { - case WriteConfigStatus::OK: - break; - - case WriteConfigStatus::CREATE_DIR_FAILED: - QMessageBox::critical(main_window, "Error", - "Could not create config directory"); - break; - - case WriteConfigStatus::GET_DATA_LOCAL_FAILED: - QMessageBox::critical(main_window, "Error", - "Could not get application local data"); - break; - - case WriteConfigStatus::CREATE_FILE_FAILED: - QMessageBox::critical(main_window, "Error", - "Could not create configuration file"); - break; - - case WriteConfigStatus::WRITE_FILE_FAILED: - QMessageBox::critical(main_window, "Error", - "Could not write configuration"); - break; - - case WriteConfigStatus::TOML_TO_STRING_FAILED: - QMessageBox::critical(main_window, "Error", - "Could not convert TOML data to string"); - break; - } + switch (status) { + case WriteConfigStatus::OK: + break; + + case WriteConfigStatus::CREATE_DIR_FAILED: + QMessageBox::critical(main_window, "Error", + "Could not create config directory"); + break; + + case WriteConfigStatus::GET_DATA_LOCAL_FAILED: + QMessageBox::critical(main_window, "Error", + "Could not get application local data"); + break; + + case WriteConfigStatus::CREATE_FILE_FAILED: + QMessageBox::critical(main_window, "Error", + "Could not create configuration file"); + break; + + case WriteConfigStatus::WRITE_FILE_FAILED: + QMessageBox::critical(main_window, "Error", + "Could not write configuration"); + break; + + case WriteConfigStatus::TOML_TO_STRING_FAILED: + QMessageBox::critical(main_window, "Error", + "Could not convert TOML data to string"); + break; + } } void Self::create_default_config(QMainWindow* main_window) { - auto local_data_raw = ffi::local_data(); + auto local_data_raw = ffi::local_data(); - stringstream string_stream; - string_stream << local_data_raw << "/Lazyboard"; - string config_path = string_stream.str(); + stringstream string_stream; + string_stream << local_data_raw << "/Lazyboard"; + string config_path = string_stream.str(); - auto is_config_exists = ffi::is_exists_path(config_path.data()); - ffi::free_c_str(local_data_raw); + auto is_config_exists = ffi::is_exists_path(config_path.data()); + ffi::free_c_str(local_data_raw); - if (!is_config_exists) { - // clang-format off + if (!is_config_exists) { + // clang-format off #if defined (LAZY_DEBUG) auto q_string_value = QString::fromStdString(config_path); qDebug() << "Config path not found, generate at: " << q_string_value << "\n"; #endif - // clang-format on + // clang-format on - auto status = ffi::write_default_config(); - on_error(status, main_window); - } + auto status = ffi::write_default_config(); + on_error(status, main_window); + } - // clang-format off + // clang-format off #if defined (LAZY_DEBUG) auto q_string_value = QString::fromStdString(config_path); qDebug() << "Found config path at: " << q_string_value << "\n"; #endif - // clang-format on + // clang-format on } \ No newline at end of file diff --git a/src/front_end/setting_widget.cxx b/src/front_end/setting_widget.cxx index 0819dd4..befe4b0 100644 --- a/src/front_end/setting_widget.cxx +++ b/src/front_end/setting_widget.cxx @@ -12,7 +12,7 @@ using Self = SettingWidget; SettingWidget::SettingWidget() { open_setting = make_unique(); } void Self::setup_widget(QGridLayout *grid_layout) { - open_setting->setText("Settings"); + open_setting->setText("Settings"); - grid_layout->addWidget(open_setting.get(), 0, 1); + grid_layout->addWidget(open_setting.get(), 0, 1); } \ No newline at end of file diff --git a/src/front_end/table_widget.cxx b/src/front_end/table_widget.cxx index 7e3f79a..d7faa82 100644 --- a/src/front_end/table_widget.cxx +++ b/src/front_end/table_widget.cxx @@ -11,15 +11,16 @@ using std::make_unique; using Self = TableWidget; Self::TableWidget() noexcept { - table_widget = make_unique(); - header_labels = {"Time", "Content", "Type", "Pinned"}; + table_widget = make_unique(); + header_labels = {"Time", "Content", "Type", "Pinned"}; } void Self::setup_widget(QGridLayout* grid_layout) { - table_widget->setRowCount(DEFAULT_MAX_ROW); - table_widget->setColumnCount(4); - table_widget->setHorizontalHeaderLabels(header_labels); - table_widget->horizontalHeader()->setSectionResizeMode(QHeaderView::Stretch); + table_widget->setRowCount(DEFAULT_MAX_ROW); + table_widget->setColumnCount(4); + table_widget->setHorizontalHeaderLabels(header_labels); + table_widget->horizontalHeader()->setSectionResizeMode( + QHeaderView::Stretch); - grid_layout->addWidget(table_widget.get(), 0, 0); + grid_layout->addWidget(table_widget.get(), 0, 0); } \ No newline at end of file diff --git a/src/main.cxx b/src/main.cxx index ab3ce5c..42baa90 100644 --- a/src/main.cxx +++ b/src/main.cxx @@ -8,10 +8,11 @@ using std::make_unique; using std::unique_ptr; int main(int argc, char *argv[]) { - unique_ptr application = make_unique(argc, argv); - unique_ptr main_window = make_unique(); + unique_ptr application = + make_unique(argc, argv); + unique_ptr main_window = make_unique(); - main_window->init_main_window(); + main_window->init_main_window(); - return application->exec(); + return application->exec(); } \ No newline at end of file diff --git a/tests-ffi/config.cxx b/tests-ffi/config.cxx index 0184d46..11c2147 100644 --- a/tests-ffi/config.cxx +++ b/tests-ffi/config.cxx @@ -9,12 +9,12 @@ using std::string; using std::uint8_t; enum WriteConfigStatus : uint8_t { - OK, - TOML_TO_STRING_FAILED, - CREATE_DIR_FAILED, - CREATE_FILE_FAILED, - WRITE_FILE_FAILED, - GET_DATA_LOCAL_FAILED, + OK, + TOML_TO_STRING_FAILED, + CREATE_DIR_FAILED, + CREATE_FILE_FAILED, + WRITE_FILE_FAILED, + GET_DATA_LOCAL_FAILED, }; extern "C" char *raw_local_data(); @@ -22,23 +22,23 @@ extern "C" void raw_free_c_str(char *str); extern "C" WriteConfigStatus raw_write_default_config(); void gen_config_test() { - auto status = raw_write_default_config(); - - assert(status != WriteConfigStatus::WRITE_FILE_FAILED); - assert(status != WriteConfigStatus::CREATE_FILE_FAILED); - assert(status != WriteConfigStatus::CREATE_DIR_FAILED); - assert(status != WriteConfigStatus::GET_DATA_LOCAL_FAILED); - assert(status != WriteConfigStatus::TOML_TO_STRING_FAILED); - assert(status == WriteConfigStatus::OK); + auto status = raw_write_default_config(); + + assert(status != WriteConfigStatus::WRITE_FILE_FAILED); + assert(status != WriteConfigStatus::CREATE_FILE_FAILED); + assert(status != WriteConfigStatus::CREATE_DIR_FAILED); + assert(status != WriteConfigStatus::GET_DATA_LOCAL_FAILED); + assert(status != WriteConfigStatus::TOML_TO_STRING_FAILED); + assert(status == WriteConfigStatus::OK); } int main() { - auto raw_result = raw_local_data(); - auto result = string(raw_result); - raw_free_c_str(raw_result); + auto raw_result = raw_local_data(); + auto result = string(raw_result); + raw_free_c_str(raw_result); - gen_config_test(); - assert(!result.empty()); + gen_config_test(); + assert(!result.empty()); - return 0; + return 0; } \ No newline at end of file diff --git a/tests-ffi/fs.cxx b/tests-ffi/fs.cxx new file mode 100644 index 0000000..4e1b5ab --- /dev/null +++ b/tests-ffi/fs.cxx @@ -0,0 +1,13 @@ + +#include +extern "C" bool raw_is_path_exists(const char *path); + +int main() { + const char *valid_path = "../tests-ffi"; + const char *wrong_path = "../abcxyzw1133"; + + assert(raw_is_path_exists(valid_path)); + assert(!raw_is_path_exists(wrong_path)); + + return 0; +} \ No newline at end of file diff --git a/tests-ffi/tests.sh b/tests-ffi/tests.sh index 08dee4d..cfbb2da 100755 --- a/tests-ffi/tests.sh +++ b/tests-ffi/tests.sh @@ -43,14 +43,20 @@ function run_ffi_test() { fi for file in *.cxx; do + build_output=$(basename "$file" ".cxx") file_name=$(basename "$file") - $clang_cxx "$file" "$static_lib_path" -o "$build_dir/$file_name" + $clang_cxx "$file" "$static_lib_path" -o "$build_dir/$build_output" echo - echo -e "\e[0;32m[+] Test for $file_name:\e[0;37m" - ./"$build_dir/$file_name" + echo -e -n "\e[0;32m[+] Test for $file_name:\e[0;37m" - echo -e "\e[0;32m[+] Test for $file_name success with status code: $?\e[0;37m" + time_test=$( { + time ./"$build_dir/$build_output" + } ) + + status_code=$? + echo -e -n "$time_test" + echo -e "\e[0;32m[+] Test for $file_name success with status code: $status_code\e[0;37m" for _ in {1..25}; do printf "_" diff --git a/tools/Makefile b/tools/Makefile new file mode 100644 index 0000000..241e091 --- /dev/null +++ b/tools/Makefile @@ -0,0 +1,3 @@ + +fmt: + @./fmt.sh \ No newline at end of file diff --git a/tools/fmt.sh b/tools/fmt.sh new file mode 100755 index 0000000..b448864 --- /dev/null +++ b/tools/fmt.sh @@ -0,0 +1,42 @@ +#!/bin/bash +set -e + +current_dir=$(basename "$(pwd)") + +function check() { + local require_tool=$1 + + if ! command -v "$require_tool" >/dev/null 2>&1; then + echo "Could not find $require_tool in system" + exit + fi +} + +function fmt() { + local clang_fmt="clang-format" + check "$clang_fmt" + local tools_dir="tools" + local clang_fmt_cfg=".clang-format" + + if [[ "$tools_dir" == "$current_dir" ]]; then + cd .. || exit 1 + fi + + if [ ! -f "$clang_fmt_cfg" ]; then + echo "$clang_fmt_cfg not found" + exit 1 + fi + + find . -name "*.cxx" -o -name "*.hxx" | while IFS= read -r file; do + file_base_name=$(basename "$file") + + echo -e "\e[0;32m[+] Format file: $file_base_name\e[0;37m" + "$clang_fmt" -style=file "$clang_fmt_cfg" "$file" >/dev/null + done +} + +function main() { + fmt +} + +main \ No newline at end of file