https://github.com/oboard/readline
A readline library for MoonBit
https://github.com/oboard/readline
moonbit
Last synced: 5 months ago
JSON representation
A readline library for MoonBit
- Host: GitHub
- URL: https://github.com/oboard/readline
- Owner: oboard
- License: apache-2.0
- Created: 2025-07-29T06:39:58.000Z (11 months ago)
- Default Branch: main
- Last Pushed: 2025-08-23T05:45:33.000Z (10 months ago)
- Last Synced: 2025-08-24T08:51:57.792Z (10 months ago)
- Topics: moonbit
- Language: C
- Homepage: https://mooncakes.io/docs/oboard/readline
- Size: 19.5 KB
- Stars: 1
- Watchers: 0
- Forks: 1
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
Awesome Lists containing this project
README
# oboard/readline
A readline library for MoonBit
## Features
- ๐ **Callback-based**: Asynchronous input handling with callback functions
- ๐งน **Resource management**: Proper cleanup with `close()` method
- ๐ **Simple API**: Easy-to-use interface for interactive console applications
- ๐ **Zero dependencies**: Lightweight implementation using only standard libraries
- ๐ **Cross-platform**: Works on JavaScript, Native (C) targets
## Installation
Add this package to your MoonBit project:
```bash
moon add oboard/readline
```
## Quick Start
```moonbit
fn main {
let rl = @readline.new()
rl.question("What is your name? ", name => {
println("Hello " + name)
rl.question("How old are you? ", age => {
println("You are " + age + " years old")
rl.close()
})
})
}
```
## API Reference
### `new() -> T`
Creates a new readline instance.
```moonbit
let rl = @readline.new()
```
### `T::question(self: T, question: String, callback: (String) -> Unit) -> Unit`
Prompts the user with a question and calls the callback function with the user's input.
**Parameters:**
- `question`: The prompt text to display
- `callback`: Function to handle the user's input
```moonbit
rl.question("Enter your name: ", input => {
println("You entered: " + input)
})
```
### `T::close(self: T) -> Unit`
Cleans up the readline instance and releases resources.
```moonbit
rl.close()
```
## Platform Support
| Platform | Implementation | Status |
|----------|----------------|--------|
| JavaScript | Browser/Node.js | โ
Supported |
| Native | C with stdio | โ
Supported |
| WebAssembly | WASM | โ Unsupported |
## Examples
### Simple Q&A
```moonbit
fn main {
let rl = @readline.new()
rl.question("What's your favorite color? ", color => {
println("Nice choice: " + color)
rl.close()
})
}
```
### Multiple Questions
```moonbit
fn main {
let rl = @readline.new()
rl.question("First name: ", first => {
rl.question("Last name: ", last => {
println("Full name: " + first + " " + last)
rl.close()
})
})
}
```
## Building and Testing
## Implementation Details
- **JavaScript**: Uses `prompt()` for browser environments and `stdin/stdout` for Node.js
- **Native**: Uses standard C `scanf()` and `printf()` functions
## License
Apache-2.0
## Contributing
Contributions are welcome! Please feel free to submit issues and pull requests.
## Changelog
### v0.1.0
- Initial release
- Cross-platform readline support
- Basic question/answer functionality
- Resource cleanup with close() method