Skip to content

Commit

Permalink
cli: Add -q/--quiet option
Browse files Browse the repository at this point in the history
  • Loading branch information
JJL772 committed Sep 5, 2024
1 parent 1cf5c34 commit 5686a1a
Show file tree
Hide file tree
Showing 3 changed files with 43 additions and 10 deletions.
26 changes: 19 additions & 7 deletions src/cli/action_convert.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -46,6 +46,7 @@ namespace opts
static int width, height;
static int nomips;
static int toDX;
static int quiet;
} // namespace opts

static bool get_version_from_str(const std::string& str, int& major, int& minor);
Expand Down Expand Up @@ -226,6 +227,15 @@ const OptionList& ActionConvert::get_options() const {
.value(false)
.type(OptType::Bool)
.help("Treat the incoming normal map as an OpenGL normal map"));

opts::quiet = opts.add(
ActionOption()
.long_opt("--quiet")
.short_opt("-q")
.value(false)
.type(OptType::Bool)
.help("Silence output messages that aren't errors")
);
};
return opts;
}
Expand Down Expand Up @@ -398,12 +408,14 @@ bool ActionConvert::process_file(
}

// Report file sizes
if (initialSize != 0) {
fmt::print(
"{} ({} bytes) -> {} ({} bytes)\n", srcFile.string(), initialSize, outFile.string(), vtfFile->GetSize());
}
else {
fmt::print("{} -> {} ({} bytes)\n", srcFile.string(), outFile.string(), vtfFile->GetSize());
if (!opts.get<bool>(opts::quiet)) {
if (initialSize != 0) {
fmt::print(
"{} ({} KiB) -> {} ({} KiB)\n", srcFile.string(), initialSize / 1024, outFile.string(), vtfFile->GetSize() / 1024);
}
else {
fmt::print("{} -> {} ({} KiB)\n", srcFile.string(), outFile.string(), vtfFile->GetSize() / 1024);
}
}

return true;
Expand Down Expand Up @@ -524,7 +536,7 @@ bool ActionConvert::add_image_data(
// Hack for VTFLib; Ensure we have an alpha channel because that's well supported in that horrible code
if (image->channels() < 4 && image->type() != imglib::ChannelType::UInt8) {
if (!image->convert(image->type(), 4)) {
std::cerr << fmt::format("Failed to convert {}\n", imageSrc.c_str());
std::cerr << fmt::format("Failed to convert {}\n", imageSrc.string());
return false;
}
}
Expand Down
13 changes: 12 additions & 1 deletion src/cli/action_extract.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ namespace opts
static int mip;
static int recursive;
static int noalpha;
static int quiet;
} // namespace opts

std::string ActionExtract::get_help() const {
Expand Down Expand Up @@ -82,6 +83,15 @@ const OptionList& ActionExtract::get_options() const {
.type(OptType::Bool)
.value(false)
.help("Exclude alpha channel from converted image"));

opts::quiet = opts.add(
ActionOption()
.short_opt("-q")
.long_opt("--quiet")
.type(OptType::Bool)
.value(false)
.help("Silence output messages that aren't errors")
);
};
return opts;
}
Expand Down Expand Up @@ -152,7 +162,8 @@ bool ActionExtract::extract_file(
outFile = vtfPath.parent_path() / vtfPath.filename().replace_extension(ext);
}

fmt::print("{} -> {}\n", vtfPath.string(), outFile.string());
if (!opts.get<bool>(opts::quiet))
fmt::print("{} -> {}\n", vtfPath.string(), outFile.string());

// Validate mipmap selection
if (mip > file_->GetMipmapCount()) {
Expand Down
14 changes: 12 additions & 2 deletions src/cli/action_pack.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -30,6 +30,7 @@ namespace opts
static int width, height, mips;
static int mconst, rconst, aoconst, hconst;
static int toDX;
static int quiet;
} // namespace opts

std::string ActionPack::get_help() const {
Expand Down Expand Up @@ -176,6 +177,15 @@ const OptionList& ActionPack::get_options() const {
.value(false)
.type(OptType::Bool)
.help("Treat the incoming normal map as a OpenGL normal map"));

opts::quiet = opts.add(
ActionOption()
.long_opt("--quiet")
.short_opt("-q")
.value(false)
.type(OptType::Bool)
.help("Silence output messages that aren't errors")
);
};
return opts;
}
Expand Down Expand Up @@ -467,8 +477,8 @@ bool ActionPack::pack_normal(

success = save_vtf(outpath, outImage, opts, true);

if (success)
std::cout << fmt::format("Finished processing {}\n", outpath.string());
if (success && !opts.get<bool>(opts::quiet))
std::cout << fmt::format("Finished {} ({} KiB)\n", outpath.string(), file_->GetSize() / 1024);
return success;
}

Expand Down

0 comments on commit 5686a1a

Please sign in to comment.