Console programming
A console program talks to the person through a terminal: it prints, asks, and can use colors, menus and progress bars. Everything here works when you run a program with otter run.
Colors, cursor positioning, menus, progress bars and secret input are console features. The web and Windows targets report them as unsupported instead of silently doing something different.
Saying things
Otter
say "Hello"
say "Total:" 42 "items"
say
say prints its values on one line with single spaces between them. With nothing after it, it prints a blank line.
Color
Otter
say "Error!" in color "red"
say "Success" in color "green"
The colors are red, green, yellow, blue, cyan, magenta, white, gray, darkgray, darkred, darkgreen, darkyellow, darkblue, darkcyan, darkmagenta and black. An unknown color name is an error that lists the valid ones - it never quietly prints in the default color.
Progress
Otter
show progress 25 percent
show progress 50 percent
show progress 100 percent
Output
[##########----------] 50%
Each call redraws the bar on the same line. The value must be between 0 and 100; anything else is an error rather than being clamped. A say after a progress bar starts on a fresh line.
Cursor
Otter
set cursor to row 5 column 10
Rows and columns count from 1. Moving the cursor only makes sense in a real terminal.
Are we in a real terminal?
Otter
if console is interactive
say "Running in a real terminal."
otherwise
say "Running noninteractively - skipping the progress bar."
.
console is interactive is false when input or output is redirected - for example when the program runs in a scheduled task or its output goes to a file. Check it before using colors, menus or progress bars in a program that others may automate. if not console is interactive covers noninteractive environments.
Console input
A complete console program