Terminal control/Unicode output: Difference between revisions

m (→‎{{header|C}}: Remove vanity tags)
Line 362:
{{out}}
<pre>△</pre>
 
=={{header|Phix}}==
Works on both linux and windows.
 
First, hide some of the grubby low-level details in a re-usable include:
<lang Phix>--
-- builtins/unicode_console.e
--
-- Implements unicode_console() -- initialises one, and returns true if supported
--
-- While there is some appeal to performing (the equivalent inline assembly of) this automatically in the
-- back-end/run-time, it would probably break too much legacy code that assumes Windows-1252 or ISO-8859.
--
include builtins\cffi.e
--set_unicode(true)
constant
--windows:
tGSH = """
HANDLE WINAPI GetStdHandle(
_In_ DWORD nStdHandle
);
""",
tSCOCP = """
BOOL WINAPI SetConsoleOutputCP(
_In_ UINT wCodePageID
);
""",
STD_OUTPUT_HANDLE = -11,
CP_UTF8 = 65001,
--linux:
envset = {"LANG","LC_ALL","LC_CTYPE"}
 
atom k32 = NULL,
xGetStdHandle,
hConsole,
xSetConsoleOutputCP
 
global function unicode_console()
-- initialises the windows console for unicode,
-- and returns true/false if unicode is supported.
bool res = false
if platform()=WINDOWS then
if k32=NULL then
puts(1,"") -- force console to exist
k32 = open_dll("kernel32.dll")
xGetStdHandle = define_cffi_func(k32,tGSH)
hConsole = c_func(xGetStdHandle,{STD_OUTPUT_HANDLE})
xSetConsoleOutputCP = define_cffi_func(k32,tSCOCP)
end if
-- the following is equivalent to running "chcp 65001":
res = c_func(xSetConsoleOutputCP,{CP_UTF8})
-- zero(false) means failure.
else -- LINUX
for i=1 to length(envset) do
if match("UTF",upper(getenv(envset[i])))!=0 then
res = true
exit
end if
end for
end if
return res
end function</lang>
That file has now been added to the standard distribution build files (0.8.0 and later, not yet uploaded)
 
We can then use it like this:
<lang Phix>include builtins\unicode_console.e
 
-- pi, root, lambda, sigma, delta
constant prlsd = "\u03C0\u221A\u03BB\u03A3\u25B3"
 
if unicode_console() then
puts(1,prlsd&"\n")
else
puts(1,"unicode is not supported\n")
end if</lang>
Note that delta does not work on Windows (see talk page) but the others do, and all five work on linux.
 
=={{header|PicoLisp}}==
7,796

edits