-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathmain.c
More file actions
69 lines (59 loc) · 1.93 KB
/
main.c
File metadata and controls
69 lines (59 loc) · 1.93 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
// Copyright (c) 2024-2026 Steffen Illhardt,
// Licensed under the MIT license ( https://opensource.org/license/mit/ ).
#include <stddef.h>
#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <wchar.h>
#include "wtswidth.h"
static inline void help(void)
{
static const char msg[] =
"* 'wtswidth' - Windows Terminal string width, version 1.3. *\n"
"Count the number of columns required to represent strings in the Windows Terminal.\n"
"Write the widths as list of numbers of character cells the passed strings occupy.\n"
"In case the wrong syntax is used, this message is displayed and 1 is returned.\n\n"
"Usage:\n"
"wtswidth a s1 [s2 [... sN]]\n"
" a Number of columns that a character with ambiguous width occupies.\n"
" This can be either 1 for \"Narrow\" or 2 for \"Wide\". The default\n"
" setting in Windows Terminal is \"Narrow\" (1).\n"
" s1..sN Strings to be measured.\n\n";
fwrite(msg, 1, sizeof(msg) - 1, stderr);
}
#if defined(_WIN32) || defined(__CYGWIN__)
// On Windows, the command line is always available as wide string anyway. So we just use that.
# include <windows.h>
int main(void)
{
int argc = 0;
wchar_t **const argv = CommandLineToArgvW(GetCommandLineW(), &argc);
if (argc < 3)
{
LocalFree(argv);
help();
return 1;
}
if (wcstol(argv[1], NULL, 10) == 2)
wtssetambwidth(wide);
for (int i = 2; i < argc; ++i)
printf_s("%d\n", wts16width((const char16_t *)argv[i], (int)wcslen(argv[i])));
LocalFree(argv);
return 0;
}
#else
// On *nix we assume to get UTF-8 as this is virtually standard.
int main(int argc, char *argv[])
{
if (argc < 3)
{
help();
return 1;
}
if (strtol(argv[1], NULL, 10) == 2)
wtssetambwidth(wide);
for (int i = 2; i < argc; ++i)
printf("%d\n", wts8width(argv[i], (int)strlen(argv[i])));
return 0;
}
#endif