startgitStatic page generator for git repositories |
git clone git://git.dimitrijedobrota.com/startgit.git |
Log | Files | Refs | README | LICENSE | HACKING | CONTRIBUTING | CODE_OF_CONDUCT | BUILDING | |
commit | 7ceb9e7270b5dfefbbfdd5397ef024d2f57b7fdf |
parent | 8f1333f0167b066b50aad6151e81e05fbde8088d |
author | Dimitrije Dobrota <mail@dimitrijedobrota.com> |
date | Mon, 20 Jan 2025 17:48:38 +0100 |
Add file size in lines or bytes
Diffstat:M | CMakeLists.txt | | | +- |
M | source/file.cpp | | | +++++++++++++ |
M | source/file.hpp | | | +++ |
M | source/main.cpp | | | +++++--- |
4 files changed, 22 insertions(+), 4 deletions(-)
diff --git a/CMakeLists.txt b/CMakeLists.txt
@@ -4,7 +4,7 @@ include(cmake/prelude.cmake)
project(
startgit
VERSION 0.1.24
VERSION 0.1.25
DESCRIPTION "Static page generator for git repositories"
HOMEPAGE_URL "https://git.dimitrijedobrota.com/stargit.git"
LANGUAGES CXX
diff --git a/source/file.cpp b/source/file.cpp
@@ -1,3 +1,6 @@
#include <algorithm>
#include <span>
#include "file.hpp"
#include <git2wrap/repository.hpp>
@@ -30,4 +33,14 @@ git2wrap::object_size_t file::get_size() const
return m_blob.get_rawsize();
}
int file::get_lines() const
{
if (m_lines != -1) {
return m_lines;
}
const auto span = std::span<const char>(get_content(), get_size());
return m_lines = static_cast<int>(std::count(span.begin(), span.end(), '\n'));
}
} // namespace startgit
diff --git a/source/file.hpp b/source/file.hpp
@@ -20,11 +20,14 @@ public:
bool is_binary() const;
const char* get_content() const;
git2wrap::object_size_t get_size() const;
int get_lines() const;
private:
std::string m_filemode;
std::filesystem::path m_path;
git2wrap::blob m_blob;
mutable int m_lines = -1;
};
} // namespace startgit
diff --git a/source/main.cpp b/source/main.cpp
@@ -231,17 +231,19 @@ void write_files_table(std::ostream& ost, const startgit::branch& branch)
ost << html::tr()
.add(html::td("Mode"))
.add(html::td("Name"))
.add(html::td("File"));
.add(html::td("Size"));
ost << html::thead();
ost << html::tbody();
for (const auto& file : branch.get_files()) {
const auto url = std::format("./file/{}.html", file.get_path().string());
const auto size = file.is_binary() ? std::format("{}B", file.get_size())
: std::format("{}L", file.get_lines());
ost << html::tr()
.add(html::td(file.get_filemode()))
.add(html::td().add(html::a(file.get_path()).set("href", url)))
.add(html::td("0"));
.add(html::td(size));
}
ost << html::tbody();
@@ -684,7 +686,7 @@ int parse_opt(int key, const char* arg, poafloc::Parser* parser)
auto* l_args = static_cast<arguments_t*>(parser->input());
switch (key) {
case 'o':
l_args->output_dir = arg;
l_args->output_dir = std::filesystem::canonical(arg).string();
break;
case 'b':
l_args->base_url = arg;