mirror of
				https://github.com/ArthurSonzogni/FTXUI.git
				synced 2025-10-31 10:38:09 +08:00 
			
		
		
		
	Add Graph.
This commit is contained in:
		| @@ -73,7 +73,7 @@ class MyComponent : public Component { | ||||
|         separator(), | ||||
|         Render(L"radiobox", radiobox), | ||||
|         separator(), | ||||
|         Render(L"input", input) | size(WIDTH, EQUAL, 20) | ||||
|         Render(L"input", input) | size(WIDTH, LESS_THAN, 30) | ||||
|       ) | border; | ||||
|   } | ||||
| }; | ||||
|   | ||||
| @@ -8,6 +8,7 @@ example(border) | ||||
| example(gauge) | ||||
| example(package_manager) | ||||
| example(separator) | ||||
| example(graph) | ||||
| example(spinner) | ||||
| example(style_blink) | ||||
| example(style_bold) | ||||
|   | ||||
| @@ -41,7 +41,7 @@ int main(int argc, const char *argv[]) | ||||
|       ), | ||||
|       filler() | ||||
|     ); | ||||
|   auto screen = Screen::TerminalOutput(document); | ||||
|   auto screen = Screen::Create(Dimension::Full(), Dimension::Fit(document)); | ||||
|   Render(screen, document.get()); | ||||
|   std::cout << screen.ToString() << std::endl; | ||||
| } | ||||
|   | ||||
| @@ -16,7 +16,7 @@ int main(int argc, const char *argv[]) | ||||
|       ) | border, | ||||
|       text(L"overlay") | border | center | ||||
|     ); | ||||
|   auto screen = Screen::TerminalOutput(document); | ||||
|   auto screen = Screen::Create(Dimension::Full(), Dimension::Fit(document)); | ||||
|   Render(screen, document.get()); | ||||
|  | ||||
|   std::cout << screen.ToString(); | ||||
|   | ||||
							
								
								
									
										65
									
								
								examples/dom/graph.cpp
									
									
									
									
									
										Normal file
									
								
							
							
						
						
									
										65
									
								
								examples/dom/graph.cpp
									
									
									
									
									
										Normal file
									
								
							| @@ -0,0 +1,65 @@ | ||||
