Adapt to tree-sitter style

This commit is contained in:
2026-04-02 06:43:21 +02:00
parent c8dc427438
commit c57a36826d
4 changed files with 149 additions and 146 deletions

View File

@@ -1,54 +1,57 @@
#include <stdexcept> #include "pixels.hpp"
#include <string>
#include <sstream>
#include <unistd.h>
#include <sys/wait.h>
#include <libscerror.hpp> #include <libscerror.hpp>
#include <libscterm.hpp> #include <libscterm.hpp>
#include "pixels.hpp" #include <sstream>
#include <stdexcept>
#include <string>
#include <sys/wait.h>
#include <unistd.h>
static std::string resize_str() { static std::string resize_str() {
sc::term t {STDOUT_FILENO}; sc::term t{STDOUT_FILENO};
int cols = t.cols() - 1; int cols = t.cols() - 1;
int rows = t.rows() * 2 - 4; int rows = t.rows() * 2 - 4;
std::ostringstream oss; std::ostringstream oss;
oss << cols << "x" << rows; oss << cols << "x" << rows;
return oss.str(); return oss.str();
} }
void convert(const std::string& path) { // try to convert to ppm using ImageMagick void convert(
int pfd[2]; const std::string &path) { // try to convert to ppm using ImageMagick
throw_if_min1_msg(pipe(pfd), "could not create pipe"); int pfd[2];
throw_if_min1_msg(pipe(pfd), "could not create pipe");
switch (fork()) { switch (fork()) {
case -1: case -1:
throw std::runtime_error {"could not fork"}; throw std::runtime_error{"could not fork"};
case 0: { // child case 0: { // child
std::string resize {resize_str()}; std::string resize{resize_str()};
throw_if_min1(dup2(pfd[1], STDOUT_FILENO)); throw_if_min1(dup2(pfd[1], STDOUT_FILENO));
throw_if_min1(close(pfd[0])); throw_if_min1(close(pfd[0]));
throw_if_min1(close(pfd[1])); throw_if_min1(close(pfd[1]));
int res = execlp("magick", "magick", path.c_str(), "-resize", resize.c_str(), "ppm:-", nullptr); int res = execlp("magick", "magick", path.c_str(), "-resize",
if (res == -1) { // probably "magick" not found, try "convert" resize.c_str(), "ppm:-", nullptr);
res = execlp("convert", "convert", path.c_str(), "-resize", resize.c_str(), "ppm:-", nullptr); if (res == -1) { // probably "magick" not found, try "convert"
if (res == -1) { // no ImageMagick, abort res = execlp("convert", "convert", path.c_str(), "-resize",
throw std::runtime_error {"no ImageMagick installed, abort"}; 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;
} }
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;
}
} }

View File

@@ -1,68 +1,67 @@
#include <iostream>
#include <cstdlib>
#include <string>
#include <stdexcept>
#include <getopt.h>
#include <netpbm/pam.h>
#include <libscstring.hpp>
#include "version.hpp"
#include "pixels.hpp"
#include "conversion.hpp" #include "conversion.hpp"
#include "pixels.hpp"
#include "version.hpp"
#include <cstdlib>
#include <getopt.h>
#include <iostream>
#include <libscstring.hpp>
#include <netpbm/pam.h>
#include <stdexcept>
#include <string>
static void print_help() { static void print_help() {
std::cout << "usage: termage [-h|--version] <imagefile>\n"; std::cout << "usage: termage [-h|--version] <imagefile>\n";
std::cout << " -h, --help show this help text and exit\n"; std::cout << " -h, --help show this help text and exit\n";
std::cout << " --version show version number and exit\n"; std::cout << " --version show version number and exit\n";
} }
int main(int argc, char* argv[]) { int main(int argc, char *argv[]) {
try { try {
pm_init("termage", 0); pm_init("termage", 0);
int opt_char, opt_val; int opt_char, opt_val;
struct option long_options[] = { struct option long_options[] = {{"help", no_argument, nullptr, 'h'},
{"help", no_argument, nullptr, 'h'}, {"version", no_argument, &opt_val, 1},
{"version", no_argument, &opt_val, 1}, {nullptr, 0, nullptr, 0}};
{nullptr, 0, nullptr, 0} while ((opt_char = getopt_long(argc, argv, "h", long_options, nullptr)) !=
}; -1) {
while ((opt_char = getopt_long(argc, argv, "h", long_options, nullptr)) != -1) { std::string arg{optarg ? optarg : ""};
std::string arg {optarg ? optarg : ""}; switch (opt_char) {
switch (opt_char) { case 0: {
case 0: { // handle long-only options here
// handle long-only options here switch (opt_val) {
switch (opt_val) { case 1:
case 1: std::cout << termage_version() << '\n';
std::cout << termage_version() << '\n'; return EXIT_SUCCESS;
return EXIT_SUCCESS;
}
break;
}
case 'h':
print_help();
return EXIT_SUCCESS;
case '?':
throw std::runtime_error("unrecognized option");
}
} }
if (optind == argc) { break;
// no file arg => read from stdin }
write_image(std::cout); case 'h':
} print_help();
for (int i = optind; i < argc; ++i) { return EXIT_SUCCESS;
try { case '?':
std::string path {argv[i]}; throw std::runtime_error("unrecognized option");
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; 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;
} }

View File

@@ -1,40 +1,41 @@
#include <memory>
#include <libscterm.hpp>
#include <netpbm/pam.h>
#include "pixels.hpp" #include "pixels.hpp"
#include <libscterm.hpp>
#include <memory>
#include <netpbm/pam.h>
void write_image(std::ostream& os, FILE* in) { void write_image(std::ostream &os, FILE *in) {
struct pam info; struct pam info;
pnm_readpaminit(in, &info, PAM_STRUCT_SIZE(tuple_type)); pnm_readpaminit(in, &info, PAM_STRUCT_SIZE(tuple_type));
std::unique_ptr<tuple, void(*)(void*)> row1 {pnm_allocpamrow(&info), pm_freerow}; std::unique_ptr<tuple, void (*)(void *)> row1{pnm_allocpamrow(&info),
std::unique_ptr<tuple, void(*)(void*)> row2 {pnm_allocpamrow(&info), pm_freerow}; pm_freerow};
for (int y = 0; y < info.height; y += 2) { std::unique_ptr<tuple, void (*)(void *)> row2{pnm_allocpamrow(&info),
tuple* t1 {row1.get()}; pm_freerow};
tuple* t2 {row2.get()}; for (int y = 0; y < info.height; y += 2) {
pnm_readpamrow(&info, row1.get()); tuple *t1{row1.get()};
if (y < info.height - 1) { tuple *t2{row2.get()};
pnm_readpamrow(&info, row2.get()); pnm_readpamrow(&info, row1.get());
} if (y < info.height - 1) {
for (int x = 0; x < info.width; ++x) { pnm_readpamrow(&info, row2.get());
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';
} }
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) { void write_image(std::ostream &os) { write_image(os, stdin); }
write_image(os, stdin);
}
void write_image(std::ostream& os, const std::string& path) { void write_image(std::ostream &os, const std::string &path) {
std::unique_ptr<FILE, void(*)(FILE*)> infile {pm_openr(path.c_str()), pm_close}; std::unique_ptr<FILE, void (*)(FILE *)> infile{pm_openr(path.c_str()),
write_image(os, infile.get()); pm_close};
write_image(os, infile.get());
} }

View File

@@ -2,18 +2,18 @@
#include <sstream> #include <sstream>
std::string termage_version() { std::string termage_version() {
#include "version.inc"
#include "commit.inc" #include "commit.inc"
std::ostringstream oss; #include "version.inc"
oss << "termage version " << version; std::ostringstream oss;
oss << "termage version " << version;
#ifdef DEBUG #ifdef DEBUG
oss << " DEBUG"; oss << " DEBUG";
#endif #endif
oss << '\n'; oss << '\n';
if (commit[0] != '\0') { if (commit[0] != '\0') {
oss << "build " << commit << ", "; oss << "build " << commit << ", ";
} }
oss << __DATE__ << ", " << __TIME__ << '\n'; oss << __DATE__ << ", " << __TIME__ << '\n';
oss << "(c) Bob Polis, all rights reserved"; oss << "(c) Bob Polis, all rights reserved";
return oss.str(); return oss.str();
} }