29 Commits

Author SHA1 Message Date
5f673b7d57 Printing not flipped
All checks were successful
Build ptprnt / build (push) Successful in 3m37s
2025-10-12 21:46:24 +02:00
58287202d8 printing but flipped 2025-10-12 21:20:17 +02:00
ae22feed4f Add meson options 2025-10-12 12:56:29 +02:00
652e687fb0 Add a trace mode for usb tracing 2025-10-12 12:56:09 +02:00
bf7ff27b8d cleanup
All checks were successful
Build ptprnt / build (push) Successful in 3m25s
2025-10-11 18:37:27 +02:00
8658e5c9fd Fix merge
All checks were successful
Build ptprnt / build (push) Successful in 3m48s
2025-10-11 17:38:47 +02:00
99b355b033 Merge branch 'master' into generate-text-part-one 2025-10-11 17:34:31 +02:00
6a593f2a40 Remove unused interface methods
All checks were successful
Build ptprnt / build (push) Successful in 3m44s
2025-10-11 17:30:50 +02:00
59b3b34edc Fix the label corruption issue
All checks were successful
Build ptprnt / build (push) Successful in 4m43s
2025-10-11 17:04:55 +02:00
6e3a5bd12f Add fake printer for testing 2025-10-11 13:00:26 +02:00
0b8ff28a60 Start refactoring printers into own directory 2025-10-11 12:29:43 +02:00
3dc5da6fc8 Fix Monochrome class with new data structure, unit tests. There is work to be done still
All checks were successful
Build ptprnt / build (push) Successful in 3m51s
2025-09-11 10:02:43 +02:00
5132eab6fa Merge branch 'master' into generate-text-part-one
All checks were successful
Build ptprnt / build (push) Successful in 2m34s
2024-05-01 11:27:54 +02:00
77c6b7bc7b Fix for the old dependency code in the CI
All checks were successful
Build ptprnt / build (push) Successful in 2m31s
2024-04-28 20:49:10 +02:00
f702ec5473 Small Todos for the next session, some clean up
Some checks failed
Build ptprnt / build (push) Failing after 32s
2024-04-28 20:44:13 +02:00
59ef4189c4 Implement Label printing interface for PrinterDriver
Some checks failed
Build ptprnt / build (push) Failing after 32s
2024-04-28 20:02:07 +02:00
bb7ab6239d Implement basic layouting
Some checks failed
Build ptprnt / build (push) Failing after 37s
2024-04-28 17:38:56 +02:00
37ee7c10f1 c++ standard needs to be c++2a as c++20 is not possible with CI
All checks were successful
Build ptprnt / build (push) Successful in 1m23s
2024-04-20 14:03:51 +02:00
d98399949c Fix printer info issue
Some checks failed
Build ptprnt / build (push) Failing after 18s
2024-04-20 14:01:52 +02:00
a47a3189d3 Fix issues after rebase 2024-04-20 13:53:12 +02:00
6857de7b1f commands restructured to be const vectors 2024-04-20 13:39:28 +02:00
5a38600e2a Got it almost working... 2024-04-20 13:36:32 +02:00
1163ae5745 Removing files that shouldn't have been staged 2024-04-20 13:34:41 +02:00
09a2e621d6 Some side tracking fixing undefined behaviour and memory vulnurabilities 2024-04-20 13:34:11 +02:00
28308dccad Rename Image class to Label 2024-04-20 13:33:45 +02:00
5f5c0f0f97 Remove debug output as build pipeline does not have pango >=v1.5.0 2024-04-20 13:28:00 +02:00
4a59b50839 Fix typo 2024-04-20 13:28:00 +02:00
79477baecd Width is no automatically chosen based on image width 2024-04-20 13:28:00 +02:00
cf8492a714 A somewhat working state, but still struggeling with pangocairo 2024-04-20 13:28:00 +02:00
29 changed files with 42 additions and 479 deletions

