console

    Cross-platform console I/O library

    Download zip
    Author
    Version
    0.1.4
    License
    Apache-2.0
    Last updated
    7 hours ago
    Downloads
    318

    Dependencies

    #sennenki/console

    Cross-platform console bindings for MoonBit, inspired by .NET System.Console.

    The package currently targets native platforms and provides:

    • text output (write, write_line, newline, error stream variants)
    • keyboard input (read, read_line, read_key, key_available)
    • cursor control and screen clearing
    • foreground/background colors
    • window and buffer size APIs
    • Ctrl+C / Ctrl+Break handler registration

    #Package layout

    • console.mbt: shared public entry helpers
    • console_unix.mbt: Unix implementation (#cfg(not(platform="windows")))
    • console_windows.mbt: Windows implementation (#cfg(platform="windows"))
    • types.mbt: public enums and key info types
    • ffi/ffi.mbt: native FFI declarations
    • ffi/stub_unix.c, ffi/stub_windows.c: platform C stubs

    #Quick usage

    @console.write_line("hello")
    let line = @console.read_line()
    @console.write_line("you typed: \{line}")

    #Cancel key handlers

    Register handlers with:

    ///|
    let id = @console.add_cancel_key_press_handler(fn(key) {
    match key {
    ControlC => @console.write_line("ctrl-c")
    ControlBreak => @console.write_line("ctrl-break")
    }
    true // true => handled, false => default terminate behavior
    })

    Unregister with @console.remove_cancel_key_press_handler(id).

    #Sync and async dispatch

    • Sync APIs (read, read_key, read_line, key_available) perform cooperative pending-signal checks.
    • For async runtimes, you can run cooperative async polling with:

    @console.start_async_cancel_dispatcher()

    Call it from an async task context.

    #Platform differences

    #Unix

    • Uses terminal/ANSI control where applicable.
    • Some window/buffer APIs are compatibility fallbacks (depends on terminal support).

    #Windows

    • Uses Win32 console APIs for key input, cursor, colors, title, buffer/window operations.
    • Signal behavior differs between CTRL_BREAK_EVENT and CTRL_C_EVENT; automation is usually more stable with CTRL_BREAK_EVENT.

    #.NET alignment notes

    This library follows .NET Console semantics where practical, with MoonBit-specific constraints:

    • no method overloading: use generic write/write_line (Show-based) and explicit API names
    • error handling currently uses abort-style failures in many places rather than .NET exception types
    • APIs unsupported on a platform may be absent or constrained by #cfg

    #Testing

    #Unit/whitebox

    moon test

    #Linux integration

    python3 integration/run_linux_integration.py

    #Windows integration

    python integration/run_windows_integration.py

    The Windows harness treats CTRL_BREAK_EVENT signal cases as required and CTRL_C_EVENT cases as best-effort.

    ConsoleColor

    pub enum ConsoleColor {
    Black
    DarkBlue
    DarkGreen
    DarkCyan
    DarkRed
    DarkMagenta
    DarkYellow
    Gray
    DarkGray
    Blue
    Green
    Cyan
    Red
    Magenta
    Yellow
    White
    } derive(Eq,
    Debug
    )

    Represents supported console colors.

    ConsoleColor::to_int

    fn ConsoleColor::to_int(self : ConsoleColor) -> Int

    Converts a console color to its platform numeric value.

    ConsoleKey

    pub(all) enum ConsoleKey {
    Backspace
    Tab
    Enter
    Escape
    Space
    PageUp
    PageDown
    End
    Home
    LeftArrow
    UpArrow
    RightArrow
    DownArrow
    Insert
    Delete
    D0
    D1
    D2
    D3
    D4
    D5
    D6
    D7
    D8
    D9
    A
    B
    C
    D
    E
    F
    G
    H
    I
    J
    K
    L
    M
    N
    O
    P
    Q
    R
    S
    T
    U
    V
    W
    X
    Y
    Z
    LeftWindows
    RightWindows
    Apps
    Sleep
    NumPad0
    NumPad1
    NumPad2
    NumPad3
    NumPad4
    NumPad5
    NumPad6
    NumPad7
    NumPad8
    NumPad9
    Multiply
    Add
    Separator
    Subtract
    Decimal
    Divide
    F1
    F2
    F3
    F4
    F5
    F6
    F7
    F8
    F9
    F10
    F11
    F12
    F13
    F14
    F15
    F16
    F17
    F18
    F19
    F20
    F21
    F22
    F23
    F24
    } derive(Eq,
    Debug
    )

    Represents logical key values returned by read_key.

    ConsoleKey::to_int

    fn ConsoleKey::to_int(self : ConsoleKey) -> Int

    Converts a console key to its numeric value.

    ConsoleKeyInfo

    pub struct ConsoleKeyInfo {
    key_char : Char
    key : ConsoleKey
    shift : Bool
    alt : Bool
    control : Bool
    }

    Describes one keyboard event with key value and modifier flags.

    ConsoleKeyInfo::new

    fn ConsoleKeyInfo::new(key_char : Char, key : ConsoleKey, shift : Bool, alt : Bool, control : Bool) -> ConsoleKeyInfo

    Creates a ConsoleKeyInfo value.

    ConsoleSpecialKey

    pub(all) enum ConsoleSpecialKey {
    ControlC
    ControlBreak
    }

    Represents cancel-signal kinds used by cancel key handlers.

    StandardHandle

    pub(all) enum StandardHandle {
    StandardInput
    StandardOutput
    StandardError
    }

    Represents standard process handles (stdin/stdout/stderr).

    add_cancel_key_press_handler

    fn add_cancel_key_press_handler(handler : (ConsoleSpecialKey) -> Bool) -> Int

    beep

    fn beep() -> Unit

    beep_tone

    fn beep_tone(frequency : Int, duration_ms : Int) -> Unit

    clear

    fn clear() -> Unit

    clear_line

    fn clear_line() -> Unit

    clear_to_end_of_line

    fn clear_to_end_of_line() -> Unit

    error_write

    fn error_write(s : String) -> Int

    error_write_line

    fn error_write_line(s : String) -> Int

    get_background_color

    fn get_background_color() -> ConsoleColor?

    get_buffer_height

    fn get_buffer_height() -> Int

    get_buffer_width

    fn get_buffer_width() -> Int

    get_cursor_left

    fn get_cursor_left() -> Int

    get_cursor_position

    fn get_cursor_position() -> (Int, Int)

    get_cursor_top

    fn get_cursor_top() -> Int

    get_foreground_color

    fn get_foreground_color() -> ConsoleColor?

    get_largest_window_height

    fn get_largest_window_height() -> Int

    get_largest_window_width

    fn get_largest_window_width() -> Int

    get_treat_control_c_as_input

    fn get_treat_control_c_as_input() -> Bool

    get_window_height

    fn get_window_height() -> Int

    get_window_left

    fn get_window_left() -> Int

    get_window_size

    fn get_window_size() -> (Int, Int)

    get_window_top

    fn get_window_top() -> Int

    get_window_width

    fn get_window_width() -> Int

    hide_cursor

    fn hide_cursor() -> Unit

    is_error_redirected

    fn is_error_redirected() -> Bool

    is_input_redirected

    fn is_input_redirected() -> Bool

    is_output_redirected

    fn is_output_redirected() -> Bool

    key_available

    fn key_available() -> Bool

    move_cursor_down

    fn move_cursor_down(lines : Int) -> Unit

    move_cursor_left

    fn move_cursor_left(cols : Int) -> Unit

    move_cursor_right

    fn move_cursor_right(cols : Int) -> Unit

    move_cursor_up

    fn move_cursor_up(lines : Int) -> Unit

    newline

    fn newline() -> Unit

    Writes an empty line to standard output.

    read

    fn read() -> Int

    read_key

    fn read_key(intercept? : Bool) -> ConsoleKeyInfo

    read_line

    fn read_line() -> String

    remove_cancel_key_press_handler

    fn remove_cancel_key_press_handler(handler_id : Int) -> Unit

    reset_color

    fn reset_color() -> Unit

    set_background_color

    fn set_background_color(color : ConsoleColor) -> Unit

    set_buffer_height

    fn set_buffer_height(height : Int) -> Unit

    set_buffer_size

    fn set_buffer_size(width : Int, height : Int) -> Unit

    set_buffer_width

    fn set_buffer_width(width : Int) -> Unit

    set_cursor_position

    fn set_cursor_position(left : Int, top : Int) -> Unit

    set_cursor_visible

    fn set_cursor_visible(visible : Bool) -> Unit

    set_foreground_color

    fn set_foreground_color(color : ConsoleColor) -> Unit

    set_treat_control_c_as_input

    fn set_treat_control_c_as_input(value : Bool) -> Unit

    set_window_height

    fn set_window_height(height : Int) -> Unit

    set_window_position

    fn set_window_position(left : Int, top : Int) -> Unit

    set_window_size

    fn set_window_size(width : Int, height : Int) -> Unit

    set_window_width

    fn set_window_width(width : Int) -> Unit

    show_cursor

    fn show_cursor() -> Unit

    start_async_cancel_dispatcher

    async fn start_async_cancel_dispatcher() -> Unit

    write

    fn[T : Show] write(value : T) -> Unit

    Writes a value to standard output without a trailing newline.

    write_line

    fn[T : Show] write_line(value : T) -> Unit

    Writes a value to standard output followed by a newline.

    Powered by MoonBit

    Site sourceReport issuePackagesBuild queueSkillsStatistics

    © 2026 mooncakes.io