13conio Module
13.1Overview
The conio
module provides following measures to work on a console screen:
- Moves the cursor where texts are printed.
- Changes text colors.
- Retrieves console size.
- Waits for keyboard input.
To utilize it, import the conio
module using import
function.
Below is an example to print a frame around a console:
import(conio)
conio.clear()
[w, h] = conio.getwinsize()
conio.moveto(0, 0) {
print('*' * w)
}
conio.moveto(0, 1 .. (h - 2)) {
print('*', ' ' * (w - 2), '*')
}
conio.moveto(0, h - 1) {
print('*' * w)
}
conio.waitkey():raise
13.2Module Function
The conio
module provides following functions:
conio.clear(region?:symbol):void
Clears the screen.
In default, it clears whole the screen. Argument region
that takes one of the symbols below would specify the region to be cleared.
`line
.. clears characters in the line where the cursor exists.`left
.. clears characters on the left side of the cursor.`right
.. clears characters on the right side of the cursor.`top
.. clears characters on the above side of the cursor.`bottom
.. clears characters on the below side of the cursor.
conio.getwinsize()
[width, height]
.
conio.setcolor(fg:symbol:nil, bg?:symbol):map:void {block?}
Sets foreground and background color of text by specifying a color symbol. Available color symbols are listed below:
`black
`blue
`green
`aqua
`cyan
`red
`purple
`magenta
`yellow
`white
`gray
`bright_blue
`bright_green
`bright_aqua
`bright_cyan
`bright_red
`bright_purple
`bright_magenta
`bright_yellow
`bright_white
If fg
is set to nil, the foreground color remains unchanged. If bg
is omitted or set to nil, the background color remains unchanged.
If block
is specified, the color is changed before evaluating the block, and then gets back to what has been set when done.
conio.moveto(x:number, y:number):map:void {block?}
Moves cursor to the specified position. The most top-left position on the screen is represented as 0, 0
.
If block
is specified, the cursor is moved before evaluating the block, and then gets back to where it has been when done.
conio.waitkey():[raise]
Waits for a keyboard input and returns a character code number associated with the key.
If :raise
attribute is specified, hitting Ctrl-C
issues a terminating signal that causes the program done.
Character code numbers of some of the special keys are defined as below:
conio.K_BACKSPACE
conio.K_TAB
conio.K_RETURN
conio.K_ESCAPE
conio.K_SPACE
conio.K_UP
conio.K_DOWN
conio.K_RIGHT
conio.K_LEFT
conio.K_INSERT
conio.K_HOME
conio.K_END
conio.K_PAGEUP
conio.K_PAGEDOWN
conio.K_DELETE