FTXUI 7.0.0
C++ functional terminal UI.
Loading...
Searching...
No Matches
app.cpp
Go to the documentation of this file.
1// Copyright 2020 Arthur Sonzogni. All rights reserved.
2// Use of this source code is governed by the MIT license that can be found in
3// the LICENSE file.
4#include <algorithm> // for any_of, copy, max, min
5#include <array> // for array
6#include <atomic>
7#include <chrono> // for operator-, milliseconds, operator>=, duration, common_type<>::type, time_point
8#include <csignal> // for signal, SIGTSTP, SIGABRT, SIGWINCH, raise, SIGFPE, SIGILL, SIGINT, SIGSEGV, SIGTERM, __sighandler_t, size_t
9#include <cstdint>
10#include <cstdio> // for fileno, stdin
12#include <ftxui/component/task.hpp> // for Task, Closure, AnimationTask
13#include <ftxui/screen/screen.hpp> // for Cell, Screen::Cursor, Screen, Screen::Cursor::Hidden
14#include <functional> // for function
15#include <initializer_list> // for initializer_list
16#include <iostream> // for cout, ostream, operator<<, basic_ostream, endl, flush
17#include <map>
18#include <memory>
19#include <stack> // for stack
20#include <string>
21#include <string_view>
22#include <thread> // for thread, sleep_for
23#include <tuple> // for _Swallow_assign, ignore
24#include <type_traits>
25#include <utility> // for move, swap
26#include <variant> // for visit, variant
27#include <vector> // for vector
28
29#include "ftxui/component/animation.hpp" // for TimePoint, Clock, Duration, Params, RequestAnimationFrame
30#include "ftxui/component/captured_mouse.hpp" // for CapturedMouse, CapturedMouseInterface
31#include "ftxui/component/component_base.hpp" // for ComponentBase
32#include "ftxui/component/event.hpp" // for Event
33#include "ftxui/component/loop.hpp" // for Loop
36#include "ftxui/component/terminal_input_parser.hpp" // for TerminalInputParser
37#include "ftxui/dom/node.hpp" // for Node, Render
38#include "ftxui/screen/cell.hpp" // for Cell
39#include "ftxui/screen/terminal.hpp" // for Dimensions, Size
40#include "ftxui/screen/util.hpp" // for util::clamp
41#include "ftxui/util/autoreset.hpp" // for AutoReset
42
43#if defined(_WIN32)
44#define DEFINE_CONSOLEV2_PROPERTIES
45#define WIN32_LEAN_AND_MEAN
46#ifndef NOMINMAX
47#define NOMINMAX
48#endif
49#include <io.h>
50#include <windows.h>
51#else
52#include <fcntl.h>
53#include <poll.h>
54#include <sys/poll.h>
55#include <sys/types.h>
56#include <termios.h> // for tcsetattr, termios, tcgetattr, TCSANOW, cc_t, ECHO, ICANON, VMIN, VTIME
57#include <unistd.h> // for STDIN_FILENO, STDOUT_FILENO, read
58#endif
59
60#if defined(__EMSCRIPTEN__)
61#include <emscripten.h>
62#endif
63
64namespace ftxui {
65
66enum class AppDimension {
68 Fixed,
71};
72
73namespace animation {
75 auto* screen = App::Active();
76 if (screen) {
77 screen->RequestAnimationFrame();
78 }
79}
80} // namespace animation
81
82#if defined(__EMSCRIPTEN__)
83extern "C" {
84EMSCRIPTEN_KEEPALIVE
85void ftxui_on_resize(int columns, int rows) {
87 columns,
88 rows,
89 });
90 std::raise(SIGWINCH);
91}
92}
93#endif
94
95struct App::Internal {
96 App* public_;
97
98 App* suspended_screen_ = nullptr;
99 const AppDimension dimension_;
100 const bool use_alternative_screen_;
101
102 bool track_mouse_ = true;
103
104 std::string set_cursor_position_;
105 std::string reset_cursor_position_;
106
107 std::atomic<bool> quit_{false};
108 bool installed_ = false;
109 bool animation_requested_ = false;
110 animation::TimePoint previous_animation_time_;
111
112 int cursor_x_ = 1;
113 int cursor_y_ = 1;
114
115 std::uint64_t frame_count_ = 0;
116 bool mouse_captured = false;
117 bool previous_frame_resized_ = false;
118
119 bool frame_valid_ = false;
120
121 bool force_handle_ctrl_c_ = true;
122 bool force_handle_ctrl_z_ = true;
123
124 int cursor_reset_shape_ = 1;
125
126 // Piped input handling state (POSIX only)
127 bool handle_piped_input_ = true;
128 bool is_stdin_a_tty_ = false;
129 bool is_stdout_a_tty_ = false;
130 // File descriptor for /dev/tty, used for piped input handling.
131 int tty_fd_ = -1;
132
133 std::string terminal_name_ = "unknown";
134 int terminal_version_ = 0;
135
136 std::string terminal_emulator_name_ = "unknown";
137 std::string terminal_emulator_version_ = "unknown";
138
139 std::vector<int> terminal_capabilities_;
140
141 // Selection API:
142 CapturedMouse selection_pending_;
143 struct SelectionData {
144 int start_x = -1;
145 int start_y = -1;
146 int end_x = -2;
147 int end_y = -2;
148 bool empty = true;
149 bool operator==(const SelectionData& other) const {
150 if (empty && other.empty) {
151 return true;
152 }
153 if (empty || other.empty) {
154 return false;
155 }
156 return start_x == other.start_x && start_y == other.start_y &&
157 end_x == other.end_x && end_y == other.end_y;
158 }
159 bool operator!=(const SelectionData& other) const {
160 return !(*this == other);
161 }
162 };
163 SelectionData selection_data_;
164 SelectionData selection_data_previous_;
165 std::unique_ptr<Selection> selection_;
166 std::function<void()> selection_on_change_;
167
168 Component component_;
169
170 // Pre-existing in Internal:
171 TerminalInputParser terminal_input_parser;
172 task::TaskRunner task_runner;
173 std::chrono::time_point<std::chrono::steady_clock> last_char_time =
174 std::chrono::steady_clock::now();
175 std::string output_buffer;
176
177 class ThrottledRequest {
178 public:
179 ThrottledRequest(App::Internal* internal, std::function<void()> send)
180 : internal_(internal), send_(std::move(send)) {}
181
182 void Request(bool force = false) {
183 if (!internal_->is_stdin_a_tty_) {
184 return;
185 }
186
187 if (force) {
188 Send();
189 return;
190 }
191
192 // Allow only one pending request at a time. This is to avoid flooding the
193 // terminal with requests.
194 if (HasPending()) {
195 return;
196 }
197
198 const auto now = std::chrono::steady_clock::now();
199 const auto delta = now - last_request_time_;
200 const auto delay = std::chrono::milliseconds(500) - delta;
201
202 if (delay <= std::chrono::milliseconds(0)) {
203 Send();
204 return;
205 }
206
207 request_queued_ = true;
208 internal_->task_runner.PostDelayedTask(
209 [this] {
210 request_queued_ = false;
211 Request();
212 },
213 delay);
214 }
215
216 void OnReply() { pending_request_ = false; }
217
218 bool HasPending() const {
219 if (pending_request_) {
220 const auto now = std::chrono::steady_clock::now();
221 if (now - last_sent_time_ < std::chrono::seconds(5)) {
222 return true;
223 }
224 }
225 return request_queued_;
226 }
227
228 private:
229 void Send() {
230 last_sent_time_ = std::chrono::steady_clock::now();
231 pending_request_ = true;
232 send_();
233 }
234
235 App::Internal* internal_;
236 std::function<void()> send_;
237 bool pending_request_ = false;
238 std::chrono::steady_clock::time_point last_request_time_ =
239 std::chrono::steady_clock::now() - std::chrono::hours(1);
240 std::chrono::steady_clock::time_point last_sent_time_ =
241 std::chrono::steady_clock::now() - std::chrono::hours(1);
242 bool request_queued_ = false;
243 };
244
245 ThrottledRequest cursor_position_request;
246
247 MultiReceiverBuffer<Event> event_buffer;
248 std::unique_ptr<MultiReceiverBuffer<Event>::Receiver> setup_receiver;
249 std::unique_ptr<MultiReceiverBuffer<Event>::Receiver> main_loop_receiver;
250
251 Internal(App* app, AppDimension dimension, bool use_alternative_screen);
252
253 void ExitNow();
254 void Install();
255 void Uninstall();
256 void PreMain();
257 void PostMain();
258 bool HasQuitted();
259 void RunOnce(const Component& component);
260 void RunOnceBlocking(Component component);
261 void HandleTask(Component component, Task& task);
262 bool HandleSelection(bool handled, Event event);
263 void Draw(Component component);
264 std::string ResetCursorPosition();
265 void RequestCursorPosition(bool force = false);
266 void TerminalSend(std::string_view);
267 void TerminalFlush();
268 void InstallPipedInputHandling();
269 void InstallTerminalInfo();
270 void Signal(int signal);
271 size_t FetchTerminalEvents();
272 void PostAnimationTask();
273};
274
275namespace {
276
277App* g_active_screen = nullptr; // NOLINT
278
279std::stack<Closure> on_exit_functions; // NOLINT
280
281void OnExit() {
282 while (!on_exit_functions.empty()) {
283 on_exit_functions.top()();
284 on_exit_functions.pop();
285 }
286}
287
288// CSI: Control Sequence Introducer
289const std::string CSI = "\x1b["; // NOLINT
290 //
291// DCS: Device Control String
292const std::string DCS = "\x1bP"; // NOLINT
293
294// ST: String Terminator
295const std::string ST = "\x1b\\"; // NOLINT
296
297// DECRQSS: Request Status String
298// DECSCUSR: Set Cursor Style
299const std::string DECRQSS_DECSCUSR = DCS + "$q q" + ST; // NOLINT
300
301// DEC: Digital Equipment Corporation
302enum class DECMode : std::uint16_t {
303 kLineWrap = 7,
304 kCursor = 25,
305
306 kMouseX10 = 9,
307 kMouseVt200 = 1000,
308 kMouseVt200Highlight = 1001,
309
310 kMouseBtnEventMouse = 1002,
311 kMouseAnyEvent = 1003,
312
313 kMouseUtf8 = 1005,
314 kMouseSgrExtMode = 1006,
315 kMouseUrxvtMode = 1015,
316 kMouseSgrPixelsMode = 1016,
317 kAlternateScreen = 1049,
318};
319
320// Device Status Report (DSR) {
321enum class DSRMode : std::uint8_t {
322 kCursor = 6,
323};
324
325std::string Serialize(const std::vector<DECMode>& parameters) {
326 bool first = true;
327 std::string out;
328 for (const DECMode parameter : parameters) {
329 if (!first) {
330 out += ";";
331 }
332 out += std::to_string(int(parameter));
333 first = false;
334 }
335 return out;
336}
337
338// DEC Private Mode Set (DECSET)
339std::string Set(const std::vector<DECMode>& parameters) {
340 return CSI + "?" + Serialize(parameters) + "h";
341}
342
343// DEC Private Mode Reset (DECRST)
344std::string Reset(const std::vector<DECMode>& parameters) {
345 return CSI + "?" + Serialize(parameters) + "l";
346}
347
348// Device Status Report (DSR)
349std::string DeviceStatusReport(DSRMode ps) {
350 return CSI + std::to_string(int(ps)) + "n";
351}
352
353class CapturedMouseImpl : public CapturedMouseInterface {
354 public:
355 explicit CapturedMouseImpl(std::function<void(void)> callback)
356 : callback_(std::move(callback)) {}
357 ~CapturedMouseImpl() override { callback_(); }
358 CapturedMouseImpl(const CapturedMouseImpl&) = delete;
359 CapturedMouseImpl(CapturedMouseImpl&&) = delete;
360 CapturedMouseImpl& operator=(const CapturedMouseImpl&) = delete;
361 CapturedMouseImpl& operator=(CapturedMouseImpl&&) = delete;
362
363 private:
364 std::function<void(void)> callback_;
365};
366
367#if !defined(_WIN32)
368std::atomic<int> g_signal_exit_count = 0; // NOLINT
369std::atomic<int> g_signal_stop_count = 0; // NOLINT
370std::atomic<int> g_signal_resize_count = 0; // NOLINT
371#else
372std::atomic<int> g_signal_exit_count = 0; // NOLINT
373#endif
374
375// Tracks whether the terminal is currently configured in raw mode.
376// Used to prevent double-restoration in emergency and normal exits.
377std::atomic<bool> g_terminal_is_raw{false};
378
379// Stores the last received deferred signal (e.g. SIGINT, SIGTERM) to be
380// re-raised during uninstallation/exit.
381std::atomic<int> g_last_signal{0}; // NOLINT
382
383#if defined(_WIN32)
384using SignalHandler = void (*)(int);
385// Stores the original signal handlers before FTXUI installed its own.
386std::map<int, SignalHandler> g_old_signal_handlers;
387
388// Stores the original console modes to restore them during exit.
389DWORD g_original_stdout_mode = 0;
390DWORD g_original_stdin_mode = 0;
391bool g_has_original_console_mode = false;
392#else
393// Stores the original sigaction structures before FTXUI installed its own.
394std::map<int, struct sigaction> g_old_sigactions;
395
396// Stores the original termios terminal settings to restore them during exit.
397struct termios g_original_termios;
398bool g_has_original_termios = false;
399int g_tty_fd = -1;
400#endif
401
402// Restores the original signal handler for the given signal and re-raises it.
403// Async-signal-safe function.
404void RestoreSignalHandlerAndRaise(int signal) {
405#if defined(_WIN32)
406 auto it = g_old_signal_handlers.find(signal);
407 auto old_handler = (it != g_old_signal_handlers.end()) ? it->second : SIG_DFL;
408 std::signal(signal, old_handler);
409#else
410 auto it = g_old_sigactions.find(signal);
411 if (it != g_old_sigactions.end()) {
412 sigaction(signal, &it->second, nullptr);
413 } else {
414 struct sigaction sa;
415 sa.sa_handler = SIG_DFL;
416 sigemptyset(&sa.sa_mask);
417 sa.sa_flags = 0;
418 sigaction(signal, &sa, nullptr);
419 }
420#endif
421 std::raise(signal);
422}
423
424// Emergency terminal state restoration.
425// Async-signal-safe function.
426void RestoreTerminalEmergency() {
427 if (!g_terminal_is_raw.exchange(false)) {
428 return;
429 }
430#if defined(_WIN32)
431 if (g_has_original_console_mode) {
432 auto stdout_handle = GetStdHandle(STD_OUTPUT_HANDLE);
433 auto stdin_handle = GetStdHandle(STD_INPUT_HANDLE);
434 SetConsoleMode(stdout_handle, g_original_stdout_mode);
435 SetConsoleMode(stdin_handle, g_original_stdin_mode);
436 }
437#else
438 if (g_has_original_termios && g_tty_fd >= 0) {
439 const char restore_seq[] =
440 "\x1b[?25h" // Show cursor.
441 "\x1b[?1049l" // Switch to normal screen buffer.
442 "\x1b[?1000l" // Disable normal mouse tracking.
443 "\x1b[?1002l" // Disable button event mouse tracking.
444 "\x1b[?1003l" // Disable all motion mouse tracking.
445 "\x1b[?1006l" // Disable SGR mouse tracking.
446 "\x1b[?1015l" // Disable Urxvt mouse tracking.
447 "\x1b[?7h"; // Enable line wrapping.
448 std::ignore = write(STDOUT_FILENO, restore_seq, sizeof(restore_seq) - 1);
449 tcsetattr(g_tty_fd, TCSANOW, &g_original_termios);
450 }
451#endif
452}
453
454// Async signal safe function
455void RecordSignal(int signal) {
456 switch (signal) {
457 // Abnormal termination (e.g. abort() or assertion failure).
458 case SIGABRT:
459 // Erroneous arithmetic operation (e.g. division by zero).
460 case SIGFPE:
461 // Illegal instruction.
462 case SIGILL:
463 // Invalid memory reference (segmentation fault).
464 case SIGSEGV:
465#if !defined(_WIN32)
466 // Bus error (e.g. bad memory access alignment).
467 case SIGBUS:
468 // Bad system call.
469 case SIGSYS:
470#endif
471 {
472 RestoreTerminalEmergency();
473 RestoreSignalHandlerAndRaise(signal);
474 break;
475 }
476
477 // Terminal interrupt (e.g. Ctrl-C).
478 case SIGINT:
479 // Termination request.
480 case SIGTERM:
481#if !defined(_WIN32)
482 // Terminal quit (e.g. Ctrl-\, produces core dump).
483 case SIGQUIT:
484 // Hangup detected on controlling terminal or death of controlling process.
485 case SIGHUP:
486#endif
487 g_last_signal.store(signal);
488 g_signal_exit_count++;
489 break;
490
491#if !defined(_WIN32)
492 // Terminal stop signal (e.g. Ctrl-Z).
493 case SIGTSTP: // NOLINT
494 g_signal_stop_count++;
495 break;
496
497 // Terminal window size change.
498 case SIGWINCH: // NOLINT
499 g_signal_resize_count++;
500 break;
501#endif
502
503 default:
504 break;
505 }
506}
507
508void ExecuteSignalHandlers() {
509 if (g_last_signal.load() != 0) {
510 App::Private::Signal(*g_active_screen, SIGABRT);
511 }
512
513 int signal_exit_count = g_signal_exit_count.exchange(0);
514 while (signal_exit_count--) {
515 App::Private::Signal(*g_active_screen, SIGABRT);
516 }
517
518#if !defined(_WIN32)
519 int signal_stop_count = g_signal_stop_count.exchange(0);
520 while (signal_stop_count--) {
521 App::Private::Signal(*g_active_screen, SIGTSTP);
522 }
523
524 int signal_resize_count = g_signal_resize_count.exchange(0);
525 while (signal_resize_count--) {
526 App::Private::Signal(*g_active_screen, SIGWINCH);
527 }
528#endif
529}
530
531void InstallSignalHandler(int sig) {
532#if defined(_WIN32)
533 auto old_signal_handler = std::signal(sig, RecordSignal);
534 g_old_signal_handlers[sig] = old_signal_handler;
535 on_exit_functions.emplace(
536 [=] { std::ignore = std::signal(sig, old_signal_handler); });
537#else
538 struct sigaction sa;
539 sa.sa_handler = RecordSignal;
540 sigemptyset(&sa.sa_mask);
541 sa.sa_flags = SA_RESTART;
542 struct sigaction old_sa;
543 sigaction(sig, &sa, &old_sa);
544 g_old_sigactions[sig] = old_sa;
545 on_exit_functions.emplace([=] { sigaction(sig, &old_sa, nullptr); });
546#endif
547}
548
549} // namespace
550
551App::Internal::Internal(App* app,
552 AppDimension dimension,
553 bool use_alternative_screen)
554 : public_(app),
555 dimension_(dimension),
556 use_alternative_screen_(use_alternative_screen),
557 terminal_input_parser([&](Event event) {
558 event_buffer.Push(std::move(event));
559 public_->RequestAnimationFrame();
560 }),
561 cursor_position_request(this, [this] {
562 TerminalSend(DeviceStatusReport(DSRMode::kCursor));
563 }) {
564 setup_receiver = event_buffer.CreateReceiver();
565 main_loop_receiver = event_buffer.CreateReceiver();
566}
567
568void App::Internal::ExitNow() {
569 quit_ = true;
570}
571
572void App::Internal::Install() {
573 frame_valid_ = false;
574
575 // Flush the buffer for stdout to ensure whatever the user has printed before
576 // is fully applied before we start modifying the terminal configuration. This
577 // is important, because we are using two different channels (stdout vs
578 // termios/WinAPI) to communicate with the terminal emulator below. See
579 // https://github.com/ArthurSonzogni/FTXUI/issues/846
580 TerminalFlush();
581
582 InstallPipedInputHandling();
583
584 // After uninstalling the new configuration, flush it to the terminal to
585 // ensure it is fully applied:
586 on_exit_functions.emplace([this] { TerminalFlush(); });
587
588 // Install signal handlers to restore the terminal state on exit. The default
589 // signal handlers are restored on exit.
590 for (const int signal : {SIGTERM, SIGSEGV, SIGINT, SIGILL, SIGABRT, SIGFPE}) {
591 InstallSignalHandler(signal);
592 }
593
594// Save the old terminal configuration and restore it on exit.
595#if defined(_WIN32)
596 // Enable VT processing on stdout and stdin
597 auto stdout_handle = GetStdHandle(STD_OUTPUT_HANDLE);
598 auto stdin_handle = GetStdHandle(STD_INPUT_HANDLE);
599
600 DWORD out_mode = 0;
601 DWORD in_mode = 0;
602 GetConsoleMode(stdout_handle, &out_mode);
603 GetConsoleMode(stdin_handle, &in_mode);
604 g_original_stdout_mode = out_mode;
605 g_original_stdin_mode = in_mode;
606 g_has_original_console_mode = true;
607 on_exit_functions.push([=] { SetConsoleMode(stdout_handle, out_mode); });
608 on_exit_functions.push([=] { SetConsoleMode(stdin_handle, in_mode); });
609
610 // https://docs.microsoft.com/en-us/windows/console/setconsolemode
611 const int enable_virtual_terminal_processing = 0x0004;
612 const int disable_newline_auto_return = 0x0008;
613 out_mode |= enable_virtual_terminal_processing;
614 out_mode |= disable_newline_auto_return;
615
616 // https://docs.microsoft.com/en-us/windows/console/setconsolemode
617 const int enable_line_input = 0x0002;
618 const int enable_echo_input = 0x0004;
619 const int enable_virtual_terminal_input = 0x0200;
620 const int enable_window_input = 0x0008;
621 in_mode &= ~enable_echo_input;
622 in_mode &= ~enable_line_input;
623 in_mode |= enable_virtual_terminal_input;
624 in_mode |= enable_window_input;
625
626 SetConsoleMode(stdin_handle, in_mode);
627 SetConsoleMode(stdout_handle, out_mode);
628#else // POSIX (Linux & Mac)
629 for (const int signal :
630 {SIGWINCH, SIGTSTP, SIGBUS, SIGSYS, SIGQUIT, SIGHUP}) {
631 InstallSignalHandler(signal);
632 }
633
634 struct termios terminal; // NOLINT
635 tcgetattr(tty_fd_, &terminal);
636 g_original_termios = terminal;
637 g_tty_fd = tty_fd_;
638 g_has_original_termios = true;
639 on_exit_functions.emplace([terminal = terminal, tty_fd_ = tty_fd_] {
640 tcsetattr(tty_fd_, TCSANOW, &terminal);
641 });
642
643 // Enabling raw terminal input mode
644 terminal.c_iflag &= ~IGNBRK; // Disable ignoring break condition
645 terminal.c_iflag &= ~BRKINT; // Disable break causing input and output to be
646 // flushed
647 terminal.c_iflag &= ~PARMRK; // Disable marking parity errors.
648 terminal.c_iflag &= ~ISTRIP; // Disable stripping 8th bit off characters.
649 terminal.c_iflag &= ~INLCR; // Disable mapping NL to CR.
650 terminal.c_iflag &= ~IGNCR; // Disable ignoring CR.
651 terminal.c_iflag &= ~ICRNL; // Disable mapping CR to NL.
652 terminal.c_iflag &= ~IXON; // Disable XON/XOFF flow control on output
653
654 terminal.c_lflag &= ~ECHO; // Disable echoing input characters.
655 terminal.c_lflag &= ~ECHONL; // Disable echoing new line characters.
656 terminal.c_lflag &= ~ICANON; // Disable Canonical mode.
657 terminal.c_lflag &= ~ISIG; // Disable sending signal when hitting:
658 // - => DSUSP
659 // - C-Z => SUSP
660 // - C-C => INTR
661 // - C-d => QUIT
662 terminal.c_lflag &= ~IEXTEN; // Disable extended input processing
663 terminal.c_cflag |= CS8; // 8 bits per byte
664
665 terminal.c_cc[VMIN] = 0; // Minimum number of characters for non-canonical
666 // read.
667 terminal.c_cc[VTIME] = 0; // Timeout in deciseconds for non-canonical read.
668
669 tcsetattr(tty_fd_, TCSANOW, &terminal);
670
671#endif
672
673 auto enable = [&](const std::vector<DECMode>& parameters) {
674 TerminalSend(Set(parameters));
675 on_exit_functions.emplace(
676 [this, parameters] { TerminalSend(Reset(parameters)); });
677 };
678
679 auto disable = [&](const std::vector<DECMode>& parameters) {
680 TerminalSend(Reset(parameters));
681 on_exit_functions.emplace(
682 [this, parameters] { TerminalSend(Set(parameters)); });
683 };
684
685 if (use_alternative_screen_) {
686 enable({
687 DECMode::kAlternateScreen,
688 });
689 }
690
691 disable({
692 DECMode::kLineWrap,
693 });
694
695 if (track_mouse_) {
696 enable({DECMode::kMouseVt200});
697 enable({DECMode::kMouseAnyEvent});
698 enable({DECMode::kMouseUrxvtMode});
699 enable({DECMode::kMouseSgrExtMode});
700 }
701
702 // After installing the new configuration, flush it to the terminal to
703 // ensure it is fully applied:
704 TerminalFlush();
705
706 InstallTerminalInfo();
707
708 quit_ = false;
709
710 PostAnimationTask();
711
712 installed_ = true;
713 g_terminal_is_raw = true;
714}
715
716void App::Internal::Uninstall() {
717 g_terminal_is_raw = false;
718 installed_ = false;
719
720 // During shutdown, wait for all of the replies.
721 if (is_stdin_a_tty_ && is_stdout_a_tty_) {
722 auto closing_receiver =
723 event_buffer.CreateReceiverAt(main_loop_receiver->index());
724 auto start = std::chrono::steady_clock::now();
725 while (cursor_position_request.HasPending()) {
726 FetchTerminalEvents();
727
728 while (closing_receiver->Has()) {
729 const auto event = closing_receiver->Pop();
730 if (event.is_cursor_position()) {
731 cursor_x_ = event.cursor_x();
732 cursor_y_ = event.cursor_y();
733 cursor_position_request.OnReply();
734 }
735 }
736
737 task_runner.RunUntilIdle();
738
739 if (std::chrono::steady_clock::now() - start >
740 std::chrono::milliseconds(400)) {
741 break;
742 }
743 std::this_thread::sleep_for(std::chrono::milliseconds(10));
744 }
745 }
746
747 OnExit();
748}
749
750void App::Internal::PreMain() {
751 // Suspend previously active screen:
752 if (g_active_screen) {
753 std::swap(suspended_screen_, g_active_screen);
754 // Reset cursor position to the top of the screen and clear the screen.
755 suspended_screen_->internal_->TerminalSend(
756 suspended_screen_->internal_->ResetCursorPosition());
757 suspended_screen_->ResetPosition(
758 suspended_screen_->internal_->output_buffer,
759 /*clear=*/true);
760 suspended_screen_->dimx_ = 0;
761 suspended_screen_->dimy_ = 0;
762
763 // Reset dimensions to force drawing the screen again next time:
764 suspended_screen_->internal_->Uninstall();
765 }
766
767 // This screen is now active:
768 g_active_screen = public_;
769 g_active_screen->internal_->Install();
770
771 previous_animation_time_ = animation::Clock::now();
772}
773
774void App::Internal::PostMain() {
775 // Put cursor position at the end of the drawing.
776 TerminalSend(ResetCursorPosition());
777
778 g_active_screen = nullptr;
779
780 // Restore suspended screen.
781 if (suspended_screen_) {
782 // Clear screen, and put the cursor at the beginning of the drawing.
783 public_->ResetPosition(output_buffer, /*clear=*/true);
784 public_->dimx_ = 0;
785 public_->dimy_ = 0;
786 Uninstall();
787 std::swap(g_active_screen, suspended_screen_);
788 g_active_screen->internal_->Install();
789 } else {
790 Uninstall();
791
792 std::cout << "\r";
793 // On final exit, keep the current drawing and reset cursor position one
794 // line after it.
795 if (!use_alternative_screen_) {
796 std::cout << "\n";
797 }
798 std::cout << std::flush;
799 }
800
801 int sig = g_last_signal.exchange(0);
802 if (sig != 0) {
803 RestoreSignalHandlerAndRaise(sig);
804 }
805}
806
807bool App::Internal::HasQuitted() {
808 return quit_;
809}
810
811void App::Internal::RunOnce(const Component& component) {
812 const AutoReset set_component(&component_, component);
813 ExecuteSignalHandlers();
814 FetchTerminalEvents();
815
816 while (!quit_ && main_loop_receiver->Has()) {
817 public_->Post(main_loop_receiver->Pop());
818 }
819
820 // Execute the pending tasks from the queue.
821 const size_t executed_task = task_runner.ExecutedTasks();
822 task_runner.RunUntilIdle();
823 // If no executed task, we can return early without redrawing the screen.
824 if (executed_task == task_runner.ExecutedTasks()) {
825 return;
826 }
827
828 ExecuteSignalHandlers();
829 Draw(component);
830
831 if (selection_data_previous_ != selection_data_) {
832 selection_data_previous_ = selection_data_;
833 if (selection_on_change_) {
834 selection_on_change_();
835 public_->Post(Event::Custom);
836 }
837 }
838}
839
840void App::Internal::RunOnceBlocking(Component component) {
841 // Set FPS to 60 at most.
842 const auto time_per_frame = std::chrono::microseconds(16666); // 1s / 60fps
843
844 auto time = std::chrono::steady_clock::now();
845 const size_t executed_task = task_runner.ExecutedTasks();
846
847 // Wait for at least one task to execute.
848 while (executed_task == task_runner.ExecutedTasks() && !HasQuitted()) {
849 RunOnce(component);
850
851 const auto now = std::chrono::steady_clock::now();
852 const auto delta = now - time;
853 time = now;
854
855 if (delta < time_per_frame) {
856 const auto sleep_duration = time_per_frame - delta;
857 std::this_thread::sleep_for(sleep_duration);
858 }
859 }
860}
861
862void App::Internal::HandleTask(Component component, Task& task) {
863 std::visit(
864 [&](auto&& arg) {
865 using T = std::decay_t<decltype(arg)>;
866 // clang-format off
867
868 // Handle Event.
869 if constexpr (std::is_same_v<T, Event>) {
870
871 if (arg.is_cursor_position()) {
872 cursor_x_ = arg.cursor_x();
873 cursor_y_ = arg.cursor_y();
874 cursor_position_request.OnReply();
875 return;
876 }
877
878 if (arg.is_cursor_shape()) {
879 cursor_reset_shape_ = arg.cursor_shape();
880 return;
881 }
882
883 if (arg.IsTerminalCapabilities()) {
884 terminal_capabilities_ = arg.TerminalCapabilities();
885 return;
886 }
887
888 if (arg.IsTerminalNameVersion()) {
889 terminal_name_ = arg.TerminalName();
890 terminal_version_ = arg.TerminalVersion();
891 return;
892 }
893
894 if (arg.IsTerminalEmulator()) {
895 terminal_emulator_name_ = arg.TerminalEmulatorName();
896 terminal_emulator_version_ = arg.TerminalEmulatorVersion();
897 return;
898 }
899
900 if (arg.is_mouse()) {
901 arg.mouse().x -= cursor_x_;
902 arg.mouse().y -= cursor_y_;
903 }
904
905 arg.screen_ = public_;
906
907 bool handled = component->OnEvent(arg);
908 handled = HandleSelection(handled, arg);
909
910 if (arg == Event::CtrlC && (!handled || force_handle_ctrl_c_)) {
911 RecordSignal(SIGINT);
912 }
913
914#if !defined(_WIN32)
915 if (arg == Event::CtrlZ && (!handled || force_handle_ctrl_z_)) {
916 RecordSignal(SIGTSTP);
917 }
918#endif
919
920 frame_valid_ = false;
921 return;
922 }
923
924 // Handle callback
925 if constexpr (std::is_same_v<T, Closure>) {
926 arg();
927 return;
928 }
929
930 // Handle Animation
931 if constexpr (std::is_same_v<T, AnimationTask>) {
932 if (!animation_requested_) {
933 return;
934 }
935
936 animation_requested_ = false;
937 const animation::TimePoint now = animation::Clock::now();
938 const animation::Duration delta = now - previous_animation_time_;
939 previous_animation_time_ = now;
940
941 animation::Params params(delta);
942 component->OnAnimation(params);
943 frame_valid_ = false;
944 return;
945 }
946 },
947 task);
948 // clang-format on
949}
950
951bool App::Internal::HandleSelection(bool handled, Event event) {
952 if (handled) {
953 selection_pending_ = nullptr;
954 selection_data_.empty = true;
955 selection_ = nullptr;
956 return true;
957 }
958
959 if (!event.is_mouse()) {
960 return false;
961 }
962
963 auto& mouse = event.mouse();
964 if (mouse.button != Mouse::Left) {
965 return false;
966 }
967
968 if (mouse.motion == Mouse::Pressed) {
969 selection_pending_ = public_->CaptureMouse();
970 selection_data_.start_x = mouse.x;
971 selection_data_.start_y = mouse.y;
972 selection_data_.end_x = mouse.x;
973 selection_data_.end_y = mouse.y;
974 return false;
975 }
976
977 if (!selection_pending_) {
978 return false;
979 }
980
981 if (mouse.motion == Mouse::Moved) {
982 if ((mouse.x != selection_data_.end_x) ||
983 (mouse.y != selection_data_.end_y)) {
984 selection_data_.end_x = mouse.x;
985 selection_data_.end_y = mouse.y;
986 selection_data_.empty = false;
987 }
988
989 return true;
990 }
991
992 if (mouse.motion == Mouse::Released) {
993 selection_pending_ = nullptr;
994 selection_data_.end_x = mouse.x;
995 selection_data_.end_y = mouse.y;
996 selection_data_.empty = false;
997 return true;
998 }
999
1000 return false;
1001}
1002
1003void App::Internal::Draw(Component component) {
1004 if (frame_valid_) {
1005 return;
1006 }
1007 auto document = component->Render();
1008 int dimx = 0;
1009 int dimy = 0;
1010 auto terminal = Terminal::Size();
1011 document->ComputeRequirement();
1012 switch (dimension_) {
1014 dimx = public_->dimx_;
1015 dimy = public_->dimy_;
1016 break;
1018 dimx = terminal.dimx;
1019 dimy = util::clamp(document->requirement().min_y, 0, terminal.dimy);
1020 break;
1022 dimx = terminal.dimx;
1023 dimy = terminal.dimy;
1024 break;
1026 dimx = util::clamp(document->requirement().min_x, 0, terminal.dimx);
1027 dimy = util::clamp(document->requirement().min_y, 0, terminal.dimy);
1028 break;
1029 }
1030
1031 // Hide cursor to prevent flickering during reset.
1032 TerminalSend("\033[?25l");
1033
1034 const bool resized =
1035 frame_count_ == 0 || (dimx != public_->dimx_) || (dimy != public_->dimy_);
1036 TerminalSend(ResetCursorPosition());
1037
1038 if (frame_count_ != 0) {
1039 // Reset the cursor position to the lower left corner to start drawing the
1040 // new frame.
1041 public_->ResetPosition(output_buffer, resized);
1042
1043 // If the terminal width decrease, the terminal emulator will start wrapping
1044 // lines and make the display dirty. We should clear it completely.
1045 if ((dimx < public_->dimx_) && !use_alternative_screen_) {
1046 TerminalSend("\033[J"); // clear terminal output
1047 TerminalSend("\033[H"); // move cursor to home position
1048 }
1049 }
1050
1051 // Resize the screen if needed.
1052 if (resized) {
1053 public_->dimx_ = dimx;
1054 public_->dimy_ = dimy;
1055 public_->cells_ = std::vector<Cell>(static_cast<size_t>(dimx) *
1056 static_cast<size_t>(dimy));
1057 Cursor cursor = public_->cursor_;
1058 cursor.x = dimx - 1;
1059 cursor.y = dimy - 1;
1060 public_->SetCursor(cursor);
1061 }
1062
1063 // Periodically request the terminal emulator the frame position relative to
1064 // the screen. This is useful for converting mouse position reported in
1065 // screen's coordinates to frame's coordinates.
1066 if (!use_alternative_screen_ && is_stdout_a_tty_) {
1067 RequestCursorPosition(previous_frame_resized_);
1068 }
1069 previous_frame_resized_ = resized;
1070
1071 selection_ = selection_data_.empty
1072 ? std::make_unique<Selection>()
1073 : std::make_unique<Selection>(
1074 selection_data_.start_x, selection_data_.start_y, //
1075 selection_data_.end_x, selection_data_.end_y);
1076 Render(*public_, document.get(), *selection_);
1077
1078 // Set cursor position for user using tools to insert CJK characters.
1079 {
1080 const int dx = public_->dimx_ - 1 - public_->cursor_.x +
1081 int(public_->dimx_ != terminal.dimx);
1082 const int dy = public_->dimy_ - 1 - public_->cursor_.y;
1083
1084 set_cursor_position_.clear();
1085 reset_cursor_position_.clear();
1086
1087 if (dy != 0) {
1088 set_cursor_position_ += "\x1B[" + std::to_string(dy) + "A";
1089 reset_cursor_position_ += "\x1B[" + std::to_string(dy) + "B";
1090 }
1091
1092 if (dx != 0) {
1093 set_cursor_position_ += "\x1B[" + std::to_string(dx) + "D";
1094 reset_cursor_position_ += "\x1B[" + std::to_string(dx) + "C";
1095 }
1096
1097 if (public_->cursor_.shape != Screen::Cursor::Hidden) {
1098 set_cursor_position_ += "\033[?25h";
1099 set_cursor_position_ +=
1100 "\033[" + std::to_string(int(public_->cursor_.shape)) + " q";
1101 }
1102 }
1103
1104 public_->ToString(output_buffer);
1105 TerminalSend(set_cursor_position_);
1106 TerminalFlush();
1107
1108 public_->Clear();
1109 frame_valid_ = true;
1110 frame_count_++;
1111}
1112
1113std::string App::Internal::ResetCursorPosition() {
1114 std::string result = std::move(reset_cursor_position_);
1115 reset_cursor_position_ = "";
1116 return result;
1117}
1118
1119void App::Internal::RequestCursorPosition(bool force) {
1120 cursor_position_request.Request(force);
1121}
1122
1123void App::Internal::TerminalSend(std::string_view s) {
1124 output_buffer += s;
1125}
1126
1127void App::Internal::TerminalFlush() {
1128 // Emscripten doesn't implement flush. We interpret zero as flush.
1129 output_buffer += '\0';
1130 std::cout << output_buffer << std::flush;
1131 output_buffer.clear();
1132}
1133
1134void App::Internal::InstallPipedInputHandling() {
1135 is_stdin_a_tty_ = false;
1136 is_stdout_a_tty_ = false;
1137#if defined(__EMSCRIPTEN__)
1138 is_stdin_a_tty_ = true;
1139 is_stdout_a_tty_ = true;
1140#elif defined(_WIN32)
1141 is_stdin_a_tty_ = _isatty(_fileno(stdin));
1142 is_stdout_a_tty_ = _isatty(_fileno(stdout));
1143#else
1144 tty_fd_ = STDIN_FILENO;
1145 is_stdout_a_tty_ = isatty(STDOUT_FILENO);
1146 // Handle piped input redirection if explicitly enabled by the application.
1147 // This allows applications to read data from stdin while still receiving
1148 // keyboard input from the terminal for interactive use.
1149 if (!handle_piped_input_) {
1150 is_stdin_a_tty_ = isatty(STDIN_FILENO);
1151 } else if (isatty(STDIN_FILENO)) {
1152 is_stdin_a_tty_ = true;
1153 } else {
1154 // Open /dev/tty for keyboard input.
1155 tty_fd_ = open("/dev/tty", O_RDONLY); // NOLINT
1156 if (tty_fd_ < 0) {
1157 // Failed to open /dev/tty (containers, headless systems, etc.)
1158 tty_fd_ = STDIN_FILENO; // Fallback to stdin.
1159 is_stdin_a_tty_ = isatty(STDIN_FILENO);
1160 } else {
1161 is_stdin_a_tty_ = true;
1162 // Close the /dev/tty file descriptor on exit.
1163 on_exit_functions.emplace([this] {
1164 close(tty_fd_);
1165 tty_fd_ = -1;
1166 });
1167 }
1168 }
1169#endif
1170}
1171
1172void App::Internal::InstallTerminalInfo() {
1173 // Request the terminal to report the current cursor shape. We will restore it
1174 // on exit.
1175 if (is_stdout_a_tty_) {
1176 TerminalSend(DECRQSS_DECSCUSR);
1177 TerminalSend("\033[>q"); // XTVERSION
1178 TerminalSend("\033[>c"); // DA2
1179 TerminalSend("\033[c"); // DA1
1180 TerminalFlush();
1181 }
1182
1183 // Wait for the cursor shape reply using the setup head.
1184 if (is_stdin_a_tty_ && is_stdout_a_tty_) {
1185 auto start = std::chrono::steady_clock::now();
1186 bool terminal_capabilities_received = false;
1187 // Wait for the cursor shape reply using the setup head.
1188 while (true) {
1189 FetchTerminalEvents();
1190 while (setup_receiver->Has()) {
1191 const auto event = setup_receiver->Pop();
1192 if (event.is_cursor_shape()) {
1193 cursor_reset_shape_ = event.cursor_shape();
1194 }
1195
1196 if (event.IsTerminalCapabilities()) {
1197 terminal_capabilities_ = event.TerminalCapabilities();
1198 terminal_capabilities_received = true;
1199 }
1200
1201 if (event.IsTerminalNameVersion()) {
1202 terminal_name_ = event.TerminalName();
1203 terminal_version_ = event.TerminalVersion();
1204 }
1205
1206 if (event.IsTerminalEmulator()) {
1207 terminal_emulator_name_ = event.TerminalEmulatorName();
1208 terminal_emulator_version_ = event.TerminalEmulatorVersion();
1209 }
1210 }
1211
1212 // Response are expected to be received in order, so we can break when
1213 // the last one (XTVERSION) is received. We also set a timeout to prevent
1214 // waiting forever in case the terminal doesn't support these queries.
1215 if (terminal_capabilities_received) {
1216 break;
1217 }
1218
1219 if (std::chrono::steady_clock::now() - start >
1220 std::chrono::milliseconds(500)) {
1221 break;
1222 }
1223 std::this_thread::sleep_for(std::chrono::milliseconds(10));
1224 }
1225 }
1226
1227 // Set quirks and color support based on terminal identification.
1228 Terminal::Quirks quirks = Terminal::GetQuirks();
1229
1230 auto safe_getenv = [](const char* name) -> std::string_view {
1231 const char* value = std::getenv(name);
1232 return value ? value : "";
1233 };
1234
1235 auto color_support = Terminal::ComputeColorSupport(
1236 safe_getenv("TERM"), safe_getenv("COLORTERM"),
1237 safe_getenv("TERM_PROGRAM"), terminal_name_, terminal_emulator_name_,
1238 terminal_capabilities_);
1239
1240 quirks.SetColorSupport(color_support);
1241
1242 const bool is_modern_emulator = (terminal_emulator_name_ != "unknown");
1243 const bool is_vt220_plus =
1244 (terminal_name_ != "vt100" && terminal_name_ != "unknown");
1245 bool reports_utf8 = false;
1246 for (const int x : terminal_capabilities_) {
1247 if (x == 52) {
1248 reports_utf8 = true;
1249 break;
1250 }
1251 }
1252
1253 // Heuristic: If the terminal emulator is modern, or it reports supporting
1254 // UTF-8 or color, we can assume it supports block characters and cursor
1255 // hiding, which are essential for a good experience. This is a heuristic, but
1256 // it allows us to work around some older terminal emulators that don't
1257 // support these features, while still providing a good experience on modern
1258 // terminal emulators that do support these features.
1259 bool modern = is_modern_emulator || is_vt220_plus || reports_utf8;
1260 if (modern) {
1261 quirks.SetBlockCharacters(true);
1262 quirks.SetCursorHiding(true);
1263 quirks.SetComponentAscii(false);
1264 }
1265
1266 Terminal::SetQuirks(quirks);
1267
1268 on_exit_functions.emplace([this] {
1269 TerminalSend("\033[?25h"); // Enable cursor.
1270 if (is_stdout_a_tty_) {
1271 TerminalSend("\033[" + std::to_string(cursor_reset_shape_) + " q");
1272 }
1273 });
1274}
1275
1276void App::Internal::Signal(int signal) {
1277 if (signal == SIGABRT) {
1278 public_->Exit();
1279 return;
1280 }
1281
1282// Windows do no support SIGTSTP / SIGWINCH
1283#if !defined(_WIN32)
1284 if (signal == SIGTSTP) {
1285 public_->Post([&] {
1286 TerminalSend(ResetCursorPosition());
1287 public_->ResetPosition(output_buffer, /*clear*/ true);
1288 Uninstall();
1289 public_->dimx_ = 0;
1290 public_->dimy_ = 0;
1291 (void)std::raise(SIGTSTP);
1292 Install();
1293 });
1294 return;
1295 }
1296
1297 if (signal == SIGWINCH) {
1298 public_->Post(Event::Special({0}));
1299 return;
1300 }
1301#endif
1302}
1303
1304size_t App::Internal::FetchTerminalEvents() {
1305#if defined(_WIN32)
1306 auto get_input_records = [&]() -> std::vector<INPUT_RECORD> {
1307 // Check if there is input in the console.
1308 auto console = GetStdHandle(STD_INPUT_HANDLE);
1309 DWORD number_of_events = 0;
1310 if (!GetNumberOfConsoleInputEvents(console, &number_of_events)) {
1311 return std::vector<INPUT_RECORD>();
1312 }
1313 if (number_of_events <= 0) {
1314 // No input, return.
1315 return std::vector<INPUT_RECORD>();
1316 }
1317 // Read the input events.
1318 std::vector<INPUT_RECORD> records(number_of_events);
1319 DWORD number_of_events_read = 0;
1320 if (!ReadConsoleInput(console, records.data(), (DWORD)records.size(),
1321 &number_of_events_read)) {
1322 return std::vector<INPUT_RECORD>();
1323 }
1324 records.resize(number_of_events_read);
1325 return records;
1326 };
1327
1328 auto records = get_input_records();
1329 if (records.size() == 0) {
1330 const auto timeout = std::chrono::steady_clock::now() - last_char_time;
1331 const size_t timeout_microseconds =
1332 std::chrono::duration_cast<std::chrono::microseconds>(timeout).count();
1333 terminal_input_parser.Timeout(timeout_microseconds);
1334 return 0;
1335 }
1336 last_char_time = std::chrono::steady_clock::now();
1337
1338 // Convert the input events to FTXUI events.
1339 // For each event, we call the terminal input parser to convert it to
1340 // Event.
1341 std::wstring wstring;
1342 for (const auto& r : records) {
1343 switch (r.EventType) {
1344 case KEY_EVENT: {
1345 auto key_event = r.Event.KeyEvent;
1346 // ignore UP key events
1347 if (key_event.bKeyDown == FALSE) {
1348 continue;
1349 }
1350 const wchar_t wc = key_event.uChar.UnicodeChar;
1351 wstring += wc;
1352 if (wc >= 0xd800 && wc <= 0xdbff) {
1353 // Wait for the Low Surrogate to arrive in the next record.
1354 continue;
1355 }
1356 for (auto it : to_string(wstring)) {
1357 terminal_input_parser.Add(it);
1358 }
1359 wstring.clear();
1360 } break;
1361 case WINDOW_BUFFER_SIZE_EVENT:
1362 public_->Post(Event::Special({0}));
1363 break;
1364 case MENU_EVENT:
1365 case FOCUS_EVENT:
1366 case MOUSE_EVENT:
1367 // TODO(mauve): Implement later.
1368 break;
1369 }
1370 }
1371 return records.size();
1372#elif defined(__EMSCRIPTEN__)
1373 // Read chars from the terminal.
1374 // We configured it to be non blocking.
1375 std::array<char, 128> out{};
1376 const ssize_t l = read(STDIN_FILENO, out.data(), out.size());
1377 if (l <= 0) {
1378 const auto timeout = std::chrono::steady_clock::now() - last_char_time;
1379 const size_t timeout_microseconds =
1380 std::chrono::duration_cast<std::chrono::microseconds>(timeout).count();
1381 terminal_input_parser.Timeout(timeout_microseconds);
1382 return 0;
1383 }
1384 last_char_time = std::chrono::steady_clock::now();
1385
1386 // Convert the chars to events.
1387 for (ssize_t i = 0; i < l; ++i) {
1388 terminal_input_parser.Add(out.at(static_cast<size_t>(i)));
1389 }
1390 return (size_t)l;
1391#else // POSIX (Linux & Mac)
1392 struct pollfd pfd = {tty_fd_, POLLIN, 0};
1393 const int poll_result = poll(&pfd, 1, 0);
1394 if (poll_result <= 0) {
1395 const auto timeout = std::chrono::steady_clock::now() - last_char_time;
1396 const size_t timeout_ms =
1397 std::chrono::duration_cast<std::chrono::milliseconds>(timeout).count();
1398 terminal_input_parser.Timeout(static_cast<int>(timeout_ms));
1399 return 0;
1400 }
1401 last_char_time = std::chrono::steady_clock::now();
1402
1403 // Read chars from the terminal.
1404 std::array<char, 128> out{};
1405 const ssize_t l = read(tty_fd_, out.data(), out.size());
1406 if (l <= 0) {
1407 return 0;
1408 }
1409
1410 // Convert the chars to events.
1411 for (ssize_t i = 0; i < l; ++i) {
1412 terminal_input_parser.Add(out.at(static_cast<size_t>(i)));
1413 }
1414 return (size_t)l;
1415#endif
1416}
1417
1418void App::Internal::PostAnimationTask() {
1419 public_->Post(AnimationTask());
1420
1421 // Repeat the animation task every 15ms. This correspond to a frame rate
1422 // of around 66fps.
1423 task_runner.PostDelayedTask([this] { PostAnimationTask(); },
1424 std::chrono::milliseconds(15));
1425}
1426
1427App::App(std::unique_ptr<Internal> internal, int dimx, int dimy)
1428 : Screen(dimx, dimy), internal_(std::move(internal)) {
1429 internal_->public_ = this;
1430}
1431
1432App::App(App&& other) noexcept : Screen(std::move(other)) {
1433 internal_ = std::move(other.internal_);
1434 if (internal_) {
1435 internal_->public_ = this;
1436 }
1437}
1438
1439App& App::operator=(App&& other) noexcept {
1440 Screen::operator=(std::move(other));
1441 internal_ = std::move(other.internal_);
1442 if (internal_) {
1443 internal_->public_ = this;
1444 }
1445 return *this;
1446}
1447
1448App::~App() = default;
1449
1450// static
1451App App::FixedSize(int dimx, int dimy) {
1452 auto internal =
1453 std::make_unique<Internal>(nullptr, AppDimension::Fixed, false);
1454 return App(std::move(internal), dimx, dimy);
1455}
1456
1457// static
1458App App::Fullscreen() {
1459 return FullscreenAlternateScreen();
1460}
1461
1462// static
1463App App::FullscreenPrimaryScreen() {
1464 auto terminal = Terminal::Size();
1465 auto internal =
1466 std::make_unique<Internal>(nullptr, AppDimension::Fullscreen, false);
1467 return App(std::move(internal), terminal.dimx, terminal.dimy);
1468}
1469
1470// static
1471App App::FullscreenAlternateScreen() {
1472 auto terminal = Terminal::Size();
1473 auto internal =
1474 std::make_unique<Internal>(nullptr, AppDimension::Fullscreen, true);
1475 return App(std::move(internal), terminal.dimx, terminal.dimy);
1476}
1477
1478// static
1479App App::FitComponent() {
1480 auto terminal = Terminal::Size();
1481 auto internal =
1482 std::make_unique<Internal>(nullptr, AppDimension::FitComponent, false);
1483 return App(std::move(internal), terminal.dimx, terminal.dimy);
1484}
1485
1486// static
1487App App::TerminalOutput() {
1488 auto terminal = Terminal::Size();
1489 auto internal =
1490 std::make_unique<Internal>(nullptr, AppDimension::TerminalOutput, false);
1491 return App(std::move(internal), terminal.dimx, terminal.dimy);
1492}
1493
1494void App::TrackMouse(bool enable) {
1495 internal_->track_mouse_ = enable;
1496}
1497
1498void App::HandlePipedInput(bool enable) {
1499 internal_->handle_piped_input_ = enable;
1500}
1501
1502// static
1503App* App::Active() {
1504 return g_active_screen;
1505}
1506
1507void App::Loop(Component component) {
1508 class Loop loop(this, std::move(component));
1509 loop.Run();
1510}
1511
1512void App::Exit() {
1513 Post([this] { internal_->ExitNow(); });
1514}
1515
1516Closure App::ExitLoopClosure() {
1517 return [this] { Exit(); };
1518}
1519
1520void App::Post(Task task) {
1521 internal_->task_runner.PostTask([this, task = std::move(task)]() mutable {
1522 if (internal_->component_) {
1523 internal_->HandleTask(internal_->component_, task);
1524 return;
1525 }
1526
1527 // If there is no component, we can still execute closures.
1528 if (std::holds_alternative<Closure>(task)) {
1529 std::get<Closure>(task)();
1530 }
1531 });
1532}
1533
1534void App::PostEvent(Event event) {
1535 internal_->event_buffer.Push(std::move(event));
1536 RequestAnimationFrame();
1537}
1538
1539// static
1540void App::PostEventOrExecute(Closure closure) {
1541 if (!closure) {
1542 return;
1543 }
1544 if (auto* app = App::Active()) {
1545 app->Post(std::move(closure));
1546 } else {
1547 closure();
1548 }
1549}
1550
1551void App::RequestAnimationFrame() {
1552 if (internal_->animation_requested_) {
1553 return;
1554 }
1555 internal_->animation_requested_ = true;
1556 auto now = animation::Clock::now();
1557 const auto time_histeresis = std::chrono::milliseconds(33);
1558 if (now - internal_->previous_animation_time_ >= time_histeresis) {
1559 internal_->previous_animation_time_ = now;
1560 }
1561}
1562
1563CapturedMouse App::CaptureMouse() {
1564 if (internal_->mouse_captured) {
1565 return nullptr;
1566 }
1567 internal_->mouse_captured = true;
1568 return std::make_unique<CapturedMouseImpl>(
1569 [this] { internal_->mouse_captured = false; });
1570}
1571
1572Closure App::WithRestoredIO(Closure fn) {
1573 return [this, fn] {
1574 internal_->Uninstall();
1575 fn();
1576 internal_->Install();
1577 };
1578}
1579
1580void App::ForceHandleCtrlC(bool force) {
1581 internal_->force_handle_ctrl_c_ = force;
1582}
1583
1584void App::ForceHandleCtrlZ(bool force) {
1585 internal_->force_handle_ctrl_z_ = force;
1586}
1587
1588std::string App::GetSelection() {
1589 if (!internal_->selection_) {
1590 return "";
1591 }
1592 return internal_->selection_->GetParts();
1593}
1594
1595void App::SelectionChange(std::function<void()> callback) {
1596 internal_->selection_on_change_ = std::move(callback);
1597}
1598
1599const std::string& App::TerminalName() const {
1600 return internal_->terminal_name_;
1601}
1602
1603int App::TerminalVersion() const {
1604 return internal_->terminal_version_;
1605}
1606
1607const std::string& App::TerminalEmulatorName() const {
1608 return internal_->terminal_emulator_name_;
1609}
1610
1611const std::string& App::TerminalEmulatorVersion() const {
1612 return internal_->terminal_emulator_version_;
1613}
1614
1615const std::vector<int>& App::TerminalCapabilities() const {
1616 return internal_->terminal_capabilities_;
1617}
1618
1619std::vector<std::string> App::TerminalCapabilityNames() const {
1620 return Event::TerminalCapabilities("", internal_->terminal_capabilities_)
1621 .TerminalCapabilityNames();
1622}
1623
1624// Loop calls these:
1625
1626void App::ExitNow() {
1627 internal_->ExitNow();
1628}
1629void App::Install() {
1630 internal_->Install();
1631}
1632void App::Uninstall() {
1633 internal_->Uninstall();
1634}
1635void App::PreMain() {
1636 internal_->PreMain();
1637}
1638void App::PostMain() {
1639 internal_->PostMain();
1640}
1641bool App::HasQuitted() {
1642 return internal_->HasQuitted();
1643}
1644void App::RunOnce(const Component& component) {
1645 internal_->RunOnce(component);
1646}
1647void App::RunOnceBlocking(Component component) {
1648 internal_->RunOnceBlocking(component);
1649}
1650void App::HandleTask(Component component, Task& task) {
1651 internal_->HandleTask(component, task);
1652}
1653bool App::HandleSelection(bool handled, Event event) {
1654 return internal_->HandleSelection(handled, event);
1655}
1656void App::Draw(Component component) {
1657 internal_->Draw(component);
1658}
1659std::string App::ResetCursorPosition() {
1660 return internal_->ResetCursorPosition();
1661}
1662void App::RequestCursorPosition(bool force) {
1663 internal_->RequestCursorPosition(force);
1664}
1665void App::TerminalSend(std::string_view s) {
1666 internal_->TerminalSend(s);
1667}
1668void App::TerminalFlush() {
1669 internal_->TerminalFlush();
1670}
1671void App::InstallPipedInputHandling() {
1672 internal_->InstallPipedInputHandling();
1673}
1674void App::InstallTerminalInfo() {
1675 internal_->InstallTerminalInfo();
1676}
1677void App::Signal(int signal) {
1678 internal_->Signal(signal);
1679}
1680size_t App::FetchTerminalEvents() {
1681 return internal_->FetchTerminalEvents();
1682}
1683void App::PostAnimationTask() {
1684 internal_->PostAnimationTask();
1685}
1686
1687Loop::Loop(App* screen, Component component)
1688 : screen_(screen), component_(std::move(component)) {
1689 screen_->PreMain();
1690}
1691
1692Loop::~Loop() {
1693 screen_->PostMain();
1694}
1695
1696bool Loop::HasQuitted() {
1697 return screen_->HasQuitted();
1698}
1699
1700void Loop::RunOnce() {
1701 screen_->RunOnce(component_);
1702}
1703
1704void Loop::RunOnceBlocking() {
1705 screen_->RunOnceBlocking(component_);
1706}
1707
1708void Loop::Run() {
1709 while (!HasQuitted()) {
1710 RunOnceBlocking();
1711 }
1712}
1713
1714} // namespace ftxui
auto PostDelayedTask(Task task, std::chrono::steady_clock::duration duration) -> void
Schedules a task to be executed after a certain duration.
Quirks GetQuirks()
Get the terminal quirks.
Definition terminal.cpp:321
Dimensions Size()
Get the terminal size.
Definition terminal.cpp:268
void SetQuirks(const Quirks &quirks)
Override terminal quirks.
Definition terminal.cpp:331
The FTXUI ftxui::animation:: namespace.
void SetFallbackSize(const Dimensions &fallbackSize)
Override terminal size in case auto-detection fails.
Definition terminal.cpp:298
Color ComputeColorSupport(std::string_view term, std::string_view colorterm, std::string_view term_program, std::string_view terminal_name, std::string_view terminal_emulator_name, const std::vector< int > &capabilities)
Compute the color support based on environment variables and terminal identification.
Definition terminal.cpp:195
std::chrono::duration< float > Duration
Definition animation.hpp:31
std::chrono::time_point< Clock > TimePoint
Definition animation.hpp:30
void RequestAnimationFrame()
Definition app.cpp:74
constexpr const T & clamp(const T &v, const T &lo, const T &hi)
Definition util.hpp:11
The FTXUI ftxui:: namespace.
Definition animation.hpp:11
std::unique_ptr< CapturedMouseInterface > CapturedMouse
std::string to_string(std::wstring_view s)
Convert a std::wstring into a UTF8 std::string.
Definition string.cpp:1594
AppDimension
Definition app.cpp:66
std::variant< Event, Closure, AnimationTask > Task
Definition task.hpp:14
void Render(Screen &screen, Node *node, Selection &selection)
Definition node.cpp:105
int value
Definition elements.hpp:188
std::function< void()> Closure
Definition task.hpp:13
std::shared_ptr< ComponentBase > Component
Definition app.hpp:23