Advertisement
herhor67

cpp window terminal

May 29th, 2023 (edited)
993
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
C++ 3.51 KB | Source Code | 0 0
  1. #include "Window.h"
  2.  
  3. // STD headers
  4. #include <cmath>
  5. #include  <csignal>
  6.  
  7. using namespace std::literals::chrono_literals;
  8.  
  9. // external libs
  10. #include "intdiv.h"
  11.  
  12. // project headers
  13.  
  14.  
  15.  
  16. Window::Window(const std::string& t) : title(t), contents(emptyElement())
  17. {
  18.     std::lock_guard<std::mutex> guard(inst_mutex);
  19.  
  20.     for (size_t i = 0; i < instances.size(); ++i)
  21.         if (instances[i] == nullptr)
  22.         {
  23.             instanceNum = i;
  24.             instances[i] = this;
  25.             return;
  26.         }
  27.  
  28.     instanceNum = instances.size();
  29.     instances.emplace_back(this);
  30. }
  31.  
  32. Window::~Window()
  33. {
  34.     instances[instanceNum] = nullptr;
  35.     std::lock_guard<std::mutex> guard(inst_mutex);
  36.     while (instances.size() > 0 && instances.back() == nullptr)
  37.         instances.pop_back();
  38. }
  39.  
  40. void Window::updateContents() {}
  41.  
  42.  
  43. // STATIC SHIT
  44.  
  45. void Window::init()
  46. {
  47.     cout << "Spawning Window manager..." << endl;
  48.     writer = std::thread(Window::writing_thread_callback);
  49.     cout << "Done." << endl;
  50. }
  51. void Window::deinit()
  52. {
  53.     //screen.Post(screen.ExitLoopClosure());
  54.     screen.Exit();
  55.     cout << "\tDespawning Window manager..." << endl;
  56.     writer_exit = true;
  57.     writer.join();
  58.     cout << "\tDone." << endl;
  59. }
  60.  
  61. void Window::writing_thread_callback()
  62. {
  63.     // TABS
  64.  
  65.     auto spinner_tab_renderer = Renderer([&]()
  66.         {
  67.             Elements entries;
  68.             for (int i = 0; i < 22; ++i)
  69.                 entries.push_back(spinner(i, 3 / 2) | bold | size(WIDTH, GREATER_THAN, 2) | border);
  70.            
  71.             return hflow(std::move(entries));
  72.         }
  73.     );
  74.  
  75.     auto spinner_tab_renderer2 = Renderer([&]()
  76.         {
  77.             std::vector<Elements> grid;
  78.  
  79.             std::lock_guard<std::mutex> lock(inst_mutex);
  80.  
  81.             size_t len = instances.size();
  82.             size_t wdh = std::round(std::sqrt(len));
  83.  
  84.             size_t i = 0;
  85.             while (i < len)
  86.             {
  87.                 Elements row;
  88.  
  89.                 for (size_t x = 0; (x < wdh) && (i < len); ++x, ++i)
  90.                 {
  91.                     Window& ins = *instances[i];
  92.  
  93.                     ins.updateContents();
  94.                     Element win = window(text(ins.title), ins.contents) | flex;
  95.  
  96.                     row.push_back(win);
  97.                 }
  98.                 grid.push_back(std::move(row));
  99.             }
  100.  
  101.             return gridbox(std::move(grid));
  102.         }
  103.     );
  104.  
  105.  
  106.     int tab_index = 0;
  107.     std::vector<std::string> tab_entries = {
  108.         "Main", "Downloaders", "Logs"
  109.     };
  110.  
  111.     auto tab_selection = Menu(&tab_entries, &tab_index, MenuOption::Horizontal());
  112.  
  113.     auto tab_content = Container::Tab(
  114.         {
  115.             spinner_tab_renderer,
  116.             spinner_tab_renderer2,
  117.         },
  118.         &tab_index);
  119.  
  120.     auto main_container = Container::Vertical({
  121.         tab_selection,
  122.         tab_content,
  123.         });
  124.  
  125.     auto main_renderer = Renderer(main_container, [&] {
  126.         return vbox({
  127.             text("PlayerManager") | bold | hcenter,
  128.             tab_selection->Render(),
  129.             separator(),
  130.             tab_content->Render() | flex,
  131.             }) | borderHeavy | color(Color::White);
  132.         });
  133.  
  134.     std::atomic<bool> refresh_ui_continue = true;
  135.  
  136.     std::thread refresh_ui([&]()
  137.         {
  138.             while (refresh_ui_continue)
  139.             {
  140.                 std::this_thread::sleep_for(100ms);
  141.                 screen.Post(Event::Custom);
  142.             }
  143.         }
  144.     );
  145.  
  146.     screen.Loop(main_renderer);
  147.     refresh_ui_continue = false;
  148.     refresh_ui.join();
  149.  
  150.     std::raise(SIGQUIT);
  151. }
  152.  
  153.  
  154. // TERMINAL
  155.  
  156. void WindowTerminal::updateContents()
  157. {
  158.     Elements innards;
  159.  
  160.     for (const auto& line : lines)
  161.         innards.push_back(line);
  162.    
  163.     if (innards.size())
  164.         innards.back() |= focus;
  165.  
  166.     contents = vbox(innards) | vscroll_indicator | yframe | yflex;
  167. }
  168.  
  169. void WindowTerminal::set_max_lines(size_t m)
  170. {
  171.     max_lines = m;
  172.     while (lines.size() > max_lines)
  173.         lines.pop_front();
  174. }
  175.  
  176. void WindowTerminal::add_line(const std::string& l)
  177. {
  178.     lines.push_back(text(l));
  179.     if (lines.size() > max_lines)
  180.         lines.pop_front();
  181. }
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement