Terminal control/Unicode output: Difference between revisions

m
syntax highlighting fixup automation
(Terminal control/Unicode output in FreeBASIC)
m (syntax highlighting fixup automation)
Line 8:
=={{header|Arturo}}==
 
<langsyntaxhighlight lang="rebol">canHandleUnicode?: function [][
any? @[
if key? env "LC_ALL" -> contains? lower get env "LC_ALL" "utf-8"
Line 19:
print "Terminal handle unicode and U+25B3 is: △"
else ->
print "Unicode is not supported on this terminal"</langsyntaxhighlight>
 
{{out}}
Line 27:
=={{header|AutoHotkey}}==
 
<langsyntaxhighlight lang="autohotkey">DllCall("AllocConsole")
hConsole:=DllCall("GetConsoleWindow","UPtr")
Stdout:=FileOpen(DllCall("GetStdHandle", "int", -11, "ptr"), "h `n")
Line 81:
Pause() {
RunWait, %comspec% /c pause>NUL
}</langsyntaxhighlight>
 
=={{header|AWK}}==
 
<langsyntaxhighlight lang="awk">#!/usr/bin/awk -f
BEGIN {
unicodeterm=1 # Assume Unicode support
Line 109:
print "HW65001 This program requires a Unicode compatible terminal"|"cat 1>&2"
exit 252 # Incompatible hardware
}</langsyntaxhighlight>
 
=={{header|BaCon}}==
<code>LANG</code> environment test borrowed from C solution.
<langsyntaxhighlight lang="freebasic">' Determine if the terminal supports Unicode
' if so display delta, if not display error message
LET lc$ = GETENVIRON$("LANG")
Line 120:
ELSE
EPRINT "Sorry, terminal is not testing as unicode ready"
END IF</langsyntaxhighlight>
 
=={{header|BBC BASIC}}==
{{works with|BBC BASIC for Windows}}
<langsyntaxhighlight lang="bbcbasic"> VDU 23,22,640;512;8,16,16,128+8 : REM Enable UTF-8 mode
*FONT Arial Unicode MS,36
PRINT CHR$(&E2)+CHR$(&96)+CHR$(&B3)</langsyntaxhighlight>
 
=={{header|C}}==
<syntaxhighlight lang="c">
<lang c>
#include<stdlib.h>
#include<stdio.h>
Line 156:
return 0;
}
</syntaxhighlight>
</lang>
Output:
<pre>
Line 163:
 
=={{header|Clojure}}==
<langsyntaxhighlight lang="clojure">
(if-not (empty? (filter #(and (not (nil? %)) (.contains (.toUpperCase %) "UTF"))
(map #(System/getenv %) ["LANG" "LC_ALL" "LC_CTYPE"])))
"Unicode is supported on this terminal and U+25B3 is : \u25b3"
"Unicode is not supported on this terminal.")
</syntaxhighlight>
</lang>
 
{{out}}
Line 175:
=={{header|Common Lisp}}==
Each implementation has a different "getenv" function, to work with various implementations was created the "my-getenv" function.
<langsyntaxhighlight lang="lisp">
(defun my-getenv (name &optional default)
#+CMU
Line 195:
(format t "Unicode is not supported on this terminal.~&")
)
</syntaxhighlight>
</lang>
{{out}}
<pre>Unicode is supported on this terminal and U+25B3 is : △</pre>
 
=={{header|Elixir}}==
<langsyntaxhighlight lang="elixir">
if ["LANG", "LC_CTYPE", "LC_ALL"]
|> Enum.map(&System.get_env/1)
Line 209:
raise "This terminal does not support Unicode."
end
</syntaxhighlight>
</lang>
 
=={{header|FreeBASIC}}==
<langsyntaxhighlight lang="freebasic">Print "Terminal handle unicode and U+25B3 is: "; WChr(&H25B3)</langsyntaxhighlight>
 
=={{header|FunL}}==
<langsyntaxhighlight lang="funl">if map( v -> System.getenv(v), ["LC_ALL", "LC_CTYPE", "LANG"]).filter( (!= null) ).exists( ('UTF' in) )
println( '\u25b3' )
else
println( 'Unicode not supported' )</langsyntaxhighlight>
 
=={{header|Go}}==
{{works with|Ubuntu 16.04}}
<langsyntaxhighlight lang="go">package main
 
import (
Line 237:
fmt.Println("This terminal does not support unicode")
}
}</langsyntaxhighlight>
 
{{out}}
Line 245:
 
=={{header|Haskell}}==
<langsyntaxhighlight Haskelllang="haskell">import System.Environment
import Data.List
import Data.Char
Line 255:
then putStrLn "UTF supported: \x25b3"
else putStrLn "UTF not supported"
</syntaxhighlight>
</lang>
Output:
<pre>
Line 269:
 
"has_unicode_support" therefore cannot simply test whether one of the variables LC_ALL, LC_TYPE and LANG contains the string UTF.
<langsyntaxhighlight lang="jq">def has_unicode_support:
def utf: if . == null then false else contains("UTF") or contains("utf") end;
env.LC_ALL
Line 283:
end ;
 
task</langsyntaxhighlight>
{{Out}}
$ jq -M -r -n -f Terminal_control.jq
Line 294:
=={{header|Jsish}}==
Detection based on code from other entries, looking in LC_ALL, LC_CTYPE, LANG for hints of 'UTF'.
<langsyntaxhighlight lang="javascript">/* Terminal Control/Unicode, in Jsish */
 
var utf = false;
Line 311:
=!EXPECTEND!=
*/</langsyntaxhighlight>
 
{{out}}
Line 319:
=={{header|Julia}}==
{{works with|Linux}}
<syntaxhighlight lang="julia">
<lang Julia>
c = '\u25b3'
 
Line 327:
println("This output device does not support Unicode.")
end
</syntaxhighlight>
</lang>
 
{{out}}
Line 336:
=={{header|Kotlin}}==
{{works with|Ubuntu|14.04}}
<langsyntaxhighlight lang="scala">// version 1.1.2
 
fun main(args: Array<String>) {
Line 344:
else
println("This terminal does not support unicode")
}</langsyntaxhighlight>
 
{{out}}
Line 352:
 
=={{header|Lasso}}==
<langsyntaxhighlight Lassolang="lasso">local(env_vars = sys_environ -> join('###'))
if(#env_vars >> regexp(`(LANG|LC_ALL|LC_CTYPE).*?UTF.*?###`)) => {
stdout('UTF supported \u25b3')
else
stdout('This terminal does not support UTF')
}</langsyntaxhighlight>
<pre>UTF supported △
</pre>
Line 363:
=={{header|M2000 Interpreter}}==
M2000 Environment has own console (with graphics support)
<syntaxhighlight lang="m2000 interpreter">
<lang M2000 Interpreter>
Module CheckIt {
If IsWine then Font "DejaVu Sans"
Line 375:
}
Checkit
</syntaxhighlight>
</lang>
 
=={{header|Mathematica}}/{{header|Wolfram Language}}==
<langsyntaxhighlight Mathematicalang="mathematica">If[StringMatchQ[$CharacterEncoding, "UTF*"], Print[FromCharacterCode[30000]], Print["UTF-8 capable terminal required"]]
->田</langsyntaxhighlight>
 
=={{header|Mercury}}==
<langsyntaxhighlight lang="mercury">:- module unicode_output.
:- interface.
 
Line 405:
else
io.write_string("Unicode is not supported on this terminal.\n", !IO)
).</langsyntaxhighlight>
Output:
<pre>Unicode is supported on this terminal and U+25B3 is : △</pre>
Line 411:
=={{header|Nemerle}}==
Typically, on a windows system, the output encoding is '''not''' UTF-8, so in an actual application it would make more sense to set <tt>Console.OutputEncoding</tt> than to merely check it.
<langsyntaxhighlight Nemerlelang="nemerle">using System.Console;
 
module UnicodeOut
Line 420:
else Write("Console encoding may not support Unicode characters.");
}
}</langsyntaxhighlight>
 
