MCPcopy Create free account
hub / github.com/Tiiny-AI/PowerInfer / put_codepoint

Function put_codepoint

common/console.cpp:245–304  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

243 }
244
245 static int put_codepoint(const char* utf8_codepoint, size_t length, int expectedWidth) {
246#if defined(_WIN32)
247 CONSOLE_SCREEN_BUFFER_INFO bufferInfo;
248 if (!GetConsoleScreenBufferInfo(hConsole, &bufferInfo)) {
249 // go with the default
250 return expectedWidth;
251 }
252 COORD initialPosition = bufferInfo.dwCursorPosition;
253 DWORD nNumberOfChars = length;
254 WriteConsole(hConsole, utf8_codepoint, nNumberOfChars, &nNumberOfChars, NULL);
255
256 CONSOLE_SCREEN_BUFFER_INFO newBufferInfo;
257 GetConsoleScreenBufferInfo(hConsole, &newBufferInfo);
258
259 // Figure out our real position if we're in the last column
260 if (utf8_codepoint[0] != 0x09 && initialPosition.X == newBufferInfo.dwSize.X - 1) {
261 DWORD nNumberOfChars;
262 WriteConsole(hConsole, &" \b", 2, &nNumberOfChars, NULL);
263 GetConsoleScreenBufferInfo(hConsole, &newBufferInfo);
264 }
265
266 int width = newBufferInfo.dwCursorPosition.X - initialPosition.X;
267 if (width < 0) {
268 width += newBufferInfo.dwSize.X;
269 }
270 return width;
271#else
272 // We can trust expectedWidth if we've got one
273 if (expectedWidth >= 0 || tty == nullptr) {
274 fwrite(utf8_codepoint, length, 1, out);
275 return expectedWidth;
276 }
277
278 fputs("\033[6n", tty); // Query cursor position
279 int x1;
280 int y1;
281 int x2;
282 int y2;
283 int results = 0;
284 results = fscanf(tty, "\033[%d;%dR", &y1, &x1);
285
286 fwrite(utf8_codepoint, length, 1, tty);
287
288 fputs("\033[6n", tty); // Query cursor position
289 results += fscanf(tty, "\033[%d;%dR", &y2, &x2);
290
291 if (results != 4) {
292 return expectedWidth;
293 }
294
295 int width = x2 - x1;
296 if (width < 0) {
297 // Calculate the width considering text wrapping
298 struct winsize w;
299 ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
300 width += w.ws_col;
301 }
302 return width;

Callers 2

replace_lastFunction · 0.70
readline_advancedFunction · 0.70

Calls

no outgoing calls

Tested by

no test coverage detected