Add spinner.

This commit is contained in:
Arthur Sonzogni 2019-01-06 22:28:15 +01:00
parent 3b0a56e114
commit acc7012f58
5 changed files with 324 additions and 3 deletions

View File

@ -13,6 +13,7 @@ example(gauge)
example(inverted)
example(package_manager)
example(separator)
example(underlined)
example(spinner)
example(style_gallery)
example(underlined)
example(vbox_hbox)

38
examples/dom/spinner.cpp Normal file
View File

@ -0,0 +1,38 @@
#include <chrono>
#include <iostream>
#include <thread>
#include "ftxui/screen/screen.hpp"
#include "ftxui/dom/elements.hpp"
#include "ftxui/util/string.hpp"
int main(int argc, const char *argv[])
{
using namespace ftxui;
using namespace ftxui::dom;
using namespace ftxui::screen;
using namespace std::chrono_literals;
std::string reset_position;
for(int index = 0; index < 200; ++index) {
std::vector<Element> entries;
for(int i = 0; i<22; ++i) {
if (i != 0)
entries.push_back(separator());
entries.push_back(
hbox(
text(to_wstring(i)) | size(5,1),
spinner(i, index) | bold
)
);
}
auto document = hbox(vbox(std::move(entries)) | frame, filler());
auto screen = Screen::TerminalOutput(document);
Render(screen, document.get());
std::cout << reset_position << screen.ToString() << std::flush;
reset_position = screen.ResetPosition();
std::this_thread::sleep_for(0.1s);
}
std::cout << std::endl;
}

View File

@ -38,6 +38,7 @@ add_library(dom
src/ftxui/dom/node_decorator.cpp
src/ftxui/dom/separator.cpp
src/ftxui/dom/size.cpp
src/ftxui/dom/spinner.cpp
src/ftxui/dom/text.cpp
src/ftxui/dom/underlined.cpp
src/ftxui/dom/util.cpp

View File

@ -30,6 +30,7 @@ Element separator();
Element gauge(float ratio);
Element frame(Element);
Element window(Child title, Child content);
Element spinner(int charset_index, size_t image_index);
// -- Decorator ---
Element bold(Element);
@ -37,10 +38,11 @@ Element dim(Element);
Element inverted(Element);
Element underlined(Element);
Element blink(Element);
Element color(Color, Element);
Element bgcolor(Color, Element);
Decorator color(Color);
Decorator bgcolor(Color);
Element color(Color, Element);
Element bgcolor(Color, Element);
// --- Util ---
Element hcenter(Element);

View File

@ -0,0 +1,279 @@
#include "ftxui/dom/node.hpp"
#include "ftxui/dom/elements.hpp"
namespace ftxui::dom {
using namespace ftxui::screen;
static const std::vector<std::vector<std::vector<std::wstring>>> elements = {
{
{L"Replaced by the gauge"},
},
{
{L". "},
{L".. "},
{L"..."},
},
{
{L"|"},
{L"/"},
{L"-"},
{L"\\"},
},
{
{L"+"},
{L"x"},
},
{
{L"| "},
{L"|| "},
{L"|||"},
},
{
{L""},
{L""},
{L""},
{L""},
{L""},
{L""},
{L""},
{L""},
},
{
{L""},
{L""},
{L""},
{L""},
{L""},
{L""},
{L""},
{L""},
{L""},
{L""},
{L""},
{L""},
{L""},
{L""},
},
{
{L""},
{L""},
{L""},
{L""},
{L""},
{L""},
{L""},
{L""},
{L""},
{L""},
{L""},
{L""},
},
{
{L""},
{L""},
{L""},
{L""},
},
{
{L""},
{L""},
{L""},
{L""},
},
{
{L""},
{L""},
{L""},
{L""},
},
{
{L""},
{L""},
{L""},
{L""},
},
{
{L""},
{L""},
{L""},
{L""},
},
{
{L""},
{L""},
{L""},
},
{
{L""},
{L""},
{L""},
{L""},
{L""},
{L""},
{L""},
{L""},
},
{
{L""},
{L""},
{L""},
{L""},
{L""},
{L""},
{L""},
{L""},
{L""},
{L""},
},
{
{L"(*----------)"},
{L"(-*---------)"},
{L"(--*--------)"},
{L"(---*-------)"},
{L"(----*------)"},
{L"(-----*-----)"},
{L"(------*----)"},
{L"(-------*---)"},
{L"(--------*--)"},
{L"(---------*-)"},
{L"(----------*)"},
{L"(---------*-)"},
{L"(--------*--)"},
{L"(-------*---)"},
{L"(------*----)"},
{L"(-----*-----)"},
{L"(----*------)"},
{L"(---*-------)"},
{L"(--*--------)"},
{L"(-*---------)"},
},
{
{L"[ ]"},
{L"[= ]"},
{L"[== ]"},
{L"[=== ]"},
{L"[==== ]"},
{L"[===== ]"},
{L"[======]"},
{L"[===== ]"},
{L"[==== ]"},
{L"[=== ]"},
{L"[== ]"},
{L"[= ]"},
},
{
{L"[ ]"},
{L"[= ]"},
{L"[== ]"},
{L"[=== ]"},
{L"[==== ]"},
{L"[===== ]"},
{L"[======]"},
{L"[ =====]"},
{L"[ ====]"},
{L"[ ===]"},
{L"[ ==]"},
{L"[ =]"},
},
{
{L"[== ]"},
{L"[== ]"},
{L"[== ]"},
{L"[== ]"},
{L"[== ]"},
{L" [== ]"},
{L"[ == ]"},
{L"[ == ]"},
{L"[ ==]"},
{L"[ ==]"},
{L"[ ==]"},
{L"[ ==]"},
{L"[ ==]"},
{L"[ ==] "},
{L"[ == ]"},
{L"[ == ]"},
},
{
{
L" ─╮",
L"",
L" ",
},
{
L"",
L"",
L"",
},
{
L" ",
L"",
L" ─╯",
},
{
L" ",
L" ",
L"╰─╯",
},
{
L" ",
L"",
L"╰─ ",
},
{
L"",
L"",
L"",
},
{
L"╭─ ",
L"",
L" ",
},
{
L"╭─╮",
L" ",
L" ",
}
},
{
{
L" /\\O ",
L" /\\/ ",
L" /\\ ",
L" / \\ ",
L"LOL LOL",
},
{
L" _O ",
L" //|_ ",
L" | ",
L" /| ",
L" LLOL ",
},
{
L" O ",
L" /_ ",
L" |\\ ",
L" / | ",
L" LOLLOL ",
}
}
};
std::unique_ptr<Node> spinner(int c, size_t index) {
if (c == 0) {
index %= 40;
if (index > 20)
index = 40-index;
return gauge(index * 0.05);
}
c %= elements.size();
index %= elements[c].size();
std::vector<Element> lines;
for(const auto& it : elements[c][index])
lines.push_back(text(it));
return vbox(std::move(lines));
}
}; // namespace ftxui::dom