attempt for wrapped process log

This commit is contained in:
Vladislav Nepogodin 2021-12-26 22:42:19 +04:00
parent 83dbb33062
commit 00e43ff34e
No known key found for this signature in database
GPG Key ID: B62C3D10C54D5DA9
8 changed files with 152 additions and 33 deletions

View File

@ -113,6 +113,13 @@ executable(
include_directories: [include_directories('src')], include_directories: [include_directories('src')],
install: false) install: false)
executable(
'test-process-tailing',
files('src/config.cpp', 'src/widgets.cpp', 'src/tui.cpp', 'src/utils.cpp', 'src/test_proccess_tailing.cpp'),
dependencies: [fmt, spdlog, ftxui, cpr, glibmm],
include_directories: [include_directories('src')],
install: false)
summary( summary(
{ {
'Build type': get_option('buildtype'), 'Build type': get_option('buildtype'),

View File

@ -1,8 +1,8 @@
#include "config.hpp" #include "config.hpp"
#include "definitions.hpp" #include "definitions.hpp" // for error_inter
#include <filesystem> #include <filesystem> // for exists
#include <memory> #include <memory> // for unique_ptr, make_unique, operator==
static std::unique_ptr<Config> s_config = nullptr; static std::unique_ptr<Config> s_config = nullptr;

View File

@ -1,11 +1,12 @@
#ifndef CONFIG_HPP #ifndef CONFIG_HPP
#define CONFIG_HPP #define CONFIG_HPP
#include <string> #include <cstdint> // for int32_t
#include <string_view> #include <string> // for string
#include <unordered_map> #include <string_view> // for string_view, hash
#include <variant> #include <unordered_map> // for unordered_map
#include <vector> #include <variant> // for variant
#include <vector> // for vector
class Config final { class Config final {
public: public:

View File

@ -1,15 +1,16 @@
#include "config.hpp" #include "config.hpp" // for Config
#include "definitions.hpp" #include "definitions.hpp" // for error_inter
#include "screen_service.hpp" #include "tui.hpp" // for init
#include "tui.hpp" #include "utils.hpp" // for exec, check_root, clear_sc...
#include "utils.hpp"
#include <chrono> // for seconds #include <chrono> // for seconds
#include <regex> // for regex_search, match_results<>::_Base_type #include <regex> // for regex_search, match_result...
#include <thread> // for sleep_for #include <thread> // for sleep_for
#include <spdlog/async.h> #include <spdlog/async.h> // for create_async
#include <spdlog/sinks/basic_file_sink.h> #include <spdlog/common.h> // for debug
#include <spdlog/sinks/basic_file_sink.h> // for basic_file_sink_mt
#include <spdlog/spdlog.h> // for set_default_logger, set_level
int main() { int main() {
const auto& tty = utils::exec("tty"); const auto& tty = utils::exec("tty");

View File

@ -2,11 +2,11 @@
#ifndef SCREEN_SERVICE_HPP #ifndef SCREEN_SERVICE_HPP
#define SCREEN_SERVICE_HPP #define SCREEN_SERVICE_HPP
#include "view.hpp" //#include "view.hpp"
/* clang-format off */ /* clang-format off */
#include <memory> //#include <memory>
#include <ftxui/component/screen_interactive.hpp> #include <ftxui/component/screen_interactive.hpp> // for ScreenInteractive
/* clang-format on */ /* clang-format on */
namespace tui { namespace tui {

View File

@ -0,0 +1,95 @@
#include <cstdio>
#include <thread>
#include <chrono> // for operator""s, chrono_literals
#include <ftxui/component/component.hpp> // for Renderer, Button
#include <ftxui/component/component_options.hpp> // for ButtonOption, Inpu...
#include <ftxui/component/screen_interactive.hpp> // for ScreenInteractive
#include <ftxui/dom/elements.hpp> // for operator|, text, Element, hbox, bold, color, filler, separator, vbox, window, gauge, Fit, size, dim, EQUAL, WIDTH
#include <ftxui/screen/screen.hpp> // for Full, Screen
#include <iostream> // for cout, endl, ostream
#include <list> // for list, operator!=, _List_iterator, _List_iterator<>::_Self
#include <memory> // for allocator, shared_ptr, allocator_traits<>::value_type
#include <string> // for string, operator<<, to_string
#include <thread> // for sleep_for
#include <utility> // for move
#include <vector> // for vector
#include <ftxui/component/captured_mouse.hpp>
#include <ftxui/component/event.hpp>
#include <ftxui/dom/node.hpp> // for Render
#include <ftxui/screen/box.hpp> // for ftxui
#include <ftxui/screen/color.hpp> // for Color, Color::Green, Color::Red, Color::RedLight
#include "subprocess.h"
#include "utils.hpp"
#include "widgets.hpp"
static bool running{true};
static std::string process_log{};
using namespace std::chrono_literals;
void execute() {
const char* const commandLine[] = {"/sbin/tail", "-f", "/tmp/smth.log", nullptr};
struct subprocess_s process;
int ret = -1;
static char data[1048576 + 1] = {0};
unsigned index = 0;
unsigned bytes_read = 0;
if ((ret = subprocess_create(commandLine, subprocess_option_enable_async | subprocess_option_combined_stdout_stderr, &process)) != 0) {
std::perror("creation failed");
std::cerr << "creation failed with: " << ret << '\n';
running = false;
return;
}
do {
bytes_read = subprocess_read_stdout(&process, data + index,
sizeof(data) - 1 - index);
index += bytes_read;
process_log = data;
} while (bytes_read != 0);
subprocess_join(&process, &ret);
subprocess_destroy(&process);
}
void execute_thread() {
while (running) {
execute();
std::this_thread::sleep_for(2s);
}
}
int main() {
using namespace ftxui;
std::jthread t(execute_thread);
auto screen = ScreenInteractive::Fullscreen();
std::jthread refresh_ui([&] {
while (running) {
std::this_thread::sleep_for(0.05s);
screen.PostEvent(Event::Custom);
}
});
auto button_option = ButtonOption();
button_option.border = false;
auto button_back = Button("Back", screen.ExitLoopClosure(), &button_option);
auto container = Container::Horizontal({button_back});
auto renderer = Renderer(container, [&] {
return tui::detail::centered_widget(container, "New CLI Installer", tui::detail::multiline_text(utils::make_multiline(process_log)) | vscroll_indicator | yframe | flex);
});
screen.Loop(renderer);
running = false;
t.join();
refresh_ui.join();
}

View File

@ -1,14 +1,22 @@
#include "widgets.hpp" #include "widgets.hpp"
#include "config.hpp" #include "utils.hpp" // for make_multiline
#include "utils.hpp"
/* clang-format off */ #include <algorithm> // for transform
#include <algorithm> // for transform #include <cstddef> // for size_t
#include <string> // for basic_string #include <iterator> // for back_insert_iterator
#include <ftxui/component/component.hpp> // for Renderer, Button #include <memory> // for shared_ptr, __shar...
#include <ftxui/component/component_options.hpp> // for ButtonOption #include <string> // for string, allocator
#include <ftxui/component/screen_interactive.hpp> // for Component, ScreenI... #include <utility> // for move
/* clang-format on */
#include <ftxui/component/captured_mouse.hpp> // for ftxui
#include <ftxui/component/component.hpp> // for Renderer, Vertical
#include <ftxui/component/component_base.hpp> // for ComponentBase, Com...
#include <ftxui/component/component_options.hpp> // for ButtonOption, Inpu...
#include <ftxui/component/screen_interactive.hpp> // for ScreenInteractive
#include <ftxui/dom/elements.hpp> // for operator|, Element
#include <ftxui/dom/node.hpp> // for Render
#include <ftxui/screen/screen.hpp> // for Full, Screen
#include <ftxui/util/ref.hpp> // for Ref
using namespace ftxui; using namespace ftxui;

View File

@ -1,12 +1,19 @@
#ifndef WIDGETS_HPP #ifndef WIDGETS_HPP
#define WIDGETS_HPP #define WIDGETS_HPP
#include <array> // for array
#include <cstdint> // for int32_t
#include <functional> // for function
#include <string> // for string
#include <string_view> // for string_view
#include <vector> // for vector
#include <ftxui/component/component_base.hpp> // for Components
#include <ftxui/component/screen_interactive.hpp> // for Component
#include <ftxui/dom/elements.hpp> // for size, GREATER_THAN
/* clang-format off */ /* clang-format off */
#include <string_view> // for string_view namespace ftxui { struct ButtonOption; }
#include <ftxui/component/component_base.hpp> // for Component
#include <ftxui/component/component_options.hpp> // for ButtonOption
#include <ftxui/dom/elements.hpp> // for Element, operator|, size
#include <ftxui/component/screen_interactive.hpp>
/* clang-format on */ /* clang-format on */
namespace tui { namespace tui {