From f6bbfd7283aa0529ea278cd966eacaa34c96ab19 Mon Sep 17 00:00:00 2001 From: WerWolv Date: Fri, 21 Jul 2023 15:17:21 +0200 Subject: [PATCH] impr: Properly print new line characters in cli --- lib/libimhex/include/hex/helpers/fmt.hpp | 5 ++ .../source/content/command_line_interface.cpp | 62 +++++++++---------- 2 files changed, 36 insertions(+), 31 deletions(-) diff --git a/lib/libimhex/include/hex/helpers/fmt.hpp b/lib/libimhex/include/hex/helpers/fmt.hpp index df87ca2e6..734c167fc 100644 --- a/lib/libimhex/include/hex/helpers/fmt.hpp +++ b/lib/libimhex/include/hex/helpers/fmt.hpp @@ -16,4 +16,9 @@ namespace hex { fmt::print(fmt::runtime(format), args...); } + template + inline void println(std::string_view format, Args... args) { + fmt::println(fmt::runtime(format), args...); + } + } \ No newline at end of file diff --git a/plugins/builtin/source/content/command_line_interface.cpp b/plugins/builtin/source/content/command_line_interface.cpp index ee22b5216..694b784cf 100644 --- a/plugins/builtin/source/content/command_line_interface.cpp +++ b/plugins/builtin/source/content/command_line_interface.cpp @@ -54,7 +54,7 @@ namespace hex::plugin::builtin { for (const auto &plugin : PluginManager::getPlugins()) { for (const auto &subCommand : plugin.getSubCommands()) { - hex::print(" --{}{: <{}} {}\n", subCommand.commandKey, "", longestCommand - subCommand.commandKey.size(), subCommand.commandDesc); + hex::println(" --{}{: <{}} {}", subCommand.commandKey, "", longestCommand - subCommand.commandKey.size(), subCommand.commandDesc); } } @@ -63,7 +63,7 @@ namespace hex::plugin::builtin { void handleOpenCommand(const std::vector &args) { if (args.empty()) { - hex::print("No files provided to open."); + hex::println("No files provided to open."); std::exit(EXIT_FAILURE); } @@ -85,8 +85,8 @@ namespace hex::plugin::builtin { void handleCalcCommand(const std::vector &args) { if (args.empty()) { - hex::print("No expression provided!"); - hex::print("Example: imhex --calc \"5 * 7\""); + hex::println("No expression provided!"); + hex::println("Example: imhex --calc \"5 * 7\""); std::exit(EXIT_FAILURE); } @@ -96,9 +96,9 @@ namespace hex::plugin::builtin { auto result = evaluator.evaluate(input); if (!result.has_value()) - hex::print("{}\n> '{}'", evaluator.getLastError().value(), input); + hex::println("{}\n> '{}'", evaluator.getLastError().value(), input); else - hex::print("{}", result.value()); + hex::println("{}", result.value()); std::exit(EXIT_SUCCESS); } @@ -106,7 +106,7 @@ namespace hex::plugin::builtin { void handlePluginsCommand(const std::vector &args) { hex::unused(args); - hex::print("Loaded plugins:\n"); + hex::println("Loaded plugins:"); for (const auto &plugin : PluginManager::getPlugins()) { hex::print("- "); @@ -116,9 +116,9 @@ namespace hex::plugin::builtin { else hex::print("\033[1m{}\033[0m", plugin.getPluginName()); - hex::print(" by {}\n", plugin.getPluginAuthor()); + hex::println(" by {}", plugin.getPluginAuthor()); - hex::print(" \033[2;3m{}\033[0m\n", plugin.getPluginDescription()); + hex::println(" \033[2;3m{}\033[0m", plugin.getPluginDescription()); } std::exit(EXIT_SUCCESS); @@ -126,8 +126,8 @@ namespace hex::plugin::builtin { void handleHashCommand(const std::vector &args) { if (args.size() != 2) { - hex::print("usage: imhex --hash "); - hex::print("Available algorithms: md5, sha1, sha224, sha256, sha384, sha512"); + hex::println("usage: imhex --hash "); + hex::println("Available algorithms: md5, sha1, sha224, sha256, sha384, sha512"); std::exit(EXIT_FAILURE); } @@ -136,7 +136,7 @@ namespace hex::plugin::builtin { wolv::io::File file(filePath, wolv::io::File::Mode::Read); if (!file.isValid()) { - hex::print("Failed to open file: {}", wolv::util::toUTF8String(filePath)); + hex::println("Failed to open file: {}", wolv::util::toUTF8String(filePath)); std::exit(EXIT_FAILURE); } @@ -158,20 +158,20 @@ namespace hex::plugin::builtin { } else if (algorithm == "sha512") { result = toVector(hex::crypt::sha512(file.readVector())); } else { - hex::print("Unknown algorithm: {}", algorithm); - hex::print("Available algorithms: md5, sha1, sha224, sha256, sha384, sha512"); + hex::println("Unknown algorithm: {}", algorithm); + hex::println("Available algorithms: md5, sha1, sha224, sha256, sha384, sha512"); std::exit(EXIT_FAILURE); } - hex::print("{}({}) = {}", algorithm, wolv::util::toUTF8String(filePath.filename()), hex::crypt::encode16(result)); + hex::println("{}({}) = {}", algorithm, wolv::util::toUTF8String(filePath.filename()), hex::crypt::encode16(result)); std::exit(EXIT_SUCCESS); } void handleEncodeCommand(const std::vector &args) { if (args.size() != 2) { - hex::print("usage: imhex --encode "); - hex::print("Available algorithms: base64, hex"); + hex::println("usage: imhex --encode "); + hex::println("Available algorithms: base64, hex"); std::exit(EXIT_FAILURE); } @@ -185,19 +185,19 @@ namespace hex::plugin::builtin { } else if (algorithm == "hex") { result = hex::crypt::encode16(data); } else { - hex::print("Unknown algorithm: {}", algorithm); - hex::print("Available algorithms: base64, hex"); + hex::println("Unknown algorithm: {}", algorithm); + hex::println("Available algorithms: base64, hex"); std::exit(EXIT_FAILURE); } - hex::print("encode_{}({}) = {}", algorithm, args[1], result); + hex::println("encode_{}({}) = {}", algorithm, args[1], result); std::exit(EXIT_SUCCESS); } void handleDecodeCommand(const std::vector &args) { if (args.size() != 2) { - hex::print("usage: imhex --decode "); - hex::print("Available algorithms: base64, hex"); + hex::println("usage: imhex --decode "); + hex::println("Available algorithms: base64, hex"); std::exit(EXIT_FAILURE); } @@ -212,8 +212,8 @@ namespace hex::plugin::builtin { auto base16 = hex::crypt::decode16(std::string(data.begin(), data.end())); result = std::string(base16.begin(), base16.end()); } else { - hex::print("Unknown algorithm: {}", algorithm); - hex::print("Available algorithms: base64, hex"); + hex::println("Unknown algorithm: {}", algorithm); + hex::println("Available algorithms: base64, hex"); std::exit(EXIT_FAILURE); } @@ -223,8 +223,8 @@ namespace hex::plugin::builtin { void handleMagicCommand(const std::vector &args) { if (args.size() != 2) { - hex::print("usage: imhex --magic "); - hex::print("Available operations: mime, desc"); + hex::println("usage: imhex --magic "); + hex::println("Available operations: mime, desc"); std::exit(EXIT_FAILURE); } @@ -238,7 +238,7 @@ namespace hex::plugin::builtin { wolv::io::File file(filePath, wolv::io::File::Mode::Read); if (!file.isValid()) { - hex::print("Failed to open file: {}", wolv::util::toUTF8String(filePath)); + hex::println("Failed to open file: {}", wolv::util::toUTF8String(filePath)); std::exit(EXIT_FAILURE); } @@ -246,13 +246,13 @@ namespace hex::plugin::builtin { if (operation == "mime") { auto result = magic::getMIMEType(data); - hex::print("{}", result); + hex::println("{}", result); } else if (operation == "desc") { auto result = magic::getDescription(data); - hex::print("{}", result); + hex::println("{}", result); } else { - hex::print("Unknown operation: {}", operation); - hex::print("Available operations: mime, desc"); + hex::println("Unknown operation: {}", operation); + hex::println("Available operations: mime, desc"); std::exit(EXIT_FAILURE); }