View File

@@ -1,22 +1,3 @@
/*
ptrnt - print labels on linux
Copyright (C) 2024-2025 Moritz Martinius
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "PrinterDriverFactory.hpp"
#include <algorithm>

View File

@@ -1,26 +1,6 @@
/*
ptrnt - print labels on linux
Copyright (C) 2024-2025 Moritz Martinius
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include <memory>
#include <string>
#include <vector>
#include <string>
#include "interface/IPrinterDriver.hpp"
#include "libusbwrap/LibUsbTypes.hpp"
@@ -65,4 +45,4 @@ class PrinterDriverFactory {
private:
};
} // namespace ptprnt
}

View File

@@ -1,6 +1,6 @@
/*
ptrnt - print labels on linux
Copyright (C) 2023-2025 Moritz Martinius
Copyright (C) 2023 Moritz Martinius
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
@@ -37,7 +37,7 @@
#include "CLI/Option.hpp"
#include "PrinterDriverFactory.hpp"
#include "graphics/LabelBuilder.hpp"
#include "graphics/Label.hpp"
#include "graphics/interface/ILabel.hpp"
#include "libusbwrap/UsbDeviceFactory.hpp"
@@ -162,21 +162,25 @@ int PtouchPrint::run() {
return 0;
}
// Use LabelBuilder to construct the label
graphics::LabelBuilder labelBuilder(printer->getPrinterInfo().pixelLines);
auto label = std::make_unique<graphics::Label>(printer->getPrinterInfo().pixelLines);
std::string labelText{};
// TODO: refactor
for (const auto& [cmd, value] : mCommands) {
switch (cmd) {
case CliCmdType::Text:
labelBuilder.addText(value);
if (labelText.empty()) {
labelText = value;
} else {
labelText = labelText + '\n' + value;
}
break;
case CliCmdType::Font:
spdlog::debug("Setting font to {}", value);
labelBuilder.setFontFamily(value);
label->setFontFamily(value);
break;
case CliCmdType::FontSize:
spdlog::debug("Setting font size to {}", std::stod(value));
labelBuilder.setFontSize(std::stod(value));
label->setFontSize(std::stod(value));
break;
case CliCmdType::HAlign:
spdlog::debug("Setting text horizontal alignment to {}", value);
@@ -184,9 +188,9 @@ int PtouchPrint::run() {
auto hPos = HALignPositionMap.find(value);
if (hPos == HALignPositionMap.end()) {
spdlog::warn("Invalid horizontal alignment specified!");
labelBuilder.setHAlign(HAlignPosition::UNKNOWN);
label->setHAlign(HAlignPosition::UNKNOWN);
} else {
labelBuilder.setHAlign(hPos->second);
label->setHAlign(hPos->second);
}
}
break;
@@ -195,10 +199,10 @@ int PtouchPrint::run() {
{
auto vPos = VALignPositionMap.find(value);
if (vPos == VALignPositionMap.end()) {
spdlog::warn("Invalid vertical alignment specified!");
labelBuilder.setVAlign(VAlignPosition::UNKNOWN);
spdlog::warn("Invalid verical alignment specified!");
label->setVAlign(VAlignPosition::UNKNOWN);
} else {
labelBuilder.setVAlign(vPos->second);
label->setVAlign(vPos->second);
}
}
break;
@@ -209,8 +213,8 @@ int PtouchPrint::run() {
break;
}
}
auto label = labelBuilder.build();
label->create(labelText);
label->writeToPng("./testlabel.png");
if (!printer->printLabel(std::move(label))) {
spdlog::error("An error occured while printing");
return -1;

View File

@@ -1,6 +1,6 @@
/*
ptrnt - print labels on linux
Copyright (C) 2023-2025 Moritz Martinius
Copyright (C) 2023 Moritz Martinius
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by

View File

@@ -1,6 +1,6 @@
/*
ptrnt - print labels on linux
Copyright (C) 2023-2025 Moritz Martinius
Copyright (C) 2023 Moritz Martinius
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by

View File

@@ -1,6 +1,6 @@
/*
ptrnt - print labels on linux
Copyright (C) 2023-2025 Moritz Martinius
Copyright (C) 2023 Moritz Martinius
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by

View File

@@ -1,6 +1,6 @@
/*
ptrnt - print labels on linux
Copyright (C) 2023-2025 Moritz Martinius
Copyright (C) 2023 Moritz Martinius
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by

View File

@@ -1,6 +1,6 @@
/*
ptrnt - print labels on linux
Copyright (C) 2023-2025 Moritz Martinius
Copyright (C) 2023 Moritz Martinius
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by

View File

@@ -1,95 +0,0 @@
/*
ptrnt - print labels on linux
Copyright (C) 2025 Moritz Martinius
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#include "LabelBuilder.hpp"
#include <spdlog/spdlog.h>
#include "Label.hpp"
namespace ptprnt::graphics {
LabelBuilder::LabelBuilder(int printerHeight) : mPrinterHeight(printerHeight) {
reset();
}
ILabelBuilder& LabelBuilder::addText(const std::string& text) {
if (!text.empty()) {
if (!mAccumulatedText.empty()) {
// Add a newline if the label already has some text accumulated
mAccumulatedText += '\n';
}
mAccumulatedText += text;
spdlog::debug("LabelBuilder: Added text '{}', total length: {}", text, mAccumulatedText.length());
}
return *this;
}
ILabelBuilder& LabelBuilder::setFontFamily(const std::string& fontFamily) {
mCurrentFontFamily = fontFamily;
spdlog::debug("LabelBuilder: Set font family to '{}'", fontFamily);
return *this;
}
ILabelBuilder& LabelBuilder::setFontSize(double fontSize) {
mCurrentFontSize = fontSize;
spdlog::debug("LabelBuilder: Set font size to {}", fontSize);
return *this;
}
ILabelBuilder& LabelBuilder::setHAlign(HAlignPosition hAlign) {
mCurrentHAlign = hAlign;
spdlog::debug("LabelBuilder: Set horizontal alignment to {}", static_cast<int>(hAlign));
return *this;
}
ILabelBuilder& LabelBuilder::setVAlign(VAlignPosition vAlign) {
mCurrentVAlign = vAlign;
spdlog::debug("LabelBuilder: Set vertical alignment to {}", static_cast<int>(vAlign));
return *this;
}
std::unique_ptr<ILabel> LabelBuilder::build() {
spdlog::debug("LabelBuilder: Building label with text: '{}'", mAccumulatedText);
auto label = std::make_unique<Label>(mPrinterHeight);
// Apply current formatting settings
label->setFontFamily(mCurrentFontFamily);
label->setFontSize(mCurrentFontSize);
label->setHAlign(mCurrentHAlign);
label->setVAlign(mCurrentVAlign);
// Create the label with accumulated text
label->create(mAccumulatedText);
return label;
}
ILabelBuilder& LabelBuilder::reset() {
mAccumulatedText.clear();
mCurrentFontFamily = DEFAULT_FONT_FAMILY;
mCurrentFontSize = DEFAULT_FONT_SIZE;
mCurrentHAlign = HAlignPosition::LEFT;
mCurrentVAlign = VAlignPosition::MIDDLE;
spdlog::debug("LabelBuilder: Reset to default state");
return *this;
}
} // namespace ptprnt::graphics

View File

@@ -1,64 +0,0 @@
/*
ptrnt - print labels on linux
Copyright (C) 2025 Moritz Martinius
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <memory>
#include <string>
#include <vector>
#include "interface/ILabel.hpp"
#include "interface/ILabelBuilder.hpp"
namespace ptprnt::graphics {
/**
* @brief Concrete implementation of ILabelBuilder
*
* Builds labels by accumulating text segments with formatting options,
* then creates a Label instance with all the collected content.
*/
class LabelBuilder : public ILabelBuilder {
public:
/**
* @brief Construct a LabelBuilder for a specific printer height
* @param printerHeight Height of the printer in pixels (tape width)
*/
explicit LabelBuilder(int printerHeight);
~LabelBuilder() override = default;
ILabelBuilder& addText(const std::string& text) override;
ILabelBuilder& setFontFamily(const std::string& fontFamily) override;
ILabelBuilder& setFontSize(double fontSize) override;
ILabelBuilder& setHAlign(HAlignPosition hAlign) override;
ILabelBuilder& setVAlign(VAlignPosition vAlign) override;
std::unique_ptr<ILabel> build() override;
ILabelBuilder& reset() override;
private:
int mPrinterHeight;
std::string mAccumulatedText;
std::string mCurrentFontFamily{DEFAULT_FONT_FAMILY};
double mCurrentFontSize{DEFAULT_FONT_SIZE};
HAlignPosition mCurrentHAlign{HAlignPosition::LEFT};
VAlignPosition mCurrentVAlign{VAlignPosition::MIDDLE};
};
} // namespace ptprnt::graphics

