foxygit / Torlinkc Log in
commits tags

/phase0/ftxui_hello.cpp · 1.26 KB

raw
// Phase 0 toolchain proof: an FTXUI ScreenInteractive::Fullscreen() component
// tree, quit on q/esc. TORLINK_PHASE0_AUTOEXIT lets this run unattended as a
// smoke test (no real TTY / CI) by exiting itself after 2 seconds.

#include <ftxui/component/component.hpp>
#include <ftxui/component/screen_interactive.hpp>
#include <ftxui/dom/elements.hpp>

#include <chrono>
#include <cstdlib>
#include <thread>

using namespace ftxui;

int main() {
  auto screen = ScreenInteractive::Fullscreen();

  auto renderer = Renderer([&] {
    return vbox({
               text("torlink phase 0 - FTXUI hello") | bold | center,
               separator(),
               text("press q or esc to quit") | dim | center,
           }) |
           border | center;
  });

  auto component = CatchEvent(renderer, [&](Event event) {
    if (event == Event::Character('q') || event == Event::Escape) {
      screen.Exit();
      return true;
    }
    return false;
  });

  std::thread auto_exit_timer;
  if (std::getenv("TORLINK_PHASE0_AUTOEXIT") != nullptr) {
    auto_exit_timer = std::thread([&] {
      std::this_thread::sleep_for(std::chrono::seconds(2));
      screen.Exit();
    });
  }

  screen.Loop(component);

  if (auto_exit_timer.joinable()) {
    auto_exit_timer.join();
  }
  return 0;
}