Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

refactor: migrate to cpp 23 #48

Merged
merged 1 commit into from
May 5, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
11 changes: 11 additions & 0 deletions .clang-tidy
Original file line number Diff line number Diff line change
Expand Up @@ -12,3 +12,14 @@ Checks: "
WarningsAsErrors: '*'
HeaderFilterRegex: ''

CheckOptions:
- { key: readability-identifier-naming.StaticConstantPrefix, value: 'k' }
- { key: readability-identifier-naming.StaticConstantCase, value: 'CamelCase' }
- { key: readability-identifier-naming.ConstexprVariablePrefix, value: 'k' }
- { key: readability-identifier-naming.ConstexprVariableCase, value: 'CamelCase' }
- { key: readability-identifier-naming.PrivateMemberPrefix, value: 'm_' }
- { key: readability-identifier-naming.PrivateMemberCase, value: 'camelBack' }
- { key: readability-identifier-naming.VariableCase, value: 'camelBack' }
- { key: readability-identifier-naming.FunctionCase, value: 'camelBack' }
- { key: readability-identifier-naming.ClassCase, value: 'PascalCase' }
- { key: readability-identifier-naming.EnumCase, value: 'PascalCase' }
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
name: Build and test
name: PR check

on:
pull_request:
Expand Down Expand Up @@ -85,11 +85,11 @@ jobs:

- name: Configure CMake (Windows)
if: matrix.os == 'windows-latest'
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTS=On -DCMAKE_TOOLCHAIN_FILE=${{github.workspace}}/vcpkg/scripts/buildsystems/vcpkg.cmake
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=Release -DCAPS_LOG_BUILD_TESTS=On -DCMAKE_TOOLCHAIN_FILE=${{github.workspace}}/vcpkg/scripts/buildsystems/vcpkg.cmake

- name: Configure CMake (Windows)
if: matrix.os != 'windows-latest'
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTS=On
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=Release -DCAPS_LOG_BUILD_TESTS=On

- name: Build
run: cmake --build ${{github.workspace}}/build --config Release
Expand All @@ -98,32 +98,24 @@ jobs:
working-directory: ${{ github.workspace }}/build
run: ctest -C ${{env.BUILD_TYPE}} --output-on-failure


static-analysis:
runs-on: ubuntu-latest
steps:
- uses: actions/checkout@v3
- uses: actions/checkout@v4
- run: sudo apt-get update && sudo apt-get install libboost-program-options-dev libgit2-dev
- run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=Release -DCAPS_LOG_BUILD_TESTS=On
- uses: cpp-linter/cpp-linter-action@v2
id: linter
env:
GITHUB_TOKEN: ${{ secrets.GITHUB_TOKEN }}
with:
version: 19
database: 'build'
style: 'file' # Use .clang-format config file
tidy-checks: '' # Use .clang-tidy config file
# only 'update' a single comment in a pull request's thread.
thread-comments: ${{ github.event_name == 'pull_request' && 'update' }}
- name: Fail fast?!
if: steps.linter.outputs.checks-failed > 0
run: exit 1

- name: Prepare environment
run: sudo apt-get update && sudo apt-get install libboost-program-options-dev libgit2-dev clang-tidy

- name: Configure CMake
run: cmake -B ${{github.workspace}}/build -DCMAKE_BUILD_TYPE=Release -DBUILD_TESTS=Off

- name: Build
run: cmake --build ${{github.workspace}}/build --config Release

- name: Static Analysis
run: ln -s ./build/compile_commands.json ./ && run-clang-tidy ./source/

code-formatting:
runs-on: ubuntu-latest

steps:
- uses: actions/checkout@v3

- uses: DoozyX/clang-format-lint-action@v0.16.2
with:
source: 'source test'
extensions: 'hpp,cpp'
clangFormatVersion: 16
7 changes: 4 additions & 3 deletions CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,8 @@ project(caps-log
LANGUAGES CXX
)

set(CMAKE_CXX_STANDARD 17)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(CMAKE_EXPORT_COMPILE_COMMANDS ON)
set(CMAKE_MODULE_PATH ${CMAKE_MODULE_PATH} "${CMAKE_SOURCE_DIR}/cmake")

Expand Down Expand Up @@ -40,7 +41,7 @@ endif()
# ------------------------------- OpenSSL -------------------------------- #

set(OPENSSL_USE_STATIC_LIBS TRUE)
find_package(OpenSSL REQUIRED)
find_package(OpenSSL REQUIRED COMPONENTS Crypto)

# ------------------------------- LibGit2 -------------------------------- #

Expand All @@ -61,7 +62,7 @@ endif()

# --------------------------------- tests -------------------------------- #

if(${BUILD_TESTS})
if(${CAPS_LOG_BUILD_TESTS})
message("Tests will be built")
include(CTest)
add_subdirectory(./test)
Expand Down
10 changes: 5 additions & 5 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -80,10 +80,10 @@ Tags apply to the entire log entry file, not specific sections.

## Encrypting Logs

