From c57a36826d457bef5f53aa53cc8872c7ddbfa189 Mon Sep 17 00:00:00 2001 From: Bob Polis Date: Thu, 2 Apr 2026 06:43:21 +0200 Subject: [PATCH] Adapt to tree-sitter style --- src/conversion.cpp | 87 +++++++++++++++++---------------- src/main.cpp | 119 ++++++++++++++++++++++----------------------- src/pixels.cpp | 67 ++++++++++++------------- src/version.cpp | 22 ++++----- 4 files changed, 149 insertions(+), 146 deletions(-) diff --git a/src/conversion.cpp b/src/conversion.cpp index 6858dc0..4bd4e00 100644 --- a/src/conversion.cpp +++ b/src/conversion.cpp @@ -1,54 +1,57 @@ -#include -#include -#include -#include -#include +#include "pixels.hpp" #include #include -#include "pixels.hpp" +#include +#include +#include +#include +#include static std::string resize_str() { - sc::term t {STDOUT_FILENO}; - int cols = t.cols() - 1; - int rows = t.rows() * 2 - 4; - std::ostringstream oss; - oss << cols << "x" << rows; - return oss.str(); + sc::term t{STDOUT_FILENO}; + int cols = t.cols() - 1; + int rows = t.rows() * 2 - 4; + std::ostringstream oss; + oss << cols << "x" << rows; + return oss.str(); } -void convert(const std::string& path) { // try to convert to ppm using ImageMagick - int pfd[2]; - throw_if_min1_msg(pipe(pfd), "could not create pipe"); +void convert( + const std::string &path) { // try to convert to ppm using ImageMagick + int pfd[2]; + throw_if_min1_msg(pipe(pfd), "could not create pipe"); - switch (fork()) { - case -1: - throw std::runtime_error {"could not fork"}; + switch (fork()) { + case -1: + throw std::runtime_error{"could not fork"}; - case 0: { // child - std::string resize {resize_str()}; + case 0: { // child + std::string resize{resize_str()}; - throw_if_min1(dup2(pfd[1], STDOUT_FILENO)); - throw_if_min1(close(pfd[0])); - throw_if_min1(close(pfd[1])); + throw_if_min1(dup2(pfd[1], STDOUT_FILENO)); + throw_if_min1(close(pfd[0])); + throw_if_min1(close(pfd[1])); - int res = execlp("magick", "magick", path.c_str(), "-resize", resize.c_str(), "ppm:-", nullptr); - if (res == -1) { // probably "magick" not found, try "convert" - res = execlp("convert", "convert", path.c_str(), "-resize", resize.c_str(), "ppm:-", nullptr); - if (res == -1) { // no ImageMagick, abort - throw std::runtime_error {"no ImageMagick installed, abort"}; - } - } - break; - } - - default: // parent - throw_if_min1(dup2(pfd[0], STDIN_FILENO)); - throw_if_min1(close(pfd[0])); - throw_if_min1(close(pfd[1])); - - write_image(std::cout, stdin); - - throw_if_min1(wait(nullptr)); - break; + int res = execlp("magick", "magick", path.c_str(), "-resize", + resize.c_str(), "ppm:-", nullptr); + if (res == -1) { // probably "magick" not found, try "convert" + res = execlp("convert", "convert", path.c_str(), "-resize", + resize.c_str(), "ppm:-", nullptr); + if (res == -1) { // no ImageMagick, abort + throw std::runtime_error{"no ImageMagick installed, abort"}; + } } + break; + } + + default: // parent + throw_if_min1(dup2(pfd[0], STDIN_FILENO)); + throw_if_min1(close(pfd[0])); + throw_if_min1(close(pfd[1])); + + write_image(std::cout, stdin); + + throw_if_min1(wait(nullptr)); + break; + } } diff --git a/src/main.cpp b/src/main.cpp index 414bc64..df5165d 100644 --- a/src/main.cpp +++ b/src/main.cpp @@ -1,68 +1,67 @@ -#include -#include -#include -#include -#include -#include -#include -#include "version.hpp" -#include "pixels.hpp" #include "conversion.hpp" +#include "pixels.hpp" +#include "version.hpp" +#include +#include +#include +#include +#include +#include +#include static void print_help() { - std::cout << "usage: termage [-h|--version] \n"; - std::cout << " -h, --help show this help text and exit\n"; - std::cout << " --version show version number and exit\n"; + std::cout << "usage: termage [-h|--version] \n"; + std::cout << " -h, --help show this help text and exit\n"; + std::cout << " --version show version number and exit\n"; } -int main(int argc, char* argv[]) { - try { - pm_init("termage", 0); - int opt_char, opt_val; - struct option long_options[] = { - {"help", no_argument, nullptr, 'h'}, - {"version", no_argument, &opt_val, 1}, - {nullptr, 0, nullptr, 0} - }; - while ((opt_char = getopt_long(argc, argv, "h", long_options, nullptr)) != -1) { - std::string arg {optarg ? optarg : ""}; - switch (opt_char) { - case 0: { - // handle long-only options here - switch (opt_val) { - case 1: - std::cout << termage_version() << '\n'; - return EXIT_SUCCESS; - } - break; - } - case 'h': - print_help(); - return EXIT_SUCCESS; - case '?': - throw std::runtime_error("unrecognized option"); - } +int main(int argc, char *argv[]) { + try { + pm_init("termage", 0); + int opt_char, opt_val; + struct option long_options[] = {{"help", no_argument, nullptr, 'h'}, + {"version", no_argument, &opt_val, 1}, + {nullptr, 0, nullptr, 0}}; + while ((opt_char = getopt_long(argc, argv, "h", long_options, nullptr)) != + -1) { + std::string arg{optarg ? optarg : ""}; + switch (opt_char) { + case 0: { + // handle long-only options here + switch (opt_val) { + case 1: + std::cout << termage_version() << '\n'; + return EXIT_SUCCESS; } - if (optind == argc) { - // no file arg => read from stdin - write_image(std::cout); - } - for (int i = optind; i < argc; ++i) { - try { - std::string path {argv[i]}; - std::string ext {sc::filename_extension(path)}; - if (ext != "ppm") { - convert(path); - } else { - write_image(std::cout, path); - } - } catch (const std::runtime_error& ex) { - std::cerr << "termage: " << ex.what() << '\n'; - } - } - } catch (const std::exception& ex) { - std::cerr << "termage: " << ex.what() << '\n'; - return EXIT_FAILURE; + break; + } + case 'h': + print_help(); + return EXIT_SUCCESS; + case '?': + throw std::runtime_error("unrecognized option"); + } } - return EXIT_SUCCESS; + if (optind == argc) { + // no file arg => read from stdin + write_image(std::cout); + } + for (int i = optind; i < argc; ++i) { + try { + std::string path{argv[i]}; + std::string ext{sc::filename_extension(path)}; + if (ext != "ppm") { + convert(path); + } else { + write_image(std::cout, path); + } + } catch (const std::runtime_error &ex) { + std::cerr << "termage: " << ex.what() << '\n'; + } + } + } catch (const std::exception &ex) { + std::cerr << "termage: " << ex.what() << '\n'; + return EXIT_FAILURE; + } + return EXIT_SUCCESS; } diff --git a/src/pixels.cpp b/src/pixels.cpp index 88aba01..0b31488 100644 --- a/src/pixels.cpp +++ b/src/pixels.cpp @@ -1,40 +1,41 @@ -#include -#include -#include #include "pixels.hpp" +#include +#include +#include -void write_image(std::ostream& os, FILE* in) { - struct pam info; - pnm_readpaminit(in, &info, PAM_STRUCT_SIZE(tuple_type)); - std::unique_ptr row1 {pnm_allocpamrow(&info), pm_freerow}; - std::unique_ptr row2 {pnm_allocpamrow(&info), pm_freerow}; - for (int y = 0; y < info.height; y += 2) { - tuple* t1 {row1.get()}; - tuple* t2 {row2.get()}; - pnm_readpamrow(&info, row1.get()); - if (y < info.height - 1) { - pnm_readpamrow(&info, row2.get()); - } - for (int x = 0; x < info.width; ++x) { - if (info.depth == 3) { // assume RGB, 1 byte per sample - os << sc::io::truecolorf(t1[x][0], t1[x][1], t1[x][2]); - if (y < info.height - 1) { - os << sc::io::truecolorb(t2[x][0], t2[x][1], t2[x][2]); - } else { - os << sc::io::defaultb; - } - os << u8"\u2580"; // unicode upper half block - } - } - os << sc::io::reset << '\n'; +void write_image(std::ostream &os, FILE *in) { + struct pam info; + pnm_readpaminit(in, &info, PAM_STRUCT_SIZE(tuple_type)); + std::unique_ptr row1{pnm_allocpamrow(&info), + pm_freerow}; + std::unique_ptr row2{pnm_allocpamrow(&info), + pm_freerow}; + for (int y = 0; y < info.height; y += 2) { + tuple *t1{row1.get()}; + tuple *t2{row2.get()}; + pnm_readpamrow(&info, row1.get()); + if (y < info.height - 1) { + pnm_readpamrow(&info, row2.get()); } + for (int x = 0; x < info.width; ++x) { + if (info.depth == 3) { // assume RGB, 1 byte per sample + os << sc::io::truecolorf(t1[x][0], t1[x][1], t1[x][2]); + if (y < info.height - 1) { + os << sc::io::truecolorb(t2[x][0], t2[x][1], t2[x][2]); + } else { + os << sc::io::defaultb; + } + os << u8"\u2580"; // unicode upper half block + } + } + os << sc::io::reset << '\n'; + } } -void write_image(std::ostream& os) { - write_image(os, stdin); -} +void write_image(std::ostream &os) { write_image(os, stdin); } -void write_image(std::ostream& os, const std::string& path) { - std::unique_ptr infile {pm_openr(path.c_str()), pm_close}; - write_image(os, infile.get()); +void write_image(std::ostream &os, const std::string &path) { + std::unique_ptr infile{pm_openr(path.c_str()), + pm_close}; + write_image(os, infile.get()); } diff --git a/src/version.cpp b/src/version.cpp index af2949b..c3dac87 100644 --- a/src/version.cpp +++ b/src/version.cpp @@ -2,18 +2,18 @@ #include std::string termage_version() { -#include "version.inc" #include "commit.inc" - std::ostringstream oss; - oss << "termage version " << version; +#include "version.inc" + std::ostringstream oss; + oss << "termage version " << version; #ifdef DEBUG - oss << " DEBUG"; + oss << " DEBUG"; #endif - oss << '\n'; - if (commit[0] != '\0') { - oss << "build " << commit << ", "; - } - oss << __DATE__ << ", " << __TIME__ << '\n'; - oss << "(c) Bob Polis, all rights reserved"; - return oss.str(); + oss << '\n'; + if (commit[0] != '\0') { + oss << "build " << commit << ", "; + } + oss << __DATE__ << ", " << __TIME__ << '\n'; + oss << "(c) Bob Polis, all rights reserved"; + return oss.str(); }