(str, desiredLength)
| 153 | } |
| 154 | |
| 155 | function 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 | |
| 186 | function truncate(str, desiredLength, truncateChar) { |
| 187 | truncateChar = truncateChar || '…'; |
no test coverage detected
searching dependent graphs…