Add cli options for formatting
All checks were successful
Build ptprnt / build (push) Successful in 1m11s

This commit is contained in:
2023-10-19 21:29:09 +02:00
parent 4b96fcfac1
commit 368d8f3599
6 changed files with 88 additions and 26 deletions

2
.vscode/launch.json vendored
View File

@@ -9,7 +9,7 @@
"type": "cppdbg", "type": "cppdbg",
"request": "launch", "request": "launch",
"program": "${workspaceFolder}/builddir/ptprnt", "program": "${workspaceFolder}/builddir/ptprnt",
"args": ["--verbose"], "args": ["-t Hello"],
"stopAtEntry": false, "stopAtEntry": false,
"cwd": "${fileDirname}", "cwd": "${fileDirname}",
"environment": [], "environment": [],

View File

@@ -23,33 +23,42 @@
#include <spdlog/common.h> #include <spdlog/common.h>
#include <spdlog/spdlog.h> #include <spdlog/spdlog.h>
#include <functional>
#include "CLI/Option.hpp"
#include "P700Printer.hpp" #include "P700Printer.hpp"
#include "graphics/Bitmap.hpp" #include "graphics/Bitmap.hpp"
#include "libusbwrap/UsbDeviceFactory.hpp" #include "libusbwrap/UsbDeviceFactory.hpp"
namespace ptprnt {
PtouchPrint::PtouchPrint(const char* versionString) : mVersionString{versionString} {} PtouchPrint::PtouchPrint(const char* versionString) : mVersionString{versionString} {}
int PtouchPrint::init(int argc, char** argv) { int PtouchPrint::init(int argc, char** argv) {
setupCliParser(); setupCliParser();
if (mVerboseFlag) {
setupLogger(spdlog::level::debug);
} else {
setupLogger(spdlog::level::critical);
}
try { try {
mApp.parse(argc, argv); mApp.parse(argc, argv);
} catch (const CLI::ParseError& e) { } catch (const CLI::ParseError& e) {
return mApp.exit(e); mApp.exit(e);
return -1;
} }
mUsbDeviceFactory.init(); if (mVerboseFlag) {
setupLogger(spdlog::level::trace);
} else {
setupLogger(spdlog::level::critical);
}
if (!mUsbDeviceFactory.init()) {
spdlog::error("Could not initialize libusb");
return -1;
}
mCompatiblePrinters = {std::make_shared<ptprnt::printer::P700Printer>()}; mCompatiblePrinters = {std::make_shared<ptprnt::printer::P700Printer>()};
return 0; return 0;
} }
int PtouchPrint::run() { int PtouchPrint::run() {
auto numFoundPrinters = getCompatiblePrinters(); auto numFoundPrinters = getCompatiblePrinters();
if (numFoundPrinters == 0) { if (numFoundPrinters == 0) {
spdlog::error( spdlog::error(
@@ -71,9 +80,13 @@ int PtouchPrint::run() {
auto status = printer->getPrinterStatus(); auto status = printer->getPrinterStatus();
spdlog::info("Detected tape width is {}mm", status.tapeWidthMm); spdlog::info("Detected tape width is {}mm", status.tapeWidthMm);
auto bm = ptprnt::graphics::Bitmap<ptprnt::graphics::ALPHA8>(512, 128); auto bm = ptprnt::graphics::Bitmap<ptprnt::graphics::ALPHA8>(512, 128);
printer->printBitmap(bm); //printer->printBitmap(bm);
//printer->printText("wurst", 1); //printer->printText("wurst", 1);
for (auto& cmd : mCommands) {
std::cout << cmd.second << std::endl;
}
return 0; return 0;
} }
@@ -105,5 +118,39 @@ void PtouchPrint::setupLogger(spdlog::level::level_enum lvl) {
} }
void PtouchPrint::setupCliParser() { void PtouchPrint::setupCliParser() {
mApp.add_option("-v,--verbose", mVerboseFlag, "Enable verbose output"); auto printVersion = [this](std::size_t) {
} std::cout << "ptprnt version: " << mVersionString << std::endl;
};
mApp.add_flag("-v,--verbose", mVerboseFlag, "Enable verbose output");
mApp.add_flag("-V,--version", printVersion, "Prints the ptprnt's version");
mApp.add_option("-t,--text",
"Text to print (can be used multple times, use formatting options before to "
"influence text layout)")
->group("Printing")
->multi_option_policy(CLI::MultiOptionPolicy::TakeAll)
->trigger_on_parse()
->each([this](std::string text) { mCommands.emplace_back(CliCmdType::Text, text); });
mApp.add_option("-f,--font", "Font used for the following text occurences")
->group("Printing")
->multi_option_policy(CLI::MultiOptionPolicy::TakeAll)
->trigger_on_parse()
->each([this](std::string font) { mCommands.emplace_back(CliCmdType::Font, font); });
mApp.add_option("-s,--fontsize", "Font size of the following text occurences")
->group("Printing")
->multi_option_policy(CLI::MultiOptionPolicy::TakeAll)
->trigger_on_parse()
->each([this](std::string size) { mCommands.emplace_back(CliCmdType::FontSize, size); });
mApp.add_option("--valign", "Vertical alignment of the following text occurences")
->group("Printing")
->multi_option_policy(CLI::MultiOptionPolicy::TakeAll)
->trigger_on_parse()
->each([this](std::string valign) { mCommands.emplace_back(CliCmdType::VAlign, valign); });
mApp.add_option("--halign", "Vertical alignment of the following text occurences")
->group("Printing")
->multi_option_policy(CLI::MultiOptionPolicy::TakeAll)
->trigger_on_parse()
->each([this](std::string halign) { mCommands.emplace_back(CliCmdType::HAlign, halign); });
}
} // namespace ptprnt

View File

@@ -27,11 +27,21 @@
#include "interface/IPrinterDriver.hpp" #include "interface/IPrinterDriver.hpp"
#include "libusbwrap/UsbDeviceFactory.hpp" #include "libusbwrap/UsbDeviceFactory.hpp"
namespace ptprnt {
enum class CliCmdType { None = 0, Text = 1, FontSize = 2, Font = 3, VAlign = 4, HAlign = 5 };
using CliCmd = std::pair<CliCmdType, std::string>;
class PtouchPrint { class PtouchPrint {
public: public:
PtouchPrint(const char* versionString); PtouchPrint(const char* versionString);
~PtouchPrint() = default; ~PtouchPrint() = default;
// This is basically a singelton application class, no need to copy or move
PtouchPrint(const PtouchPrint&) = delete;
PtouchPrint& operator=(const PtouchPrint&) = delete;
PtouchPrint(PtouchPrint&&) = delete;
PtouchPrint& operator=(PtouchPrint&&) = delete;
int init(int argc, char** argv); int init(int argc, char** argv);
int run(); int run();
@@ -43,11 +53,13 @@ class PtouchPrint {
// member variables // member variables
CLI::App mApp{ptprnt::APP_DESC}; CLI::App mApp{ptprnt::APP_DESC};
libusbwrap::UsbDeviceFactory mUsbDeviceFactory; libusbwrap::UsbDeviceFactory mUsbDeviceFactory{};
std::vector<std::shared_ptr<ptprnt::IPrinterDriver>> mCompatiblePrinters; std::vector<std::shared_ptr<ptprnt::IPrinterDriver>> mCompatiblePrinters{};
std::vector<std::shared_ptr<ptprnt::IPrinterDriver>> mDetectedPrinters; std::vector<std::shared_ptr<ptprnt::IPrinterDriver>> mDetectedPrinters{};
std::vector<CliCmd> mCommands{};
std::string mVersionString = ""; std::string mVersionString = "";
// CLI flags // CLI flags
bool mVerboseFlag = false; bool mVerboseFlag = false;
}; };
} // namespace ptprnt

View File

@@ -29,11 +29,11 @@
#include "libusbwrap/interface/IUsbDevice.hpp" #include "libusbwrap/interface/IUsbDevice.hpp"
namespace libusbwrap { namespace libusbwrap {
UsbDeviceFactory::UsbDeviceFactory() {}
UsbDeviceFactory::~UsbDeviceFactory() { UsbDeviceFactory::~UsbDeviceFactory() {
// ToDo: SEGV here if (mDeviceListInitialized) {
libusb_free_device_list(mLibusbDeviceList, 0); libusb_free_device_list(mLibusbDeviceList, 1);
}
} }
std::vector<std::shared_ptr<IUsbDevice>> UsbDeviceFactory::findAllDevices() { std::vector<std::shared_ptr<IUsbDevice>> UsbDeviceFactory::findAllDevices() {
@@ -53,7 +53,7 @@ int UsbDeviceFactory::refreshDeviceList() {
} else if (ret == 0) { } else if (ret == 0) {
spdlog::warn("No USB devices found"); spdlog::warn("No USB devices found");
} }
mDeviceListInitialized = true;
return ret; return ret;
} }

View File

@@ -24,12 +24,14 @@
namespace libusbwrap { namespace libusbwrap {
class UsbDeviceFactory : public IUsbDeviceFactory { class UsbDeviceFactory : public IUsbDeviceFactory {
public: public:
UsbDeviceFactory(); UsbDeviceFactory() = default;
~UsbDeviceFactory(); virtual ~UsbDeviceFactory();
// delete copy ctor and assignment // delete copy ctor and assignment
UsbDeviceFactory(const UsbDeviceFactory&) = delete; UsbDeviceFactory(const UsbDeviceFactory&) = delete;
UsbDeviceFactory& operator=(UsbDeviceFactory&) = delete; UsbDeviceFactory& operator=(UsbDeviceFactory&) = delete;
UsbDeviceFactory(UsbDeviceFactory&&) = delete;
UsbDeviceFactory& operator=(UsbDeviceFactory&&) = delete;
bool init(); bool init();
/** /**
@@ -56,6 +58,7 @@ class UsbDeviceFactory : public IUsbDeviceFactory {
uint16_t pid); uint16_t pid);
// members // members
libusb_context* mLibusbCtx{nullptr}; libusb_context* mLibusbCtx{nullptr};
libusb_device** mLibusbDeviceList; libusb_device** mLibusbDeviceList{};
bool mDeviceListInitialized = false;
}; };
} // namespace libusbwrap } // namespace libusbwrap

View File

@@ -23,7 +23,7 @@
#include "PtouchPrint.hpp" #include "PtouchPrint.hpp"
int main(int argc, char** argv) { int main(int argc, char** argv) {
PtouchPrint ptouchprnt(PROJ_VERSION); ptprnt::PtouchPrint ptouchprnt(PROJ_VERSION);
int ret = ptouchprnt.init(argc, argv); int ret = ptouchprnt.init(argc, argv);
if (ret != 0) { if (ret != 0) {
return ret; return ret;