Skip to content

Commit

Permalink
增加Code Box
Browse files Browse the repository at this point in the history
  • Loading branch information
NGLSG committed Jun 5, 2023
1 parent e2ab145 commit e457d7a
Show file tree
Hide file tree
Showing 3 changed files with 26 additions and 21 deletions.
38 changes: 24 additions & 14 deletions sample/Application.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -95,7 +95,6 @@ void Application::Vits(std::string text) {
}

void Application::render_code_box() {

ImGuiStyle &style = ImGui::GetStyle();
style.Colors[ImGuiCol_WindowBg] = ImVec4(0.95f, 0.95f, 0.95f, 1.0f);
style.Colors[ImGuiCol_TitleBg] = ImVec4(0.85f, 0.85f, 0.85f, 1.0f);
Expand All @@ -113,19 +112,19 @@ void Application::render_code_box() {
style.Colors[ImGuiCol_ScrollbarGrabHovered] = ImVec4(0.8f, 0.8f, 0.8f, 1.0f);
style.Colors[ImGuiCol_ScrollbarGrabActive] = ImVec4(0.9f, 0.9f, 0.9f, 1.0f);

ImGui::SetNextWindowSize(ImVec2(400, 300));
ImGui::Begin("Code Box", nullptr,
ImGuiWindowFlags_NoNav | ImGuiWindowFlags_NoCollapse | ImGuiWindowFlags_NoSavedSettings);

ImGui::Begin("Code Box", nullptr, ImGuiWindowFlags_MenuBar | ImGuiWindowFlags_NoNav | ImGuiWindowFlags_NoTitleBar);
ImGui::PushStyleVar(ImGuiStyleVar_FrameRounding, 5.0f);
// 显示 vector 中的值
for (const auto &value: codes) {
// 显示按钮
if (ImGui::Button(value.c_str(), ImVec2(-1, 0))) {
// 将值复制到剪贴板
glfwSetClipboardString(nullptr, value.c_str());

static bool show_codes = false;
for (auto &it: codes) {
show_codes = ImGui::CollapsingHeader(it.first.c_str());
if (show_codes) {
for (const auto &code: it.second) {
if (ImGui::Button(code.c_str(), ImVec2(-1, 0))) {
glfwSetClipboardString(nullptr, code.c_str());
}
}
}
ImGui::SameLine();
}

ImGui::PopStyleVar();
Expand Down Expand Up @@ -509,8 +508,19 @@ void Application::render_input_box() {
bot.content = response;
add_chat_record(bot);
it = submit_futures.erase(it);
for (auto &i: Utils::GetAllCodesFromText(response)) {
codes.emplace_back(i);
auto tmp_code = Utils::GetAllCodesFromText(response);
for (auto &i: tmp_code) {
std::size_t pos = i.find('\n'); // 查找第一个换行符的位置
std::string codetype;
if (pos != std::string::npos) { // 如果找到了换行符
codetype = i.substr(0, pos);
i = i.substr(pos + 1); // 删除第一行
}
if (codes.contains(codetype)) {
codes[codetype].emplace_back(i);
} else {
codes.insert({codetype, {i}});
}
}
if (vits && vitsData.enable) {
std::string VitsText = translator->translate(Utils::ExtractNormalText(response), vitsData.lanType);
Expand Down
4 changes: 2 additions & 2 deletions sample/Application.h
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@
#include "utils.h"

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

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

std::vector<std::string> mdirs;
std::vector<std::string> codes;
std::map<std::string, std::vector<std::string>> codes;


bool vits = true;
Expand Down
5 changes: 0 additions & 5 deletions src/utils.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -804,11 +804,6 @@ std::vector<std::string> Utils::GetAllCodesFromText(const std::string &text) {
searchStart = matchResult.suffix().first;
if (matchResult.size() >= 2) {
std::string codeString = matchResult[1].str();

std::size_t pos = codeString.find('\n'); // 查找第一个换行符的位置
if (pos != std::string::npos) { // 如果找到了换行符
codeString = codeString.substr(pos + 1); // 删除第一行
}
codeStrings.push_back(codeString);
}
}
Expand Down

0 comments on commit e457d7a

Please sign in to comment.