mirror of
https://github.com/LadybirdBrowser/ladybird.git
synced 2024-11-22 23:50:19 +00:00
shuf: Support the output of a limited number of lines
This patch adds the "-n"/"--head-count" optional argument to specifiy the maximum number of shuffled lines to output. Idea from Andreas' FIXME roulette :^)
This commit is contained in:
parent
e2771db50d
commit
44ef0ac41c
Notes:
sideshowbarker
2024-07-18 02:47:59 +09:00
Author: https://github.com/Baitinq Commit: https://github.com/SerenityOS/serenity/commit/44ef0ac41c Pull-request: https://github.com/SerenityOS/serenity/pull/16244 Reviewed-by: https://github.com/TobyAsE ✅
1 changed files with 4 additions and 2 deletions
|
@ -20,9 +20,11 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
|
||||
Core::ArgsParser args_parser;
|
||||
StringView path;
|
||||
Optional<size_t> head_count;
|
||||
bool is_zero_terminated = false;
|
||||
|
||||
args_parser.add_positional_argument(path, "File", "file", Core::ArgsParser::Required::No);
|
||||
args_parser.add_option(head_count, "Output at most \"count\" lines", "head-count", 'n', "count");
|
||||
args_parser.add_option(is_zero_terminated, "Split input on \\0, not newline", "zero-terminated", 'z');
|
||||
|
||||
args_parser.parse(arguments);
|
||||
|
@ -65,8 +67,8 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
|
|||
}
|
||||
|
||||
Array<u8, 1> output_delimiter = { '\n' };
|
||||
for (auto const& line : lines) {
|
||||
TRY(Core::System::write(STDOUT_FILENO, line));
|
||||
for (size_t i = 0; i < min(head_count.value_or(lines.size()), lines.size()); ++i) {
|
||||
TRY(Core::System::write(STDOUT_FILENO, lines.at(i)));
|
||||
TRY(Core::System::write(STDOUT_FILENO, output_delimiter));
|
||||
}
|
||||
|
||||
|
|
Loading…
Reference in a new issue