<commit_before>$:.unshift File.expand_path('../../../lib', __FILE__)

require_relative 'utils.rb'
require 'slight'
@default_engine = Slight::Engine.new
@slout = STDOUT

at_exit{
  puts "\n--bye--".light_blue
  @slout.close
}

Signal.trap("INT") do
  puts "Terminating...".light_blue
  puts "*** Exit now ***".light_blue
  exit 1
end

def main
  print_logo
  buff = ""
  loop do
    print "sl:> "
    case line = STDIN.readline.sub(/[\n\r]/,'')
    when /^\\/ # build-in commands
      case line
      when "\\h"
        print_help
      when "\\eg"
        print_example
      when /^\\spool@/
        fn = line.split('@')[1] || ""
        case fn.strip
        when "off"
          puts "spool turned off".light_blue
          @slout.close
          @slout = STDOUT
        when ""
          puts "spool turned off. output path not set.".red
        else
          puts "spool turned on".light_blue
          puts "OUTPUT PATH=\"#{fn}\"".light_blue
          unless @slout == STDOUT then
            puts "spool was alread turned on".red
          else
            @slout = File.open(fn, 'w+')
          end
        end
      when "\\q"
        puts "*** Exit now ***".light_blue
        exit 0
      when /\\(v|version|ver)/
        puts "ver #{Slight::VERSION}\n\n"
      else
        puts "Invalid command. type \\h for help.".red
      end
    when /^@/
      fn = line.sub('@','')
      puts "LOAD PATH=\"#{fn}\"".light_blue
      sl_handler(fn, is_file=true)
      buff.clear
      puts ""
    when ";"
      if buff.size > 0 then
        sl_handler(buff)
        buff.clear
        puts ""
      end
    else
      buff << line << "\n"
    end
  end
end

def sl_handler(buff, is_file=false)
  if is_file
    @slout.puts @default_engine.render(buff).green
  else
    @slout.puts @default_engine.render("console",buff).green
  end
#rescue Slight::DSLException => errs
#  STDERR.puts "Source Data Issue:".yellow
#  STDERR.puts errs.message.red
#  STDERR.puts [errs.inspect, errs.backtrace.join("\n")].join("\n").red
rescue Exception => errs2
  errno = errs2.message.split(":")[1].to_i - 1
  buff.split("\n").each_with_index do |line, i|
    if i == errno then
      puts "=>#{i+1} #{line}".red
    else
      puts "  #{i+1} #{line}".yellow
    end
  end
  puts ""
  STDERR.puts errs2.message.red
  #STDERR.puts [errs2.inspect, errs2.backtrace.join("\n")].join("\n")
end

def print_help
  puts " @file    => load and compile file dynamically. E.g. @/tmp/page.slight".green
  puts " \\spool   => set output. E.g. Open: \\spool@/tmp/output. Turn off: \\spool@off".green
  puts " \\eg      => example".green
  puts " \\q       => exit".green
  puts " \\v       => show version (also: \\ver, \\version)".green
  puts
end

def print_example<commit_msg>fixed spool issue in repl<commit_after>$:.unshift File.expand_path('../../../lib', __FILE__)

require_relative 'utils.rb'
require 'slight'
@default_engine = Slight::Engine.new
@slout = STDOUT

at_exit{
  puts "\n--bye--".light_blue
  @slout.close
}

Signal.trap("INT") do
  puts "Terminating...".light_blue
  puts "*** Exit now ***".light_blue
  exit 1
end

def main
  print_logo
  buff = ""
  loop do
    print "sl:> "
    case line = STDIN.readline.sub(/[\n\r]/,'')
    when /^\\/ # build-in commands
      case line
      when "\\h"
        print_help
      when "\\eg"
        print_example
      when /^\\spool@/
        fn = line.split('@')[1] || ""
        case fn.strip
        when "off"
          unless @slout == STDOUT
            puts "spool turned off".light_blue
            @slout.close
            @slout = STDOUT
          else
            puts "spool was alread turned off".red
          end
        when ""
          puts "spool turned off. output path not set.".red
        else
          unless @slout == STDOUT then
            puts "spool was alread turned on".red
          else
            puts "spool turned on".light_blue
            puts "OUTPUT PATH=\"#{fn}\"".light_blue
            @slout = File.open(fn, 'w+')
          end
        end
      when "\\q"
        puts "*** Exit now ***".light_blue
        exit 0
      when /\\(v|version|ver)/
        puts "ver #{Slight::VERSION}\n\n"
      else
        puts "Invalid command. type \\h for help.".red
      end
    when /^@/
      if buff.size == 0 then
        fn = line.sub('@','')
        puts "LOAD PATH=\"#{fn}\"".light_blue
        sl_handler(fn, is_file=true)
        buff.clear
        puts ""
      else
        buff << line << "\n"
      end
    when ";"
      if buff.size > 0 then
        sl_handler(buff)
        buff.clear
        puts ""
      end
    else
      buff << line << "\n"
    end
  end
end

def sl_handler(buff, is_file=false)
  if is_file
    output = @default_engine.render(buff)
  else
    output = @default_engine.render("console",buff)
  end

  if @slout == STDOUT
    @slout.puts output.green
  else
    @slout.puts output
  end
#rescue Slight::DSLException => errs
#  STDERR.puts "Source Data Issue:".yellow
#  STDERR.puts errs.message.red
#  STDERR.puts [errs.inspect, errs.backtrace.join("\n")].join("\n").red
rescue Exception => err
  errno = err.message.split(":")[1].to_i - 1
  buff.split("\n").each_with_index do |line, i|
    if i == errno then
      puts "=>#{i+1} #{line}".red
    else
      puts "  #{i+1} #{line}".yellow
    end
  end
  puts ""
  STDERR.puts err.message.red
  #STDERR.puts [err.inspect, err.backtrace.join("\n")].join("\n")
end

def print_help
  puts " @file    => load and compile file dynamically. E.g. @/tmp/page.slight".green
  puts " \\spool   => set output. E.g. Open: \\spool@/tmp/output. Turn off: \\spool@off".green
  puts " \\eg      => example".green
  puts " \\q       => exit".green
  puts " \\v       => show version (also: \\ver, \\version)".green
  puts
end

def print_example
{'fraction_non_alphanumeric': 0.1565578019699326, 'fraction_numerical': 0.0038016243303957147, 'mean_word_length': 1.962128966223132, 'pattern_counts': {'":': 2, '<': 15, '<?xml version=': 0, '>': 23, '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': '4113450', 'n_tokens_mistral': 2051, 'n_tokens_neox': 2059, 'n_words': 502}