From 76fdfba51037a604f953290c8a544305432d93d8 Mon Sep 17 00:00:00 2001 From: Matteo Paonessa Date: Sat, 2 Nov 2019 15:00:01 +0100 Subject: [PATCH] Quiet option --- .travis.yml | 2 +- CMakeLists.txt | 2 +- README.md | 9 ++++++--- config.h | 4 ++-- src/error.c | 12 ++++++++---- src/helper.c | 23 ++++++++++++++--------- src/main.c | 9 +++++---- src/shared.c | 20 ++++++++++++++++++++ src/shared.h | 23 +++++++++++++++++++++++ src/utils.c | 16 +++++++++++++++- src/utils.h | 2 ++ 11 files changed, 97 insertions(+), 25 deletions(-) create mode 100644 src/shared.c create mode 100644 src/shared.h diff --git a/.travis.yml b/.travis.yml index accffb9..88d767c 100644 --- a/.travis.yml +++ b/.travis.yml @@ -1,6 +1,6 @@ language: c -dist: trusty +dist: bionic sudo: required compiler: diff --git a/CMakeLists.txt b/CMakeLists.txt index a41c947..0fc06b6 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -3,7 +3,7 @@ project(caesiumclt) # The version number. set(VERSION_MAJOR 0) -set(VERSION_MINOR 13) +set(VERSION_MINOR 14) set(VERSION_PATCH 0) configure_file( diff --git a/README.md b/README.md index 5cb4b57..d7e0167 100644 --- a/README.md +++ b/README.md @@ -1,5 +1,5 @@ ## Caesium CommandLineTools -##### caesium-clt - v0.13.1-beta (build 20191013) - Copyright © Matteo Paonessa, 2019. All Rights Reserved. +##### caesium-clt - v0.14.0-beta (build 20191102) - Copyright © Matteo Paonessa, 2019. All Rights Reserved. [![Build Status](https://travis-ci.org/Lymphatus/caesium-clt.svg?branch=master)](https://travis-ci.org/Lymphatus/caesium-clt) ---------- @@ -13,7 +13,7 @@ ---------- ###### TESTED PLATFORMS -* Mac OS X Mojave (v10.14.4) +* Mac OS X Catalina (v10.15) * Ubuntu 19.04 ---------- @@ -46,6 +46,8 @@ See INSTALL.md for more details. - `-d, --dry-run` If this option is set, no files will be compressed, but the entire process will just be simulated. Useful for checking if all the files will be correctly handled. +- `-Q, --quiet` + Suppress all output. Output from the libcaesium library will still be outputted. - `-h, --help` Displays a summary of the command line arguments, much like this one you're reading. - `-v, --version` @@ -95,7 +97,8 @@ $ caesiumclt -q 0 -RS -o ~/output/ ~/Pictures ---------- ###### CHANGELOG -* 0.13.0-beta - Bugfix +* 0.14.0-beta - Added --quiet option +* 0.13.1-beta - Bugfix * 0.13.0-beta - Bugfix * 0.12.1-beta - Bugfix * 0.12.0-beta - Resizing (experimental) diff --git a/config.h b/config.h index 74642bf..ea28c19 100644 --- a/config.h +++ b/config.h @@ -1,4 +1,4 @@ #define VERSION_MAJOR 0 -#define VERSION_MINOR 13 -#define VERSION_PATCH 1 +#define VERSION_MINOR 14 +#define VERSION_PATCH 0 diff --git a/src/error.c b/src/error.c index e5f94da..babac3c 100644 --- a/src/error.c +++ b/src/error.c @@ -20,13 +20,17 @@ #include #include "error.h" +#include "utils.h" +#include "shared.h" void display_error(error_level level, int code) { char *error_level = ((level) ? "[WARNING]" : "[ERROR]"); - fprintf(stderr, "%s %d: %s\n", - error_level, - code, - get_error_message(code)); + + print_to_console(stderr, verbose, "%s %d: %s\n", + error_level, + code, + get_error_message(code)); + if (level == ERROR) { exit(-code); } diff --git a/src/helper.c b/src/helper.c index 14ca370..e06f19d 100644 --- a/src/helper.c +++ b/src/helper.c @@ -29,6 +29,7 @@ #include "utils.h" #include "config.h" #include "error.h" +#include "shared.h" cclt_options parse_arguments(char **argv, cs_image_pars *options) { struct optparse opts; @@ -45,6 +46,7 @@ cclt_options parse_arguments(char **argv, cs_image_pars *options) { {"keep-structure", 'S', OPTPARSE_NONE}, {"overwrite", 'O', OPTPARSE_REQUIRED}, {"dry-run", 'd', OPTPARSE_NONE}, + {"quiet", 'Q', OPTPARSE_NONE}, {"version", 'v', OPTPARSE_NONE}, {"help", 'h', OPTPARSE_NONE}, {0} @@ -115,14 +117,17 @@ cclt_options parse_arguments(char **argv, cs_image_pars *options) { parameters.dry_run = true; break; case 'v': - fprintf(stdout, "%d.%d.%d\n", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH); + print_to_console(stdout, 1, "%d.%d.%d\n", VERSION_MAJOR, VERSION_MINOR, VERSION_PATCH); exit(EXIT_SUCCESS); + case 'Q': + verbose = 0; + break; case 'h': print_help(); break; case '?': default: - fprintf(stderr, "%s: %s\n", argv[0], opts.errmsg); + print_to_console(stderr, verbose, "%s: %s\n", argv[0], opts.errmsg); display_error(ERROR, 2); } } @@ -131,7 +136,7 @@ cclt_options parse_arguments(char **argv, cs_image_pars *options) { bool files_flag = false, folders_flag = false; char resolved_path[MAX_PATH_SIZE]; - fprintf(stdout, "%s\n", "Collecting files..."); + print_to_console(stdout, verbose,"%s\n", "Collecting files..."); while ((arg = optparse_arg(&opts))) { if (folders_flag) { @@ -271,17 +276,17 @@ int start_compression(cclt_options *options, cs_image_pars *parameters) { bool f_exists = file_exists(output_full_path); if (f_exists) { if (options->overwrite == none) { - fprintf(stdout, "[SKIPPED] %s\n", output_full_path); + print_to_console(stdout, verbose, "[SKIPPED] %s\n", output_full_path); options->output_total_size += get_file_size(output_full_path); goto free_and_go_on_with_next_file; } else if (options->overwrite == prompt) { - fprintf(stdout, "Overwrite %s? [y/n]\n", output_full_path); + print_to_console(stdout, verbose, "Overwrite %s? [y/n]\n", output_full_path); int prompt = getchar(); if (prompt == '\n') { prompt = getchar(); } if (prompt != 'y' && prompt != 'Y') { - fprintf(stdout, "[SKIPPED] %s\n", output_full_path); + print_to_console(stdout, verbose, "[SKIPPED] %s\n", output_full_path); options->output_total_size += get_file_size(output_full_path); goto free_and_go_on_with_next_file; } @@ -293,7 +298,7 @@ int start_compression(cclt_options *options, cs_image_pars *parameters) { overwriting = true; } - fprintf(stdout, "(%d/%d) %s -> %s\n", + print_to_console(stdout, verbose, "(%d/%d) %s -> %s\n", i + 1, options->files_count, filename, @@ -309,13 +314,13 @@ int start_compression(cclt_options *options, cs_image_pars *parameters) { char *human_output_size = get_human_size(output_file_size); if (options->overwrite == bigger && get_file_size(original_output_full_path) <= output_file_size) { - fprintf(stdout, "Resulting file is bigger. Skipping.\n"); + print_to_console(stdout, verbose, "Resulting file is bigger. Skipping.\n"); remove(output_full_path); options->output_total_size += get_file_size(original_output_full_path); goto free_and_go_on_with_next_file; } options->output_total_size += output_file_size; - fprintf(stdout, "%s -> %s [%.2f%%]\n", + print_to_console(stdout, verbose, "%s -> %s [%.2f%%]\n", human_input_size, human_output_size, ((float) output_file_size - input_file_size) * 100 / input_file_size); diff --git a/src/main.c b/src/main.c index 041f6f4..4ad0f63 100755 --- a/src/main.c +++ b/src/main.c @@ -20,6 +20,7 @@ #include #include #include "utils.h" +#include "shared.h" int main(int argc, char *argv[]) { @@ -52,13 +53,13 @@ int main(int argc, char *argv[]) //Output the compression results - fprintf(stdout, "-------------------------------\n"); + print_to_console(stdout, verbose, "-------------------------------\n"); compression_time / 1000 % 60 >= 1 ? - fprintf(stdout, "Compression completed in %lum%lus\n", + print_to_console(stdout, verbose, "Compression completed in %lum%lus\n", compression_time / 1000 / 60, compression_time / 1000 % 60) : - fprintf(stdout, "Compression completed in %lum%lus%lums\n", + print_to_console(stdout, verbose, "Compression completed in %lum%lus%lums\n", compression_time / 1000 / 60, compression_time / 1000 % 60, compression_time % 1000); - fprintf(stdout, "%s -> %s [%.2f%% | %s]\n", + print_to_console(stdout, verbose, "%s -> %s [%.2f%% | %s]\n", get_human_size(options.input_total_size), get_human_size(options.output_total_size), ((float) options.output_total_size - options.input_total_size) * 100 / options.input_total_size, get_human_size((options.output_total_size - options.input_total_size))); diff --git a/src/shared.c b/src/shared.c new file mode 100644 index 0000000..fafe5fc --- /dev/null +++ b/src/shared.c @@ -0,0 +1,20 @@ +/* + * + * Copyright 2019 Matteo Paonessa + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#include "shared.h" + +int verbose = 1; diff --git a/src/shared.h b/src/shared.h new file mode 100644 index 0000000..145e22a --- /dev/null +++ b/src/shared.h @@ -0,0 +1,23 @@ +/* + * + * Copyright 2019 Matteo Paonessa + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + * + */ + +#ifndef CAESIUMCLT_SHARED_H +#define CAESIUMCLT_SHARED_H + +extern int verbose; + +#endif //CAESIUMCLT_SHARED_H diff --git a/src/utils.c b/src/utils.c index 9ae1f0e..e98db12 100644 --- a/src/utils.c +++ b/src/utils.c @@ -22,6 +22,7 @@ #include #include #include +#include #ifdef _WIN32 #include #endif @@ -32,7 +33,7 @@ void print_help() { - fprintf(stdout, + print_to_console(stdout, 1, "CaesiumCLT - Caesium Command Line Tools\n\n" "Usage: caesiumclt [OPTIONS] INPUT...\n" "Command line image compressor.\n\n" @@ -46,6 +47,7 @@ void print_help() "\t-S, --keep-structure\tkeep the folder structure, use with -R\n" "\t-O, --overwrite\t\tOverwrite policy: all, none, prompt, bigger. Default is bigger.\n" "\t-d, --dry-run\t\tdo not really compress files but just show output paths\n" + "\t-Q, --quiet\t\tsuppress all output\n" "\t-h, --help\t\tdisplay this help and exit\n" "\t-v, --version\t\toutput version information and exit\n\n"); exit(EXIT_SUCCESS); @@ -280,6 +282,18 @@ overwrite_policy parse_overwrite_policy(const char* overwrite_string) return bigger; } +void print_to_console(FILE* buffer, int verbose, const char* format, ...) +{ + if (!verbose) { + return; + } + + va_list args; + + va_start(args, format); + vfprintf(buffer, format, args); + va_end(args); +} #ifdef _WIN32 diff --git a/src/utils.h b/src/utils.h index f9fd685..cfd35d1 100644 --- a/src/utils.h +++ b/src/utils.h @@ -42,6 +42,8 @@ double parse_scale_factor(const char* factor_string); overwrite_policy parse_overwrite_policy(const char* overwrite_string); +void print_to_console(FILE* buffer, int verbose, const char* format, ...); + #ifdef _WIN32 char *str_replace(char *orig, char *rep, char *with); char *strsep(char **stringp, const char *delim);