Console

Text output for console programs.

Output goes to standard output. In a console window, text is written as UTF-16, so every character shows correctly. When the output is redirected to a file or a pipe, it is written as UTF-8.

OpenConsole 🟡

Result = OpenConsole([Title$ [, Mode]])

Prepares the console for output. Call it once before printing.

Parameter Meaning
Title$ the window's title (see ConsoleTitle)
Mode text mode. Ignored for now

Returns nonzero on success.

Notes. x programs are built as console programs, so a console already exists and OpenConsole always succeeds. Calling it keeps programs compatible with PureBasic, where it is required.

CloseConsole ✅

CloseConsole()

Ends console output. Optional; does nothing at the moment.

Print(Text$)

Writes text without starting a new line.

OpenConsole()
Print("Loading")
Print("...")
PrintN(" done")

Output:

Loading... done

PrintN ✅

PrintN(Text$)

Writes text, then starts a new line. PrintN("") writes an empty line.

OpenConsole()
PrintN("first")
PrintN("")
PrintN("third")

Output:

first

third

Input ✅

String$ = Input()

Waits for a line and returns it, without the line break. At the end of the input it returns #PB_Input_Eof (Chr(4)). That happens when redirected input runs out, after Ctrl+D (Linux), or after Ctrl+Z then Return (Windows).

Typed lines are read as Unicode, and redirected input as UTF-8. For example, a program that echoes its input:

OpenConsole()
Repeat
  line$ = Input()
  If line$ = #PB_Input_Eof : Break : EndIf
  PrintN(UCase(line$))
ForEver

echo hello | upper prints HELLO.

Inkey, RawKey ✅

String$ = Inkey()
Result = RawKey()

Inkey doesn't wait. It returns the character of a key that was pressed, or "" if no key was pressed, or the key isn't a character (an arrow, F1 and the like). Call RawKey after Inkey: it returns the key's code, or 0 if no key was pressed. The codes are Windows' virtual key codes on both systems:

Key Code
Backspace, Tab, Return, Escape 8, 9, 13, 27
Letters their capitals, 65 to 90
Page Up, Page Down, End, Home 33, 34, 35, 36
Left, Up, Right, Down 37, 38, 39, 40
Insert, Delete 45, 46
F1 to F12 112 to 123
Shift, Ctrl, Alt (Windows) 16, 17, 18

On Windows, a key such as Ctrl pressed on its own is reported too, as PureBasic does. A terminal on Linux doesn't send those.

On Linux, the first Inkey switches the terminal to reading single keys without showing them. Input switches it back for its line, and it is restored when the program ends. Ctrl+C still ends the program while Inkey is in use.

OpenConsole()
PrintN("Press keys; Escape ends")
Repeat
  k$ = Inkey()
  If k$ <> ""
    PrintN("'" + k$ + "' code " + Str(RawKey()))
  ElseIf RawKey()
    PrintN("special key " + Str(RawKey()))
  Else
    Delay(20)
  EndIf
Until k$ = Chr(27)

ConsoleColor ✅

ConsoleColor(CharacterColor, BackgroundColor)

The colours of the text printed after it, each from 0 to 15:

0 black 4 red 8 dark grey 12 bright red
1 blue 5 magenta 9 bright blue 13 bright magenta
2 green 6 brown 10 bright green 14 yellow
3 cyan 7 light grey 11 bright cyan 15 white

ConsoleLocate, ClearConsole, ConsoleCursor, EnableGraphicalConsole ✅

ConsoleLocate(x, y)
ClearConsole()
ConsoleCursor(Height)
EnableGraphicalConsole(State)

ConsoleLocate moves the cursor to column x and line y, both counted from 0, so the next Print goes there. ClearConsole empties the window in the current background colour, and puts the cursor at the top left. ConsoleCursor sets the cursor's height: 0 hides it, 1 is an underline, 10 a full block.

PureBasic needs EnableGraphicalConsole(1) before these three, and only has them on Windows. x has them on Linux too, with ANSI escape sequences, and doesn't need graphical mode for them. EnableGraphicalConsole is accepted for compatibility.

When the output isn't a console or terminal (when it is redirected to a file or a pipe), colours, positions and the cursor are left out, so the output stays plain text:

OpenConsole()
EnableGraphicalConsole(1)
ClearConsole()
ConsoleColor(14, 1)
ConsoleLocate(10, 5)
Print("Hello")
ConsoleColor(7, 0)
ConsoleCursor(1)
PrintN("!")

Output:

Hello!

ConsoleTitle ✅

ConsoleTitle(Title$)

The console window's title. On Linux it is set through the terminal, as most terminals allow.

ConsoleError ✅

ConsoleError(Message$)

Writes the message and a line break to the standard error output.

ReadConsoleData, WriteConsoleData ✅

Result = ReadConsoleData(*Buffer, Size)
Result = WriteConsoleData(*Buffer, Size)

Raw bytes from the standard input, and to the standard output. Both return the number of bytes. ReadConsoleData waits for some input, and returns 0 at the end of it. Bytes Input has read ahead come first.

OpenConsole()
*m = Ascii("raw bytes" + #LF$)
PrintN(Str(WriteConsoleData(*m, 10)))

Output:

raw bytes
10