From 84fd011c499cd36e4d734b6be3250a46014a68ff Mon Sep 17 00:00:00 2001 From: Lucas CHOLLET Date: Tue, 8 Nov 2022 01:58:46 +0100 Subject: [PATCH] less: Use LibLine to compute line arrangement with fixed line width --- Userland/Utilities/CMakeLists.txt | 3 +- Userland/Utilities/less.cpp | 47 +++++-------------------------- 2 files changed, 9 insertions(+), 41 deletions(-) diff --git a/Userland/Utilities/CMakeLists.txt b/Userland/Utilities/CMakeLists.txt index cadc73746b..f56eaa66ae 100644 --- a/Userland/Utilities/CMakeLists.txt +++ b/Userland/Utilities/CMakeLists.txt @@ -4,7 +4,7 @@ list(APPEND REQUIRED_TARGETS arp base64 basename cat chmod chown clear comm cp cut date dd df diff dirname dmesg du echo env expr false fgrep file find grep groups head host hostname id ifconfig kill killall ln logout ls mkdir mount mv nproc pgrep pidof ping pkill pmap ps readlink realpath reboot rm rmdir route seq shutdown sleep sort stat stty su tail test - touch tr true umount uname uniq uptime w wc which whoami xargs yes less + touch tr true umount uname uniq uptime w wc which whoami xargs yes ) list(APPEND RECOMMENDED_TARGETS adjtime aplay abench asctl bt checksum chres cksum copy fortune gunzip gzip init install keymap lsirq lsof lspci man mknod mktemp @@ -95,6 +95,7 @@ target_link_libraries(jail-create PRIVATE LibCore LibMain) target_link_libraries(js PRIVATE LibCrypto LibJS LibLine LibLocale LibTextCodec) link_with_locale_data(js) target_link_libraries(keymap PRIVATE LibKeyboard) +target_link_libraries(less PRIVATE LibLine) target_link_libraries(lspci PRIVATE LibPCIDB) target_link_libraries(lsusb PRIVATE LibUSBDB) target_link_libraries(man PRIVATE LibMarkdown) diff --git a/Userland/Utilities/less.cpp b/Userland/Utilities/less.cpp index 28e2e31fdd..7d69ae68b4 100644 --- a/Userland/Utilities/less.cpp +++ b/Userland/Utilities/less.cpp @@ -4,19 +4,12 @@ * SPDX-License-Identifier: BSD-2-Clause */ -#include -#include #include -#include -#include -#include -#include #include #include +#include #include #include -#include -#include #include #include #include @@ -59,41 +52,15 @@ static ErrorOr teardown_tty(bool switch_buffer) static Vector wrap_line(String const& string, size_t width) { - Utf8View utf8(string); - Vector splits; - - size_t offset = 0; - - bool in_ansi = false; - // for (auto codepoint : string) { - for (auto it = utf8.begin(); it != utf8.end(); ++it) { - if (offset >= width) { - splits.append(utf8.byte_offset_of(it)); - offset = 0; - } - - if (*it == '\e') - in_ansi = true; - - if (!in_ansi) { - if (*it == '\t') { - // Tabs are a special case, because their width is variable. - offset += (8 - (offset % 8)); - } else { - // FIXME: calculate the printed width of the character. - offset++; - } - } - - if (isalpha(*it)) - in_ansi = false; - } + auto const result = Line::Editor::actual_rendered_string_metrics(string, {}, width); Vector spans; size_t span_start = 0; - for (auto split : splits) { - spans.append(string.substring_view(span_start, split - span_start)); - span_start = split; + for (auto const& line_metric : result.line_metrics) { + VERIFY(line_metric.bit_length.has_value()); + auto const bit_length = line_metric.bit_length.value(); + spans.append(string.substring_view(span_start, bit_length)); + span_start += bit_length; } spans.append(string.substring_view(span_start));