Skip to main content

aardio Console Program Getting Started Guide

1. Import console Library

First, we need to import the console library. This is the first step in writing aardio console programs:

import console;

2. Output Text

Use the console.log() function to easily output text to the console:

import console;

console.log("Hello, aardio!");
console.pause();

Note: We added console.pause() at the end of the program to keep the console window open until the user presses any key.

The console.log function supports any number of parameters of any type and will call the tostring function to convert all parameters to strings. Therefore, writing something like console.log(tostring(time())) is unnecessary and can be simplified to console.log(time()).

To print object details, use the console.dump() function:

import console; 

// Print table object
console.dump({1,2,3});
console.pause();

To display table content in JSON format, use `console.dumpJson':

import console; 

// Print table object in JSON format
console.dumpJson({1,2,3});
console.pause();

Note: All output functions in the console library automatically call console.open to open the console.

For simple examples, you can also use print("Hello, aardio!"); to automatically open the console and pause before exiting, but print has limited functionality and may be rewritten by template code.

3. Get User Input

To get user input, use the console.getText() function:

import console;

var name = console.getText("Please enter your name: ");
console.log("Hello, " ++ name ++ "!");
console.pause();

4. Loading and Progress Animation

Example:

// Loading animation and progress
import console;
import console.progress;

// Show loading animation
console.showLoading(" Please wait");
sleep(2000); // Simulate time-consuming operation waiting 2 seconds

// Create progress bar
var bar = console.progress();

// Set progress
for(i=1;100;1){
bar.setProgress(i, i + "% loading......");
sleep(30);
}

Call console.showLoading to display a loading animation. Subsequent calls to console.log will automatically stop the animation.

5. Clear Screen

To clear all content in the console, use console.clearScreen():

import console;

console.log("This text will be cleared");
thread.delay(2000); // Pause 2 seconds

console.clearScreen();
console.log("Screen cleared!");

console.pause();

6. Set Console Colors

Add color to your console with console.setTextColor():

import console;

console.setTextColor("red");
console.log("This is red text");

console.setTextColor("green");
console.log("This is green text");

console.setTextColor("blue");
console.log("This is blue text");

console.pause();

Color parameters can use values from the console.color table (numeric) or names (strings).

Alternative writing:

import console;

console.writeColorText("This is red text", "red");
console.writeColorText("This is green text", "green");
console.writeColorText("This is blue text", "blue");

console.pause();

console.writeColorText and console.writeText do not add newlines at the end.

7. Practice: a Simple Menu

Combine what we've learned to create an interactive menu:

import console;

while(true) {
console.clearScreen();
console.log("Welcome to aardio Console Program!");
console.log("1. Say hello");
console.log("2. Show current time");
console.log("3. Exit");

var choice = console.getText("Please choose (1-3): ");

if(choice == "1") {
console.log("Hello!");
}
elseif(choice == "2") {
console.log("Current time: ", time());
}
elseif(choice == "3") {
break;
}
else {
console.log("Invalid choice, please try again.");
}

console.more(1);
}

// Output message and pause
console.logPause("Thank you for using, goodbye!");

This example demonstrates creating a simple interactive menu using multiple console library functions.

The console library also provides console.choice for creating menus:

import console;

var choiceIndex, choiceText = console.choice({
"New File";
"Open File";
"Save File";
"Exit Program";
}, "Please enter serial number:");

console.choice2 creates menus using arrow keys and Enter/Esc, with different interaction patterns.

Note: Console library functions have the capability to automatically open the console, but you must actively call console.pause() when exiting a thread to pause the console. This is because the console library is extensively used in aardio, and there's a high likelihood that we might invoke console library functions when creating threads or child processes. If console.pause() were executed by default, it could lead to unwanted pause operations (for the same reason, we should avoid directly using the print function in library code, as it has default console-pausing behavior).

If you need the console to automatically pause upon exit (to prevent the console from closing automatically), simply change import console to import console.int.