fix: G-code preview drops comment text and cuts non-ASCII lines short (#15448)

This commit is contained in:
Kris Austin
2026-09-06 15:42:57 -03:00
committed by GitHub
parent 85dc866425
commit 0365304ae0
+27 -10
View File
@@ -785,6 +785,19 @@ void GCodeViewer::SequentialView::GCodeWindow::load_gcode(const std::string& fil
} }
} }
// Byte offset just past the first count characters of str, or its length if it is shorter.
static size_t utf8_offset(const std::string& str, size_t count)
{
const char* const begin = str.c_str();
const char* const end = begin + str.size();
const char* pos = begin;
for (size_t i = 0; i < count && pos < end; ++i) {
unsigned int codepoint = 0;
pos += ImTextCharFromUtf8(&codepoint, pos, end);
}
return pos - begin;
}
//BBS: GUI refactor: move to right //BBS: GUI refactor: move to right
void GCodeViewer::SequentialView::GCodeWindow::render(float top, float bottom, float right, uint64_t curr_line_id) const void GCodeViewer::SequentialView::GCodeWindow::render(float top, float bottom, float right, uint64_t curr_line_id) const
{ {
@@ -796,23 +809,27 @@ void GCodeViewer::SequentialView::GCodeWindow::render(float top, float bottom, f
// read line from file // read line from file
const size_t start = id == 1 ? 0 : m_lines_ends[id - 2]; const size_t start = id == 1 ? 0 : m_lines_ends[id - 2];
const size_t original_len = m_lines_ends[id - 1] - start; const size_t original_len = m_lines_ends[id - 1] - start;
const size_t len = std::min(original_len, (size_t) 55); // A character is four bytes at most, so 55 of them always fit in 220.
const size_t len = std::min(original_len, (size_t) 55 * 4);
std::string gline(m_file.data() + start, len); std::string gline(m_file.data() + start, len);
// If original line is longer than 55 characters, truncate and append "..." // If original line is longer than 55 characters, truncate and append "...".
if (original_len > 55) // The cut must land on a character boundary or it leaves half a character behind.
gline = gline.substr(0, 52) + "..."; if (len < original_len || utf8_offset(gline, 55) < gline.size())
gline = gline.substr(0, utf8_offset(gline, 52)) + "...";
std::string command, parameters, comment; std::string command, parameters, comment;
// extract comment const size_t comment_start = gline.find(';');
std::vector<std::string> tokens; if (comment_start == std::string::npos)
boost::split(tokens, gline, boost::is_any_of(";"), boost::token_compress_on); command = gline;
command = tokens.front(); else {
if (tokens.size() > 1) command = gline.substr(0, comment_start);
comment = ";" + tokens.back(); comment = gline.substr(comment_start);
}
// extract gcode command and parameters // extract gcode command and parameters
if (!command.empty()) { if (!command.empty()) {
std::vector<std::string> tokens;
boost::split(tokens, command, boost::is_any_of(" "), boost::token_compress_on); boost::split(tokens, command, boost::is_any_of(" "), boost::token_compress_on);
command = tokens.front(); command = tokens.front();
if (tokens.size() > 1) { if (tokens.size() > 1) {