Skip to content

Instantly share code, notes, and snippets.

@LaylBongers
Created March 22, 2018 20:50
Show Gist options
  • Save LaylBongers/509992dec7cca2019474776d9e463185 to your computer and use it in GitHub Desktop.
Save LaylBongers/509992dec7cca2019474776d9e463185 to your computer and use it in GitHub Desktop.
#![feature(proc_macro)]
#[macro_use]
extern crate stdweb;
pub mod export;
use std::fmt::{Write};
struct Console {
buffer: String,
}
impl Console {
pub fn new() -> Self {
Console {
buffer: String::new(),
}
}
}
impl Write for Console {
fn write_str(&mut self, s: &str) -> Result<(), ::std::fmt::Error> {
// If we just console.log what we get, every value in a write/writeln would be a separate
// line in console, instead we accumulate until we hit a newline, then output that.
for c in s.chars() {
match c {
'\n' => {
js!( console.log(@{&self.buffer}); );
self.buffer.clear();
}
_ => self.buffer.push(c)
}
}
Ok(())
}
}
struct Sair {
console: Console,
value: i32,
}
impl Sair {
pub fn new() -> Self {
let mut console = Console::new();
writeln!(console, "Initializing SAIR").unwrap();
Sair {
console,
value: 0,
}
}
pub fn update(&mut self) {
writeln!(self.console, "Value: {}", self.value).unwrap();
self.value += 1;
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment