Repository navigation
Expand file tree
/
Copy pathscratchpad.cpp
More file actions
455 lines (384 loc) · 12.8 KB
/
Copy pathscratchpad.cpp
File metadata and controls
455 lines (384 loc) · 12.8 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
//
// Created by Kyle on 18.06.24.
//
#include "order_book_component.hpp"
#include "ftxui/component/component.hpp"
#include "ftxui/dom/elements.hpp"
#include "data_utils.hpp" // Include the utility header
#include <random>
using namespace ftxui;
// Function to create order book table
ftxui::Element OrderBookTable(const std::vector<OrderBookComponent::Order>& orders, bool is_bid) {
ftxui::Elements rows;
for (const auto& order : orders) {
auto price_text = text(FormatFloat(order.price)) | flex | (is_bid ? color(Color::Green) : color(Color::Red));
auto size_text = text(FormatFloat(order.size)) | flex;
if (order.is_user_order) {
price_text = price_text | bold;
size_text = size_text | bold | bgcolor(Color::BlueLight);
}
rows.push_back(hbox({
price_text,
separator(),
size_text,
}) | xflex);
}
return vbox(std::move(rows));
}
OrderBookComponent::OrderBookComponent()
: last_update_time_(std::chrono::steady_clock::now()),
random_engine_(std::random_device{}()),
price_distribution_(45000, 55000),
volume_distribution_(1, 5) {
InitializeTable();
order_book_component_ = Container::Vertical({
Renderer([&] {
return text("Order Book") | bold | hcenter | size(HEIGHT, LESS_THAN, 1);
}),
Renderer(table_container_, [&] {
return table_container_->Render() | vscroll_indicator | frame | flex;
})
});
}
void OrderBookComponent::InitializeTable() {
table_container_ = Container::Vertical({});
bids_ = GenerateSimulatedOrders<OrderBookComponent::Order>(true, 50, 50000, 100);
asks_ = GenerateSimulatedOrders<OrderBookComponent::Order>(false, 50, 50500, 100);
// Create rows for bids and asks
for (int i = 0; i < 50; ++i) {
auto row = Make<TableRow>(i, &bids_, &asks_);
table_container_->Add(row);
rows_.push_back(row);
}
}
ftxui::Component OrderBookComponent::GetRenderer() {
return order_book_component_;
}
void OrderBookComponent::Update() {
// Periodic update to change row values
UpdateRowValuesIfNeeded();
}
void OrderBookComponent::UpdateRowValuesIfNeeded() {
auto now = std::chrono::steady_clock::now();
auto elapsed = std::chrono::duration_cast<std::chrono::seconds>(now - last_update_time_).count();
if (elapsed >= 5) {
bids_ = GenerateSimulatedOrders<OrderBookComponent::Order>(true, 50, 50000, 100);
asks_ = GenerateSimulatedOrders<OrderBookComponent::Order>(false, 50, 50500, 100);
last_update_time_ = now; // Reset the timestamp
}
}
// Definition of TableRow
OrderBookComponent::TableRow::TableRow(int index, std::vector<Order>* bids, std::vector<Order>* asks)
: index_(index), bids_(bids), asks_(asks) {}
ftxui::Element OrderBookComponent::TableRow::Render() {
const auto& bid_order = (*bids_)[index_];
const auto& ask_order = (*asks_)[index_];
auto bid_price = text(FormatFloat(bid_order.price)) | color(Color::Green) | flex;
auto bid_volume = text(FormatFloat(bid_order.size)) | color(Color::Green) | flex;
auto ask_price = text(FormatFloat(ask_order.price)) | color(Color::Red) | flex;
auto ask_volume = text(FormatFloat(ask_order.size)) | color(Color::Red) | flex;
if (bid_order.is_user_order) {
bid_price = bid_price | bold;
bid_volume = bid_volume | bold | bgcolor(Color::BlueLight);
}
if (ask_order.is_user_order) {
ask_price = ask_price | bold;
ask_volume = ask_volume | bold | bgcolor(Color::BlueLight);
}
auto element = hbox({
bid_price | size(WIDTH, LESS_THAN, 15) | flex,
separator() | color(Color::GrayDark),
bid_volume | size(WIDTH, LESS_THAN, 15) | flex,
filler(),
ask_price | size(WIDTH, LESS_THAN, 15) | flex,
separator() | color(Color::GrayDark),
ask_volume | size(WIDTH, LESS_THAN, 15) | flex
}) | xflex;
if (Focused()) {
element = focus(element);
element |= inverted;
} else if (Active()) {
element = select(element);
element |= inverted;
}
return element | size(HEIGHT, EQUAL, 1);
}
bool OrderBookComponent::TableRow::Focusable() const {
return true;
}
//=========
#include <thread>
#include <atomic>
#include "ftxui/component/component.hpp"
#include "ftxui/component/screen_interactive.hpp"
#include "ftxui/dom/elements.hpp"
#include "htop_component.hpp"
#include "flags_component.hpp"
#include "order_book_component.hpp"
#include "bar_chart_component.hpp"
using namespace ftxui;
int main() {
auto screen = ScreenInteractive::Fullscreen();
// Flags
FlagsComponent flags_component;
auto flags_renderer = flags_component.GetRenderer();
// Htop
HtopComponent htop_component;
// Order Book
OrderBookComponent order_book_component;
auto order_book_renderer = order_book_component.GetRenderer();
// Bar Chart Component
BarChartComponent bar_chart_component;
int tab_index = 0;
std::vector<std::string> tab_entries = {
"flags", "orderbook", "barchart", "chart 2"
};
auto tab_selection =
Menu(&tab_entries, &tab_index, MenuOption::HorizontalAnimated());
auto tab_content = Container::Tab(
{
flags_renderer,
order_book_renderer,
Renderer([&] { return bar_chart_component.Render(); }),
Renderer([&] { return htop_component.Render(); }),
},
&tab_index);
auto exit_button = Button(
"Exit", [&] { screen.Exit(); }, ButtonOption::Animated());
auto main_container = Container::Vertical({
Container::Horizontal({
tab_selection,
exit_button,
}),
tab_content,
});
auto main_renderer = Renderer(main_container, [&] {
return vbox({
text("FTXUI Terminal TUI Demo") | bold | hcenter,
hbox({
tab_selection->Render() | flex,
exit_button->Render(),
}),
tab_content->Render() | flex,
});
});
std::atomic<bool> refresh_ui_continue = true;
std::thread refresh_ui([&] {
while (refresh_ui_continue) {
using namespace std::chrono_literals;
std::this_thread::sleep_for(0.05s);
screen.Post([&] {
flags_component.Update();
order_book_component.Update();
bar_chart_component.Update();
htop_component.Update();
});
screen.Post(Event::Custom);
}
});
screen.Loop(main_renderer);
refresh_ui_continue = false;
refresh_ui.join();
return 0;
}
auto output = Renderer([&] {
Elements children = {
text("Server status, connections and outputs are printed here."),
separator() | color(Color::Yellow),
};
for (size_t i = std::max(0, (int) sharedVec.vec.size() - 9); i < sharedVec.vec.size(); ++i) {
std::lock_guard<std::mutex> lock(sharedVec.vecMutex);
children.push_back(text(sharedVec.vec[i]));
}
return window(text("Output"), vbox(children) | color(Color::Yellow4) | flex) | color(Color::Yellow4);
});
cmake_minimum_required(VERSION 3.26)
project(prologix_emulator LANGUAGES CXX VERSION 1.0.0)
# Specify the C++ compiler based on platform
if(NOT CMAKE_CXX_COMPILER)
if(WIN32)
set(CMAKE_CXX_COMPILER "C:/MinGW/bin/g++.exe") # Adjust path as per your MinGW installation
# Optionally, for clang through MinGW:
# set(CMAKE_CXX_COMPILER "C:/MinGW/bin/clang++.exe")
elseif(APPLE)
set(CMAKE_CXX_COMPILER "/usr/bin/clang++")
elseif(UNIX)
set(CMAKE_CXX_COMPILER "/usr/bin/g++")
endif()
endif()
# Boost
set(BOOST_INCLUDE_LIBRARIES thread filesystem system program_options asio date_time)
set(BOOST_ENABLE_CMAKE ON)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/../bin)
include(FetchContent)
FetchContent_Declare(
Boost
GIT_REPOSITORY https://github.com/boostorg/boost.git
GIT_PROGRESS TRUE
GIT_TAG boost-1.85.0 # Update to the required Boost version
)
FetchContent_MakeAvailable(Boost)
# FTXUI
FetchContent_Declare(
ftxui
GIT_REPOSITORY https://github.com/ArthurSonzogni/ftxui
GIT_TAG main # Important: Specify a version or a commit hash here.
)
FetchContent_MakeAvailable(ftxui)
# Include Boost and FTXUI headers
include_directories(${Boost_INCLUDE_DIRS})
include_directories(${ftxui_SOURCE_DIR}/include)
# Add source files to the project
set(SOURCES
src/Commander.cpp
src/Functions.cpp
src/Parser.cpp
src/Responder.cpp
src/TCPServer.cpp
src/TCPSession.cpp
src/UDPServer.cpp
src/main.cpp
)
set(HEADERS
src/Commander.h
src/Functions.h
src/Parser.h
src/Responder.h
src/TCPServer.h
src/TCPSession.h
src/UDPServer.h
src/ClientList.cpp
src/ClientList.h
)
# Add executable
add_executable(prologix_emulator ${SOURCES} ${HEADERS})
# Link FTXUI libraries
target_link_libraries(prologix_emulator
PRIVATE ftxui::component
PRIVATE ftxui::screen
PRIVATE ftxui::dom
)
# Link Boost libraries
target_link_libraries(prologix_emulator
PRIVATE Boost::asio
PRIVATE Boost::filesystem
PRIVATE Boost::thread
PRIVATE Boost::program_options
PRIVATE Boost::system
)
# Set C++ standard
if(CMAKE_VERSION VERSION_GREATER 3.12)
set_property(TARGET prologix_emulator PROPERTY CXX_STANDARD 20)
endif()
# Ensure Boost is properly found
if(NOT TARGET Boost::asio OR NOT TARGET Boost::filesystem OR NOT TARGET Boost::thread OR NOT TARGET Boost::program_options)
message(FATAL_ERROR "Required Boost libraries not found!")
endif()
# Ensure FTXUI is properly found
if(NOT TARGET ftxui::screen OR NOT TARGET ftxui::dom)
message(FATAL_ERROR "FTXUI libraries not found!")
endif()
cmake_minimum_required(VERSION 3.26)
project(prologix_emulator LANGUAGES CXX VERSION 1.0.0)
# Specify the C++ compiler based on platform
if(NOT CMAKE_CXX_COMPILER)
if(WIN32)
# For Windows, set appropriate compiler and platform
if(CMAKE_SIZEOF_VOID_P EQUAL 8)
# 64-bit Windows (assuming MinGW 64-bit)
set(CMAKE_CXX_COMPILER "C:/msys64/mingw64/bin/g++.exe") # Adjust path as per your MinGW installation
# Set _WIN32_WINNT to target Windows 10
add_definitions(-D_WIN32_WINNT=0x0A00)
else()
# 32-bit Windows (adjust path as needed)
#set(CMAKE_CXX_COMPILER "C:/path/to/32bit/g++.exe")
endif()
elseif(APPLE)
# For macOS
set(CMAKE_CXX_COMPILER "/usr/bin/clang++")
elseif(UNIX)
# For Linux or other UNIX-like systems
set(CMAKE_CXX_COMPILER "/usr/bin/g++")
endif()
endif()
# Boost
set(BOOST_INCLUDE_LIBRARIES thread filesystem system program_options asio date_time)
set(BOOST_ENABLE_CMAKE ON)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/../bin)
include(FetchContent)
FetchContent_Declare(
Boost
GIT_REPOSITORY https://github.com/boostorg/boost.git
GIT_PROGRESS TRUE
GIT_TAG boost-1.85.0 # Update to the required Boost version
)
FetchContent_MakeAvailable(Boost)
# FTXUI
FetchContent_Declare(
ftxui
GIT_REPOSITORY https://github.com/ArthurSonzogni/ftxui
GIT_TAG main # Important: Specify a version or a commit hash here.
)
FetchContent_MakeAvailable(ftxui)
# Include Boost and FTXUI headers
include_directories(${Boost_INCLUDE_DIRS})
include_directories(${ftxui_SOURCE_DIR}/include)
# Add source files to the project
set(SOURCES
src/Commander.cpp
src/Functions.cpp
src/Parser.cpp
src/Responder.cpp
src/TCPServer.cpp
src/TCPSession.cpp
src/UDPServer.cpp
src/main.cpp
)
set(HEADERS
src/Commander.h
src/Functions.h
src/Parser.h
src/Responder.h
src/TCPServer.h
src/TCPSession.h
src/UDPServer.h
src/ClientList.cpp
src/ClientList.h
)
set(CMAKE_RUNTIME_OUTPUT_DIRECTORY ${CMAKE_BINARY_DIR}/binaryy)
# Add executable
add_executable(prologix_emulator ${SOURCES} ${HEADERS})
# Link FTXUI libraries
target_link_libraries(prologix_emulator
PRIVATE ftxui::component
PRIVATE ftxui::screen
PRIVATE ftxui::dom
)
# Link Boost libraries
target_link_libraries(prologix_emulator
PRIVATE Boost::asio
PRIVATE Boost::filesystem
PRIVATE Boost::thread
PRIVATE Boost::program_options
PRIVATE Boost::system
)
# Set C++ standard
set_property(TARGET prologix_emulator PROPERTY CXX_STANDARD 20)
set_property(TARGET prologix_emulator PROPERTY CXX_STANDARD_REQUIRED ON)
# Ensure Boost is properly found
if(NOT TARGET Boost::asio OR NOT TARGET Boost::filesystem OR NOT TARGET Boost::thread OR NOT TARGET Boost::program_options)
message(FATAL_ERROR "Required Boost libraries not found!")
endif()
# Ensure FTXUI is properly found
if(NOT TARGET ftxui::screen OR NOT TARGET ftxui::dom)
message(FATAL_ERROR "FTXUI libraries not found!")
endif()
Component input_list = Container::Vertical({});
std::vector<std::string> items(100, "");
for (size_t i = 0; i < items.size(); ++i) {
input_list->Add(Input(&(items[i]), "placeholder " + std::to_string(i)));
}
auto renderer = Renderer(input_list, [&] {
return input_list->Render() | vscroll_indicator | frame | border |
size(HEIGHT, LESS_THAN, 10);
});