MCPcopy Create free account
hub / github.com/cli-table/cli-table3 / truncateWidthWithAnsi

Function truncateWidthWithAnsi

src/utils.js:155–184  ·  view source on GitHub ↗
(str, desiredLength)

Source from the content-addressed store, hash-verified

153}
154
155function truncateWidthWithAnsi(str, desiredLength) {
156 let code = codeRegex(true);
157 let split = str.split(codeRegex());
158 let splitIndex = 0;
159 let retLen = 0;
160 let ret = '';
161 let myArray;
162 let state = {};
163
164 while (retLen < desiredLength) {
165 myArray = code.exec(str);
166 let toAdd = split[splitIndex];
167 splitIndex++;
168 if (retLen + strlen(toAdd) > desiredLength) {
169 toAdd = truncateWidth(toAdd, desiredLength - retLen);
170 }
171 ret += toAdd;
172 retLen += strlen(toAdd);
173
174 if (retLen < desiredLength) {
175 if (!myArray) {
176 break;
177 } // full-width chars may cause a whitespace which cannot be filled
178 ret += myArray[0];
179 updateState(state, myArray);
180 }
181 }
182
183 return unwindState(state, ret);
184}
185
186function truncate(str, desiredLength, truncateChar) {
187 truncateChar = truncateChar || '…';

Callers 1

truncateFunction · 0.85

Calls 5

codeRegexFunction · 0.85
strlenFunction · 0.85
truncateWidthFunction · 0.85
updateStateFunction · 0.85
unwindStateFunction · 0.85

Tested by

no test coverage detected

Used in the wild real call sites across dependent graphs

searching dependent graphs…