| #include "ftxui/dom/elements.hpp" | ||||
| #include "ftxui/dom/graph.hpp" | ||||
| #include "ftxui/screen/screen.hpp" | ||||
| #include "ftxui/screen/string.hpp" | ||||
| #include <chrono> | ||||
| #include <cmath> | ||||
| #include <iostream> | ||||
| #include <thread> | ||||
|  | ||||
| class Graph : public ftxui::GraphFunction { | ||||
|  public: | ||||
|   std::vector<int> operator()(int width, int height) { | ||||
|     std::vector<int> output(width); | ||||
|     for (int i = 0; i < width; ++i) { | ||||
|       float v = 0; | ||||
|       v += 0.1 * sin((i + shift) * 0.1); | ||||
|       v += 0.2 * sin((i + shift+10) * 0.15); | ||||
|       v += 0.1 * sin((i + shift) * 0.03); | ||||
|       // v += 0.2*sin((i+shift)*0.3); | ||||
|       // v += 0.1*sin((i+shift)*0.9); | ||||
|       v *= height; | ||||
|       v += 0.5 * height; | ||||
|       output[i] = v; | ||||
|     } | ||||
|     return output; | ||||
|   } | ||||
|   int shift = 0; | ||||
| }; | ||||
|  | ||||
| int main(int argc, const char* argv[]) { | ||||
|   using namespace ftxui; | ||||
|   using namespace std::chrono_literals; | ||||
|  | ||||
|   Graph my_graph; | ||||
|  | ||||
|   std::string reset_position; | ||||
|   for (int i = 0;; ++i) { | ||||
|     auto document = | ||||
|       window(text(L"Your graphs"), | ||||
|         hbox( | ||||
|           vbox( | ||||
|             graph(my_graph), separator(), | ||||
|             graph(my_graph) | inverted | ||||
|           ) | flex, | ||||
|           separator(), | ||||
|           vbox( | ||||
|             graph(my_graph) | color(Color::BlueLight), separator(), | ||||
|             graph(my_graph) | color(Color::RedLight), separator(), | ||||
|             graph(my_graph) | color(Color::YellowLight) | ||||
|           ) | flex | ||||
|         ) | ||||
|       ) | size(HEIGHT, GREATER_THAN, 40); | ||||
|  | ||||
|     auto screen = Screen::Create(Dimension::Full(), Dimension::Fit(document)); | ||||
|     Render(screen, document.get()); | ||||
|     std::cout << reset_position << screen.ToString() << std::flush; | ||||
|  | ||||
|     reset_position = screen.ResetPosition(); | ||||
|  | ||||
|     std::this_thread::sleep_for(0.03s); | ||||
|     my_graph.shift++; | ||||
|   } | ||||
|  | ||||
|   return 0; | ||||
| } | ||||
| @@ -43,7 +43,7 @@ int main(int argc, const char *argv[]) | ||||
|       | size(WIDTH, LESS_THAN, 50) | ||||
|       ; | ||||
|  | ||||
|   auto screen = Screen::TerminalOutput(document); | ||||
|   auto screen = Screen::Create(Dimension::Full(), Dimension::Fit(document)); | ||||
|   Render(screen, document.get()); | ||||
|   std::cout << screen.ToString() << std::endl; | ||||
|  | ||||
|   | ||||
| @@ -39,7 +39,7 @@ int main(int argc, const char* argv[]) { | ||||
|             paragraph(L" A spinner "), spinner(6, i / 10)) | | ||||
|         border; | ||||
|  | ||||
|     auto screen = Screen::TerminalFullscreen(); | ||||
|     auto screen = Screen::Create(Dimension::Full()); | ||||
|     Render(screen, document.get()); | ||||
|     std::cout << reset_position << screen.ToString() << std::flush; | ||||
|  | ||||
|   | ||||
| @@ -115,7 +115,7 @@ int main(int argc, const char *argv[]) | ||||
|  | ||||
|     // Draw. | ||||
|     auto document = render(); | ||||
|     auto screen = Screen::TerminalOutput(document); | ||||
|     auto screen = Screen::Create(Dimension::Full(), Dimension::Fit(document)); | ||||
|     Render(screen, document.get()); | ||||
|     std::cout << reset_position << screen.ToString() << std::flush; | ||||
|     reset_position = screen.ResetPosition(); | ||||
|   | ||||
| @@ -24,7 +24,7 @@ int main(int argc, const char *argv[]) | ||||
|       ) | flex | ||||
|     ); | ||||
|  | ||||
|   auto screen = Screen::TerminalFullscreen(); | ||||
|   auto screen = Screen::Create(Dimension::Full(), Dimension::Fit(document)); | ||||
|   Render(screen, document.get()); | ||||
|   std::cout << screen.ToString(); | ||||
|   getchar(); | ||||
|   | ||||
| @@ -9,17 +9,18 @@ int main(int argc, const char *argv[]) | ||||
|     hbox( | ||||
|       text(L"left-column"), | ||||
|       separator(), | ||||
|       flex(vbox( | ||||
|         center(text(L"right-column")) | flex, | ||||
|       vbox( | ||||
|         center(text(L"right-top")) | flex, | ||||
|         separator(), | ||||
|         center(text(L"bottom-column")) | ||||
|       )) | ||||
|     ); | ||||
|   auto screen = Screen::TerminalFullscreen(); | ||||
|         center(text(L"bottom-bottom")) | ||||
|       ) | flex, | ||||
|       separator(), | ||||
|       text(L"right-column") | ||||
|     ) | border; | ||||
|   auto screen = Screen::Create(Dimension::Full(), Dimension::Fit(document)); | ||||
|   Render(screen, document.get()); | ||||
|  | ||||
|   std::cout << screen.ToString(); | ||||
|   getchar(); | ||||
|   std::cout << screen.ToString() << std::endl; | ||||
|  | ||||
|   return 0; | ||||
| } | ||||
|   | ||||
| @@ -22,7 +22,7 @@ int main(int argc, const char *argv[]) | ||||
|     ); | ||||
|   } | ||||
|   auto document = hbox(std::move(content)); | ||||
|   auto screen = Screen::FitDocument(document); | ||||
|   auto screen = Screen::Create(Dimension::Fit(document)); | ||||
|   Render(screen, document.get()); | ||||
|   std::cout << screen.ToString() << std::endl; | ||||
|  | ||||
|   | ||||
| @@ -26,7 +26,7 @@ int main(int argc, const char *argv[]) | ||||
|       ); | ||||
|     } | ||||
|     auto document = hbox(vbox(std::move(entries)) | border, filler()); | ||||
|     auto screen = Screen::TerminalOutput(document); | ||||
|     auto screen = Screen::Create(Dimension::Full(), Dimension::Fit(document)); | ||||
|     Render(screen, document.get()); | ||||
|     std::cout << reset_position << screen.ToString() << std::flush; | ||||
|     reset_position = screen.ResetPosition(); | ||||
|   | ||||
| @@ -11,7 +11,7 @@ int main(int argc, const char *argv[]) | ||||
|       text(L"blink") | blink, | ||||
|       text(L". Do you like it?") | ||||
|     ); | ||||
|   auto screen = Screen::TerminalOutput(document); | ||||
|   auto screen = Screen::Create(Dimension::Full(), Dimension::Fit(document)); | ||||
|   Render(screen, document.get()); | ||||
|  | ||||
|   std::cout << screen.ToString(); | ||||
|   | ||||
| @@ -11,7 +11,7 @@ int main(int argc, const char *argv[]) | ||||
|       text(L"bold") | bold, | ||||
|       text(L". Do you like it?") | ||||
|     ); | ||||
|   auto screen = Screen::TerminalOutput(document); | ||||
|   auto screen = Screen::Create(Dimension::Full(), Dimension::Fit(document)); | ||||
|   Render(screen, document.get()); | ||||
|  | ||||
|   std::cout << screen.ToString(); | ||||
|   | ||||
| @@ -48,7 +48,7 @@ int main(int argc, const char *argv[]) | ||||
|       filler() | ||||
|     ); | ||||
|  | ||||
|   auto screen = Screen::TerminalOutput(document); | ||||
|   auto screen = Screen::Create(Dimension::Full(), Dimension::Fit(document)); | ||||
|   Render(screen, document.get()); | ||||
|  | ||||
|   std::cout << screen.ToString(); | ||||
|   | ||||
| @@ -11,7 +11,7 @@ int main(int argc, const char *argv[]) | ||||
|       text(L"dim") | dim, | ||||
|       text(L". Do you like it?") | ||||
|     ); | ||||
|   auto screen = Screen::TerminalOutput(document); | ||||
|   auto screen = Screen::Create(Dimension::Full(), Dimension::Fit(document)); | ||||
|   Render(screen, document.get()); | ||||
|  | ||||
|   std::cout << screen.ToString(); | ||||
|   | ||||
| @@ -16,7 +16,7 @@ int main(int argc, const char *argv[]) | ||||
|       text(L"color")     | color(Color::Blue)   , text(L" ") , | ||||
|       text(L"bgcolor")   | bgcolor(Color::Blue) | ||||
|     ); | ||||
|   auto screen = Screen::TerminalOutput(document); | ||||
|   auto screen = Screen::Create(Dimension::Full(), Dimension::Fit(document)); | ||||
|   Render(screen, document.get()); | ||||
|  | ||||
|   std::cout << screen.ToString(); | ||||
|   | ||||
| @@ -11,7 +11,7 @@ int main(int argc, const char *argv[]) | ||||
|       text(L"inverted") | inverted, | ||||
|       text(L". Do you like it?") | ||||
|     ); | ||||
|   auto screen = Screen::TerminalOutput(document); | ||||
|   auto screen = Screen::Create(Dimension::Full(), Dimension::Fit(document)); | ||||
|   Render(screen, document.get()); | ||||
|  | ||||
|   std::cout << screen.ToString(); | ||||
|   | ||||
| @@ -11,7 +11,7 @@ int main(int argc, const char *argv[]) | ||||
|       text(L"underlined") | underlined, | ||||
|       text(L". Do you like it?") | ||||
|     ); | ||||
|   auto screen = Screen::TerminalOutput(document); | ||||
|   auto screen = Screen::Create(Dimension::Full(), Dimension::Fit(document)); | ||||
|   Render(screen, document.get()); | ||||
|  | ||||
|   std::cout << screen.ToString(); | ||||
|   | ||||
| @@ -28,7 +28,7 @@ int main(int argc, const char *argv[]) | ||||
|         text(L"south-east") | ||||
|       ) | ||||
|     ); | ||||
|   auto screen = Screen::TerminalFullscreen(); | ||||
|   auto screen = Screen::Create(Dimension::Full()); | ||||
|   Render(screen, document.get()); | ||||
|  | ||||
|   std::cout << screen.ToString(); | ||||
|   | ||||
		Reference in New Issue
	
	Block a user
	 ArthurSonzogni
					ArthurSonzogni