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,14 +1,14 @@
#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;
@@ -16,26 +16,29 @@ static std::string resize_str() {
return oss.str(); return oss.str();
} }
void convert(const std::string& path) { // try to convert to ppm using ImageMagick void convert(
const std::string &path) { // try to convert to ppm using ImageMagick
int pfd[2]; int pfd[2];
throw_if_min1_msg(pipe(pfd), "could not create pipe"); 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",
resize.c_str(), "ppm:-", nullptr);
if (res == -1) { // probably "magick" not found, try "convert" if (res == -1) { // probably "magick" not found, try "convert"
res = execlp("convert", "convert", path.c_str(), "-resize", resize.c_str(), "ppm:-", nullptr); res = execlp("convert", "convert", path.c_str(), "-resize",
resize.c_str(), "ppm:-", nullptr);
if (res == -1) { // no ImageMagick, abort if (res == -1) { // no ImageMagick, abort
throw std::runtime_error {"no ImageMagick installed, abort"}; throw std::runtime_error{"no ImageMagick installed, abort"};
} }
} }
break; break;

View File

@@ -1,13 +1,13 @@
#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";
@@ -15,17 +15,16 @@ static void print_help() {
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)) !=
while ((opt_char = getopt_long(argc, argv, "h", long_options, nullptr)) != -1) { -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
@@ -49,18 +48,18 @@ int main(int argc, char* argv[]) {
} }
for (int i = optind; i < argc; ++i) { for (int i = optind; i < argc; ++i) {
try { try {
std::string path {argv[i]}; std::string path{argv[i]};
std::string ext {sc::filename_extension(path)}; std::string ext{sc::filename_extension(path)};
if (ext != "ppm") { if (ext != "ppm") {
convert(path); convert(path);
} else { } else {
write_image(std::cout, path); write_image(std::cout, path);
} }
} catch (const std::runtime_error& ex) { } catch (const std::runtime_error &ex) {
std::cerr << "termage: " << ex.what() << '\n'; std::cerr << "termage: " << ex.what() << '\n';
} }
} }
} catch (const std::exception& ex) { } catch (const std::exception &ex) {
std::cerr << "termage: " << ex.what() << '\n'; std::cerr << "termage: " << ex.what() << '\n';
return EXIT_FAILURE; return EXIT_FAILURE;
} }

View File

@@ -1,16 +1,18 @@
#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};
std::unique_ptr<tuple, void (*)(void *)> row2{pnm_allocpamrow(&info),
pm_freerow};
for (int y = 0; y < info.height; y += 2) { for (int y = 0; y < info.height; y += 2) {
tuple* t1 {row1.get()}; tuple *t1{row1.get()};
tuple* t2 {row2.get()}; tuple *t2{row2.get()};
pnm_readpamrow(&info, row1.get()); pnm_readpamrow(&info, row1.get());
if (y < info.height - 1) { if (y < info.height - 1) {
pnm_readpamrow(&info, row2.get()); pnm_readpamrow(&info, row2.get());
@@ -30,11 +32,10 @@ void write_image(std::ostream& os, FILE* in) {
} }
} }
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()),
pm_close};
write_image(os, infile.get()); write_image(os, infile.get());
} }

View File

@@ -2,8 +2,8 @@
#include <sstream> #include <sstream>
std::string termage_version() { std::string termage_version() {
#include "version.inc"
#include "commit.inc" #include "commit.inc"
#include "version.inc"
std::ostringstream oss; std::ostringstream oss;
oss << "termage version " << version; oss << "termage version " << version;
#ifdef DEBUG #ifdef DEBUG