`Caps-Log` can encrypt your logs using the AES encryption algorithm. If no
password is provided at startup, the logs will be unreadable. If you edit or
create new logs for an encrypted repository, the final log will be encrypted. To
encrypt/decrypt a repository:
`Caps-Log` can encrypt your logs using the AES encryption algorithm.
If you open an encrypted log repo, you will be prompted to enter a password.
In case you enter a wrong password, caps-log will notify you end exit.
Same if you provide a password for a non encrypted log repository.

```
# Note: Caps-Log will ignore files not matching the log filename format.
Expand Down Expand Up @@ -182,7 +182,7 @@ by typing `caps-log` in your terminal.
If you wish to build and run the tests, execute:

```shell
mkdir build && cd build && cmake .. -DBUILD_TESTS=ON
mkdir build && cd build && cmake .. -DCAPS_LOG_BUILD_TESTS=ON
make
ctest
```
11 changes: 4 additions & 7 deletions source/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -1,15 +1,10 @@
configure_file(version.hpp.in version.hpp)

add_executable(
caps-log

config.cpp
config.hpp
main.cpp

./date/date.cpp
./date/date.hpp

./log/annual_log_data.cpp
./log/annual_log_data.hpp
./log/local_log_repository.cpp
Expand All @@ -22,7 +17,7 @@ add_executable(

./utils/crypto.cpp
./utils/crypto.hpp
./utils/map.hpp
./utils/date.hpp
./utils/string.hpp
./utils/task_executor.hpp
./utils/git_repo.cpp
Expand All @@ -42,9 +37,9 @@ add_executable(
./view/promptable.hpp
)


target_include_directories(caps-log PRIVATE ./ ${CMAKE_CURRENT_BINARY_DIR})


target_link_libraries(caps-log
ftxui::screen
ftxui::dom
Expand All @@ -56,4 +51,6 @@ target_link_libraries(caps-log
${LIBGIT2_LIBRARIES}
)

target_compile_definitions(caps-log PRIVATE CAPS_LOG_VERSION_STRING="${CAPS_LOG_VERSION}")

install(TARGETS caps-log DESTINATION "bin")
72 changes: 34 additions & 38 deletions source/app.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -2,21 +2,15 @@

#include "editor/editor_base.hpp"
#include "log/annual_log_data.hpp"
#include "log/log_file.hpp"
#include "log/log_repository_base.hpp"
#include "utils/git_repo.hpp"
#include "utils/string.hpp"
#include "utils/task_executor.hpp"
#include "view/annual_view.hpp"
#include "view/annual_view_base.hpp"
#include "view/input_handler.hpp"

#include <algorithm>
#include <cassert>
#include <functional>
#include <iostream>
#include <memory>
#include <string_view>
#include <thread>
#include <vector>

namespace caps_log {
Expand All @@ -26,7 +20,7 @@ using namespace log;
using namespace view;
using namespace utils;

inline static const std::string LOG_BASE_TEMPLATE{"# %d. %m. %y."};
static const std::string kLogBaseTemplate{"# %d. %m. %y."};

/**
* A class that has the interface of GitRepo but extends each method to provide callback
Expand All @@ -38,44 +32,45 @@ class AsyncGitRepo {
utils::ThreadedTaskExecutor m_taskExec;

public:
AsyncGitRepo(GitRepo repo) : m_repo{std::move(repo)} {}
explicit AsyncGitRepo(GitRepo repo) : m_repo{std::move(repo)} {}

void pull(std::function<void()> acallback) {
m_taskExec.Post([this, callback = std::move(acallback)] {
m_taskExec.post([this, callback = std::move(acallback)] {
m_repo.pull();
callback();
});
}

void push(std::function<void()> acallback) {
m_taskExec.Post([this, callback = std::move(acallback)]() {
m_taskExec.post([this, callback = std::move(acallback)]() {
m_repo.push();
callback();
});
}

void commitAll(std::function<void(bool)> acallback) {
m_taskExec.Post([this, callback = std::move(acallback)]() {
m_taskExec.post([this, callback = std::move(acallback)]() {
auto somethingCommited = m_repo.commitAll();
callback(somethingCommited);
});
}
};

class App : public InputHandlerBase {
unsigned m_displayedYear;
std::chrono::year m_displayedYear;
std::shared_ptr<AnnualViewBase> m_view;
std::shared_ptr<LogRepositoryBase> m_repo;
std::shared_ptr<EditorBase> m_editor;
AnnualLogData m_data;

std::vector<const YearMap<bool> *> m_tagMaps;
std::vector<const YearMap<bool> *> m_sectionMaps;
std::vector<const date::AnnualMap<bool> *> m_tagMaps;
std::vector<const date::AnnualMap<bool> *> m_sectionMaps;
bool m_skipFirstLine;

std::optional<AsyncGitRepo> m_gitRepo;

void updateViewSectionsAndTagsAfterLogChange(const Date &dateOfChangedLog) {
void
updateViewSectionsAndTagsAfterLogChange(const std::chrono::year_month_day &dateOfChangedLog) {
m_data.collect(m_repo, dateOfChangedLog, m_skipFirstLine);
m_view->setTagMenuItems(makeMenuTitles(m_data.tagMap));
m_view->setSectionMenuItems(makeMenuTitles(m_data.sectionMap));
Expand All @@ -95,9 +90,9 @@ class App : public InputHandlerBase {
App(std::shared_ptr<AnnualViewBase> view, std::shared_ptr<LogRepositoryBase> repo,
std::shared_ptr<EditorBase> editor, bool skipFirstLine = true,
std::optional<GitRepo> gitRepo = std::nullopt)
: m_displayedYear(Date::getToday().year), m_view{std::move(view)}, m_repo{std::move(repo)},
m_editor{std::move(editor)},
m_data{AnnualLogData::collect(m_repo, date::Date::getToday().year, skipFirstLine)},
: m_displayedYear(date::getToday().year()), m_view{std::move(view)},
m_repo{std::move(repo)}, m_editor{std::move(editor)},
m_data{AnnualLogData::collect(m_repo, date::getToday().year(), skipFirstLine)},
m_skipFirstLine{skipFirstLine} {
m_view->setInputHandler(this);
// if pass not prowided and repo is encrypted
Expand All @@ -113,21 +108,21 @@ class App : public InputHandlerBase {

bool handleInputEvent(const UIEvent &event) override {
switch (event.type) {
case UIEvent::ROOT_EVENT:
case UIEvent::RootEvent:
return handleRootEvent(event.input);
case UIEvent::UI_STARTED:
case UIEvent::UiStarted:
handleUiStarted();
break;
case UIEvent::FOCUSED_DATE_CHANGE:
case UIEvent::FocusedDateChange:
handleFocusedDateChange(event.input);
break;
case UIEvent::FOCUSED_TAG_CHANGE:
case UIEvent::FocusedTagChange:
handleFocusedTagChange(event.input);
break;
case UIEvent::FOCUSED_SECTION_CHANGE:
case UIEvent::FocusedSectionChange:
handleFocusedSectionChange(event.input);
break;
case UIEvent::CALENDAR_BUTTON_CLICK:
case UIEvent::CalendarButtonClick:
handleCalendarButtonClick();
break;
};
Expand Down Expand Up @@ -204,7 +199,7 @@ class App : public InputHandlerBase {
}

void displayYear(int diff) {
m_displayedYear += diff;
m_displayedYear = std::chrono::year{(int)m_displayedYear + diff};
m_data = AnnualLogData::collect(m_repo, m_displayedYear, m_skipFirstLine);
m_view->showCalendarForYear(m_displayedYear);
m_view->setHighlightedLogsMap(nullptr);
Expand All @@ -216,7 +211,7 @@ class App : public InputHandlerBase {
auto date = m_view->getFocusedDate();
auto log = m_repo->read(date);
if (not log.has_value()) {
m_repo->write({date, date.formatToString(LOG_BASE_TEMPLATE)});
m_repo->write({date, date::formatToString(date, kLogBaseTemplate)});
// this looks a little awkward, it's easy to forget to reread after write
log = m_repo->read(date);
}
Expand All @@ -233,11 +228,13 @@ class App : public InputHandlerBase {
});
}

static bool noMeaningfullContent(const std::string &content, const Date &date) {
return content == date.formatToString(LOG_BASE_TEMPLATE) || content.empty();
static bool noMeaningfullContent(const std::string &content,
const std::chrono::year_month_day &date) {
return content == date::formatToString(date, kLogBaseTemplate) || content.empty();
}

static const YearMap<bool> *findOrNull(const StringYearMap &map, const std::string &key) {
static const date::AnnualMap<bool> *findOrNull(const date::StringYearMap &map,
const std::string &key) {
auto result = map.find(key);
if (result == map.end()) {
return nullptr;
Expand All @@ -251,13 +248,13 @@ class App : public InputHandlerBase {
* map for said menu item is appropriate. Alternative would getting the string and looking up
* in the map.
*/
static void updatePointersForHighlightMaps(std::vector<const YearMap<bool> *> &vec,
const StringYearMap &map) {
static void updatePointersForHighlightMaps(std::vector<const date::AnnualMap<bool> *> &vec,
const date::StringYearMap &map) {
vec.clear();
vec.reserve(map.size());
vec.push_back(nullptr); // 0 index = no highlighted days
for (auto const &imap : map) {
vec.emplace_back(&imap.second);
for (auto const &[_, annual_map] : map) {
vec.emplace_back(&annual_map);
}
}

Expand All @@ -266,13 +263,12 @@ class App : public InputHandlerBase {
* title will be made of a key in the @p map and the number of days it has been
* mantioned (days set).
*/
static std::vector<std::string> makeMenuTitles(const StringYearMap &map) {
static std::vector<std::string> makeMenuTitles(const date::StringYearMap &map) {
std::vector<std::string> strs;
strs.reserve(map.size());
strs.push_back(" ----- ");
for (auto const &imap : map) {
strs.push_back(std::string{"("} + std::to_string(imap.second.daysSet()) + ") - " +
imap.first);
for (auto const &[str, annual_map] : map) {
strs.push_back(std::string{"("} + std::to_string(annual_map.daysSet()) + ") - " + str);
}
return std::move(strs);
}
Expand Down
Loading
Loading