summaryrefslogtreecommitdiff
path: root/Userland/Utilities
diff options
context:
space:
mode:
authorKyle Ambroff-Kao <kyle@ambroffkao.com>2021-10-30 00:46:51 -0700
committerAndreas Kling <kling@serenityos.org>2021-10-31 12:34:37 +0100
commit0329a37271b7aa0f2937e26b2ebccc3bc943d9d4 (patch)
tree805899fd175c1d8d15a42f97e4bd72cc831cd0d4 /Userland/Utilities
parentfed9cb5d2d3833c02b27abb4a4e19fed85a6c27e (diff)
downloadserenity-0329a37271b7aa0f2937e26b2ebccc3bc943d9d4.zip
Userland: Handle terminal window resizing in less(1)
Before this patch less would query the terminal geometry only at startup and use this information to render the file when appropriate. If the terminal is resized then the output is broken in several different ways because of this. This patch adds a SIGWINCH signal handler receive notification any time the terminal is resized. This signal handler just sets a flag to notify the main loop that a resize has occurred. The main loop of the program just calls get_key_sequence() to get input from the user, interpreting keystrokes as commands like scroll up or down. The get_key_sequence() function has been changed to return Optional<String>, so it either returns a keystroke from the user or it returns nothing as an empty Optional. While the user is not pressing any keys on the keyboard, the program is blocking on a read() system call in get_key_sequence(). When SIGWINCH is received, this read() will return with -1 and errno is set to EINTR since the system call was interrupted by the signal. When this happens we just return an empty Optional. The mainloop now checks to see if a resize has been requested by checking the flag, and if it has it performs a resize. init() now just calls resize() since the required logic is the same. Setters for m_filename and m_prompt are removed because these are now just initialized by the constructor, as they never change for the life of the program.
Diffstat (limited to 'Userland/Utilities')
-rw-r--r--Userland/Utilities/less.cpp106
1 files changed, 73 insertions, 33 deletions
diff --git a/Userland/Utilities/less.cpp b/Userland/Utilities/less.cpp
index f785dae5c8..37672ccc51 100644
--- a/Userland/Utilities/less.cpp
+++ b/Userland/Utilities/less.cpp
@@ -11,6 +11,7 @@
#include <AK/Utf8View.h>
#include <AK/Vector.h>
#include <LibCore/ArgsParser.h>
+#include <csignal>
#include <ctype.h>
#include <fcntl.h>
#include <stdio.h>
@@ -19,7 +20,9 @@
#include <unistd.h>
static struct termios g_save;
-static struct winsize g_wsize;
+
+// Flag set by a SIGWINCH signal handler to notify the main loop that the window has been resized.
+static Atomic<bool> g_resized { false };
static void setup_tty(bool switch_buffer)
{
@@ -28,11 +31,6 @@ static void setup_tty(bool switch_buffer)
perror("tcgetattr(3)");
}
- // Get the window size.
- if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &g_wsize) == -1) {
- perror("ioctl(2)");
- }
-
struct termios raw = g_save;
raw.c_lflag &= ~(ECHO | ICANON);
@@ -95,11 +93,11 @@ static Vector<String> wrap_line(Utf8View const& string, size_t width)
class Pager {
public:
- Pager(FILE* file, FILE* tty, size_t width, size_t height)
+ Pager(StringView const& filename, FILE* file, FILE* tty, StringView const& prompt)
: m_file(file)
, m_tty(tty)
- , m_width(width)
- , m_height(height)
+ , m_filename(filename)
+ , m_prompt(prompt)
{
}
@@ -184,13 +182,44 @@ public:
void init()
{
- while (m_lines.size() < m_height) {
- if (!read_line())
+ resize();
+ }
+
+ void resize()
+ {
+ // First, we get the current size of the window.
+ struct winsize window;
+ if (ioctl(STDOUT_FILENO, TIOCGWINSZ, &window) == -1) {
+ perror("ioctl(2)");
+ return;
+ }
+
+ auto original_height = m_height;
+
+ m_width = window.ws_col;
+ m_height = window.ws_row;
+
+ // If the window is now larger than it was before, read more lines of
+ // the file so that there is enough data to fill the whole screen.
+ //
+ // m_height is initialized to 0, so if the terminal was 80x25 when
+ // this is called for the first time, then additional_lines will be 80
+ // and 80 lines of text will be buffered.
+ auto additional_lines = m_height - original_height;
+ while (additional_lines > 0) {
+ if (!read_line()) {
+ // End of file has been reached.
break;
+ }
+ --additional_lines;
}
- write_range(0, m_height - 1);
+
+ // Next, we repaint the whole screen. We need to figure out what line was at the top
+ // of the screen, and seek there and re-display everything again.
+ clear_status();
+ out("\e[2J\e[0G\e[0d");
+ write_range(m_line, m_height - 1);
status_line();
- m_line = 0;
fflush(m_tty);
}
@@ -215,16 +244,6 @@ public:
out(m_tty, " \e[27m");
}
- void set_filename(StringView const& filename)
- {
- m_filename = filename;
- }
-
- void set_prompt(StringView const& prompt)
- {
- m_prompt = prompt;
- }
-
bool read_line()
{
char* line = nullptr;
@@ -302,19 +321,26 @@ private:
FILE* m_file;
FILE* m_tty;
- size_t m_width;
- size_t m_height;
+ size_t m_width { 0 };
+ size_t m_height { 0 };
String m_filename;
String m_prompt;
};
-static String get_key_sequence()
+/// Return the next key sequence, or nothing if a signal is received while waiting
+/// to read the next sequence.
+static Optional<String> get_key_sequence()
{
// We need a buffer to handle ansi sequences.
char buff[8];
+
ssize_t n = read(STDOUT_FILENO, buff, sizeof(buff));
- return String(buff, n);
+ if (n > 0) {
+ return String(buff, n);
+ } else {
+ return {};
+ }
}
static void cat_file(FILE* file)
@@ -337,7 +363,7 @@ static void cat_file(FILE* file)
int main(int argc, char** argv)
{
- if (pledge("stdio rpath tty", nullptr) < 0) {
+ if (pledge("stdio rpath tty sigaction", nullptr) < 0) {
perror("pledge");
return 1;
}
@@ -367,6 +393,12 @@ int main(int argc, char** argv)
exit(1);
}
+ // On SIGWINCH set this flag so that the main-loop knows when the terminal
+ // has been resized.
+ signal(SIGWINCH, [](auto) {
+ g_resized = true;
+ });
+
if (pledge("stdio tty", nullptr) < 0) {
perror("pledge");
return 1;
@@ -386,14 +418,22 @@ int main(int argc, char** argv)
setup_tty(!dont_switch_buffer);
- Pager pager(file, stdout, g_wsize.ws_col, g_wsize.ws_row);
- pager.set_filename(filename);
- pager.set_prompt(prompt);
-
+ Pager pager(filename, file, stdout, prompt);
pager.init();
StringBuilder modifier_buffer = StringBuilder(10);
- for (String sequence;; sequence = get_key_sequence()) {
+ for (Optional<String> sequence_value;; sequence_value = get_key_sequence()) {
+ if (g_resized) {
+ g_resized = false;
+ pager.resize();
+ }
+
+ if (!sequence_value.has_value()) {
+ continue;
+ }
+
+ const auto& sequence = sequence_value.value();
+
if (sequence.to_uint().has_value()) {
modifier_buffer.append(sequence);
} else {