Jump to content

Terminal control/Dimensions: Difference between revisions

Added Wren
(Terminal control/Dimensions en Yabasic)
(Added Wren)
Line 837:
 
\Desktop></pre>
 
=={{header|Wren}}==
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_dimensions.wren */
 
class C {
foreign static terminalWidth
foreign static terminalHeight
}
 
var w = C.terminalWidth
var h = C.terminalHeight
System.print("The dimensions of the terminal are:")
System.print(" Width = %(w)")
System.print(" Height = %(h)")</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 <sys/ioctl.h>
#include <unistd.h>
#include "wren.h"
 
void C_terminalWidth(WrenVM* vm) {
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
wrenSetSlotDouble(vm, 0, (double)w.ws_col);
}
 
void C_terminalHeight(WrenVM* vm) {
struct winsize w;
ioctl(STDOUT_FILENO, TIOCGWINSZ, &w);
wrenSetSlotDouble(vm, 0, (double)w.ws_row);
}
 
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, "terminalWidth") == 0) {
return C_terminalWidth;
} else if (isStatic && strcmp(signature, "terminalHeight") == 0) {
return C_terminalHeight;
}
}
}
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_dimensions.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>
The dimensions of the terminal are:
Width = 80
Height = 24
</pre>
 
=={{header|XPL0}}==
Line 851 ⟶ 967:
80x25
</pre>
 
 
=={{header|Yabasic}}==
9,485

edits

Cookies help us deliver our services. By using our services, you agree to our use of cookies.