From 9f31a4284921bf71f4763f02a5b53649ee05a4d0 Mon Sep 17 00:00:00 2001
From: Xavier L'Heureux <xavier.lheureux@icloud.com>
Date: Tue, 16 Jul 2019 15:07:13 -0400
Subject: [PATCH] feat: Add a fake interactive mode, where stdin is not a
 terminal

IDEs like IntelliJ run "interactive" scripts without a tty. To make it
possible to use Ion without needing to restart it at eache error, add
the '-f' flag, which will use the stdin like a file, but won't exit on
error. This should NOT be used for being lazy with scripts
---
 src/binary/mod.rs | 11 ++++++-----
 src/main.rs       | 17 +++++++++++++++++
 tests/help.out    | 11 ++++++-----
 3 files changed, 29 insertions(+), 10 deletions(-)

diff --git a/src/binary/mod.rs b/src/binary/mod.rs
index 64b256e4..abdef86d 100644
--- a/src/binary/mod.rs
+++ b/src/binary/mod.rs
@@ -34,11 +34,12 @@ USAGE:
     ion [FLAGS] [OPTIONS] [args]...
 
 FLAGS:
-    -h, --help           Prints help information
-    -i, --interactive    Force interactive mode
-    -n, --no-execute     Do not execute any commands, perform only syntax checking
-    -x                   Print commands before execution
-    -v, --version        Print the version, platform and revision of Ion then exit
+    -f, --fake-interactive    Use a fake interactive mode, where errors don't exit the shell
+    -h, --help                Prints help information
+    -i, --interactive         Force interactive mode
+    -n, --no-execute          Do not execute any commands, perform only syntax checking
+    -x                        Print commands before execution
+    -v, --version             Print the version, platform and revision of Ion then exit
 
 OPTIONS:
     -c <command>             Evaluate given commands instead of reading from the commandline
diff --git a/src/main.rs b/src/main.rs
index efd507a1..d66c44a3 100644
--- a/src/main.rs
+++ b/src/main.rs
@@ -57,6 +57,12 @@ struct CommandLineArgs {
     /// Print commands before execution
     #[cfg_attr(feature = "advanced_arg_parsing", structopt(short = "-x"))]
     print_commands: bool,
+    /// Use a fake interactive mode, where errors don't exit the shell
+    #[cfg_attr(
+        feature = "advanced_arg_parsing",
+        structopt(short = "-f", long = "--fake-interactive")
+    )]
+    fake_interactive: bool,
     /// Force interactive mode
     #[cfg_attr(feature = "advanced_arg_parsing", structopt(short = "-i", long = "--interactive"))]
     interactive: bool,
@@ -88,8 +94,10 @@ fn parse_args() -> CommandLineArgs {
     let mut no_execute = false;
     let mut print_commands = false;
     let mut interactive = false;
+    let mut fake_interactive = false;
     let mut version = false;
     let mut additional_arguments = Vec::new();
+
     while let Some(arg) = args.next() {
         match arg.as_str() {
             "-o" => {
@@ -115,6 +123,7 @@ fn parse_args() -> CommandLineArgs {
                 process::exit(0);
             }
             "-i" | "--interactive" => interactive = true,
+            "-f" | "--fake-interactive" => fake_interactive = true,
             _ => {
                 additional_arguments.push(arg);
             }
@@ -124,6 +133,7 @@ fn parse_args() -> CommandLineArgs {
         key_bindings,
         print_commands,
         interactive,
+        fake_interactive,
         no_execute,
         command,
         version,
@@ -213,6 +223,13 @@ fn main() {
         }
         interactive.add_callbacks();
         interactive.execute_interactive();
+    } else if command_line_args.fake_interactive {
+        let mut reader = BufReader::new(stdin());
+        loop {
+            if let Err(err) = shell.execute_command(&mut reader) {
+                eprintln!("ion: {}", err);
+            }
+        }
     } else {
         shell.execute_command(BufReader::new(stdin()))
     }
diff --git a/tests/help.out b/tests/help.out
index 0f702d30..d13d7a67 100644
--- a/tests/help.out
+++ b/tests/help.out
@@ -6,11 +6,12 @@ USAGE:
     ion [FLAGS] [OPTIONS] [args]...
 
 FLAGS:
-    -h, --help           Prints help information
-    -i, --interactive    Force interactive mode
-    -n, --no-execute     Do not execute any commands, perform only syntax checking
-    -x                   Print commands before execution
-    -v, --version        Print the version, platform and revision of Ion then exit
+    -f, --fake-interactive    Use a fake interactive mode, where errors don't exit the shell
+    -h, --help                Prints help information
+    -i, --interactive         Force interactive mode
+    -n, --no-execute          Do not execute any commands, perform only syntax checking
+    -x                        Print commands before execution
+    -v, --version             Print the version, platform and revision of Ion then exit
 
 OPTIONS:
     -c <command>             Evaluate given commands instead of reading from the commandline
-- 
GitLab