Skip to content

Commit e457d7a

Browse files
committed
增加Code Box
1 parent e2ab145 commit e457d7a

3 files changed

Lines changed: 26 additions & 21 deletions

File tree

sample/Application.cpp

Lines changed: 24 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -95,7 +95,6 @@ void Application::Vits(std::string text) {
9595
}
9696

9797
void Application::render_code_box() {
98-
9998
ImGuiStyle &style = ImGui::GetStyle();
10099
style.Colors[ImGuiCol_WindowBg] = ImVec4(0.95f, 0.95f, 0.95f, 1.0f);
101100
style.Colors[ImGuiCol_TitleBg] = ImVec4(0.85f, 0.85f, 0.85f, 1.0f);
@@ -113,19 +112,19 @@ void Application::render_code_box() {
113112
style.Colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.8f, 0.8f, 0.8f, 1.0f);
114113
style.Colors[ImGuiCol_ScrollbarGrabActive] = ImVec4(0.9f, 0.9f, 0.9f, 1.0f);
115114

116-
ImGui::SetNextWindowSize(ImVec2(400, 300));
117-
ImGui::Begin("Code Box", nullptr,
118-
ImGuiWindowFlags_NoNav | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoSavedSettings);
119-
115+
ImGui::Begin("Code Box", nullptr, ImGuiWindowFlags_MenuBar | ImGuiWindowFlags_NoNav | ImGuiWindowFlags_NoTitleBar);
120116
ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 5.0f);
121-
// 显示 vector 中的值
122-
for (const auto &value: codes) {
123-
// 显示按钮
124-
if (ImGui::Button(value.c_str(), ImVec2(-1, 0))) {
125-
// 将值复制到剪贴板
126-
glfwSetClipboardString(nullptr, value.c_str());
117+
118+
static bool show_codes = false;
119+
for (auto &it: codes) {
120+
show_codes = ImGui::CollapsingHeader(it.first.c_str());
121+
if (show_codes) {
122+
for (const auto &code: it.second) {
123+
if (ImGui::Button(code.c_str(), ImVec2(-1, 0))) {
124+
glfwSetClipboardString(nullptr, code.c_str());
125+
}
126+
}
127127
}
128-
ImGui::SameLine();
129128
}
130129

131130
ImGui::PopStyleVar();
@@ -509,8 +508,19 @@ void Application::render_input_box() {
509508
bot.content = response;
510509
add_chat_record(bot);
511510
it = submit_futures.erase(it);
512-
for (auto &i: Utils::GetAllCodesFromText(response)) {
513-
codes.emplace_back(i);
511+
auto tmp_code = Utils::GetAllCodesFromText(response);
512+
for (auto &i: tmp_code) {
513+
std::size_t pos = i.find('\n'); // 查找第一个换行符的位置
514+
std::string codetype;
515+
if (pos != std::string::npos) { // 如果找到了换行符
516+
codetype = i.substr(0, pos);
517+
i = i.substr(pos + 1); // 删除第一行
518+
}
519+
if (codes.contains(codetype)) {
520+
codes[codetype].emplace_back(i);
521+
} else {
522+
codes.insert({codetype, {i}});
523+
}
514524
}
515525
if (vits && vitsData.enable) {
516526
std::string VitsText = translator->translate(Utils::ExtractNormalText(response), vitsData.lanType);

sample/Application.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
#include "utils.h"
1919

2020
#define TEXT_BUFFER 1024
21-
const std::string VERSION = reinterpret_cast<const char *>(u8"Listner v1.0.dev");
21+
const std::string VERSION = reinterpret_cast<const char *>(u8"Listener v1.1");
2222

2323
enum State {
2424
OK = 0,
@@ -102,7 +102,7 @@ class Application {
102102
const std::vector<std::string> roles = {"user", "system", "assistant"};
103103

104104
std::vector<std::string> mdirs;
105-
std::vector<std::string> codes;
105+
std::map<std::string, std::vector<std::string>> codes;
106106

107107

108108
bool vits = true;

src/utils.cpp

Lines changed: 0 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -804,11 +804,6 @@ std::vector<std::string> Utils::GetAllCodesFromText(const std::string &text) {
804804
searchStart = matchResult.suffix().first;
805805
if (matchResult.size() >= 2) {
806806
std::string codeString = matchResult[1].str();
807-
808-
std::size_t pos = codeString.find('\n'); // 查找第一个换行符的位置
809-
if (pos != std::string::npos) { // 如果找到了换行符
810-
codeString = codeString.substr(pos + 1); // 删除第一行
811-
}
812807
codeStrings.push_back(codeString);
813808
}
814809
}

0 commit comments

Comments
 (0)