beef up stdin module too. This can serve two purposes, to test/interact with other modules (e.g. like a one-person chatroom) or to bridge to irc/discord.

This commit is contained in:
Adrian Malacoda 2018-02-25 07:28:40 -06:00
parent b0c2928e78
commit fec6f7f274
2 changed files with 32 additions and 9 deletions

View File

@ -3,7 +3,7 @@ use std::io;
use modules::EventLoop;
use toml::value::Table;
use {MessageSender, Message, User};
use {MessageSender, Message, User, Channel};
use std::sync::Arc;
use std::sync::mpsc::Receiver;
@ -11,7 +11,12 @@ use transformable_channels::mpsc::ExtSender;
use event::{Event, Envelope};
pub struct StdinModule {}
pub struct StdinModule {
name: String,
channel: Option<String>
}
const DEFAULT_NICK: &'static str = "user";
#[derive(Debug)]
pub struct StdinMessageSender {
@ -21,31 +26,49 @@ pub struct StdinMessageSender {
impl MessageSender for StdinMessageSender {
fn send_message (&self, message: &str) {
debug!("Send message to stdout: {:?}", message);
println!("{}: {}", self.name, message);
println!("@{}: {}", self.name, message);
}
}
impl StdinModule {
pub fn new (_: &Table, config: &Table) -> Box<EventLoop> {
Box::new(StdinModule {})
pub fn new (_: &Table, configuration: &Table) -> Box<EventLoop> {
Box::new(StdinModule {
name: configuration.get("name")
.and_then(|value| value.as_str())
.unwrap_or(DEFAULT_NICK)
.to_owned(),
channel: configuration.get("channel")
.and_then(|value| value.as_str())
.map(String::from)
})
}
}
impl EventLoop for StdinModule {
fn run(&self, sender: Box<ExtSender<Event>>, _: Receiver<Arc<Envelope>>) {
let name = self.name.clone();
let channel = self.channel.clone();
loop {
let mut input = String::new();
match io::stdin().read_line(&mut input) {
Ok(_) => {
let message = Message {
author: User {
name: String::from("Dave"),
name: name.clone(),
sender: Box::new(StdinMessageSender {
name: String::from("Dave")
name: name.clone()
})
},
content: input,
channel: None
channel: channel.as_ref().map(|channel| Channel {
name: channel.to_owned(),
description: "".to_owned(),
sender: Box::new(StdinMessageSender {
name: channel.clone()
}),
topic: "".to_owned()
})
};
match sender.send(Event::Message { message: message }) {

View File

@ -96,7 +96,7 @@ end
[lua3]
type = "lua"
children = ["lua", "irc"]
#children = ["lua", "irc"]
code = """
local clock = os.clock
function sleep(n) -- seconds