From ebb822def9a3ec954f56bd7e1bd6709b268e95cb Mon Sep 17 00:00:00 2001 From: Tim Ledbetter Date: Sun, 24 Sep 2023 22:02:36 +0100 Subject: [PATCH] less: Add the `-F` option to quit if the input fits on one screen --- Base/usr/share/man/man1/less.md | 1 + Userland/Utilities/less.cpp | 19 +++++++++++++++++-- 2 files changed, 18 insertions(+), 2 deletions(-) diff --git a/Base/usr/share/man/man1/less.md b/Base/usr/share/man/man1/less.md index 447aee96c4..fa2afcd8c7 100644 --- a/Base/usr/share/man/man1/less.md +++ b/Base/usr/share/man/man1/less.md @@ -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` diff --git a/Userland/Utilities/less.cpp b/Userland/Utilities/less.cpp index 9c3b784f23..53ebfb409f 100644 --- a/Userland/Utilities/less.cpp +++ b/Userland/Utilities/less.cpp @@ -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 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 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 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 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);