=={{header|Nim}}==
<langsyntaxhighlight lang="nim">import os, strutils
 
if "utf" in getEnv("LANG").toLower:
echo "Unicode is supported on this terminal and U+25B3 is: △"
else:
echo "Unicode is not supported on this terminal."</langsyntaxhighlight>
 
=={{header|Perl}}==
Much like Raku...
<langsyntaxhighlight lang="perl">die "Terminal can't handle UTF-8"
unless $ENV{LC_ALL} =~ /utf-8/i or $ENV{LC_CTYPE} =~ /utf-8/i or $ENV{LANG} =~ /utf-8/i;
 
print "△ \n";</langsyntaxhighlight>
 
=={{header|Phix}}==
Line 441:
 
The following (grubby low-level details are hidden away in) builtins/unicode_console.e which is now included in the standard distribution:
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">include</span> <span style="color: #000000;">builtins</span><span style="color: #0000FF;">\</span><span style="color: #000000;">cffi</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
Line 484:
<span style="color: #008080;">return</span> <span style="color: #000000;">res</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">function</span>
<!--</langsyntaxhighlight>-->
Which can be used like this:
<!--<langsyntaxhighlight Phixlang="phix">(phixonline)-->
<span style="color: #008080;">with</span> <span style="color: #008080;">javascript_semantics</span>
<span style="color: #008080;">include</span> <span style="color: #000000;">builtins</span><span style="color: #0000FF;">\</span><span style="color: #000000;">unicode_console</span><span style="color: #0000FF;">.</span><span style="color: #000000;">e</span>
Line 498:
<span style="color: #7060A8;">puts</span><span style="color: #0000FF;">(</span><span style="color: #000000;">1</span><span style="color: #0000FF;">,</span><span style="color: #008000;">"unicode is not supported\n"</span><span style="color: #0000FF;">)</span>
<span style="color: #008080;">end</span> <span style="color: #008080;">if</span>
<!--</langsyntaxhighlight>-->
Note that delta does not work on Windows (see talk page) but the others do, and all five work on linux and under pwa/p2js.
 
