Quiet option

This commit is contained in:
Matteo Paonessa 2019-11-02 15:00:01 +01:00
parent 4a229a3bbd
commit 76fdfba510
11 changed files with 97 additions and 25 deletions

View File

@ -1,6 +1,6 @@
language: c
dist: trusty
dist: bionic
sudo: required
compiler:

View File

@ -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(

View File

@ -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)

View File

@ -1,4 +1,4 @@
#define VERSION_MAJOR 0
#define VERSION_MINOR 13
#define VERSION_PATCH 1
#define VERSION_MINOR 14
#define VERSION_PATCH 0

View File

@ -20,13 +20,17 @@
#include <stdlib.h>
#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);
}

View File

@ -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);

View File

@ -20,6 +20,7 @@
#include <stdio.h>
#include <caesium.h>
#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)));

20
src/shared.c Normal file
View File

@ -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;

23
src/shared.h Normal file
View File

@ -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

View File

@ -22,6 +22,7 @@
#include <limits.h>
#include <math.h>
#include <errno.h>
#include <stdarg.h>
#ifdef _WIN32
#include <stdint.h>
#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

View File

@ -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);