FTXUI 6.1.9
C++ functional terminal UI.
Loading...
Searching...
No Matches
screen.cpp
Go to the documentation of this file.
1// Copyright 2020 Arthur Sonzogni. All rights reserved.
2// このソースコードの使用は、LICENSEファイルにあるMITライセンスによって管理されています。
3#include <cstddef> // for size_t
4#include <cstdint>
5#include <iostream> // for operator<<, stringstream, basic_ostream, flush, cout, ostream
6#include <limits>
7#include <map> // for _Rb_tree_const_iterator, map, operator!=, operator==
8#include <sstream> // IWYU pragma: keep
9#include <utility> // for pair
10
11#include "ftxui/screen/image.hpp" // for Image
12#include "ftxui/screen/pixel.hpp" // for Pixel
14#include "ftxui/screen/string.hpp" // for string_width
15#include "ftxui/screen/terminal.hpp" // for Dimensions, Size
16
17#if defined(_WIN32)
18#define WIN32_LEAN_AND_MEAN
19#ifndef NOMINMAX
20#define NOMINMAX
21#endif
22#include <windows.h>
23#endif
24
25// 式がfalseである可能性が高いことを示唆するマクロ。
26#if !defined(FTXUI_UNLIKELY)
27#if defined(COMPILER_GCC) || defined(__clang__)
28#define FTXUI_UNLIKELY(x) __builtin_expect(!!(x), 0)
29#else
30#define FTXUI_UNLIKELY(x) (x)
31#endif // defined(COMPILER_GCC)
32#endif // !defined(FTXUI_UNLIKELY)
33
34#if !defined(FTXUI_LIKELY)
35#if defined(COMPILER_GCC) || defined(__clang__)
36#define FTXUI_LIKELY(x) __builtin_expect(!!(x), 1)
37#else
38#define FTXUI_LIKELY(x) (x)
39#endif // defined(COMPILER_GCC)
40#endif // !defined(FTXUI_LIKELY)
41
42namespace ftxui {
43
44namespace {
45
46#if defined(_WIN32)
47void WindowsEmulateVT100Terminal() {
48 static bool done = false;
49 if (done) {
50 return;
51 }
52 done = true;
53
54 // stdoutおよびstdinでVT処理を有効にする
55 auto stdout_handle = GetStdHandle(STD_OUTPUT_HANDLE);
56
57 DWORD out_mode = 0;
58 GetConsoleMode(stdout_handle, &out_mode);
59
60 // https://docs.microsoft.com/en-us/windows/console/setconsolemode
61 const int enable_virtual_terminal_processing = 0x0004;
62 const int disable_newline_auto_return = 0x0008;
63 out_mode |= enable_virtual_terminal_processing;
64 out_mode |= disable_newline_auto_return;
65
66 SetConsoleMode(stdout_handle, out_mode);
67}
68#endif
69
70// NOLINTNEXTLINE(readability-function-cognitive-complexity)
71void UpdatePixelStyle(const Screen* screen,
72 std::stringstream& ss,
73 const Pixel& prev,
74 const Pixel& next) {
75 // See https://gist.github.com/egmontkob/eb114294efbcd5adb1944c9f3cb5feda
76 if (FTXUI_UNLIKELY(next.hyperlink != prev.hyperlink)) {
77 ss << "\x1B]8;;" << screen->Hyperlink(next.hyperlink) << "\x1B\\";
78 }
79
80 // 太字
81 if (FTXUI_UNLIKELY((next.bold ^ prev.bold) | (next.dim ^ prev.dim))) {
82 // BOLD_AND_DIM_RESET:
83 ss << ((prev.bold && !next.bold) || (prev.dim && !next.dim) ? "\x1B[22m"
84 : "");
85 ss << (next.bold ? "\x1B[1m" : ""); // 太字設定
86 ss << (next.dim ? "\x1B[2m" : ""); // Dim設定
87 }
88
89 // 下線
90 if (FTXUI_UNLIKELY(next.underlined != prev.underlined ||
91 next.underlined_double != prev.underlined_double)) {
92 ss << (next.underlined ? "\x1B[4m"// 下線
93 : next.underlined_double ? "\x1B[21m" // 二重下線
94 : "\x1B[24m"); // 下線リセット
95 }
96
97 // 点滅
98 if (FTXUI_UNLIKELY(next.blink != prev.blink)) {
99 ss << (next.blink ? "\x1B[5m" // 点滅設定
100 : "\x1B[25m"); // 点滅リセット
101 }
102
103 // 反転
104 if (FTXUI_UNLIKELY(next.inverted != prev.inverted)) {
105 ss << (next.inverted ? "\x1B[7m" // 反転設定
106 : "\x1B[27m"); // 反転リセット
107 }
108
109 // 斜体
110 if (FTXUI_UNLIKELY(next.italic != prev.italic)) {
111 ss << (next.italic ? "\x1B[3m" // 斜体設定
112 : "\x1B[23m"); // 斜体リセット
113 }
114
115 // 打ち消し線
116 if (FTXUI_UNLIKELY(next.strikethrough != prev.strikethrough)) {
117 ss << (next.strikethrough ? "\x1B[9m" // 打ち消し線
118 : "\x1B[29m"); // 打ち消し線リセット
119 }
120
121 if (FTXUI_UNLIKELY(next.foreground_color != prev.foreground_color ||
122 next.background_color != prev.background_color)) {
123 ss << "\x1B[" + next.foreground_color.Print(false) + "m";
124 ss << "\x1B[" + next.background_color.Print(true) + "m";
125 }
126}
127
128struct TileEncoding {
129 std::uint8_t left : 2;
130 std::uint8_t top : 2;
131 std::uint8_t right : 2;
132 std::uint8_t down : 2;
133 std::uint8_t round : 1;
134
135 // clang-format off
136 bool operator<(const TileEncoding& other) const {
137 if (left < other.left) { return true; }
138 if (left > other.left) { return false; }
139 if (top < other.top) { return true; }
140 if (top > other.top) { return false; }
141 if (right < other.right) { return true; }
142 if (right > other.right) { return false; }
143 if (down < other.down) { return true; }
144 if (down > other.down) { return false; }
145 if (round < other.round) { return true; }
146 if (round > other.round) { return false; }
147 return false;
148 }
149 // clang-format on
150};
151
152// clang-format off
153const std::map<std::string, TileEncoding> tile_encoding = { // NOLINT
154 {"─", {1, 0, 1, 0, 0}},
155 {"━", {2, 0, 2, 0, 0}},
156 {"╍", {2, 0, 2, 0, 0}},
157
158 {"│", {0, 1, 0, 1, 0}},
159 {"┃", {0, 2, 0, 2, 0}},
160 {"╏", {0, 2, 0, 2, 0}},
161
162 {"┌", {0, 0, 1, 1, 0}},
163 {"┍", {0, 0, 2, 1, 0}},
164 {"┎", {0, 0, 1, 2, 0}},
165 {"┏", {0, 0, 2, 2, 0}},
166
167 {"┐", {1, 0, 0, 1, 0}},
168 {"┑", {2, 0, 0, 1, 0}},
169 {"┒", {1, 0, 0, 2, 0}},
170 {"┓", {2, 0, 0, 2, 0}},
171
172 {"└", {0, 1, 1, 0, 0}},
173 {"┕", {0, 1, 2, 0, 0}},
174 {"┖", {0, 2, 1, 0, 0}},
175 {"┗", {0, 2, 2, 0, 0}},
176
177 {"┘", {1, 1, 0, 0, 0}},
178 {"┙", {2, 1, 0, 0, 0}},
179 {"┚", {1, 2, 0, 0, 0}},
180 {"┛", {2, 2, 0, 0, 0}},
181
182 {"├", {0, 1, 1, 1, 0}},
183 {"┝", {0, 1, 2, 1, 0}},
184 {"┞", {0, 2, 1, 1, 0}},
185 {"┟", {0, 1, 1, 2, 0}},
186 {"┠", {0, 2, 1, 2, 0}},
187 {"┡", {0, 2, 2, 1, 0}},
188 {"┢", {0, 1, 2, 2, 0}},
189 {"┣", {0, 2, 2, 2, 0}},
190
191 {"┤", {1, 1, 0, 1, 0}},
192 {"┥", {2, 1, 0, 1, 0}},
193 {"┦", {1, 2, 0, 1, 0}},
194 {"┧", {1, 1, 0, 2, 0}},
195 {"┨", {1, 2, 0, 2, 0}},
196 {"┩", {2, 2, 0, 1, 0}},
197 {"┪", {2, 1, 0, 2, 0}},
198 {"┫", {2, 2, 0, 2, 0}},
199
200 {"┬", {1, 0, 1, 1, 0}},
201 {"┭", {2, 0, 1, 1, 0}},
202 {"┮", {1, 0, 2, 1, 0}},
203 {"┯", {2, 0, 2, 1, 0}},
204 {"┰", {1, 0, 1, 2, 0}},
205 {"┱", {2, 0, 1, 2, 0}},
206 {"┲", {1, 0, 2, 2, 0}},
207 {"┳", {2, 0, 2, 2, 0}},
208
209 {"┴", {1, 1, 1, 0, 0}},
210 {"┵", {2, 1, 1, 0, 0}},
211 {"┶", {1, 1, 2, 0, 0}},
212 {"┷", {2, 1, 2, 0, 0}},
213 {"┸", {1, 2, 1, 0, 0}},
214 {"┹", {2, 2, 1, 0, 0}},
215 {"┺", {1, 2, 2, 0, 0}},
216 {"┻", {2, 2, 2, 0, 0}},
217
218 {"┼", {1, 1, 1, 1, 0}},
219 {"┽", {2, 1, 1, 1, 0}},
220 {"┾", {1, 1, 2, 1, 0}},
221 {"┿", {2, 1, 2, 1, 0}},
222 {"╀", {1, 2, 1, 1, 0}},
223 {"╁", {1, 1, 1, 2, 0}},
224 {"╂", {1, 2, 1, 2, 0}},
225 {"╃", {2, 2, 1, 1, 0}},
226 {"╄", {1, 2, 2, 1, 0}},
227 {"╅", {2, 1, 1, 2, 0}},
228 {"╆", {1, 1, 2, 2, 0}},
229 {"╇", {2, 2, 2, 1, 0}},
230 {"╈", {2, 1, 2, 2, 0}},
231 {"╉", {2, 2, 1, 2, 0}},
232 {"╊", {1, 2, 2, 2, 0}},
233 {"╋", {2, 2, 2, 2, 0}},
234
235 {"═", {3, 0, 3, 0, 0}},
236 {"║", {0, 3, 0, 3, 0}},
237
238 {"╒", {0, 0, 3, 1, 0}},
239 {"╓", {0, 0, 1, 3, 0}},
240 {"╔", {0, 0, 3, 3, 0}},
241
242 {"╕", {3, 0, 0, 1, 0}},
243 {"╖", {1, 0, 0, 3, 0}},
244 {"╗", {3, 0, 0, 3, 0}},
245
246 {"╘", {0, 1, 3, 0, 0}},
247 {"╙", {0, 3, 1, 0, 0}},
248 {"╚", {0, 3, 3, 0, 0}},
249
250 {"╛", {3, 1, 0, 0, 0}},
251 {"╜", {1, 3, 0, 0, 0}},
252 {"╝", {3, 3, 0, 0, 0}},
253
254 {"╞", {0, 1, 3, 1, 0}},
255 {"╟", {0, 3, 1, 3, 0}},
256 {"╠", {0, 3, 3, 3, 0}},
257
258 {"╡", {3, 1, 0, 1, 0}},
259 {"╢", {1, 3, 0, 3, 0}},
260 {"╣", {3, 3, 0, 3, 0}},
261
262 {"╤", {3, 0, 3, 1, 0}},
263 {"╥", {1, 0, 1, 3, 0}},
264 {"╦", {3, 0, 3, 3, 0}},
265
266 {"╧", {3, 1, 3, 0, 0}},
267 {"╨", {1, 3, 1, 0, 0}},
268 {"╩", {3, 3, 3, 0, 0}},
269
270 {"╪", {3, 1, 3, 1, 0}},
271 {"╫", {1, 3, 1, 3, 0}},
272 {"╬", {3, 3, 3, 3, 0}},
273
274 {"╭", {0, 0, 1, 1, 1}},
275 {"╮", {1, 0, 0, 1, 1}},
276 {"╯", {1, 1, 0, 0, 1}},
277 {"╰", {0, 1, 1, 0, 1}},
278
279 {"╴", {1, 0, 0, 0, 0}},
280 {"╵", {0, 1, 0, 0, 0}},
281 {"╶", {0, 0, 1, 0, 0}},
282 {"╷", {0, 0, 0, 1, 0}},
283
284 {"╸", {2, 0, 0, 0, 0}},
285 {"╹", {0, 2, 0, 0, 0}},
286 {"╺", {0, 0, 2, 0, 0}},
287 {"╻", {0, 0, 0, 2, 0}},
288
289 {"╼", {1, 0, 2, 0, 0}},
290 {"╽", {0, 1, 0, 2, 0}},
291 {"╾", {2, 0, 1, 0, 0}},
292 {"╿", {0, 2, 0, 1, 0}},
293};
294// clang-format on
295
296template <class A, class B>
297std::map<B, A> InvertMap(const std::map<A, B> input) {
298 std::map<B, A> output;
299 for (const auto& it : input) {
300 output[it.second] = it.first;
301 }
302 return output;
303}
304
305const std::map<TileEncoding, std::string> tile_encoding_inverse = // NOLINT
306 InvertMap(tile_encoding);
307
308void UpgradeLeftRight(std::string& left, std::string& right) {
309 const auto it_left = tile_encoding.find(left);
310 if (it_left == tile_encoding.end()) {
311 return;
312 }
313 const auto it_right = tile_encoding.find(right);
314 if (it_right == tile_encoding.end()) {
315 return;
316 }
317
318 if (it_left->second.right == 0 && it_right->second.left != 0) {
319 TileEncoding encoding_left = it_left->second;
320 encoding_left.right = it_right->second.left;
321 const auto it_left_upgrade = tile_encoding_inverse.find(encoding_left);
322 if (it_left_upgrade != tile_encoding_inverse.end()) {
323 left = it_left_upgrade->second;
324 }
325 }
326
327 if (it_right->second.left == 0 && it_left->second.right != 0) {
328 TileEncoding encoding_right = it_right->second;
329 encoding_right.left = it_left->second.right;
330 const auto it_right_upgrade = tile_encoding_inverse.find(encoding_right);
331 if (it_right_upgrade != tile_encoding_inverse.end()) {
332 right = it_right_upgrade->second;
333 }
334 }
335}
336
337void UpgradeTopDown(std::string& top, std::string& down) {
338 const auto it_top = tile_encoding.find(top);
339 if (it_top == tile_encoding.end()) {
340 return;
341 }
342 const auto it_down = tile_encoding.find(down);
343 if (it_down == tile_encoding.end()) {
344 return;
345 }
346
347 if (it_top->second.down == 0 && it_down->second.top != 0) {
348 TileEncoding encoding_top = it_top->second;
349 encoding_top.down = it_down->second.top;
350 const auto it_top_down = tile_encoding_inverse.find(encoding_top);
351 if (it_top_down != tile_encoding_inverse.end()) {
352 top = it_top_down->second;
353 }
354 }
355
356 if (it_down->second.top == 0 && it_top->second.down != 0) {
357 TileEncoding encoding_down = it_down->second;
358 encoding_down.top = it_top->second.down;
359 const auto it_down_top = tile_encoding_inverse.find(encoding_down);
360 if (it_down_top != tile_encoding_inverse.end()) {
361 down = it_down_top->second;
362 }
363 }
364}
365
366bool ShouldAttemptAutoMerge(Pixel& pixel) {
367 return pixel.automerge && pixel.character.size() == 3;
368}
369
370} // namespace
371
372/// 固定された次元。
373/// @see Fit
374/// @see Full
375Dimensions Dimension::Fixed(int v) {
376 return {v, v};
377}
378
379/// ターミナルの次元を使用します。
380/// @see Fixed
381/// @see Fit
382Dimensions Dimension::Full() {
383 return Terminal::Size();
384}
385
386// static
387/// X軸とY軸に沿って指定された次元を持つスクリーンを作成します。
389 return {width.dimx, height.dimy};
390}
391
392// static
393/// 指定された次元を持つスクリーンを作成します。
395 return {dimension.dimx, dimension.dimy};
396}
397
398Screen::Screen(int dimx, int dimy) : Image{dimx, dimy} {
399#if defined(_WIN32)
400 // この呼び出しの位置は少し奇妙ですが、Screenオブジェクトをインスタンス化する人は最終的に
401 // コンソールに何かを出力したいと仮定できます。そうでない場合は、Imageのインスタンスを使用してください。
402 // すべての入出力操作にはUTF8が必要なため、ここでUTF8エンコーディングに切り替えます。 SetConsoleOutputCP(CP_UTF8);
403 SetConsoleCP(CP_UTF8);
404 WindowsEmulateVT100Terminal();
405#endif
406}
407
408/// ターミナルにScreenを表示するために使用できるstd::stringを生成します。
409/// @note stdoutをフラッシュすることを忘れないでください。または、Screen::Print()を使用できます。
410std::string Screen::ToString() const {
411 std::stringstream ss;
412
413 const Pixel default_pixel;
414 const Pixel* previous_pixel_ref = &default_pixel;
415
416 for (int y = 0; y < dimy_; ++y) {
417 // New line in between two lines.
418 if (y != 0) {
419 UpdatePixelStyle(this, ss, *previous_pixel_ref, default_pixel);
420 previous_pixel_ref = &default_pixel;
421 ss << "\r\n";
422 }
423
424 // After printing a fullwith character, we need to skip the next cell.
425 bool previous_fullwidth = false;
426 for (const auto& pixel : pixels_[y]) {
427 if (!previous_fullwidth) {
428 UpdatePixelStyle(this, ss, *previous_pixel_ref, pixel);
429 previous_pixel_ref = &pixel;
430 if (pixel.character.empty()) {
431 ss << " ";
432 } else {
433 ss << pixel.character;
434 }
435 }
436 previous_fullwidth = (string_width(pixel.character) == 2);
437 }
438 }
439
440 // Reset the style to default:
441 UpdatePixelStyle(this, ss, *previous_pixel_ref, default_pixel);
442
443 return ss.str();
444}
445
446// Screenをターミナルに表示します。
447void Screen::Print() const {
448 std::cout << ToString() << '\0' << std::flush;
449}
450
451/// @brief カーソル位置を画面の先頭にリセットするために出力する文字列を返します。
452///
453/// ```cpp
454/// std::string reset_position;
455/// while(true) {
456/// auto document = render();
457/// auto screen = Screen::Create(Dimension::Full(), Dimension::Fit(document));
458/// Render(screen, document);
459/// std::cout << reset_position << screen.ToString() << std::flush;
460/// reset_position = screen.ResetPosition();
461///
462/// using namespace std::chrono_literals;
463/// std::this_thread::sleep_for(0.01s);
464/// }
465/// ```
466///
467/// @return カーソル位置を先頭にリセットするために出力する文字列。
468std::string Screen::ResetPosition(bool clear) const {
469 std::stringstream ss;
470 if (clear) {
471 ss << "\r"; // MOVE_LEFT;
472 ss << "\x1b[2K"; // CLEAR_SCREEN;
473 for (int y = 1; y < dimy_; ++y) {
474 ss << "\x1B[1A"; // MOVE_UP;
475 ss << "\x1B[2K"; // CLEAR_LINE;
476 }
477 } else {
478 ss << "\r"; // MOVE_LEFT;
479 for (int y = 1; y < dimy_; ++y) {
480 ss << "\x1B[1A"; // MOVE_UP;
481 }
482 }
483 return ss.str();
484}
485
486/// @brief 画面からすべてのピクセルをクリアします。
488 Image::Clear();
489
490 cursor_.x = dimx_ - 1;
491 cursor_.y = dimy_ - 1;
492
493 hyperlinks_ = {
494 "",
495 };
496}
497
498// clang-format off
500 // ボックス描画文字を結合します。
501 for (int y = 0; y < dimy_; ++y) {
502 for (int x = 0; x < dimx_; ++x) {
503 // ボックス描画文字は正確に3バイトを使用します。
504 Pixel& cur = pixels_[y][x];
505 if (!ShouldAttemptAutoMerge(cur)) {
506 continue;
507 }
508
509 if (x > 0) {
510 Pixel& left = pixels_[y][x-1];
511 if (ShouldAttemptAutoMerge(left)) {
512 UpgradeLeftRight(left.character, cur.character);
513 }
514 }
515 if (y > 0) {
516 Pixel& top = pixels_[y-1][x];
517 if (ShouldAttemptAutoMerge(top)) {
518 UpgradeTopDown(top.character, cur.character);
519 }
520 }
521 }
522 }
523}
524// clang-format on
525
526std::uint8_t Screen::RegisterHyperlink(const std::string& link) {
527 for (std::size_t i = 0; i < hyperlinks_.size(); ++i) {
528 if (hyperlinks_[i] == link) {
529 return i;
530 }
531 }
532 if (hyperlinks_.size() == std::numeric_limits<std::uint8_t>::max()) {
533 return 0;
534 }
535 hyperlinks_.push_back(link);
536 return hyperlinks_.size() - 1;
537}
538
539const std::string& Screen::Hyperlink(std::uint8_t id) const {
540 if (id >= hyperlinks_.size()) {
541 return hyperlinks_[0];
542 }
543 return hyperlinks_[id];
544}
545
546/// @brief 現在の選択スタイルを返します。
547/// @see SetSelectionStyle
551
552/// @brief 現在の選択スタイルを設定します。
553/// @see GetSelectionStyle
555 selection_style_ = std::move(decorator);
556}
557
558} // namespace ftxui
std::function< void(Pixel &)> SelectionStyle
Definition screen.hpp:71
void ApplyShader()
Definition screen.cpp:499
const SelectionStyle & GetSelectionStyle() const
現在の選択スタイルを返します。
Definition screen.cpp:548
const std::string & Hyperlink(uint8_t id) const
Definition screen.cpp:539
std::string ToString() const
Definition screen.cpp:410
static Screen Create(Dimensions dimension)
指定された次元を持つスクリーンを作成します。
Definition screen.cpp:394
uint8_t RegisterHyperlink(const std::string &link)
Definition screen.cpp:526
std::string character
Definition pixel.hpp:44
Screen(int dimx, int dimy)
Definition screen.cpp:398
std::string ResetPosition(bool clear=false) const
カーソル位置を画面の先頭にリセットするために出力する文字列を返します。
Definition screen.cpp:468
Cursor cursor_
Definition screen.hpp:76
void Clear()
画面からすべてのピクセルをクリアします。
Definition screen.cpp:487
SelectionStyle selection_style_
Definition screen.hpp:80
void SetSelectionStyle(SelectionStyle decorator)
現在の選択スタイルを設定します。
Definition screen.cpp:554
std::vector< std::string > hyperlinks_
Definition screen.hpp:77
void Print() const
Definition screen.cpp:447
std::vector< std::vector< Pixel > > pixels_
Definition image.hpp:46
ピクセルの長方形グリッド。
Definition image.hpp:17
ピクセルの長方形グリッド。
Definition screen.hpp:25
Dimensions Size()
ターミナルサイズを取得します。
Definition terminal.cpp:87
Dimensionsは、ターミナルのサイズを表す構造体です。
Definition terminal.hpp:10
Unicode文字とそれに関連付けられたスタイル。
Definition pixel.hpp:14
Dimensions Fixed(int)
Dimensions Full()
FTXUI ftxui:: 名前空間
Definition animation.hpp:9
int string_width(const std::string &)
Definition string.cpp:1324
std::uint8_t top
Definition screen.cpp:130
#define FTXUI_UNLIKELY(x)
Definition screen.cpp:30
std::uint8_t left
Definition screen.cpp:129
std::uint8_t down
Definition screen.cpp:132
std::uint8_t right
Definition screen.cpp:131
std::uint8_t round
Definition screen.cpp:133