From dda41ddb3223fe17117c45efd00833c0861c4690 Mon Sep 17 00:00:00 2001 From: whitequark Date: Thu, 17 Aug 2017 15:46:18 +0000 Subject: [PATCH] CLI: fall back to noninteractive-with-prompt. Sometimes it's useful to delegate REPL input handling to another piece of software, e.g. SublimeREPL, but behave in the same way, e.g. showing the prompt, in all other ways. --- src/bin/rink.rs | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/src/bin/rink.rs b/src/bin/rink.rs index 772f066..b1db8c3 100644 --- a/src/bin/rink.rs +++ b/src/bin/rink.rs @@ -52,6 +52,18 @@ fn main_interactive() { use std::rc::Rc; use std::cell::RefCell; + let mut rl = match Reader::new("rink") { + Err(_) => { + // If we can't initialize linefeed on this terminal for some reason, + // e.g. it being a pipe instead of a tty, use the noninteractive version + // with prompt instead. + let stdin_handle = stdin(); + return main_noninteractive(stdin_handle.lock(), true); + }, + Ok(rl) => rl + }; + rl.set_prompt("> "); + struct RinkCompleter(Rc>); impl Completer for RinkCompleter { @@ -203,8 +215,6 @@ fn main_interactive() { }; let ctx = Rc::new(RefCell::new(ctx)); let completer = RinkCompleter(ctx.clone()); - let mut rl = Reader::new("rink").unwrap(); - rl.set_prompt("> "); rl.set_completer(Rc::new(completer)); let mut hpath = rink::config_dir();