Skip to content

Commit

Permalink
cat: Add the -n line numbering feature
Browse files Browse the repository at this point in the history
Adds the -n line numbering feature with the same formatting as gnu's cat
function.
  • Loading branch information
PerrinJS authored and nico committed Jul 12, 2024
1 parent 1dca789 commit f30dc92
Showing 1 changed file with 29 additions and 1 deletion.
30 changes: 29 additions & 1 deletion Userland/Utilities/cat.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -11,15 +11,36 @@
#include <LibCore/System.h>
#include <LibMain/Main.h>

struct LineTracker {
size_t line_count = 1;
bool display_line_number = true;
};

static void output_buffer_with_line_numbers(LineTracker& line_tracker, ReadonlyBytes buffer_span)
{
for (auto const curr_value : buffer_span) {
if (line_tracker.display_line_number) {
out("{: >6}\t", line_tracker.line_count);
line_tracker.line_count++;
line_tracker.display_line_number = false;
}
if (curr_value == '\n')
line_tracker.display_line_number = true;
out("{:c}", curr_value);
}
}

ErrorOr<int> serenity_main(Main::Arguments arguments)
{
TRY(Core::System::pledge("stdio rpath"));

Vector<StringView> paths;
bool show_lines = false;

Core::ArgsParser args_parser;
args_parser.set_general_help("Concatenate files or pipes to stdout.");
args_parser.add_positional_argument(paths, "File path", "path", Core::ArgsParser::Required::No);
args_parser.add_option(show_lines, "Number all output lines", "number", 'n');
args_parser.parse(arguments);

if (paths.is_empty())
Expand All @@ -37,11 +58,18 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)

TRY(Core::System::pledge("stdio"));

// used only if we are using the -n option
LineTracker line_tracker;

Array<u8, 32768> buffer;
for (auto const& file : files) {
while (!file->is_eof()) {
auto const buffer_span = TRY(file->read_some(buffer));
out("{:s}", buffer_span);
if (show_lines) {
output_buffer_with_line_numbers(line_tracker, buffer_span);
} else {
out("{:s}", buffer_span);
}
}
}

Expand Down

0 comments on commit f30dc92

Please sign in to comment.