mirror of
https://github.com/ArthurSonzogni/FTXUI.git
synced 2025-09-18 09:08:08 +08:00
Add webassembly support
This commit is contained in:
@@ -38,6 +38,11 @@ namespace ftxui {
|
||||
|
||||
namespace {
|
||||
|
||||
void Flush() {
|
||||
// Emscripten doesn't implement flush. We interpret zero as flush.
|
||||
std::cout << std::flush << (char)0;
|
||||
}
|
||||
|
||||
constexpr int timeout_milliseconds = 20;
|
||||
constexpr int timeout_microseconds = timeout_milliseconds * 1000;
|
||||
#if defined(_WIN32)
|
||||
@@ -90,8 +95,25 @@ void EventListener(std::atomic<bool>* quit,
|
||||
}
|
||||
}
|
||||
|
||||
#else
|
||||
#elif defined(__EMSCRIPTEN__)
|
||||
#include <emscripten.h>
|
||||
|
||||
// Read char from the terminal.
|
||||
void EventListener(std::atomic<bool>* quit, Sender<Event> out) {
|
||||
(void)timeout_microseconds;
|
||||
auto parser = TerminalInputParser(std::move(out));
|
||||
|
||||
char c;
|
||||
while (!*quit) {
|
||||
while(read(STDIN_FILENO, &c, 1), c)
|
||||
parser.Add(c);
|
||||
|
||||
emscripten_sleep(1);
|
||||
parser.Timeout(1);
|
||||
}
|
||||
}
|
||||
|
||||
#else
|
||||
#include <sys/time.h>
|
||||
|
||||
int CheckStdinReady(int usec_timeout) {
|
||||
@@ -260,12 +282,13 @@ void ScreenInteractive::Loop(Component* component) {
|
||||
// Hide the cursor and show it at exit.
|
||||
std::cout << HIDE_CURSOR;
|
||||
std::cout << DISABLE_LINE_WRAP;
|
||||
std::cout << std::flush;
|
||||
Flush();
|
||||
on_exit_functions.push([&] {
|
||||
std::cout << reset_cursor_position;
|
||||
std::cout << SHOW_CURSOR;
|
||||
std::cout << ENABLE_LINE_WRAP;
|
||||
std::cout << std::endl;
|
||||
Flush();
|
||||
});
|
||||
|
||||
auto event_listener =
|
||||
@@ -276,7 +299,8 @@ void ScreenInteractive::Loop(Component* component) {
|
||||
if (!event_receiver_->HasPending()) {
|
||||
std::cout << reset_cursor_position << ResetPosition();
|
||||
Draw(component);
|
||||
std::cout << ToString() << set_cursor_position << std::flush;
|
||||
std::cout << ToString() << set_cursor_position;
|
||||
Flush();
|
||||
Clear();
|
||||
}
|
||||
Event event;
|
||||
|
@@ -42,7 +42,7 @@ TEST(TextTest, ScreenBigger2) {
|
||||
Screen screen(6, 2);
|
||||
Render(screen, element);
|
||||
|
||||
EXPECT_EQ("test \n ", screen.ToString());
|
||||
EXPECT_EQ("test \r\n ", screen.ToString());
|
||||
}
|
||||
|
||||
// See https://github.com/ArthurSonzogni/FTXUI/issues/2#issuecomment-504871456
|
||||
@@ -51,8 +51,8 @@ TEST(TextTest, CJK) {
|
||||
Screen screen(6, 3);
|
||||
Render(screen, element);
|
||||
EXPECT_EQ(
|
||||
"┌────┐\n"
|
||||
"│测试│\n"
|
||||
"┌────┐\r\n"
|
||||
"│测试│\r\n"
|
||||
"└────┘",
|
||||
screen.ToString());
|
||||
}
|
||||
@@ -63,8 +63,8 @@ TEST(TextTest, CJK_2) {
|
||||
Screen screen(5, 3);
|
||||
Render(screen, element);
|
||||
EXPECT_EQ(
|
||||
"┌───┐\n"
|
||||
"│测试\n"
|
||||
"┌───┐\r\n"
|
||||
"│测试\r\n"
|
||||
"└───┘",
|
||||
screen.ToString());
|
||||
}
|
||||
@@ -75,8 +75,8 @@ TEST(TextTest, CJK_3) {
|
||||
Screen screen(4, 3);
|
||||
Render(screen, element);
|
||||
EXPECT_EQ(
|
||||
"┌──┐\n"
|
||||
"│测│\n"
|
||||
"┌──┐\r\n"
|
||||
"│测│\r\n"
|
||||
"└──┘",
|
||||
screen.ToString());
|
||||
}
|
||||
|
@@ -6,6 +6,7 @@ using namespace ftxui;
|
||||
using namespace ftxui;
|
||||
|
||||
std::string rotate(std::string str) {
|
||||
str.erase(std::remove(str.begin(), str.end(), '\r'), str.end());
|
||||
str.erase(std::remove(str.begin(), str.end(), '\n'), str.end());
|
||||
return str;
|
||||
}
|
||||
|
@@ -2,6 +2,7 @@
|
||||
|
||||
#include <algorithm>
|
||||
#include <sstream>
|
||||
#include <iostream>
|
||||
|
||||
#include "ftxui/dom/node.hpp"
|
||||
#include "ftxui/screen/string.hpp"
|
||||
@@ -149,6 +150,7 @@ Screen::Screen(int dimx, int dimy)
|
||||
}
|
||||
|
||||
/// Produce a std::string that can be used to print the Screen on the terminal.
|
||||
/// Don't forget to flush stdout. Alternatively, you can use Screen::Print();
|
||||
std::string Screen::ToString() {
|
||||
std::wstringstream ss;
|
||||
|
||||
@@ -158,7 +160,7 @@ std::string Screen::ToString() {
|
||||
for (int y = 0; y < dimy_; ++y) {
|
||||
if (y != 0) {
|
||||
UpdatePixelStyle(ss, previous_pixel, final_pixel);
|
||||
ss << '\n';
|
||||
ss << L"\r\n";
|
||||
}
|
||||
for (int x = 0; x < dimx_;) {
|
||||
auto& pixel = pixels_[y][x];
|
||||
@@ -181,6 +183,10 @@ std::string Screen::ToString() {
|
||||
return to_string(ss.str());
|
||||
}
|
||||
|
||||
void Screen::Print() {
|
||||
std::cout << ToString() << std::flush << (char)0;
|
||||
}
|
||||
|
||||
/// @brief Access a character a given position.
|
||||
/// @param x The character position along the x-axis.
|
||||
/// @param y The character position along the y-axis.
|
||||
@@ -254,6 +260,7 @@ void Screen::ApplyShader() {
|
||||
}
|
||||
}
|
||||
}
|
||||
|
||||
// clang-format on
|
||||
|
||||
} // namespace ftxui
|
||||
|
@@ -19,7 +19,7 @@ namespace ftxui {
|
||||
|
||||
Terminal::Dimensions Terminal::Size() {
|
||||
#if defined(__EMSCRIPTEN__)
|
||||
return Dimensions{80, 43};
|
||||
return Dimensions{140, 43};
|
||||
#elif defined(_WIN32)
|
||||
CONSOLE_SCREEN_BUFFER_INFO csbi;
|
||||
int columns, rows;
|
||||
@@ -48,6 +48,10 @@ bool Contains(const std::string& s, const char* key) {
|
||||
static bool cached = false;
|
||||
Terminal::Color cached_supported_color;
|
||||
Terminal::Color ComputeColorSupport() {
|
||||
#if defined(__EMSCRIPTEN__)
|
||||
return Terminal::Color::TrueColor;
|
||||
#endif
|
||||
|
||||
std::string COLORTERM = Safe(std::getenv("COLORTERM"));
|
||||
if (Contains(COLORTERM, "24bit") || Contains(COLORTERM, "truecolor"))
|
||||
return Terminal::Color::TrueColor;
|
||||
|
Reference in New Issue
Block a user