=={{header|PicoLisp}}==
<langsyntaxhighlight PicoLisplang="picolisp">(if (sub? "UTF-8" (or (sys "LC_ALL") (sys "LC_CTYPE") (sys "LANG")))
(prinl (char (hex "25b3")))
(quit "UTF-8 capable terminal required") )</langsyntaxhighlight>
 
=={{header|Python}}==
<langsyntaxhighlight Pythonlang="python">import sys
 
if "UTF-8" in sys.stdout.encoding:
print("△")
else:
raise Exception("Terminal can't handle UTF-8")</langsyntaxhighlight>
 
=={{header|R}}==
<langsyntaxhighlight Rlang="r">if (any(grepl("UTF", toupper(Sys.getenv(c("LANG", "LC_ALL", "LC_CTYPE")))))) {
cat("Unicode is supported on this terminal and U+25B3 is : \u25b3\n")
} else {
cat("Unicode is not supported on this terminal.")
}</langsyntaxhighlight>
 
{{out}}
Line 525:
 
=={{header|Racket}}==
<langsyntaxhighlight lang="racket">
#lang racket
(displayln
Line 531:
(or (getenv "LC_ALL") (getenv "LC_CTYPE") (getenv "LANG")))
"\u25b3" "No Unicode detected."))
</syntaxhighlight>
</lang>
 
=={{header|Raku}}==
(formerly Perl 6)
<syntaxhighlight lang="raku" perl6line>die "Terminal can't handle UTF-8"
unless first(*.defined, %*ENV<LC_ALL LC_CTYPE LANG>) ~~ /:i 'utf-8'/;
say "△";</langsyntaxhighlight>
{{out}}
<pre>△</pre>
 
=={{header|Ruby}}==
<langsyntaxhighlight lang="ruby">#encoding: UTF-8 # superfluous in Ruby >1.9.3
 
if ENV.values_at("LC_ALL","LC_CTYPE","LANG").compact.first.include?("UTF-8")
Line 549:
raise "Terminal can't handle UTF-8"
end
</syntaxhighlight>
</lang>
 