View File

@@ -1,6 +1,6 @@
/*
ptrnt - print labels on linux
Copyright (C) 2023-2025 Moritz Martinius
Copyright (C) 2023 Moritz Martinius
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by

View File

@@ -1,6 +1,6 @@
/*
ptrnt - print labels on linux
Copyright (C) 2023-2025 Moritz Martinius
Copyright (C) 2023 Moritz Martinius
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by

View File

@@ -1,6 +1,6 @@
/*
ptrnt - print labels on linux
Copyright (C) 2024-2025 Moritz Martinius
Copyright (C) 2023 Moritz Martinius
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by

View File

@@ -1,88 +0,0 @@
/*
ptrnt - print labels on linux
Copyright (C) 2025 Moritz Martinius
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <memory>
#include <string>
#include "ILabel.hpp"
namespace ptprnt::graphics {
/**
* @brief Builder interface for creating labels with text and formatting options
*
* The LabelBuilder provides a fluent API for constructing labels with various
* text elements, fonts, sizes, and alignment options. It separates the construction
* logic from the label rendering logic, making it easier to test and maintain.
*/
class ILabelBuilder {
public:
virtual ~ILabelBuilder() = default;
/**
* @brief Add text to the label with current formatting settings
* @param text The text to add
* @return Reference to this builder for method chaining
*/
virtual ILabelBuilder& addText(const std::string& text) = 0;
/**
* @brief Set the font family for subsequent text additions
* @param fontFamily Font family name (e.g., "sans", "serif", "monospace")
* @return Reference to this builder for method chaining
*/
virtual ILabelBuilder& setFontFamily(const std::string& fontFamily) = 0;
/**
* @brief Set the font size for subsequent text additions
* @param fontSize Font size in points
* @return Reference to this builder for method chaining
*/
virtual ILabelBuilder& setFontSize(double fontSize) = 0;
/**
* @brief Set horizontal alignment for subsequent text additions
* @param hAlign Horizontal alignment position
* @return Reference to this builder for method chaining
*/
virtual ILabelBuilder& setHAlign(HAlignPosition hAlign) = 0;
/**
* @brief Set vertical alignment for subsequent text additions
* @param vAlign Vertical alignment position
* @return Reference to this builder for method chaining
*/
virtual ILabelBuilder& setVAlign(VAlignPosition vAlign) = 0;
/**
* @brief Build and return the label with all added content
* @return Unique pointer to the constructed label
*/
virtual std::unique_ptr<ILabel> build() = 0;
/**
* @brief Reset the builder to initial state
* @return Reference to this builder for method chaining
*/
virtual ILabelBuilder& reset() = 0;
};
} // namespace ptprnt::graphics

