2020-01-18 08:38:21 +00:00
|
|
|
/*
|
|
|
|
* Copyright (c) 2018-2020, Andreas Kling <kling@serenityos.org>
|
|
|
|
*
|
2021-04-22 08:24:48 +00:00
|
|
|
* SPDX-License-Identifier: BSD-2-Clause
|
2020-01-18 08:38:21 +00:00
|
|
|
*/
|
|
|
|
|
2023-12-16 14:19:34 +00:00
|
|
|
#include <AK/ByteString.h>
|
2019-11-04 11:44:32 +00:00
|
|
|
#include <AK/Vector.h>
|
2020-02-06 14:04:03 +00:00
|
|
|
#include <LibCore/ArgsParser.h>
|
2023-06-07 15:11:37 +00:00
|
|
|
#include <LibCore/File.h>
|
2022-01-30 10:17:58 +00:00
|
|
|
#include <LibMain/Main.h>
|
2019-11-04 11:44:32 +00:00
|
|
|
|
2020-01-27 17:25:36 +00:00
|
|
|
enum NumberStyle {
|
|
|
|
NumberAllLines,
|
|
|
|
NumberNonEmptyLines,
|
|
|
|
NumberNoLines,
|
|
|
|
};
|
|
|
|
|
2022-01-30 10:17:58 +00:00
|
|
|
ErrorOr<int> serenity_main(Main::Arguments arguments)
|
2019-11-04 11:44:32 +00:00
|
|
|
{
|
2020-01-27 17:25:36 +00:00
|
|
|
NumberStyle number_style = NumberNonEmptyLines;
|
|
|
|
int increment = 1;
|
2023-02-28 20:41:43 +00:00
|
|
|
StringView separator = " "sv;
|
2020-01-27 17:25:36 +00:00
|
|
|
int start_number = 1;
|
|
|
|
int number_width = 6;
|
2023-06-07 15:11:37 +00:00
|
|
|
Vector<StringView> filenames;
|
2019-11-04 11:44:32 +00:00
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
Core::ArgsParser args_parser;
|
2019-11-04 11:44:32 +00:00
|
|
|
|
2020-02-02 11:34:39 +00:00
|
|
|
Core::ArgsParser::Option number_style_option {
|
2022-07-12 20:13:38 +00:00
|
|
|
Core::ArgsParser::OptionArgumentMode::Required,
|
2020-01-27 17:25:36 +00:00
|
|
|
"Line numbering style: 't' for non-empty lines, 'a' for all lines, 'n' for no lines",
|
|
|
|
"body-numbering",
|
|
|
|
'b',
|
|
|
|
"style",
|
2023-02-21 11:44:41 +00:00
|
|
|
[&number_style](StringView s) {
|
|
|
|
if (s == "t"sv)
|
2020-01-27 17:25:36 +00:00
|
|
|
number_style = NumberNonEmptyLines;
|
2023-02-21 11:44:41 +00:00
|
|
|
else if (s == "a"sv)
|
2020-01-27 17:25:36 +00:00
|
|
|
number_style = NumberAllLines;
|
2023-02-21 11:44:41 +00:00
|
|
|
else if (s == "n"sv)
|
2020-01-27 17:25:36 +00:00
|
|
|
number_style = NumberNoLines;
|
|
|
|
else
|
|
|
|
return false;
|
2019-11-04 11:44:32 +00:00
|
|
|
|
2020-01-27 17:25:36 +00:00
|
|
|
return true;
|
2019-11-04 11:44:32 +00:00
|
|
|
}
|
2020-01-27 17:25:36 +00:00
|
|
|
};
|
2019-11-04 11:44:32 +00:00
|
|
|
|
2020-01-27 17:25:36 +00:00
|
|
|
args_parser.add_option(move(number_style_option));
|
|
|
|
args_parser.add_option(increment, "Line count increment", "increment", 'i', "number");
|
|
|
|
args_parser.add_option(separator, "Separator between line numbers and lines", "separator", 's', "string");
|
|
|
|
args_parser.add_option(start_number, "Initial line number", "startnum", 'v', "number");
|
|
|
|
args_parser.add_option(number_width, "Number width", "width", 'w', "number");
|
2023-06-07 15:11:37 +00:00
|
|
|
args_parser.add_positional_argument(filenames, "Files to process", "file", Core::ArgsParser::Required::No);
|
2022-01-30 10:17:58 +00:00
|
|
|
args_parser.parse(arguments);
|
2019-11-04 11:44:32 +00:00
|
|
|
|
2023-06-07 15:11:37 +00:00
|
|
|
if (filenames.is_empty())
|
|
|
|
filenames.append(""sv);
|
|
|
|
|
|
|
|
for (auto const filename : filenames) {
|
|
|
|
auto maybe_file = Core::File::open_file_or_standard_stream(filename, Core::File::OpenMode::Read);
|
|
|
|
if (maybe_file.is_error()) {
|
|
|
|
warnln("Failed to open {}: {}", filename, maybe_file.release_error());
|
|
|
|
continue;
|
2019-11-04 11:44:32 +00:00
|
|
|
}
|
|
|
|
|
2023-06-07 15:11:37 +00:00
|
|
|
auto file = maybe_file.release_value();
|
|
|
|
|
2020-01-27 17:25:36 +00:00
|
|
|
int line_number = start_number - increment; // so the line number can start at 1 when added below
|
2023-06-07 15:12:50 +00:00
|
|
|
Optional<u8> previous_character;
|
2023-06-07 15:11:37 +00:00
|
|
|
u8 next_character;
|
|
|
|
for (Bytes bytes = TRY(file->read_some({ &next_character, 1 })); bytes.size() != 0; bytes = TRY(file->read_some(bytes))) {
|
2023-06-07 15:12:50 +00:00
|
|
|
if (!previous_character.has_value() || previous_character == '\n') {
|
2020-01-27 17:25:36 +00:00
|
|
|
if (next_character == '\n' && number_style != NumberAllLines) {
|
|
|
|
// Skip printing line count on empty lines.
|
2021-05-31 14:43:25 +00:00
|
|
|
outln();
|
2019-11-04 11:44:32 +00:00
|
|
|
continue;
|
|
|
|
}
|
2020-01-27 17:25:36 +00:00
|
|
|
if (number_style != NumberNoLines)
|
2021-05-31 14:43:25 +00:00
|
|
|
out("{1:{0}}{2}", number_width, (line_number += increment), separator);
|
2019-11-04 11:44:32 +00:00
|
|
|
else
|
2021-05-31 14:43:25 +00:00
|
|
|
out("{1:{0}}", number_width, "");
|
2019-11-04 11:44:32 +00:00
|
|
|
}
|
|
|
|
putchar(next_character);
|
|
|
|
previous_character = next_character;
|
|
|
|
}
|
2023-06-07 15:11:37 +00:00
|
|
|
|
2023-06-07 15:12:50 +00:00
|
|
|
if (previous_character.has_value() && previous_character != '\n')
|
2021-05-31 14:43:25 +00:00
|
|
|
outln(); // for cases where files have no trailing newline
|
2019-11-04 11:44:32 +00:00
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|