diff options
| author | Thijs Schreijer <thijs@thijsschreijer.nl> | 2024-06-19 22:05:33 +0200 |
|---|---|---|
| committer | Thijs Schreijer <thijs@thijsschreijer.nl> | 2024-06-19 22:05:33 +0200 |
| commit | e0871d7be63dd428d4a2b9a3db4e033894165cef (patch) | |
| tree | 9a844f267ec9ec99ba745680453b683dd73a6f5b | |
| parent | 8996a5022fa82e5d5335f71580d0cd6b6d323c9b (diff) | |
| download | luasystem-e0871d7be63dd428d4a2b9a3db4e033894165cef.tar.gz luasystem-e0871d7be63dd428d4a2b9a3db4e033894165cef.tar.bz2 luasystem-e0871d7be63dd428d4a2b9a3db4e033894165cef.zip | |
add system.CODEPAGE_UTF8 for 65001 codepage
| -rw-r--r-- | doc_topics/03-terminal.md | 4 | ||||
| -rw-r--r-- | examples/compat.lua | 2 | ||||
| -rw-r--r-- | examples/readline.lua | 2 | ||||
| -rw-r--r-- | spec/04-term_spec.lua | 14 | ||||
| -rw-r--r-- | src/term.c | 4 | ||||
| -rw-r--r-- | system/init.lua | 5 |
6 files changed, 18 insertions, 13 deletions
diff --git a/doc_topics/03-terminal.md b/doc_topics/03-terminal.md index 06a6b96..9bad359 100644 --- a/doc_topics/03-terminal.md +++ b/doc_topics/03-terminal.md | |||
| @@ -51,8 +51,8 @@ recent versions of Lua also have UTF-8 support. So `luasystem` also focusses on | |||
| 51 | 51 | ||
| 52 | On Windows UTF-8 output can be enabled by setting the output codepage like this: | 52 | On Windows UTF-8 output can be enabled by setting the output codepage like this: |
| 53 | 53 | ||
| 54 | -- setup Windows output codepage to UTF-8; 65001 | 54 | -- setup Windows output codepage to UTF-8 |
| 55 | sys.setconsoleoutputcp(65001) | 55 | sys.setconsoleoutputcp(sys.CODEPAGE_UTF8) |
| 56 | 56 | ||
| 57 | Terminal input is handled by the [`_getwchar()`](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/getchar-getwchar) function on Windows which returns | 57 | Terminal input is handled by the [`_getwchar()`](https://learn.microsoft.com/en-us/cpp/c-runtime-library/reference/getchar-getwchar) function on Windows which returns |
| 58 | UTF-16 surrogate pairs. `luasystem` will automatically convert those to UTF-8. | 58 | UTF-16 surrogate pairs. `luasystem` will automatically convert those to UTF-8. |
diff --git a/examples/compat.lua b/examples/compat.lua index a59d964..c712105 100644 --- a/examples/compat.lua +++ b/examples/compat.lua | |||
| @@ -12,7 +12,7 @@ if sys.windows then | |||
| 12 | os.getenv = sys.getenv -- luacheck: ignore | 12 | os.getenv = sys.getenv -- luacheck: ignore |
| 13 | 13 | ||
| 14 | -- Set console output to UTF-8 encoding. | 14 | -- Set console output to UTF-8 encoding. |
| 15 | sys.setconsoleoutputcp(65001) | 15 | sys.setconsoleoutputcp(sys.CODEPAGE_UTF8) |
| 16 | 16 | ||
| 17 | -- Set up the terminal to handle ANSI escape sequences on Windows. | 17 | -- Set up the terminal to handle ANSI escape sequences on Windows. |
| 18 | if sys.isatty(io.stdout) then | 18 | if sys.isatty(io.stdout) then |
diff --git a/examples/readline.lua b/examples/readline.lua index 286522c..ff215dd 100644 --- a/examples/readline.lua +++ b/examples/readline.lua | |||
| @@ -442,7 +442,7 @@ local backup = sys.termbackup() | |||
| 442 | sys.setconsoleflags(io.stdout, sys.getconsoleflags(io.stdout) + sys.COF_VIRTUAL_TERMINAL_PROCESSING) | 442 | sys.setconsoleflags(io.stdout, sys.getconsoleflags(io.stdout) + sys.COF_VIRTUAL_TERMINAL_PROCESSING) |
| 443 | sys.setconsoleflags(io.stdin, sys.getconsoleflags(io.stdin) + sys.CIF_VIRTUAL_TERMINAL_INPUT) | 443 | sys.setconsoleflags(io.stdin, sys.getconsoleflags(io.stdin) + sys.CIF_VIRTUAL_TERMINAL_INPUT) |
| 444 | -- set output to UTF-8 | 444 | -- set output to UTF-8 |
| 445 | sys.setconsoleoutputcp(65001) | 445 | sys.setconsoleoutputcp(sys.CODEPAGE_UTF8) |
| 446 | 446 | ||
| 447 | -- setup Posix terminal to disable canonical mode and echo | 447 | -- setup Posix terminal to disable canonical mode and echo |
| 448 | sys.tcsetattr(io.stdin, sys.TCSANOW, { | 448 | sys.tcsetattr(io.stdin, sys.TCSANOW, { |
diff --git a/spec/04-term_spec.lua b/spec/04-term_spec.lua index d5b4eee..e888920 100644 --- a/spec/04-term_spec.lua +++ b/spec/04-term_spec.lua | |||
| @@ -8,7 +8,7 @@ describe("Terminal:", function() | |||
| 8 | 8 | ||
| 9 | setup(function() | 9 | setup(function() |
| 10 | wincodepage = system.getconsoleoutputcp() | 10 | wincodepage = system.getconsoleoutputcp() |
| 11 | assert(system.setconsoleoutputcp(65001)) -- set to UTF8 | 11 | assert(system.setconsoleoutputcp(system.CODEPAGE_UTF8)) -- set to UTF8 |
| 12 | end) | 12 | end) |
| 13 | 13 | ||
| 14 | teardown(function() | 14 | teardown(function() |
| @@ -346,8 +346,8 @@ describe("Terminal:", function() | |||
| 346 | end) | 346 | end) |
| 347 | 347 | ||
| 348 | local new_cp | 348 | local new_cp |
| 349 | if old_cp ~= 65001 then | 349 | if old_cp ~= system.CODEPAGE_UTF8 then |
| 350 | new_cp = 65001 -- set to UTF8 | 350 | new_cp = system.CODEPAGE_UTF8 -- set to UTF8 |
| 351 | else | 351 | else |
| 352 | new_cp = 850 -- another common one | 352 | new_cp = 850 -- another common one |
| 353 | end | 353 | end |
| @@ -403,8 +403,8 @@ describe("Terminal:", function() | |||
| 403 | end) | 403 | end) |
| 404 | 404 | ||
| 405 | local new_cp | 405 | local new_cp |
| 406 | if old_cp ~= 65001 then | 406 | if old_cp ~= system.CODEPAGE_UTF8 then |
| 407 | new_cp = 65001 -- set to UTF8 | 407 | new_cp = system.CODEPAGE_UTF8 -- set to UTF8 |
| 408 | else | 408 | else |
| 409 | new_cp = 850 -- another common one | 409 | new_cp = 850 -- another common one |
| 410 | end | 410 | end |
| @@ -578,8 +578,8 @@ describe("Terminal:", function() | |||
| 578 | 578 | ||
| 579 | -- get the console page... | 579 | -- get the console page... |
| 580 | local new_cp | 580 | local new_cp |
| 581 | if old_cp ~= 65001 then | 581 | if old_cp ~= system.CODEPAGE_UTF8 then |
| 582 | new_cp = 65001 -- set to UTF8 | 582 | new_cp = system.CODEPAGE_UTF8 -- set to UTF8 |
| 583 | else | 583 | else |
| 584 | new_cp = 850 -- another common one | 584 | new_cp = 850 -- another common one |
| 585 | end | 585 | end |
| @@ -1042,7 +1042,7 @@ static int lst_getconsolecp(lua_State *L) { | |||
| 1042 | /*** | 1042 | /*** |
| 1043 | Sets the current console code page (Windows). | 1043 | Sets the current console code page (Windows). |
| 1044 | @function setconsolecp | 1044 | @function setconsolecp |
| 1045 | @tparam int cp the code page to set, use 65001 for UTF-8 | 1045 | @tparam int cp the code page to set, use `system.CODEPAGE_UTF8` (65001) for UTF-8 |
| 1046 | @treturn[1] bool `true` on success (always `true` on Posix systems) | 1046 | @treturn[1] bool `true` on success (always `true` on Posix systems) |
| 1047 | */ | 1047 | */ |
| 1048 | static int lst_setconsolecp(lua_State *L) { | 1048 | static int lst_setconsolecp(lua_State *L) { |
| @@ -1076,7 +1076,7 @@ static int lst_getconsoleoutputcp(lua_State *L) { | |||
| 1076 | /*** | 1076 | /*** |
| 1077 | Sets the current console output code page (Windows). | 1077 | Sets the current console output code page (Windows). |
| 1078 | @function setconsoleoutputcp | 1078 | @function setconsoleoutputcp |
| 1079 | @tparam int cp the code page to set, use 65001 for UTF-8 | 1079 | @tparam int cp the code page to set, use `system.CODEPAGE_UTF8` (65001) for UTF-8 |
| 1080 | @treturn[1] bool `true` on success (always `true` on Posix systems) | 1080 | @treturn[1] bool `true` on success (always `true` on Posix systems) |
| 1081 | */ | 1081 | */ |
| 1082 | static int lst_setconsoleoutputcp(lua_State *L) { | 1082 | static int lst_setconsoleoutputcp(lua_State *L) { |
diff --git a/system/init.lua b/system/init.lua index 0c94d35..ee43c4b 100644 --- a/system/init.lua +++ b/system/init.lua | |||
| @@ -7,6 +7,11 @@ | |||
| 7 | local system = require 'system.core' | 7 | local system = require 'system.core' |
| 8 | 8 | ||
| 9 | 9 | ||
| 10 | --- UTF8 codepage. | ||
| 11 | -- To be used with `system.setconsoleoutputcp` and `system.setconsolecp`. | ||
| 12 | -- @field CODEPAGE_UTF8 The Windows CodePage for UTF8. | ||
| 13 | system.CODEPAGE_UTF8 = 65001 | ||
| 14 | |||
| 10 | do | 15 | do |
| 11 | local backup_mt = {} | 16 | local backup_mt = {} |
| 12 | 17 | ||
