Terminal control/Unicode output: Difference between revisions

Added Wren
(Add R)
(Added Wren)
Line 616:
 
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.
 
=={{header|Wren}}==
{{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.
<lang ecmascript>/* terminal_control_unicode_output.wren */
 
class C {
foreign static isUnicodeSupported
}
 
if (C.isUnicodeSupported) {
System.print("Unicode is supported on this terminal and U+25B3 is : \u25b3")
} else {
System.print("Unicode is not supported on this terminal.")
}</lang>
<br>
We now embed this Wren script in the following C program, compile and run it.
<lang c>#include <stdlib.h>
#include <stdio.h>
#include <string.h>
#include "wren.h"
 
void C_isUnicodeSupported(WrenVM* vm) {
char *str = getenv("LANG");
bool us = false;
int i;
for (i = 0; str[i + 2] != 0; i++) {
if ((str[i] == 'u' && str[i + 1] == 't' && str[i + 2] == 'f') ||
(str[i] == 'U' && str[i + 1] == 'T' && str[i + 2] == 'F')) {
us = true;
break;
}
}
wrenSetSlotBool(vm, 0, us);
}
 
WrenForeignMethodFn bindForeignMethod(
WrenVM* vm,
const char* module,
const char* className,
bool isStatic,
const char* signature) {
if (strcmp(module, "main") == 0) {
if (strcmp(className, "C") == 0) {
if (isStatic && strcmp(signature, "isUnicodeSupported") == 0) {
return C_isUnicodeSupported;
}
}
}
return NULL;
}
 
static void writeFn(WrenVM* vm, const char* text) {
printf("%s", text);
}
 
void errorFn(WrenVM* vm, WrenErrorType errorType, const char* module, const int line, const char* msg) {
switch (errorType) {
case WREN_ERROR_COMPILE:
printf("[%s line %d] [Error] %s\n", module, line, msg);
break;
case WREN_ERROR_STACK_TRACE:
printf("[%s line %d] in %s\n", module, line, msg);
break;
case WREN_ERROR_RUNTIME:
printf("[Runtime Error] %s\n", msg);
break;
}
}
 
char *readFile(const char *fileName) {
FILE *f = fopen(fileName, "r");
fseek(f, 0, SEEK_END);
long fsize = ftell(f);
rewind(f);
char *script = malloc(fsize + 1);
fread(script, 1, fsize, f);
fclose(f);
script[fsize] = 0;
return script;
}
 
int main() {
WrenConfiguration config;
wrenInitConfiguration(&config);
config.writeFn = &writeFn;
config.errorFn = &errorFn;
config.bindForeignMethodFn = &bindForeignMethod;
WrenVM* vm = wrenNewVM(&config);
const char* module = "main";
const char* fileName = "terminal_control_unicode_output.wren";
char *script = readFile(fileName);
WrenInterpretResult result = wrenInterpret(vm, module, script);
switch (result) {
case WREN_RESULT_COMPILE_ERROR:
printf("Compile Error!\n");
break;
case WREN_RESULT_RUNTIME_ERROR:
printf("Runtime Error!\n");
break;
case WREN_RESULT_SUCCESS:
break;
}
wrenFreeVM(vm);
free(script);
return 0;
}</lang>
 
{{out}}
<pre>
Unicode is supported on this terminal and U+25B3 is : △
</pre>
 
=={{header|zkl}}==
9,482

edits