<commit_before>require 'readline'
require './lib/rbsh_helper'
require './lib/rbsh_variables'
require './lib/shell'

class Rbsh
  
  def initialize
    @shell ||= Shell.new
    if File.exists?(ENV['HOME'] + '/.rbsh_history')
      File.open(ENV['HOME'] + '/.rbsh_history', 'r') do |f|
        while line = f.gets ; Readline::HISTORY.push(line.chomp) ; end
      end
    end
  end

  def self.alias(*args)
    Shell.alias(*args)
  end
  
  def main(argv=[])
    while RbshVariables.running?
      if argv.empty?
        RbshVariables.command = Readline.readline(RbshHelper.parse_ps1(@shell.PS1.to_s), true)
        if RbshVariables.command
          File.open(ENV['HOME'] + '/.rbsh_history', 'a') { |f| f.write(RbshVariables.command + "\n") }
        end
        execute_command
      else
        argv.each do |cmd|
          RbshVariables.command = cmd
          execute_command
        end
        argv=[]
      end
    end
  end
  
  def execute_command
    if RbshVariables.command.nil?
      print "\n"
      exit
    elsif RbshVariables.command.strip == '#'
      multi_line
    else
      RbshVariables.system_command = false

      # special case for builtins
      split_com = RbshVariables.command.split(/\s+/, 2)
      if @shell.respond_to?(split_com.first)
        begin
          output = split_com.length == 1 ? @shell.send(split_com.first.to_sym) : @shell.send(split_com.first.to_sym, split_com.last)
        rescue Exception => e
          output = nil
          puts "Exception: #{e.message} (#{e.class})"
        end
      else
        begin
          output = eval(RbshVariables.command, RbshVariables.context)
        rescue NameError => e
          @shell.system_call
        rescue SyntaxError => e
          @shell.system_call
        rescue ArgumentError => e
          @shell.system_call
        end
      end

      if !RbshVariables.system_command? && output
        output = output.inspect
        puts '=> ' + output<commit_msg>fixed bug with leading whitespace<commit_after>require 'readline'
require './lib/rbsh_helper'
require './lib/rbsh_variables'
require './lib/shell'

class Rbsh
  
  def initialize
    @shell ||= Shell.new
    if File.exists?(ENV['HOME'] + '/.rbsh_history')
      File.open(ENV['HOME'] + '/.rbsh_history', 'r') do |f|
        while line = f.gets ; Readline::HISTORY.push(line.chomp) ; end
      end
    end
  end

  def self.alias(*args)
    Shell.alias(*args)
  end
  
  def main(argv=[])
    while RbshVariables.running?
      if argv.empty?
        RbshVariables.command = Readline.readline(RbshHelper.parse_ps1(@shell.PS1.to_s), true)
        if RbshVariables.command
          File.open(ENV['HOME'] + '/.rbsh_history', 'a') { |f| f.write(RbshVariables.command + "\n") }
        end
        execute_command
      else
        argv.each do |cmd|
          RbshVariables.command = cmd
          execute_command
        end
        argv=[]
      end
    end
  end
  
  def execute_command
    if RbshVariables.command.nil?
      print "\n"
      exit
    elsif RbshVariables.command.strip == '#'
      multi_line
    else
      RbshVariables.system_command = false

      # special case for builtins
      split_com = RbshVariables.command.lstrip.split(/\s+/, 2)
      if @shell.respond_to?(split_com.first)
        begin
          output = split_com.length == 1 ? @shell.send(split_com.first.to_sym) : @shell.send(split_com.first.to_sym, split_com.last)
        rescue Exception => e
          output = nil
          puts "Exception: #{e.message} (#{e.class})"
        end
      else
        begin
          output = eval(RbshVariables.command, RbshVariables.context)
        rescue NameError => e
          @shell.system_call
        rescue SyntaxError => e
          @shell.system_call
        rescue ArgumentError => e
          @shell.system_call
        end
      end

      if !RbshVariables.system_command? && output
        output = output.inspect
        puts '=> ' + output
{'fraction_non_alphanumeric': 0.11601929423711602, 'fraction_numerical': 0.002030972328002031, 'mean_word_length': 2.1369426751592355, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '1814363', 'n_tokens_mistral': 1269, 'n_tokens_neox': 1260, 'n_words': 263}