Commits

seydar committed f6a98e8 Draft

cleaned up Session#display and made it so runnables can have their input reopened

Comments (0)

Files changed (6)

 *   improve library usage of executables
 *   interprocess communication
     -> dafuq does this even mean, why did i write it?
-*   make it so STDERR isn't alone in the if self[:err] thing in
-    chitin/commands/executable.rb. also, make chitin/session.rb send
-    STDERR output to STDERR and not STDOUT.
 
 QUIRKS:
 *   menu doesn't erase itself sometimes

lib/chitin/commands/executable.rb

     include Runnable
   
     def initialize(path, *args)
+      super()
+
       @path = path
       @args = process_args(args)
     end
         # since executables normally read from STDIN, we can link them.
         IO.open(0).reopen self[:in] # STDIN to the program comes from 
         IO.open(1).reopen self[:out]
-        IO.open(2).reopen self[:err] if self[:err]
+        IO.open(2).reopen self[:err]
   
-        self[:in].close  # These can be closed
-        self[:out].close # because there is already an opening
-        self[:err].close if self[:err] # via IO 0, 1, and 2.
+        # These can be closed because there is already an opening
+        # via IO 0, 1, and 2.
+        close_all
         # And it is important that they be closed, otherwise
         # we'll have a hanging pipe that will hold everything up.
-        # In the future, we'll use `close_all` to make it simple.
   
         exec path, *args
       end
     end
 
     def inspect
-      "#<Chitin::Executable @path=#{@path.inspect} @args=#{@args.inspect}>"
+      "#<Chitin::Executable #{super}>"
     end
 
     def to_s

lib/chitin/commands/pipe.rb

   
     def initialize(*parts)
       @parts = parts
+
+      super()
   
       link_all
     end

lib/chitin/commands/ruby.rb

     attr_reader :pid
   
     def initialize(*arr, &block)
+      super()
+
       raise "Need at least a method name" unless [String, Symbol].include? arr.first.class
       @chains = []
       latest  = [*arr]

lib/chitin/commands/runnable.rb

 module Chitin
   module Runnable
 
+    def initialize
+      set_in STDIN
+      set_out STDOUT
+      set_err STDERR
+    end
+
     def <(io)
       case io
       when IO, File
       run
     end
   
+    # I'm pretty sure this would induce a memory leak if Ruby
+    # weren't GCed. Suggestions are appreciated.
     def set_in(other)
-      unless self[:in]
-        r, w = IO.pipe
-        w.close
-        self[:in=, r]
-      end
+      r, w = IO.pipe
+      w.close
+      self[:in=, r]
       self[:in].reopen other
     end
   
+    # I'm pretty sure this would induce a memory leak if Ruby
+    # weren't GCed. Suggestions are appreciated.
     def set_out(other)
-      unless self[:out]
-        r, w = IO.pipe
-        r.close
-        self[:out=, w]
-      end
+      r, w = IO.pipe
+      r.close
+      self[:out=, w]
       self[:out].reopen other
     end
   
+    # I'm pretty sure this would induce a memory leak if Ruby
+    # weren't GCed. Suggestions are appreciated.
     def set_err(other)
-      unless self[:err]
-        r, w = IO.pipe
-        r.close
-        self[:err=, w]
-      end
+      r, w = IO.pipe
+      r.close
+      self[:err=, w]
       self[:err].reopen other
     end
   

lib/chitin/session.rb

         res = [res] unless Array === res
   
         res.each do |res|
-          # set up the inputs and outputs
-          res[:set_in,  STDIN]  unless res[:in]
-          res[:set_out, STDOUT] unless res[:out]
-          res[:set_err, STDOUT] unless res[:err]
-  
           res[:run]
           res[:wait] unless res[:bg]
         end
       else # else it's a standard ruby type (or a pipe returning as such)
   
         if Pipe === res || StringMethod === res
-          # set up the inputs and outputs
-          res[:set_in,  STDIN]  unless res[:in]
-          res[:set_out, STDOUT] unless res[:out]
-          res[:set_err, STDOUT] unless res[:err]
-
           val = res[:raw_run]
         else
           val = res