mirror of
https://github.com/SerenityOS/serenity.git
synced 2025-01-23 09:51:57 -05:00
less: Use LibLine to compute line arrangement with fixed line width
This commit is contained in:
parent
299ca10fd5
commit
84fd011c49
2 changed files with 9 additions and 41 deletions
|
@ -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
|
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
|
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
|
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
|
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
|
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)
|
target_link_libraries(js PRIVATE LibCrypto LibJS LibLine LibLocale LibTextCodec)
|
||||||
link_with_locale_data(js)
|
link_with_locale_data(js)
|
||||||
target_link_libraries(keymap PRIVATE LibKeyboard)
|
target_link_libraries(keymap PRIVATE LibKeyboard)
|
||||||
|
target_link_libraries(less PRIVATE LibLine)
|
||||||
target_link_libraries(lspci PRIVATE LibPCIDB)
|
target_link_libraries(lspci PRIVATE LibPCIDB)
|
||||||
target_link_libraries(lsusb PRIVATE LibUSBDB)
|
target_link_libraries(lsusb PRIVATE LibUSBDB)
|
||||||
target_link_libraries(man PRIVATE LibMarkdown)
|
target_link_libraries(man PRIVATE LibMarkdown)
|
||||||
|
|
|
@ -4,19 +4,12 @@
|
||||||
* SPDX-License-Identifier: BSD-2-Clause
|
* SPDX-License-Identifier: BSD-2-Clause
|
||||||
*/
|
*/
|
||||||
|
|
||||||
#include <AK/Format.h>
|
|
||||||
#include <AK/HashMap.h>
|
|
||||||
#include <AK/LexicalPath.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/ArgsParser.h>
|
||||||
#include <LibCore/System.h>
|
#include <LibCore/System.h>
|
||||||
|
#include <LibLine/Editor.h>
|
||||||
#include <LibMain/Main.h>
|
#include <LibMain/Main.h>
|
||||||
#include <csignal>
|
#include <csignal>
|
||||||
#include <ctype.h>
|
|
||||||
#include <fcntl.h>
|
|
||||||
#include <stdio.h>
|
#include <stdio.h>
|
||||||
#include <sys/ioctl.h>
|
#include <sys/ioctl.h>
|
||||||
#include <termios.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)
|
static Vector<StringView> wrap_line(String const& string, size_t width)
|
||||||
{
|
{
|
||||||
Utf8View utf8(string);
|
auto const result = Line::Editor::actual_rendered_string_metrics(string, {}, width);
|
||||||
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;
|
|
||||||
}
|
|
||||||
|
|
||||||
Vector<StringView> spans;
|
Vector<StringView> spans;
|
||||||
size_t span_start = 0;
|
size_t span_start = 0;
|
||||||
for (auto split : splits) {
|
for (auto const& line_metric : result.line_metrics) {
|
||||||
spans.append(string.substring_view(span_start, split - span_start));
|
VERIFY(line_metric.bit_length.has_value());
|
||||||
span_start = split;
|
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));
|
spans.append(string.substring_view(span_start));
|
||||||
|
|
||||||
|
|
Loading…
Add table
Reference in a new issue