How to resolve the algorithm Terminal control/Dimensions step by step in the Wren programming language

Published on 12 May 2024 09:40 PM

How to resolve the algorithm Terminal control/Dimensions step by step in the Wren programming language

Table of Contents

Problem Statement

Determine the height and width of the terminal, and store this information into variables for subsequent use.

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Terminal control/Dimensions step by step in the Wren programming language

Source code in the wren programming language

/* 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)")

#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;
}


  

You may also check:How to resolve the algorithm Array length step by step in the Pony programming language
You may also check:How to resolve the algorithm Sum digits of an integer step by step in the Ring programming language
You may also check:How to resolve the algorithm Exponentiation order step by step in the D programming language
You may also check:How to resolve the algorithm Determine if a string has all unique characters step by step in the jq programming language
You may also check:How to resolve the algorithm Filter step by step in the Clean programming language