less: Use LibLine to compute line arrangement with fixed line width

This commit is contained in:
Lucas CHOLLET 2022-11-08 01:58:46 +01:00 committed by Ali Mohammad Pur
parent 299ca10fd5
commit 84fd011c49
Notes: sideshowbarker 2024-07-17 06:28:38 +09:00
2 changed files with 9 additions and 41 deletions

View file

@ -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)

View file

@ -4,19 +4,12 @@
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/Format.h>
#include <AK/HashMap.h>
#include <AK/LexicalPath.h>
#include <AK/String.h>
#include <AK/StringBuilder.h>
#include <AK/Utf8View.h>
#include <AK/Vector.h>
#include <LibCore/ArgsParser.h>
#include <LibCore/System.h>
#include <LibLine/Editor.h>
#include <LibMain/Main.h>
#include <csignal>
#include <ctype.h>
#include <fcntl.h>
#include <stdio.h>
#include <sys/ioctl.h>
#include <termios.h>
@ -59,41 +52,15 @@ static ErrorOr<void> teardown_tty(bool switch_buffer)
static Vector<StringView> wrap_line(String const& string, size_t width)
{
Utf8View utf8(string);
Vector<size_t> 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<StringView> 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));