From 78681400909e395372a5e6fe743fbdc7b1c39752 Mon Sep 17 00:00:00 2001 From: Teddy Wing Date: Sat, 6 Aug 2016 04:42:17 -0400 Subject: Parse options from STDIN Read STDIN and get all lines that start with "e: ", "u: ", and "p: ". If none are found, exit. These lines get added to the `options` vector which then gets displayed in the TUI. Since the contents of the vector are different from our hard-coded one, use a borrow on the clipboard write call. --- src/main.rs | 26 +++++++++++++++++++------- 1 file changed, 19 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/main.rs b/src/main.rs index ee06d29..ce0f4fe 100644 --- a/src/main.rs +++ b/src/main.rs @@ -6,6 +6,8 @@ use clipboard::ClipboardContext; use rustty::{Terminal, Event, Cell, Color, Attr}; use rustty::ui::Painter; +use std::io::{self, BufRead}; +use std::process; use std::time::Duration; struct Point { @@ -19,6 +21,22 @@ fn strip_key(line: &str) -> &str { } fn main() { + let mut options = Vec::new(); + let stdin = io::stdin(); + for line in stdin.lock().lines() { + let line = line.expect("Error reading from STDIN"); + + if line.starts_with("e: ") || + line.starts_with("u: ") || + line.starts_with("p: ") { + options.push(line); + } + } + + if options.is_empty() { + process::exit(1); + } + let mut term = Terminal::new().unwrap(); term.swap_buffers().unwrap(); @@ -36,12 +54,6 @@ fn main() { term.printline_with_cell(selection.x, selection.y, "->", knockout_cell); term.printline(5, 2, "test"); - let options = vec![ - "e: booya@kacha.ch", - "u: booyakacha", - "p: secret" - ]; - for (i, s) in options.iter().enumerate() { term.printline(5, i + 3, s) } @@ -75,7 +87,7 @@ fn main() { } } '\x0D' => { - match clipboard_ctx.set_contents(strip_key(options[selection.y - 3]).to_owned()) { + match clipboard_ctx.set_contents(strip_key(&options[selection.y - 3]).to_owned()) { Ok(_) => { term.printline_with_cell(selection.x, selection.y, "->", green_cell); }, -- cgit v1.2.3