INT WINAPI TABSAT(LPWSTR psz)
{
INT argc;
WCHAR szUty[16], **argv = GetArgs(psz, &argc); // CommandLineToArgvW with dummy argv[0]
DWORD dwWritten;
WORD wWidth = 0;
BOOL bUseWidth;
if ( argc < 3 || !(bUseWidth = !lstrcmpi(argv[1], L"/w")) && lstrcmpi(argv[1], L"/c") || !(wWidth = (WORD) _wtoi(argv[2])) )
{
Printf(L"%s\r\n", item[itabsat].Help);
goto byebye;
}
HANDLE hConOut = OpenConOut(); // L"CONOUT$"
// cursor to BOL, save cursor position, clear tabs (except at BOL and EOL)
WriteConsole(hConOut, L"\x1b[G\x1b[s\x1b[3g", 10, &dwWritten, NULL);
if ( bUseWidth ) /* use tab width */
{
CONSOLE_SCREEN_BUFFER_INFO csbi;
GetConsoleScreenBufferInfo(hConOut, &csbi);
wsprintf(szUty, L"\x1b[%uC\x1bH", wWidth); // cursor forward w columns and set tab
for (WORD i = 1; i <= csbi.dwSize.X / wWidth; i += 1)
WriteConsole(hConOut, szUty, lstrlen(szUty), &dwWritten, NULL);
}
else /* use specified columns */
{
for (INT i = 2; i < argc; i++)
{
wsprintf(szUty, L"\x1b[%uG\x1bH", (WORD)_wtoi(argv[i])); // cursor to named column and set tab
WriteConsole(hConOut, szUty, lstrlen(szUty), &dwWritten, NULL);
}
}
/* restore cursor position */
WriteConsole(hConOut, L"\x1b[u", 3, &dwWritten, NULL);
CloseHandle(hConOut);
byebye :
LocalFree(argv);
return 0;
}