MCPcopy Create free account
hub / github.com/LadybugDB/ladybug / truncateText

Function truncateText

tools/shell/linenoise.cpp:2004–2061  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

2002}
2003
2004static void truncateText(char*& buf, size_t& len, size_t pos, size_t cols, size_t plen,
2005 bool highlight, size_t& render_pos, char* highlightBuf) {
2006 if (Utf8Proc::isValid(buf, len)) {
2007 // UTF8 in prompt, handle rendering.
2008 size_t remainingRenderWidth = cols - plen - 1;
2009 size_t startPos = 0;
2010 size_t charPos = 0;
2011 size_t prevPos = 0;
2012 size_t totalRenderWidth = 0;
2013 while (charPos < len) {
2014 uint32_t charRenderWidth = Utf8Proc::renderWidth(buf, charPos);
2015 prevPos = charPos;
2016 charPos = utf8proc_next_grapheme(buf, len, charPos);
2017 totalRenderWidth += charPos - prevPos;
2018 if (totalRenderWidth >= remainingRenderWidth) {
2019 if (prevPos >= pos) {
2020 // We passed the cursor: break, we no longer need to render.
2021 charPos = prevPos;
2022 break;
2023 } else {
2024 // We did not pass the cursor yet, meaning we still need to render.
2025 // Remove characters from the start until it fits again.
2026 while (totalRenderWidth >= remainingRenderWidth) {
2027 uint32_t startCharWidth = Utf8Proc::renderWidth(buf, startPos);
2028 uint32_t newStart = utf8proc_next_grapheme(buf, len, startPos);
2029 totalRenderWidth -= newStart - startPos;
2030 startPos = newStart;
2031 render_pos -= startCharWidth;
2032 }
2033 }
2034 }
2035 if (prevPos < pos) {
2036 render_pos += charRenderWidth;
2037 }
2038 }
2039 std::string highlight_buffer;
2040 if (highlight) {
2041 bool is_shell_command = buf[0] == ':';
2042 auto tokens = highlightingTokenize(buf, len, is_shell_command);
2043 highlight_buffer = linenoiseHighlightText(buf, len, startPos, charPos, tokens);
2044 } else {
2045 highlight_buffer = std::string(buf + startPos, charPos - startPos);
2046 }
2047 std::strcpy(highlightBuf, highlight_buffer.c_str());
2048 len = highlight_buffer.size();
2049 } else {
2050 // Invalid UTF8: fallback.
2051 while ((plen + pos) >= cols) {
2052 buf++;
2053 len--;
2054 pos--;
2055 }
2056 while (plen + len > cols) {
2057 len--;
2058 }
2059 render_pos = pos;
2060 }
2061}

Callers 3

refreshSingleLineFunction · 0.85
refreshSearchMultiLineFunction · 0.85
refreshSearchFunction · 0.85

Calls 3

highlightingTokenizeFunction · 0.85
linenoiseHighlightTextFunction · 0.85
sizeMethod · 0.45

Tested by

no test coverage detected