aboutsummaryrefslogtreecommitdiff
path: root/src/main.rs
diff options
context:
space:
mode:
authorAndy Wortman <ixineeringeverywhere@gmail.com>2017-10-23 01:19:20 -0700
committerAndy Wortman <ixineeringeverywhere@gmail.com>2017-10-23 01:19:20 -0700
commitaaed866268616b145026dea6e40b5ab5d57c79c0 (patch)
tree2d86c2251adb63778123f960487745d9eb4efbdf /src/main.rs
parentb9547026cb70ac407aaae79920f623bd66c57c34 (diff)
thingiemapoo grows a real display!
kind of. add DisplayInfo struct that eventually can be used for rendering to any particular interface. I care primarily about rendering to a CLI, so. to support this: many instances of rendering with println!() are moved to at least be orchestrated by display::paint, which will eventually become smart enough to handle a reserved area for state notifications from the client, and buffer input nicely, .... more code moved over to use TweetId instead of bare strings because DisplayInfo is currently a member on TwitterCache, any cases of writing to DisplayInfo also involve writing to TwitterCache, which means TwitterCache is mut like... everywhere. Also, invalid TweetId in IdConversions ends up logging, meaning anything calling that conversion requires a mut TwitterCache, by the above paragraph.
Diffstat (limited to 'src/main.rs')
-rw-r--r--src/main.rs43
1 files changed, 7 insertions, 36 deletions
diff --git a/src/main.rs b/src/main.rs
index e53e6b6..4352fe9 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -28,6 +28,7 @@ use linestream::LineStream;
mod tw;
mod display;
+mod commands;
//Change these values to your real Twitter API credentials
static consumer_key: &str = "T879tHWDzd6LvKWdYVfbJL4Su";
@@ -258,7 +259,7 @@ fn do_ui(ui_rx_orig: chan::Receiver<Vec<u8>>, twete_rx: chan::Receiver<Vec<u8>>,
if line == "reconnect\n".as_bytes() {
return Some((ui_rx_orig.clone(), connect_twitter_stream()));
} else {
- handle_user_input(line, &mut tweeter, &mut queryer);
+ tweeter.handle_user_input(line, &mut queryer);
}
}
None => std::process::exit(0)
@@ -268,11 +269,15 @@ fn do_ui(ui_rx_orig: chan::Receiver<Vec<u8>>, twete_rx: chan::Receiver<Vec<u8>>,
},
ui_rx_a.recv() -> user_input => match user_input {
Some(line) => {
- handle_user_input(line, &mut tweeter, &mut queryer);
+ tweeter.handle_user_input(line, &mut queryer);
},
None => println!("UI thread hung up...")
}
+ // and then we can introduce a channel that just sends a message every 100 ms or so
+ // that acts as a clock!
}
+ // one day display_info should be distinct
+ display::paint(tweeter);
}
}
@@ -308,40 +313,6 @@ fn url_encode(s: &str) -> String {
.replace("]", "%5d")
}
-mod commands;
-use commands::Command;
-
-// is there a nice way to make this accept commands: Iterable<&'a Command>? eg either a Vec or an
-// array or whatever?
-// (extra: WITHOUT having to build an iterator?)
-// ((extra 2: when compiled with -O3, how does `commands` iteration look? same as array?))
-fn parse_word_command<'a, 'b>(line: &'b str, commands: &[&'a Command]) -> Option<(&'b str, &'a Command)> {
- for cmd in commands.into_iter() {
- if cmd.params == 0 {
- if line == cmd.keyword {
- return Some(("", &cmd));
- }
- } else if line.starts_with(cmd.keyword) {
- if line.find(" ").map(|x| x == cmd.keyword.len()).unwrap_or(false) {
- // let inner_twid = u64::from_str(&linestr.split(" ").collect::<Vec<&str>>()[1]).unwrap();
- return Some((line.get((cmd.keyword.len() + 1)..).unwrap().trim(), &cmd));
- }
- }
- }
- return None
-}
-
-fn handle_user_input(line: Vec<u8>, tweeter: &mut tw::TwitterCache, mut queryer: &mut Queryer) {
- let command_bare = String::from_utf8(line).unwrap();
- let command = command_bare.trim();
- if let Some((line, cmd)) = parse_word_command(&command, commands::COMMANDS) {
- (cmd.exec)(line.to_owned(), tweeter, &mut queryer);
- } else {
- println!("I don't know what {} means", command);
- }
- println!(""); // temporaryish because there's no visual distinction between output atm
-}
-
fn connect_twitter_stream() -> chan::Receiver<Vec<u8>> {
let (twete_tx, twete_rx) = chan::sync::<Vec<u8>>(0);