Skip to content

Commit 04cc836

Browse files
committed
auto merge of #5864 : huonw/rust/rusti-detect-tty, r=graydon
This is a fairly large hack, just passing an extra argument through, and I can't test it at all, since rusti doesn't work here to begin with.
2 parents 7158102 + c2b1534 commit 04cc836

File tree

1 file changed

+44
-30
lines changed

1 file changed

+44
-30
lines changed

src/librusti/rusti.rc

Lines changed: 44 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -29,7 +29,7 @@ extern mod rustc(vers = "0.7-pre");
2929
extern mod syntax(vers = "0.7-pre");
3030

3131
use core::*;
32-
use core::io::WriterUtil;
32+
use core::io::{ReaderUtil, WriterUtil};
3333
use rustc::driver::{driver, session};
3434
use syntax::{ast, diagnostic};
3535
use syntax::ast_util::*;
@@ -241,23 +241,29 @@ fn compile_crate(src_filename: ~str, binary: ~str) -> Option<bool> {
241241

242242
/// Tries to get a line from rl after outputting a prompt. Returns
243243
/// None if no input was read (e.g. EOF was reached).
244-
fn get_line(prompt: ~str) -> Option<~str> {
245-
let result = unsafe { rl::read(prompt) };
244+
fn get_line(use_rl: bool, prompt: ~str) -> Option<~str> {
245+
if use_rl {
246+
let result = unsafe { rl::read(prompt) };
246247

247-
if result.is_none() {
248-
return None;
248+
match result {
249+
None => None,
250+
Some(line) => {
251+
unsafe { rl::add_history(line) };
252+
Some(line)
253+
}
254+
}
255+
} else {
256+
if io::stdin().eof() {
257+
None
258+
} else {
259+
Some(io::stdin().read_line())
260+
}
249261
}
250-
251-
let line = result.get();
252-
253-
unsafe { rl::add_history(line) };
254-
255-
return Some(line);
256262
}
257263

258264
/// Run a command, e.g. :clear, :exit, etc.
259265
fn run_cmd(repl: &mut Repl, _in: @io::Reader, _out: @io::Writer,
260-
cmd: ~str, args: ~[~str]) -> CmdAction {
266+
cmd: ~str, args: ~[~str], use_rl: bool) -> CmdAction {
261267
let mut action = action_none;
262268
match cmd {
263269
~"exit" => repl.running = false,
@@ -313,7 +319,7 @@ fn run_cmd(repl: &mut Repl, _in: @io::Reader, _out: @io::Writer,
313319
let mut multiline_cmd = ~"";
314320
let mut end_multiline = false;
315321
while (!end_multiline) {
316-
match get_line(~"rusti| ") {
322+
match get_line(use_rl, ~"rusti| ") {
317323
None => fail!(~"unterminated multiline command :{ .. :}"),
318324
Some(line) => {
319325
if str::trim(line) == ~":}" {
@@ -333,7 +339,8 @@ fn run_cmd(repl: &mut Repl, _in: @io::Reader, _out: @io::Writer,
333339

334340
/// Executes a line of input, which may either be rust code or a
335341
/// :command. Returns a new Repl if it has changed.
336-
fn run_line(repl: &mut Repl, in: @io::Reader, out: @io::Writer, line: ~str)
342+
fn run_line(repl: &mut Repl, in: @io::Reader, out: @io::Writer, line: ~str,
343+
use_rl: bool)
337344
-> Option<Repl> {
338345
if line.starts_with(~":") {
339346
let full = line.substr(1, line.len() - 1);
@@ -349,11 +356,11 @@ fn run_line(repl: &mut Repl, in: @io::Reader, out: @io::Writer, line: ~str)
349356
vec::slice(split, 1, len).to_vec()
350357
} else { ~[] };
351358

352-
match run_cmd(repl, in, out, cmd, args) {
359+
match run_cmd(repl, in, out, cmd, args, use_rl) {
353360
action_none => { }
354361
action_run_line(multiline_cmd) => {
355362
if !multiline_cmd.is_empty() {
356-
return run_line(repl, in, out, multiline_cmd);
363+
return run_line(repl, in, out, multiline_cmd, use_rl);
357364
}
358365
}
359366
}
@@ -386,30 +393,37 @@ pub fn main() {
386393
stmts: ~""
387394
};
388395

389-
io::println("WARNING: The Rust REPL is experimental and may be");
390-
io::println("unstable. If you encounter problems, please use the");
391-
io::println("compiler instead.");
392-
393-
unsafe {
394-
do rl::complete |line, suggest| {
395-
if line.starts_with(":") {
396-
suggest(~":clear");
397-
suggest(~":exit");
398-
suggest(~":help");
399-
suggest(~":load");
396+
let istty = unsafe { libc::isatty(libc::STDIN_FILENO as i32) } != 0;
397+
398+
// only print this stuff if the user is actually typing into rusti
399+
if istty {
400+
io::println("WARNING: The Rust REPL is experimental and may be");
401+
io::println("unstable. If you encounter problems, please use the");
402+
io::println("compiler instead.");
403+
404+
unsafe {
405+
do rl::complete |line, suggest| {
406+
if line.starts_with(":") {
407+
suggest(~":clear");
408+
suggest(~":exit");
409+
suggest(~":help");
410+
suggest(~":load");
411+
}
400412
}
401413
}
402414
}
403415

404416
while repl.running {
405-
match get_line(repl.prompt) {
417+
match get_line(istty, repl.prompt) {
406418
None => break,
407419
Some(line) => {
408420
if line.is_empty() {
409-
io::println(~"()");
421+
if istty {
422+
io::println(~"()");
423+
}
410424
loop;
411425
}
412-
match run_line(&mut repl, in, out, line) {
426+
match run_line(&mut repl, in, out, line, istty) {
413427
Some(new_repl) => repl = new_repl,
414428
None => { }
415429
}

0 commit comments

Comments
 (0)