summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorThijs Schreijer <thijs@thijsschreijer.nl>2024-06-19 22:05:33 +0200
committerThijs Schreijer <thijs@thijsschreijer.nl>2024-06-19 22:05:33 +0200
commite0871d7be63dd428d4a2b9a3db4e033894165cef (patch)
tree9a844f267ec9ec99ba745680453b683dd73a6f5b
parent8996a5022fa82e5d5335f71580d0cd6b6d323c9b (diff)
downloadluasystem-e0871d7be63dd428d4a2b9a3db4e033894165cef.zip
add system.CODEPAGE_UTF8 for 65001 codepage
-rw-r--r--doc_topics/03-terminal.md4
-rw-r--r--examples/compat.lua2
-rw-r--r--examples/readline.lua2
-rw-r--r--spec/04-term_spec.lua14
-rw-r--r--src/term.c4
-rw-r--r--system/init.lua5
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
On Windows UTF-8 output can be enabled by setting the output codepage like this:
- -- setup Windows output codepage to UTF-8; 65001
- sys.setconsoleoutputcp(65001)
+ -- setup Windows output codepage to UTF-8
+ sys.setconsoleoutputcp(sys.CODEPAGE_UTF8)
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
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
os.getenv = sys.getenv -- luacheck: ignore
-- Set console output to UTF-8 encoding.
- sys.setconsoleoutputcp(65001)
+ sys.setconsoleoutputcp(sys.CODEPAGE_UTF8)
-- Set up the terminal to handle ANSI escape sequences on Windows.
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()
sys.setconsoleflags(io.stdout, sys.getconsoleflags(io.stdout) + sys.COF_VIRTUAL_TERMINAL_PROCESSING)
sys.setconsoleflags(io.stdin, sys.getconsoleflags(io.stdin) + sys.CIF_VIRTUAL_TERMINAL_INPUT)
-- set output to UTF-8
-sys.setconsoleoutputcp(65001)
+sys.setconsoleoutputcp(sys.CODEPAGE_UTF8)
-- setup Posix terminal to disable canonical mode and echo
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()
setup(function()
wincodepage = system.getconsoleoutputcp()
- assert(system.setconsoleoutputcp(65001)) -- set to UTF8
+ assert(system.setconsoleoutputcp(system.CODEPAGE_UTF8)) -- set to UTF8
end)
teardown(function()
@@ -346,8 +346,8 @@ describe("Terminal:", function()
end)
local new_cp
- if old_cp ~= 65001 then
- new_cp = 65001 -- set to UTF8
+ if old_cp ~= system.CODEPAGE_UTF8 then
+ new_cp = system.CODEPAGE_UTF8 -- set to UTF8
else
new_cp = 850 -- another common one
end
@@ -403,8 +403,8 @@ describe("Terminal:", function()
end)
local new_cp
- if old_cp ~= 65001 then
- new_cp = 65001 -- set to UTF8
+ if old_cp ~= system.CODEPAGE_UTF8 then
+ new_cp = system.CODEPAGE_UTF8 -- set to UTF8
else
new_cp = 850 -- another common one
end
@@ -578,8 +578,8 @@ describe("Terminal:", function()
-- get the console page...
local new_cp
- if old_cp ~= 65001 then
- new_cp = 65001 -- set to UTF8
+ if old_cp ~= system.CODEPAGE_UTF8 then
+ new_cp = system.CODEPAGE_UTF8 -- set to UTF8
else
new_cp = 850 -- another common one
end
diff --git a/src/term.c b/src/term.c
index db3c300..d8cc38e 100644
--- a/src/term.c
+++ b/src/term.c
@@ -1042,7 +1042,7 @@ static int lst_getconsolecp(lua_State *L) {
/***
Sets the current console code page (Windows).
@function setconsolecp
-@tparam int cp the code page to set, use 65001 for UTF-8
+@tparam int cp the code page to set, use `system.CODEPAGE_UTF8` (65001) for UTF-8
@treturn[1] bool `true` on success (always `true` on Posix systems)
*/
static int lst_setconsolecp(lua_State *L) {
@@ -1076,7 +1076,7 @@ static int lst_getconsoleoutputcp(lua_State *L) {
/***
Sets the current console output code page (Windows).
@function setconsoleoutputcp
-@tparam int cp the code page to set, use 65001 for UTF-8
+@tparam int cp the code page to set, use `system.CODEPAGE_UTF8` (65001) for UTF-8
@treturn[1] bool `true` on success (always `true` on Posix systems)
*/
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 @@
local system = require 'system.core'
+--- UTF8 codepage.
+-- To be used with `system.setconsoleoutputcp` and `system.setconsolecp`.
+-- @field CODEPAGE_UTF8 The Windows CodePage for UTF8.
+system.CODEPAGE_UTF8 = 65001
+
do
local backup_mt = {}