An implementation of the console_draw
API using the termbox backend
If you are thinking of using ncurses, but all you really need is the ability to
- draw chars or strings to arbitrary locations on your terminal
- get keyboard input events
- draw a cursor on the screen
then Termbox Console will fit your needs perfectly!
These examples can also be found in the examples
directory.
Draw the text '"hello world"' to the screen at a specific location.
extern crate termbox_console;
extern crate console_draw;
use std::io::timer::sleep;
use std::time::duration::Duration;
use console_draw::ConsoleCanvas;
use termbox_console::TermboxConsole;
fn main() {
let mut console = TermboxConsole::new();
console.draw(5, 5, "hello world");
console.present();
sleep(Duration::milliseconds(1000));
}
Bounce some text back and forth until the user presses 'q'.
extern crate termbox_console;
extern crate console_draw;
use std::io::timer::sleep;
use std::time::duration::Duration;
use console_draw::{
ConsoleCanvas,
Character,
Resize
};
use termbox_console::TermboxConsole;
fn main() {
let mut console = TermboxConsole::new();
let message = "<marquee> Hello World </marquee>";
let mut width = console.width();
let mut pos = 0i;
let mut direction = 1i;
'outer: loop {
console.clear();
console.draw(pos as uint, 0, message);
console.draw(5, 5, "press 'q' to quit");
console.present();
pos += direction;
if pos == 0 || pos as uint + message.len() > width - 1 {
direction *= -1;
}
for update in console {
match update {
Character('q') => { break 'outer; }
Resize(w, _) => { width = w; }
_ => { }
}
}
sleep(Duration::milliseconds(50));
}
}
Draw what the user types.
extern crate termbox_console;
extern crate console_draw;
use console_draw::{
ConsoleCanvas,
Character,
Special,
Enter,
Backspace
};
use termbox_console::TermboxConsole;
fn main() {
let mut console = TermboxConsole::new();
let mut buffer = String::new();
'outer: loop {
console.clear();
console.draw(0, 0, buffer.as_slice());
console.draw(5, 5, "press [enter] to quit");
console.present();
for update in console {
match update {
Special(Enter) => {
break 'outer;
}
Special(Backspace) => {
buffer.pop_char();
}
Character(x) => {
buffer.push_char(x);
}
_ => { }
}
}
}
}