-
Notifications
You must be signed in to change notification settings - Fork 4
Expand file tree
/
Copy pathslash_dropdown.cpp
More file actions
276 lines (248 loc) · 10.5 KB
/
Copy pathslash_dropdown.cpp
File metadata and controls
276 lines (248 loc) · 10.5 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
#include "slash_dropdown.hpp"
#include "../commands/command_registry.hpp"
#include "../commands/slash_command_ranking.hpp"
#include "picker_scroll.hpp"
#include "tui/text_style.hpp"
#include "tui/theme_palette.hpp"
#include <ftxui/dom/elements.hpp>
#include <ftxui/screen/string.hpp>
#include <ftxui/screen/terminal.hpp>
#include <algorithm>
#include <string>
#include <vector>
namespace acecode {
namespace {
constexpr int kNarrowTerminalColumns = 40;
constexpr const char* kHorizontalLine = "\xE2\x94\x80";
bool contains_whitespace(const std::string& s) {
for (char c : s) {
if (c == ' ' || c == '\t' || c == '\n' || c == '\r') return true;
}
return false;
}
std::string repeat_utf8(const char* glyph, int count) {
std::string out;
if (count <= 0) return out;
const std::string g(glyph);
out.reserve(g.size() * static_cast<size_t>(count));
for (int i = 0; i < count; ++i) out += g;
return out;
}
std::string truncate_cells(const std::string& value, int max_cells) {
if (max_cells <= 0) return {};
if (ftxui::string_width(value) <= max_cells) return value;
if (max_cells <= 3) return value.substr(0, static_cast<size_t>(max_cells));
std::string out;
int width = 0;
for (const auto& glyph : ftxui::Utf8ToGlyphs(value)) {
if (glyph.empty()) continue;
const int glyph_width = std::max(0, ftxui::string_width(glyph));
if (width + glyph_width > max_cells - 3) break;
out += glyph;
width += glyph_width;
}
return out + "...";
}
} // namespace
void refresh_slash_dropdown(TuiState& state, const CommandRegistry& reg) {
// Determine whether we're in slash-command position.
const bool in_command_position =
!state.input_text.empty() &&
state.input_text[0] == '/' &&
!contains_whitespace(state.input_text);
// Leaving command position clears the Esc-dismissal flag so the next '/'
// can re-open the dropdown.
if (!in_command_position) {
state.slash_dropdown_dismissed_for_input = false;
state.slash_dropdown_active = false;
state.slash_dropdown_items.clear();
state.slash_dropdown_selected = 0;
state.slash_dropdown_view_offset = 0;
state.slash_dropdown_total_matches = 0;
return;
}
// Suppress while another overlay owns the UI.
if (state.resume_picker_active || state.rewind_picker_active ||
state.model_picker_open || state.mode_picker_open ||
state.confirm_pending || state.ask_pending) {
state.slash_dropdown_active = false;
state.slash_dropdown_items.clear();
state.slash_dropdown_selected = 0;
state.slash_dropdown_view_offset = 0;
state.slash_dropdown_total_matches = 0;
return;
}
if (state.slash_dropdown_dismissed_for_input) {
state.slash_dropdown_active = false;
state.slash_dropdown_items.clear();
state.slash_dropdown_selected = 0;
state.slash_dropdown_view_offset = 0;
state.slash_dropdown_total_matches = 0;
return;
}
// Build and rank the matching candidate list. Match relevance remains the
// primary key; persisted usage replaces alphabetical order for equal tiers.
const std::string query = state.input_text.substr(1); // drop leading '/'
std::vector<SlashCommandCandidate> candidates;
candidates.reserve(reg.commands().size());
for (const auto& entry : reg.commands()) {
const auto& cmd = entry.second;
candidates.push_back({cmd.name, cmd.description});
}
auto ranked = rank_slash_command_candidates(
query, candidates, state.slash_command_usage_counts);
state.slash_dropdown_total_matches = static_cast<int>(ranked.size());
if (ranked.empty()) {
state.slash_dropdown_active = false;
state.slash_dropdown_items.clear();
state.slash_dropdown_selected = 0;
state.slash_dropdown_view_offset = 0;
return;
}
// Remember previously-selected command name so we can preserve selection
// across filter updates.
std::string prev_selected_name;
if (state.slash_dropdown_active &&
state.slash_dropdown_selected >= 0 &&
state.slash_dropdown_selected <
static_cast<int>(state.slash_dropdown_items.size())) {
prev_selected_name =
state.slash_dropdown_items[state.slash_dropdown_selected].name;
}
// Keep the full ranked list — viewport scrolling in the renderer handles
// overflow, so the user can reach commands beyond the first kSlashDropdownVisibleRows
// via Arrow / PgUp / PgDn / Home / End instead of needing to refine the filter.
state.slash_dropdown_items.clear();
state.slash_dropdown_items.reserve(ranked.size());
for (auto& candidate : ranked) {
state.slash_dropdown_items.push_back(
{std::move(candidate.name), std::move(candidate.description)});
}
int new_selected = 0;
const auto exact_match = std::find_if(
state.slash_dropdown_items.begin(),
state.slash_dropdown_items.end(),
[&query](const auto& item) { return item.name == query; });
if (exact_match != state.slash_dropdown_items.end()) {
// A fully typed built-in must win over a previously-highlighted fuzzy
// match. Otherwise `/skills` can remain pinned to `skill-creator`
// merely because that skill's description also contains "skills".
new_selected = static_cast<int>(
std::distance(
state.slash_dropdown_items.begin(), exact_match));
} else if (!prev_selected_name.empty()) {
for (int i = 0; i < static_cast<int>(state.slash_dropdown_items.size()); ++i) {
if (state.slash_dropdown_items[i].name == prev_selected_name) {
new_selected = i;
break;
}
}
}
state.slash_dropdown_selected = new_selected;
state.slash_dropdown_view_offset = acecode::tui::scroll_to_keep_visible(
state.slash_dropdown_selected, state.slash_dropdown_view_offset,
acecode::tui::kSlashDropdownVisibleRows,
static_cast<int>(state.slash_dropdown_items.size()));
state.slash_dropdown_active = true;
}
ftxui::Element render_slash_dropdown(const TuiState& state,
bool conhost_compat_layout) {
using namespace ftxui;
if (!state.slash_dropdown_active || state.slash_dropdown_items.empty()) {
return emptyElement();
}
const int term_cols = Terminal::Size().dimx;
const bool narrow = term_cols > 0 && term_cols < kNarrowTerminalColumns;
const int total = static_cast<int>(state.slash_dropdown_items.size());
const int visible = std::min(acecode::tui::kSlashDropdownVisibleRows, total);
const int offset = std::clamp(state.slash_dropdown_view_offset, 0,
std::max(0, total - visible));
const int items_above = offset;
const int items_below = std::max(0, total - offset - visible);
Elements rows;
std::vector<std::string> compat_rows;
const int compat_max_cols = std::max(1, term_cols > 4 ? term_cols - 4 : term_cols);
if (items_above > 0) {
if (conhost_compat_layout) {
compat_rows.push_back(" ^ " + std::to_string(items_above) + " more above");
} else {
rows.push_back(
text(" \xE2\x86\x91 " + std::to_string(items_above) + " more above")
| tui::readable_secondary());
}
}
for (int i = offset; i < offset + visible; ++i) {
const auto& item = state.slash_dropdown_items[i];
const bool selected = (i == state.slash_dropdown_selected);
Element row;
if (conhost_compat_layout) {
std::string line = " /" + item.name;
if (!narrow && !item.description.empty()) {
line += " - " + item.description;
}
line = truncate_cells(line, compat_max_cols);
compat_rows.push_back(line);
row = text(line);
} else if (narrow) {
row = text(" /" + item.name + " ");
} else {
std::string desc = item.description;
if (desc.size() > 60) {
size_t cut = 59;
// Walk back to UTF-8 character boundary.
while (cut > 0 && (static_cast<unsigned char>(desc[cut]) & 0xC0) == 0x80)
--cut;
desc = desc.substr(0, cut) + "\xE2\x80\xA6"; // ellipsis
}
row = hbox({
text(" /" + item.name + " "),
text("\xE2\x80\x94 ") | color(tui::theme().ui.text_dim),
text(desc) | color(tui::theme().ui.text_muted),
});
}
if (selected) {
row = row | bold | color(tui::theme().ui.selection_fg) | bgcolor(tui::theme().ui.selection_bg);
} else {
row = row | color(tui::theme().ui.text_muted);
}
rows.push_back(row);
}
if (items_below > 0) {
if (conhost_compat_layout) {
compat_rows.push_back(" v " + std::to_string(items_below) + " more below");
} else {
rows.push_back(
text(" \xE2\x86\x93 " + std::to_string(items_below) + " more below")
| tui::readable_secondary());
}
}
if (conhost_compat_layout) {
int frame_width = 1;
for (const auto& line : compat_rows) {
frame_width = std::max(frame_width, ftxui::string_width(line));
}
frame_width = std::min(frame_width, compat_max_cols);
Elements compat_elements;
compat_elements.push_back(text(repeat_utf8(kHorizontalLine, frame_width)));
for (int i = 0; i < static_cast<int>(compat_rows.size()); ++i) {
const int item_index = offset + i - (items_above > 0 ? 1 : 0);
const bool selected =
item_index >= offset &&
item_index < offset + visible &&
item_index == state.slash_dropdown_selected;
Element row = text(truncate_cells(compat_rows[i], frame_width));
if (selected) {
row = row | bold | color(Color::White) |
bgcolor(Color::RGB(0, 80, 120));
} else {
row = row | color(tui::theme().ui.text_muted);
}
compat_elements.push_back(row);
}
compat_elements.push_back(text(repeat_utf8(kHorizontalLine, frame_width)));
return vbox(std::move(compat_elements)) | color(tui::theme().ui.border);
}
Element body = vbox(std::move(rows));
return body | border | color(tui::theme().ui.border);
}
} // namespace acecode