Add support for italics (#1009)

Co-authored-by: Ken Reneris <ms/devops kreneris@microsoft.com>
Co-authored-by: ArthurSonzogni <sonzogniarthur@gmail.com>
This commit is contained in:
KenReneris
2025-03-22 10:03:43 -07:00
committed by GitHub
parent bc682d25a6
commit 2c9a828402
14 changed files with 107 additions and 3 deletions

View File

@@ -75,6 +75,7 @@ class DBox : public Node {
acc->bold = pixel.bold;
acc->dim = pixel.dim;
acc->inverted = pixel.inverted;
acc->italic = pixel.italic;
acc->underlined = pixel.underlined;
acc->underlined_double = pixel.underlined_double;
acc->strikethrough = pixel.strikethrough;

35
src/ftxui/dom/italic.cpp Normal file
View File

@@ -0,0 +1,35 @@
// Copyright 2025 Arthur Sonzogni. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.
#include <memory> // for make_shared
#include <utility> // for move
#include "ftxui/dom/elements.hpp" // for Element, underlinedDouble
#include "ftxui/dom/node.hpp" // for Node
#include "ftxui/dom/node_decorator.hpp" // for NodeDecorator
#include "ftxui/screen/box.hpp" // for Box
#include "ftxui/screen/screen.hpp" // for Pixel, Screen
namespace ftxui {
/// @brief Apply a underlinedDouble to text.
/// @ingroup dom
Element italic(Element child) {
class Impl : public NodeDecorator {
public:
using NodeDecorator::NodeDecorator;
void Render(Screen& screen) override {
for (int y = box_.y_min; y <= box_.y_max; ++y) {
for (int x = box_.x_min; x <= box_.x_max; ++x) {
screen.PixelAt(x, y).italic = true;
}
}
Node::Render(screen);
}
};
return std::make_shared<Impl>(std::move(child));
}
} // namespace ftxui

View File

@@ -0,0 +1,22 @@
// Copyright 2025 Arthur Sonzogni. All rights reserved.
// Use of this source code is governed by the MIT license that can be found in
// the LICENSE file.
#include <string> // for allocator, string
#include "ftxui/dom/elements.hpp" // for operator|, text, bold, Element
#include "ftxui/dom/node.hpp" // for Render
#include "ftxui/screen/screen.hpp" // for Screen, Pixel
#include "gtest/gtest.h" // for Test, AssertionResult, EXPECT_TRUE, Message, TEST, TestPartResult
// NOLINTBEGIN
namespace ftxui {
TEST(ItalicTest, Basic) {
auto element = text("text") | italic;
Screen screen(5, 1);
Render(screen, element);
EXPECT_TRUE(screen.PixelAt(0, 0).italic);
}
} // namespace ftxui
// NOLINTEND

View File

@@ -106,6 +106,12 @@ void UpdatePixelStyle(const Screen* screen,
: "\x1B[27m"); // INVERTED_RESET
}
// Italics
if (FTXUI_UNLIKELY(next.italic != prev.italic)) {
ss << (next.italic ? "\x1B[3m" // ITALIC_SET
: "\x1B[23m"); // ITALIC_RESET
}
// StrikeThrough
if (FTXUI_UNLIKELY(next.strikethrough != prev.strikethrough)) {
ss << (next.strikethrough ? "\x1B[9m" // CROSSED_OUT