View File

@@ -1,6 +1,6 @@
/*
ptrnt - print labels on linux
Copyright (C) 2023-2025 Moritz Martinius
Copyright (C) 2023 Moritz Martinius
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by

View File

@@ -1,6 +1,6 @@
/*
ptrnt - print labels on linux
Copyright (C) 2023-2024 Moritz Martinius
Copyright (C) 2023 Moritz Martinius
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by

View File

@@ -1,6 +1,6 @@
/*
ptrnt - print labels on linux
Copyright (C) 2023-2024 Moritz Martinius
Copyright (C) 2023 Moritz Martinius
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by

View File

@@ -1,6 +1,6 @@
/*
ptrnt - print labels on linux
Copyright (C) 2023-2024 Moritz Martinius
Copyright (C) 2023 Moritz Martinius
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by

View File

@@ -1,6 +1,6 @@
/*
ptrnt - print labels on linux
Copyright (C) 2023-2024 Moritz Martinius
Copyright (C) 2023 Moritz Martinius
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by

View File

@@ -1,6 +1,6 @@
/*
ptrnt - print labels on linux
Copyright (C) 2023-2025 Moritz Martinius
Copyright (C) 2023 Moritz Martinius
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by

View File

@@ -1,22 +1,3 @@
/*
ptrnt - print labels on linux
Copyright (C) 2023-2024 Moritz Martinius
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <sys/types.h>

View File

@@ -1,22 +1,3 @@
/*
ptrnt - print labels on linux
Copyright (C) 2023 Moritz Martinius
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with this program. If not, see <https://www.gnu.org/licenses/>.
*/
#pragma once
#include <cstdint>
@@ -28,8 +9,8 @@
namespace libusbwrap {
class IUsbDeviceFactory {
public:
virtual ~IUsbDeviceFactory() = default;
virtual std::vector<std::unique_ptr<IUsbDevice>> findAllDevices() = 0;
virtual ~IUsbDeviceFactory() = default;
virtual std::vector<std::unique_ptr<IUsbDevice>> findAllDevices() = 0;
virtual std::vector<std::unique_ptr<IUsbDevice>> findDevices(uint16_t vid, uint16_t pid) = 0;
};
} // namespace libusbwrap