=={{header|Scala}}==
Ad hoc in the REPL:
{{libheader|Scala}}<langsyntaxhighlight Scalalang="scala">scala> println(s"Unicode is supported on this terminal and U+25B3 is : \u25b3")
Unicode is supported on this terminal and U+25B3 is : △</langsyntaxhighlight>
 
=={{header|Seed7}}==
Line 562:
output file). STD_CONSOLE supports Unicode under Linux and Windows.
 
<langsyntaxhighlight lang="seed7">$ include "seed7_05.s7i";
include "environment.s7i";
include "console.s7i";
Line 575:
writeln("Unicode is not supported on this terminal.");
end if;
end func;</langsyntaxhighlight>
 
=={{header|Sidef}}==
<langsyntaxhighlight lang="ruby">if (/\bUTF-?8/i ~~ [ENV{"LC_ALL","LC_CTYPE","LANG"}]) {
say "△"
} else {
die "Terminal can't handle UTF-8.\n";
}</langsyntaxhighlight>
 
=={{header|Tcl}}==
Tcl configures the standard output channel to use the system encoding by default. The system encoding is formally the encoding for use when communicating with the OS (e.g., for filenames) but is virtually always correlated with the default terminal encoding.<langsyntaxhighlight lang="tcl"># Check if we're using one of the UTF or "unicode" encodings
if {[string match utf-* [encoding system]] || [string match *unicode* [encoding system]]} {
puts "\u25b3"
} else {
error "terminal does not support unicode (probably)"
}</langsyntaxhighlight>Note that idiomatic Tcl code would not perform such a check; it would just produce the output which would be translated as best as possible (possibly into the target encoding's placeholder character).
 
=={{header|UNIX Shell}}==
Line 599:
 
{{works with|Bourne Shell}}
<langsyntaxhighlight lang="bash">unicode_tty() {
# LC_ALL supersedes LC_CTYPE, which supersedes LANG.
# Set $1 to environment value.
Line 622:
echo "HW65001 This program requires a Unicode compatible terminal" >&2
exit 252 # Incompatible hardware
fi</langsyntaxhighlight>
 
The terminal might support UTF-8, but its fonts might not have every Unicode character. Unless they have U+25B3, the output will not look correct. Greek letters like U+25B3 tend to be common, but some fonts might not have Chinese characters (for example), and almost no fonts have dead scripts such as Cuneiform.
Line 629:
{{trans|C}}
As there is currently no way to obtain this information via Wren CLI, we instead embed a Wren script in a C application and ask the host program to get it for us.
<langsyntaxhighlight lang="ecmascript">/* terminal_control_unicode_output.wren */
 
class C {
Line 639:
} else {
System.print("Unicode is not supported on this terminal.")
}</langsyntaxhighlight>
<br>
We now embed this Wren script in the following C program, compile and run it.
<langsyntaxhighlight lang="c">#include <stdlib.h>
#include <stdio.h>
#include <string.h>
Line 731:
free(script);
return 0;
}</langsyntaxhighlight>
 
{{out}}
Line 740:
=={{header|zkl}}==
This code works for Unix/Linux, Windows XP cmd terminals don't support UTF-8.
<langsyntaxhighlight lang="zkl">if(System.isUnix and T("LC_CTYPE","LC_LANG","LANG").apply(System.getenv)
.filter().filter("holds","UTF"))
println("This terminal supports UTF-8 (\U25B3;)");
else println("I have doubts about UTF-8 on this terminal.");</langsyntaxhighlight>
{{out}}
<pre>
Line 751:
=={{header|ZX Spectrum Basic}}==
 
<langsyntaxhighlight lang="zxbasic">10 REM There is no Unicode delta in ROM
20 REM So we first define a custom character
30 FOR l=0 TO 7
Line 761:
9500 REM data for our custom delta
9510 DATA 0,0,8,20,34,65,127,0
</syntaxhighlight>
</lang>
 
{{omit from|BASIC}}{{omit from|GUISS}}
{{omit from|Axe}}
{{omit from|BASIC}}{{omit from|GUISS}}
{{omit from|GUISS}}
10,327

edits