less: Add the -F option to quit if the input fits on one screen

This commit is contained in:
Tim Ledbetter 2023-09-24 22:02:36 +01:00 committed by Andreas Kling
parent 938a287d3e
commit ebb822def9
2 changed files with 18 additions and 2 deletions

View file

@ -23,6 +23,7 @@ but largely incompatible with
* `-X`, `--no-init`: Don't switch to the xterm alternate buffer on startup.
* `-N`, `--line-numbers`: Show line numbers before printed lines.
* `-e`, `--quit-at-eof`: Immediately exit less when the last line of the document is reached.
* `-F`, `--quit-if-one-screen`: Exit immediately if the entire file can be displayed on one screen.
* `-m`, `--emulate-more`: Apply `-Xe`, set the prompt to `--More--`, and disable
scrollback. This option is automatically applied when `less` is executed as `more`

View file

@ -179,7 +179,7 @@ public:
resize(false);
}
void resize(bool clear = true)
void populate_line_buffer()
{
// First, we get the current size of the window.
struct winsize window;
@ -207,6 +207,11 @@ public:
}
--additional_lines;
}
}
void resize(bool clear = true)
{
populate_line_buffer();
reflow();
bound_cursor();
@ -522,6 +527,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
DeprecatedString prompt = "?f%f :.(line %l)?e (END):.";
bool dont_switch_buffer = false;
bool quit_at_eof = false;
bool quit_if_one_screen = false;
bool emulate_more = false;
bool show_line_numbers = false;
@ -534,6 +540,7 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
args_parser.add_option(dont_switch_buffer, "Don't use xterm alternate buffer", "no-init", 'X');
args_parser.add_option(show_line_numbers, "Show line numbers", "line-numbers", 'N');
args_parser.add_option(quit_at_eof, "Exit when the end of the file is reached", "quit-at-eof", 'e');
args_parser.add_option(quit_if_one_screen, "Exit immediately if the entire file can be displayed on one screen", "quit-if-one-screen", 'F');
args_parser.add_option(emulate_more, "Pretend that we are more(1)", "emulate-more", 'm');
args_parser.parse(arguments);
@ -567,6 +574,15 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
return 0;
}
Pager pager(filename, file, stdout, prompt, show_line_numbers);
pager.populate_line_buffer();
if (quit_if_one_screen && pager.at_end()) {
pager.init();
pager.clear_status();
return 0;
}
TRY(setup_tty(!dont_switch_buffer));
ScopeGuard teardown_guard([] {
@ -585,7 +601,6 @@ ErrorOr<int> serenity_main(Main::Arguments arguments)
ignore_action.sa_handler = { SIG_IGN };
TRY(Core::System::sigaction(SIGINT, &ignore_action, nullptr));
Pager pager(filename, file, stdout, prompt, show_line_numbers);
pager.init();
StringBuilder modifier_buffer = StringBuilder(10);