Skip to content

Commit

Permalink
Add printer (#18)
Browse files Browse the repository at this point in the history
  • Loading branch information
antonioborondo authored Dec 30, 2023
1 parent 367daed commit 01f804a
Show file tree
Hide file tree
Showing 7 changed files with 79 additions and 10 deletions.
5 changes: 4 additions & 1 deletion CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ set(CMAKE_CXX_STANDARD 17)

add_library(time_tracker_common STATIC
src/db.cc
src/printer.cc
src/record.cc
src/timestamp.cc
src/type.cc
Expand All @@ -21,8 +22,9 @@ find_package(SQLite3 REQUIRED)

target_link_libraries(time_tracker_common
PRIVATE
fmt::fmt
SQLite::SQLite3
PUBLIC
fmt::fmt
)

add_executable(time_tracker
Expand All @@ -39,6 +41,7 @@ target_link_libraries(time_tracker

add_executable(time_tracker_test
test/db_test.cc
test/printer_test.cc
test/record_test.cc
test/timestamp_test.cc
test/type_test.cc
Expand Down
12 changes: 3 additions & 9 deletions src/main.cc
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
#include "db.h"
#include "printer.h"
#include "record.h"
#include "type.h"

Expand Down Expand Up @@ -27,6 +28,7 @@ int main(int argc, char** argv)
po::store(po::parse_command_line(argc, argv, options_description), variables_map);

Db db;
Printer printer;
if(variables_map.count("start"))
{
const Record record{Type::kStart};
Expand All @@ -41,15 +43,7 @@ int main(int argc, char** argv)
}
else if(variables_map.count("summary"))
{
std::cout << "+-------+---------------------+\n";
std::cout << "| Type | Timestamp |\n";
std::cout << "+-------+---------------------+\n";
auto records{db.GetRecords()};
for(const auto& record: records)
{
std::cout << "| " << record.GetType() << " | " << record.GetTimestamp() << " |\n";
}
std::cout << "+-------+---------------------+\n";
printer.PrintRecords(db.GetRecords());

std::cout << "\nTotal time: " << db.Summary() << "\n";
}
Expand Down
26 changes: 26 additions & 0 deletions src/printer.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,26 @@
#include "printer.h"

#include "record.h"
#include "timestamp.h"
#include "type.h"

#include <fmt/format.h>

Printer::Printer(std::ostream& sink):
sink_{sink}
{
}

void Printer::PrintRecords(const std::vector<Record> records)
{
sink_ << "+-------+---------------------+\n";
sink_ << "| Type | Timestamp |\n";
sink_ << "+-------+---------------------+\n";

for(const auto& record: records)
{
sink_ << fmt::format("| {:<5} | {:<19} |\n", record.GetType(), record.GetTimestamp());
}

sink_ << "+-------+---------------------+\n";
}
17 changes: 17 additions & 0 deletions src/printer.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#pragma once

#include "record.h"

#include <iostream>
#include <ostream>
#include <vector>

class Printer
{
std::ostream& sink_;

public:
Printer(std::ostream& sink = std::cout);

void PrintRecords(const std::vector<Record> records);
};
6 changes: 6 additions & 0 deletions src/timestamp.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <fmt/ostream.h>

#include <ostream>
#include <string>

Expand Down Expand Up @@ -27,3 +29,7 @@ bool operator==(const Timestamp& lhs, const Timestamp& rhs);
bool operator!=(const Timestamp& lhs, const Timestamp& rhs);

std::ostream& operator<<(std::ostream& os, const Timestamp& timestamp);

template<>
struct fmt::formatter<Timestamp>: ostream_formatter
{};
6 changes: 6 additions & 0 deletions src/type.h
Original file line number Diff line number Diff line change
@@ -1,5 +1,7 @@
#pragma once

#include <fmt/ostream.h>

#include <ostream>

enum class Type
Expand All @@ -9,3 +11,7 @@ enum class Type
};

std::ostream& operator<<(std::ostream& os, const Type& type);

template<>
struct fmt::formatter<Type>: ostream_formatter
{};
17 changes: 17 additions & 0 deletions test/printer_test.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
#include "printer.h"

#include <gtest/gtest.h>

#include <sstream>

TEST(PrinterTest, PrintNoRecords)
{
std::ostringstream sink;
Printer printer{sink};

std::vector<Record> records;

printer.PrintRecords(records);

ASSERT_EQ("+-------+---------------------+\n| Type | Timestamp |\n+-------+---------------------+\n+-------+---------------------+\n", sink.str());
}

0 comments on commit 01f804a

Please sign in to comment.