Skip to content

Commit

Permalink
add system.CODEPAGE_UTF8 for 65001 codepage
Browse files Browse the repository at this point in the history
  • Loading branch information
Tieske committed Jun 19, 2024
1 parent 8996a50 commit e0871d7
Show file tree
Hide file tree
Showing 6 changed files with 18 additions and 13 deletions.
4 changes: 2 additions & 2 deletions doc_topics/03-terminal.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
2 changes: 1 addition & 1 deletion examples/compat.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
2 changes: 1 addition & 1 deletion examples/readline.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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, {
Expand Down
14 changes: 7 additions & 7 deletions spec/04-term_spec.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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()
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions src/term.c
Original file line number Diff line number Diff line change
Expand Up @@ -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) {
Expand Down Expand Up @@ -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) {
Expand Down
5 changes: 5 additions & 0 deletions system/init.lua
Original file line number Diff line number Diff line change
Expand Up @@ -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 = {}

Expand Down

0 comments on commit e0871d7

Please sign in to comment.