View File

@@ -1,6 +1,6 @@
/*
ptrnt - print labels on linux
Copyright (C) 2022-2023 Moritz Martinius
Copyright (C) 2023 Moritz Martinius
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by

View File

@@ -12,7 +12,6 @@ ptprnt_hpps = files (
'PrinterDriverFactory.hpp',
'graphics/Bitmap.hpp',
'graphics/Label.hpp',
'graphics/LabelBuilder.hpp',
'graphics/Monochrome.hpp'
)
@@ -22,7 +21,6 @@ ptprnt_srcs = files (
'printers/P700Printer.cpp',
'printers/FakePrinter.cpp',
'graphics/Label.cpp',
'graphics/LabelBuilder.cpp',
'graphics/Bitmap.cpp',
'graphics/Monochrome.cpp',
'libusbwrap/UsbDeviceFactory.cpp',

View File

@@ -1,6 +1,6 @@
/*
ptrnt - print labels on linux
Copyright (C) 2025 Moritz Martinius
Copyright (C) 2023 Moritz Martinius
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by

View File

@@ -1,6 +1,6 @@
/*
ptrnt - print labels on linux
Copyright (C) 2025 Moritz Martinius
Copyright (C) 2023 Moritz Martinius
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by

View File

@@ -1,6 +1,6 @@
/*
ptrnt - print labels on linux
Copyright (C) 2025 Moritz Martinius
Copyright (C) 2023 Moritz Martinius
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by

View File

@@ -1,6 +1,6 @@
/*
ptrnt - print labels on linux
Copyright (C) 2025 Moritz Martinius
Copyright (C) 2023 Moritz Martinius
This program is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by

View File

@@ -1,115 +0,0 @@
#!/bin/bash
# update_copyright.sh
# Updates copyright year ranges in a source file based on git history
#
# Usage: ./update_copyright.sh [--dry-run] <file>
#
# Examples:
# # Update a single file
# ./update_copyright.sh src/main.cpp
#
# # Dry-run on a single file
# ./update_copyright.sh --dry-run src/main.cpp
#
# # Update all C++ files using find -exec
# find src \( -name "*.cpp" -o -name "*.hpp" \) -exec ./update_copyright.sh {} \;
#
# # Dry-run on all C++ files
# find src \( -name "*.cpp" -o -name "*.hpp" \) -exec ./update_copyright.sh --dry-run {} \;
set -e
# Check for dry-run flag
DRY_RUN=false
FILE=""
if [ "$1" = "--dry-run" ]; then
DRY_RUN=true
FILE="$2"
elif [ "$1" = "--help" ] || [ "$1" = "-h" ]; then
grep "^#" "$0" | sed 's/^# \?//'
exit 0
else
FILE="$1"
fi
# Check if file argument provided
if [ -z "$FILE" ]; then
echo "Error: No file specified"
echo "Usage: $0 [--dry-run] <file>"
echo "Run '$0 --help' for more information"
exit 1
fi
# Check if file exists
if [ ! -f "$FILE" ]; then
echo "Error: File not found: $FILE"
exit 1
fi
# Get the repository root
REPO_ROOT=$(git rev-parse --show-toplevel)
cd "$REPO_ROOT"
# Copyright holder name
COPYRIGHT_HOLDER="Moritz Martinius"
# Function to get first and last commit years for a file
get_years_for_file() {
local file="$1"
# Get the year of the first commit that touched this file
first_year=$(git log --follow --format=%ad --date=format:%Y --reverse "$file" 2>/dev/null | head -1)
# Get the year of the last commit that touched this file
last_year=$(git log --follow --format=%ad --date=format:%Y -1 "$file" 2>/dev/null)
# If file is not in git, use current year
if [ -z "$first_year" ]; then
first_year=$(date +%Y)
last_year=$(date +%Y)
fi
echo "$first_year $last_year"
}
# Get years from git history
read -r first_year last_year <<< "$(get_years_for_file "$FILE")"
# Determine the copyright year string
if [ "$first_year" = "$last_year" ]; then
year_string="$first_year"
else
year_string="$first_year-$last_year"
fi
# Check if file has a copyright notice
if ! grep -q "Copyright (C)" "$FILE"; then
echo "No copyright notice found in $FILE, skipping"
exit 0
fi
if [ "$DRY_RUN" = true ]; then
# Just show what would be changed
current_year=$(grep "Copyright (C)" "$FILE" | sed -n 's/.*Copyright (C) \([0-9]\{4\}\(-[0-9]\{4\}\)\?\).*/\1/p' | head -1)
if [ "$current_year" != "$year_string" ]; then
echo "Would update $FILE: $current_year$year_string"
else
echo "No change needed for $FILE (already $year_string)"
fi
else
# Update the copyright line
# Matches patterns like "Copyright (C) 2023 Moritz Martinius" or "Copyright (C) 2023-2024 Moritz Martinius"
# Handle variable whitespace between year and name
# Get current year from file for comparison
current_year=$(grep "Copyright (C)" "$FILE" | sed -n 's/.*Copyright (C) \([0-9]\{4\}\(-[0-9]\{4\}\)\?\).*/\1/p' | head -1)
if [ "$current_year" = "$year_string" ]; then
echo "No changes needed for $FILE (already $year_string)"
else
sed -i "s/Copyright (C) [0-9]\{4\}\(-[0-9]\{4\}\)\? \+$COPYRIGHT_HOLDER/Copyright (C) $year_string $COPYRIGHT_HOLDER/" "$FILE"
echo "✓ Updated $FILE: $current_year$year_string"
fi
fi