<commit_before>
require 'flor'
require 'flor/unit'


module Flor::Tools
  class Repl

    def initialize(env)

      unit = Flor::Unit.new("envs/#{env}/etc/conf.json")

      #pp unit.conf
      unit.conf[:unit] = 'repl'

      #unit.hooker.add('journal', Flor::Journal)
      if unit.conf['sto_uri'].match(/memory/)
        unit.storage.delete_tables
        unit.storage.migrate
      end
      unit.start

      @lines = []
      @payload = {}
      @vars = {}
      @prompt = 'flor> '

      loop do

        line = prompt_and_read

        break unless line
        next if line.strip == ''

        cmd = "cmd_#{line.split(/\s/).first}".to_sym

        if cmd.size > 4 && methods.include?(cmd)
          begin
            send(cmd, line)
          rescue StandardError, NotImplementedError => err
            p err
            err.backtrace[0, 7].each { |l| puts "  #{l}" }
          end
        else
          @lines << line
        end
      end

      $stdout.puts
    end

    protected

    def hlp_launch
      %{ launches the current execution code }
    end
    alias hlp_run hlp_launch

    def cmd_launch(line)

      fail NotImplementedError
    end
    alias cmd_run cmd_launch

    def hlp_help
      %{ displays this help }
    end
    def cmd_help(line)

      puts
      puts "## available commands:"
      puts
      COMMANDS.each do |cmd|
        print "* #{cmd}"
        if hlp = (send("hlp_#{cmd}") rescue nil); print " - #{hlp.strip}"; end
        puts
      end
      puts
    end

    def hlp_exit
      %{ exits this repl, with the given int exit code or 0 }
    end
    def cmd_exit(line)

      exit(line.split(/\s+/)[1].to_i)
    end

    def hlp_list
      %{ lists the lines of the current execution code }
    end
    def cmd_list(line)

      _rs, _dg, _yl = Flor.colours({})

      lw = [ 2, @lines.size.to_s.length ].max
      sw = 5 - lw

      @lines.each_with_index do |l, i|
        puts "#{_dg}% #{sw}s%0#{lw}i #{_yl}%s#{_rs}" % [ '', i + 1, l ]
      end
    end

    def hlp_parse
      %{ parses the current execution code and displays its tree }
    end
    def cmd_parse(line)

      Flor.print_tree(
        Flor::Lang.parse(@lines.join("\n"), nil, {}),
        '0',
        headers: false)
    end

    def cmd_new(line)

      fail NotImplementedError
    end

    def cmd_save(line)

      fail NotImplementedError
    end

    def cmd_cont(line)

      fail NotImplementedError
    end

    #
    # use Readline if possible

    COMMANDS = self.allocate.methods \
      .select { |m| m.to_s.match(/^cmd_/) }.collect { |m| m[4..-1] }.sort

    begin
      require 'readline'
      def prompt_and_read
        Readline.readline(@prompt, true)
      end
      Readline.completion_proc =
        proc { |s| COMMANDS.grep(/^#{Regexp.escape(s)}/) }
      #Readline.completion_append_character =
      #  " "
    rescue LoadError => le<commit_msg>Implement repl save and cat<commit_after>
require 'flor'
require 'flor/unit'


module Flor::Tools
  class Repl

    def initialize(env)

      unit = Flor::Unit.new("envs/#{env}/etc/conf.json")

      #pp unit.conf
      unit.conf[:unit] = 'repl'

      #unit.hooker.add('journal', Flor::Journal)
      if unit.conf['sto_uri'].match(/memory/)
        unit.storage.delete_tables
        unit.storage.migrate
      end
      unit.start

      @lines = []
      @payload = {}
      @vars = {}
      @prompt = 'flor> '

      loop do

        line = prompt_and_read

        break unless line
        next if line.strip == ''

        cmd = "cmd_#{line.split(/\s/).first}".to_sym

        if cmd.size > 4 && methods.include?(cmd)
          begin
            send(cmd, line)
          rescue StandardError, NotImplementedError => err
            p err
            err.backtrace[0, 7].each { |l| puts "  #{l}" }
          end
        else
          @lines << line
        end
      end

      $stdout.puts
    end

    protected

    def hlp_launch
      %{ launches the current execution code }
    end
    alias hlp_run hlp_launch

    def cmd_launch(line)

      fail NotImplementedError
    end
    alias cmd_run cmd_launch

    def hlp_help
      %{ displays this help }
    end
    def cmd_help(line)

      puts
      puts "## available commands:"
      puts
      COMMANDS.each do |cmd|
        print "* #{cmd}"
        if hlp = (send("hlp_#{cmd}") rescue nil); print " - #{hlp.strip}"; end
        puts
      end
      puts
    end

    def hlp_exit
      %{ exits this repl, with the given int exit code or 0 }
    end
    def cmd_exit(line)

      exit(line.split(/\s+/)[1].to_i)
    end

    def hlp_list
      %{ lists the lines of the current execution code }
    end
    def do_list(lines)

      _rs, _dg, _yl = Flor.colours({})

      lw = [ 2, lines.size.to_s.length ].max
      sw = 5 - lw

      lines.each_with_index do |l, i|
        puts "#{_dg}% #{sw}s%0#{lw}i #{_yl}%s#{_rs}" % [ '', i + 1, l ]
      end
    end
    def cmd_list(line)

      do_list(@lines)
    end

    def hlp_parse
      %{ parses the current execution code and displays its tree }
    end
    def cmd_parse(line)

      Flor.print_tree(
        Flor::Lang.parse(@lines.join("\n"), nil, {}),
        '0',
        headers: false)
    end

    def cmd_new(line)

      fail NotImplementedError
    end

    def hlp_save
      %{ saves the current execution code to the given file }
    end
    def cmd_save(line)

      fname = line.split(/\s+/)[1]

      File.open(fname, 'wb') { |f| f.puts @lines }
    end

    def hlp_cat
      %{ outputs the content of the give file }
    end
    def cmd_cat(line)

      fname = line.split(/\s+/)[1]

      do_list(File.readlines(fname).collect(&:chomp))
    end

    def cmd_cont(line)

      fail NotImplementedError
    end

    #
    # use Readline if possible

    COMMANDS = self.allocate.methods \
      .select { |m| m.to_s.match(/^cmd_/) }.collect { |m| m[4..-1] }.sort

    begin
      require 'readline'
      def prompt_and_read
        Readline.readline(@prompt, true)
      end
      Readline.completion_proc =
        proc { |s| COMMANDS.grep(/^#{Regexp.escape(s)}/) }
      #Readline.completion_append_character =
      #  " "
    rescue LoadError => le
{'fraction_non_alphanumeric': 0.12033597157163625, 'fraction_numerical': 0.004199644645453077, 'mean_word_length': 1.8209567198177676, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 11, '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': '7183559', 'n_tokens_mistral': 2168, 'n_tokens_neox': 2073, 'n_words': 516}