Commits

Clark C. Evans committed e8def18

adding input() to ctl.script

  • Participants
  • Parent commits 5bd8979

Comments (0)

Files changed (2)

File src/htsql/ctl/encode.py

     """
 
     def run(self):
-        isatty = os.isatty(self.ctl.stdin.fileno())
-        if isatty:
-           self.ctl.out("Type your query fragment and finish with a blank line.")
-        if isatty:
-            chunks = []
-            while True:
-                chunk = self.ctl.stdin.readline().rstrip()
-                if not chunk:
-                    self.ctl.out("The percent-encoded fragment is:")
-                    self.ctl.out()
-                    break
-                chunks.append(chunk)
-            input = "\n".join(chunks)
-        else:
-            input = self.ctl.stdin.read().rstrip()
+        input = self.ctl.input("What is the query fragment?")
         self.ctl.out(urllib.quote(input))
-        if isatty:
+        if self.ctl.isatty:
             self.ctl.out()

File src/htsql/ctl/script.py

 from .routine import Argument, Routine
 from .option import Option
 from ..core.util import listof, trim_doc
-
+import os
 
 class Script(object):
     """
         self.stdin = stdin
         self.stdout = stdout
         self.stderr = stderr
+        self.isatty = os.isatty(self.stdin.fileno())
+       
         # A mapping of routine_class.name -> routine_class.
         self.routine_by_name = {}
         # A mapping of option.short_name | option.long_name -> option.
             #   nothing.
             return exc
 
+    def input(self, prompt):
+        """
+        If this is a terminal, get input from the user with a prompt,
+        reading to first blank line.  Otherwise, read standard input.
+        """ 
+        if not self.isatty:
+            return self.stdin.read().rstrip()
+        self.out(prompt)
+        chunks = []
+        while True:
+            chunk = self.stdin.readline().rstrip()
+            if not chunk:
+                break
+            chunks.append(chunk)
+        return "\n".join(chunks)
+
     def out(self, *values, **options):
         """
         Print the values to the standard output stream.