file: Add -b option to omit file name from output

This commit is contained in:
Tim Ledbetter 2023-07-21 21:47:27 +01:00 committed by Tim Flynn
parent fd9b3bdc94
commit 6710622bf7
2 changed files with 9 additions and 3 deletions

View file

@ -17,6 +17,7 @@ First, an attempt is made to identify a given file based on predetermined binary
## Options
* `--help`: Display this message
* `-b`, `--brief`: Do not prepend file names to output lines
* `-I`, `--mime-type`: Only show mime type.
## Arguments

View file

@ -197,10 +197,12 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
Vector<StringView> paths;
bool flag_mime_only = false;
bool flag_brief_mode = false;
Core::ArgsParser args_parser;
args_parser.set_general_help("Determine type of files");
args_parser.add_option(flag_mime_only, "Only print mime type", "mime-type", 'I');
args_parser.add_option(flag_brief_mode, "Do not prepend file names to output lines", "brief", 'b');
args_parser.add_positional_argument(paths, "Files to identify", "files", Core::ArgsParser::Required::Yes);
args_parser.parse(arguments);
@ -217,18 +219,21 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
struct stat file_stat = TRY(Core::System::lstat(path));
if (!flag_brief_mode)
out("{}: ", path);
auto file_size_in_bytes = file_stat.st_size;
if (S_ISDIR(file_stat.st_mode)) {
outln("{}: directory", path);
outln("directory");
} else if (!file_size_in_bytes) {
outln("{}: empty", path);
outln("empty");
} else {
auto file_name_guess = Core::guess_mime_type_based_on_filename(path);
auto mime_type = Core::guess_mime_type_based_on_sniffed_bytes(*file).value_or(file_name_guess);
auto human_readable_description = TRY(get_description_from_mime_type(mime_type, path));
if (!human_readable_description.has_value())
human_readable_description = TRY(String::from_utf8(mime_type));
outln("{}: {}", path, flag_mime_only ? mime_type : *human_readable_description);
outln("{}", flag_mime_only ? mime_type : *human_readable_description);
}
}