-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathTerminalWorkspace.cppm
More file actions
264 lines (216 loc) · 9.58 KB
/
TerminalWorkspace.cppm
File metadata and controls
264 lines (216 loc) · 9.58 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
// Copyright (C) 2026 mxreal64
// Licensed under the GPL-3.0 License
module;
#include <ftxui/dom/elements.hpp>
#include <ftxui/component/component.hpp>
#include <ftxui/component/component_base.hpp>
#include <ftxui/component/screen_interactive.hpp>
#include <ftxui/component/event.hpp>
#include <ftxui/screen/color.hpp>
export module TerminalWorkspace;
import std;
import MuxParser;
export namespace MuxUI {
class Workspace {
private:
MuxEngine engine_;
std::string parse_time_str_ = "Parsed in: 0.000 μs";
std::string text_buffer_;
std::string file_path_;
int active_tab_ = 1; // 0 = Editor, 1 = Preview
bool save_mode_active_ = false;
std::string save_path_target_ = "untitled.md";
int scroll_offset_ = 0;
int max_visible_lines_ = 20;
ftxui::Component editor_input_field_;
ftxui::Component save_input_field_;
ftxui::Component global_layout_container_;
void load_file_to_buffer() noexcept {
if (file_path_.empty()) return;
std::ifstream file(file_path_, std::ios::in | std::ios::binary);
if (!file.is_open()) return;
text_buffer_ = std::string((std::istreambuf_iterator<char>(file)),
std::istreambuf_iterator<char>());
}
public:
explicit Workspace(std::string_view initial_file) : file_path_(initial_file) {
load_file_to_buffer();
editor_input_field_ = ftxui::Input(&text_buffer_, "Write your Markdown here...");
save_input_field_ = ftxui::Input(&save_path_target_, "Enter path...");
global_layout_container_ = ftxui::Container::Vertical({
editor_input_field_,
save_input_field_
});
execute_parsing_pipeline();
}
void execute_parsing_pipeline() noexcept {
auto start_time = std::chrono::high_resolution_clock::now();
engine_.parse_buffer(text_buffer_);
auto end_time = std::chrono::high_resolution_clock::now();
auto duration = std::chrono::duration_cast<std::chrono::nanoseconds>(end_time - start_time).count();
parse_time_str_ = std::format("Parsed in: {:.3f} μs", duration / 1000.0);
}
ftxui::Element RenderPane() {
ftxui::Elements preview_rows;
const auto& tokens = engine_.get_tokens();
for (std::size_t i = static_cast<std::size_t>(scroll_offset_); i < tokens.size(); ++i) {
const auto& token = tokens[i];
if (token.type == MuxBlockType::HorizontalRule) {
preview_rows.push_back(ftxui::separator());
continue;
}
if (token.type == MuxBlockType::EmptyLine) {
preview_rows.push_back(ftxui::text(""));
continue;
}
auto inline_spans = MuxEngine::tokenize_inline(token.text_slice);
ftxui::Elements row_spans;
for (const auto& span : inline_spans) {
ftxui::Element span_el = ftxui::text(std::string(span.text));
switch (span.style) {
case MuxTextStyle::Bold: span_el = ftxui::bold(span_el); break;
case MuxTextStyle::Italic: span_el = ftxui::italic(span_el); break;
case MuxTextStyle::InlineCode: span_el = span_el | ftxui::color(ftxui::Color::Green) | ftxui::bgcolor(ftxui::Color::RGB(18, 18, 18)); break;
default: break;
}
row_spans.push_back(span_el);
}
ftxui::Element assembled_row = ftxui::hbox(std::move(row_spans));
switch (token.type) {
case MuxBlockType::Heading1:
preview_rows.push_back(ftxui::hbox({
ftxui::text("=== ") | ftxui::bold | ftxui::color(ftxui::Color::Yellow),
ftxui::bold(assembled_row) | ftxui::color(ftxui::Color::Yellow),
ftxui::text(" ===") | ftxui::bold | ftxui::color(ftxui::Color::Yellow)
}));
break;
case MuxBlockType::Heading2:
preview_rows.push_back(ftxui::hbox({
ftxui::text("▶ ") | ftxui::bold | ftxui::color(ftxui::Color::Cyan),
ftxui::bold(assembled_row) | ftxui::color(ftxui::Color::Cyan)
}));
break;
case MuxBlockType::Heading3:
preview_rows.push_back(ftxui::hbox({
ftxui::text("● ") | ftxui::bold | ftxui::color(ftxui::Color::BlueLight),
ftxui::bold(assembled_row) | ftxui::color(ftxui::Color::BlueLight)
}));
break;
case MuxBlockType::BulletItem:
preview_rows.push_back(ftxui::hbox({ftxui::text(" • ") | ftxui::color(ftxui::Color::Yellow), assembled_row}));
break;
case MuxBlockType::CodeBlockLine:
preview_rows.push_back(ftxui::hbox({ftxui::text(" "), assembled_row | ftxui::color(ftxui::Color::Green)}));
break;
default:
preview_rows.push_back(assembled_row);
break;
}
}
auto scroll_pane = ftxui::vbox(std::move(preview_rows))
| ftxui::vscroll_indicator
| ftxui::frame
| ftxui::flex;
ftxui::Element center_view;
if (active_tab_ == 0) {
center_view = ftxui::window(ftxui::text(" EDITOR ") | ftxui::bold, editor_input_field_->Render() | ftxui::flex);
} else {
center_view = ftxui::window(ftxui::text(" MUX LIVE PREVIEW ") | ftxui::bold, scroll_pane) | ftxui::bgcolor(ftxui::Color::Black);
}
ftxui::Element bottom_bar;
if (save_mode_active_) {
bottom_bar = ftxui::hbox({
ftxui::text(" Save Path: ") | ftxui::bold | ftxui::color(ftxui::Color::Yellow),
save_input_field_->Render() | ftxui::flex
});
} else {
bottom_bar = ftxui::hbox({
ftxui::text(" [Ctrl+T] Toggle View | [Ctrl+S] Save | [Ctrl+Q] Quit ") | ftxui::color(ftxui::Color::BlueLight),
ftxui::filler(),
ftxui::text(parse_time_str_) | ftxui::color(ftxui::Color::GrayLight)
});
}
return ftxui::vbox(ftxui::Elements{
center_view | ftxui::flex,
ftxui::separator(),
bottom_bar
});
}
bool HandleEvent(ftxui::Event event) {
if (event == ftxui::Event::CtrlT) {
active_tab_ = (active_tab_ == 0) ? 1 : 0;
scroll_offset_ = 0;
return true;
}
if (event == ftxui::Event::CtrlS) {
save_mode_active_ = !save_mode_active_;
if (save_mode_active_) {
save_input_field_->TakeFocus(); // <-- FORCE KEYBOARD FOCUS INTO THE FIELD
}
return true;
}
// --- 1. EXPLICIT INTERCEPT FOR SAVE FIELD IF ACTIVE ---
if (save_mode_active_) {
if (event == ftxui::Event::Escape || event == ftxui::Event::Return) {
save_mode_active_ = false; // Close bar on Confirm or Cancel
return true;
}
// Route all characters, backspaces, and deletes straight to the string buffer
return save_input_field_->OnEvent(event);
}
// --- 2. PREVIEW WINDOW SCROLL NAVIGATION ---
if (active_tab_ == 1) {
const int total_lines = static_cast<int>(engine_.get_tokens().size());
if (event == ftxui::Event::ArrowDown) {
if (scroll_offset_ < total_lines - 5) {
scroll_offset_++;
return true;
}
}
if (event == ftxui::Event::ArrowUp) {
if (scroll_offset_ > 0) {
scroll_offset_--;
return true;
}
}
if (event.is_mouse()) {
if (event.mouse().button == ftxui::Mouse::WheelDown) {
if (scroll_offset_ < total_lines - 5) {
scroll_offset_ += 2;
if (scroll_offset_ > total_lines - 5) scroll_offset_ = total_lines - 5;
return true;
}
}
if (event.mouse().button == ftxui::Mouse::WheelUp) {
if (scroll_offset_ > 0) {
scroll_offset_ -= 2;
if (scroll_offset_ < 0) scroll_offset_ = 0;
return true;
}
}
}
}
if (active_tab_ == 0) {
bool handled = editor_input_field_->OnEvent(event);
if (handled) execute_parsing_pipeline();
return handled;
}
return global_layout_container_->OnEvent(event);
}
void run() noexcept {
auto screen = ftxui::ScreenInteractive::Fullscreen();
auto main_renderer = ftxui::Renderer(global_layout_container_, [&] {
max_visible_lines_ = screen.dimy() - 4;
return RenderPane();
});
auto root_loop = ftxui::CatchEvent(main_renderer, [&](ftxui::Event event) {
if (event == ftxui::Event::CtrlQ) {
screen.ExitLoopClosure()();
return true;
}
return HandleEvent(event);
});
screen.Loop(root_loop);
}
};
} // namespace MuxUI