Commits

seydar committed 9ff9426

redid the RC file

Comments (0)

Files changed (4)

     -> underscores also are being treated as word delimiters
     -> figure out how to use Coolline::Editor#word_boundary?
 *   cursor display issues with the menu
+*   get the RC stuff figured out so that the hierarchy is neat and
+    pre/post_processing can be done as a builtin
 
   rc = File.expand_path('~/.chitinrc')
 end
 
-RC = Module.new
+Builtins.class_eval File.read(rc)
 
-module RC
-  extend self
-  include Builtins # inherit defaults from Builtins
-
-  def pre_processing
-    @pre_processing ||= {:default => []}
-  end
-
-  def pre_process(label=:default, &block)
-    if label == :default
-      pre_processing[label] << block
-    else
-      pre_processing[label] = block
-    end
-  end
-
-  def post_processing
-    @post_processing ||= {:default => []}
-    @post_processing.default = Proc.new {|*args| args.size > 1 ? args : args.first }
-    @post_processing
-  end
-
-  def post_process(label=:default, &block)
-    if label == :default
-      post_processing[label] << block
-    else
-      post_processing[label] = block
-    end
-  end
-
-  # If the code is syntactically correct, return nil.
-  # Else, return the error.
-  def syntax_error_for(code) 
-    eval "return nil\n#{code}" 
-  rescue SyntaxError => e
-    e
-  end 
-end
-
-RC.class_eval File.read(rc)
-
-SESSION = Session.new RC
+SESSION = Session.new Builtins
 SESSION.start
 
   val = val.strip == '..' ? 'cd ".."' : val
 end
 
-pre_process do |val|
-  # if there is an unclosed string, close it and run it again.
-  # smart compilers are bad... but this ain't a compiler
-  #
-  # option: make it ask for confirmation first
-  # settable in chitinrc, perjaps?
-  if (e = syntax_error_for(val)) &&
-     e.message =~ /unterminated string meets end of file/
-
-    if syntax_error_for(val + '\'')
-      unless syntax_error_for(val + '"')
-        val << '"'
-      end
-    else
-      val << '\''
-    end
-
-  end
-
-  val
-end
-
-# You can use error classes as a name and if the error comes up,
-# block of code will be run.
+# Sample usage of pre_process and post_process.
+# This is taken from the lib/chitin/commands/builtins.rb
+#
+# pre_process do |val|
+#   # if there is an unclosed string, close it and run it again.
+#   # smart compilers are bad... but this ain't a compiler
+#   #
+#   # option: make it ask for confirmation first
+#   # settable in chitinrc, perjaps?
+#   if (e = syntax_error_for(val)) &&
+#      e.message =~ /unterminated string meets end of file/
 # 
-# post_process SyntaxError do |e, val|
-#   # sample
+#     if syntax_error_for(val + '\'')
+#       unless syntax_error_for(val + '"')
+#         val << '"'
+#       end
+#     else
+#       val << '\''
+#     end
+# 
+#   end
+# 
+#   val
+# end
+# 
+# # You can use error classes as a name and if the error comes up,
+# # block of code will be run.
+# # 
+# # post_process SyntaxError do |e, val|
+# #   # sample
+# # end
+# 
+# post_process :color do |val|
+#   Wirble::Colorize.colorize val
 # end
 
-post_process :color do |val|
-  Wirble::Colorize.colorize val
-end
-

lib/chitin/commands/builtins.rb

 ENV['HOME'] ||= Etc.getpwent(ENV['USER']).dir
 
 module Builtins
+  extend self
 
-  # The standard prompt. Must return a string. Override this to provide
-  # a custom prompt.
-  def big_prompt
-    "+-#{'-' * ENV['USER'].length}--#{'-' * short_pwd.length}-+\n" +
-    "| #{ENV['USER']}: #{short_pwd} |\n" +
-    "+-#{'-' * ENV['USER'].length}--#{'-' * short_pwd.length}-+ "
+  module RCControl
+    def pre_processing
+      @pre_processing ||= {:default => []}
+    end
+
+    def pre_process(label=:default, &block)
+      if label == :default
+        pre_processing[label] << block
+      else
+        pre_processing[label] = block
+      end
+    end
+
+    def post_processing
+      @post_processing ||= {:default => []}
+      @post_processing.default = Proc.new {|*args| args.size > 1 ? args : args.first }
+      @post_processing
+    end
+
+    def post_process(label=:default, &block)
+      if label == :default
+        post_processing[label] << block
+      else
+        post_processing[label] = block
+      end
+    end
   end
+  include RCControl
 
-  def small_prompt
-    "#{ENV['USER']}: #{short_pwd} % "
-  end
+  # If the code is syntactically correct, return nil.
+  # Else, return the error.
+  def syntax_error_for(code) 
+    eval "return nil\n#{code}" 
+  rescue SyntaxError => e
+    e
+  end 
 
-  def prompt
-    big_prompt
+  module Prompts
+    # The standard prompt. Must return a string. Override this to provide
+    # a custom prompt.
+    def big_prompt
+      "+-#{'-' * ENV['USER'].length}--#{'-' * short_pwd.length}-+\n" +
+      "| #{ENV['USER']}: #{short_pwd} |\n" +
+      "+-#{'-' * ENV['USER'].length}--#{'-' * short_pwd.length}-+ "
+    end
+
+    def small_prompt
+      "#{ENV['USER']}: #{short_pwd} % "
+    end
+
+    def prompt
+      big_prompt
+    end
   end
 
   # I'm putting these in their own module so that you can compress them easily
   end
   include ExecutableBinaries
 
+  pre_process do |val|
+    # if there is an unclosed string, close it and run it again.
+    # smart compilers are bad... but this ain't a compiler
+    #
+    # option: make it ask for confirmation first
+    # settable in chitinrc, perjaps?
+    if (e = syntax_error_for(val)) &&
+       e.message =~ /unterminated string meets end of file/
+  
+      if syntax_error_for(val + '\'')
+        unless syntax_error_for(val + '"')
+          val << '"'
+        end
+      else
+        val << '\''
+      end
+  
+    end
+  
+    val
+  end
+  
+  # You can use error classes as a name and if the error comes up,
+  # block of code will be run.
+  # 
+  # post_process SyntaxError do |e, val|
+  #   # sample
+  # end
+  
+  post_process :color do |val|
+    Wirble::Colorize.colorize val
+  end
 end