<commit_before>
require 'io/console'
require 'terminal-table'

require 'flor'
require 'flor/unit'


module Flor::Tools

  class Shell

    def initialize(argv=nil)

      env = ENV['FLOR_ENV'] || 'shell'
      @root = "envs/#{env}"

      prepare_home

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

      @unit.conf['unit'] = 'cli'
      #unit.hooker.add('journal', Flor::Journal)

      prepare_db
      prepare_hooks

      @hook = 'on'

      @unit.start

      @flow_path = File.join(@root, 'home/scratch.flo')
      @ra_flow_path = File.join(@root, 'home/ra_scratch.flo')
      @payload_path = File.join(@root, 'home/payload.json')
      @variables_path = File.join(@root, 'home/variables.json')

      @c = Flor.colours({})

      if argv && argv.any?
        do_eval(argv.join(' '))
      else
        print_header
        do_loop
      end
    end

    attr_reader :c

    protected

    def prepare_home

      home = File.join(@root, 'home')
      return unless Dir.exists?(home)

      %w[ payload.json scratch.flo variables.json ].each do |fn|

        hfn = File.join(home, fn); next if File.exists?(hfn)

        FileUtils.cp(hfn + '.template', hfn)
        puts ".. prepared #{hfn}"
      end
    end

    def prepare_db

      @unit.storage.delete_tables if @unit.conf['sto_uri'].match(/memory/)
      @unit.storage.migrate unless @unit.storage.ready?
    end

    def prepare_hooks

      @unit.hook do |message|

        if @hook == 'off'

          # do nothing

        elsif ! message['consumed']

          # do nothing

        elsif %w[ terminated failed ].include?(message['point'])

          sleep 0.4 # let eventual debug print its stuff

          message['payload'] = message.delete('payload')
          message['consumed'] = message.delete('consumed')
            # reorganize to make payload/consumed stand out

          #puts Flor.to_d(message, colour: true, indent: 1, width: true)
          #puts Flor.to_d(message, colour: true)

          c = Flor.colours
          s = StringIO.new
          s << c.dg("\n  /---\n")
          message.each do |k, v|
            s << c.dg << ('  | ') << k << ': ' << v.inspect << c.rs << "\n"
          end
          s << c.dg("  \\---")
          puts s.string
        end
      end
    end

    def print_header

      puts "flosh - a flor #{Flor::VERSION} shell"
    end

    def prompt

      root = @c.dark_gray(@root + '/')

      time = Time.now.strftime(' %H:%M:%S')

      ec = @unit.executions.where(status: 'active').count
      exes = ' ' + @c.yellow("ex#{ec}")

      ti = @unit.timers.where(status: 'active').count
      tis = ti > 0 ? ' ' + @c.yellow("ti#{ti}") : ''

      ta = nil
      if Dir.exist?(File.join(@root, 'var/tasks'))
        ta = Dir[File.join(@root, 'var/tasks/**/*.json')].count
      end
      tas = ta > 0 ? ' ' + @c.yellow("ta#{ta}") : ''
<commit_msg>Display git commit hash when starting flosh<commit_after>
require 'io/console'
require 'terminal-table'

require 'flor'
require 'flor/unit'


module Flor::Tools

  class Shell

    def initialize(argv=nil)

      env = ENV['FLOR_ENV'] || 'shell'
      @root = "envs/#{env}"

      prepare_home

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

      @unit.conf['unit'] = 'cli'
      #unit.hooker.add('journal', Flor::Journal)

      prepare_db
      prepare_hooks

      @hook = 'on'

      @unit.start

      @flow_path = File.join(@root, 'home/scratch.flo')
      @ra_flow_path = File.join(@root, 'home/ra_scratch.flo')
      @payload_path = File.join(@root, 'home/payload.json')
      @variables_path = File.join(@root, 'home/variables.json')

      @c = Flor.colours({})

      if argv && argv.any?
        do_eval(argv.join(' '))
      else
        print_header
        do_loop
      end
    end

    attr_reader :c

    protected

    def prepare_home

      home = File.join(@root, 'home')
      return unless Dir.exists?(home)

      %w[ payload.json scratch.flo variables.json ].each do |fn|

        hfn = File.join(home, fn); next if File.exists?(hfn)

        FileUtils.cp(hfn + '.template', hfn)
        puts ".. prepared #{hfn}"
      end
    end

    def prepare_db

      @unit.storage.delete_tables if @unit.conf['sto_uri'].match(/memory/)
      @unit.storage.migrate unless @unit.storage.ready?
    end

    def prepare_hooks

      @unit.hook do |message|

        if @hook == 'off'

          # do nothing

        elsif ! message['consumed']

          # do nothing

        elsif %w[ terminated failed ].include?(message['point'])

          sleep 0.4 # let eventual debug print its stuff

          message['payload'] = message.delete('payload')
          message['consumed'] = message.delete('consumed')
            # reorganize to make payload/consumed stand out

          #puts Flor.to_d(message, colour: true, indent: 1, width: true)
          #puts Flor.to_d(message, colour: true)

          c = Flor.colours
          s = StringIO.new
          s << c.dg("\n  /---\n")
          message.each do |k, v|
            s << c.dg << ('  | ') << k << ': ' << v.inspect << c.rs << "\n"
          end
          s << c.dg("  \\---")
          puts s.string
        end
      end
    end

    def print_header

      git = (' ' + `git log -1`.lines.first.split.last[0, 7]) rescue ''
      git = "#{@c.yellow}#{git}#{@c.reset}"

      puts "flosh - a flor #{Flor::VERSION}#{git} shell"
    end

    def prompt

      root = @c.dark_gray(@root + '/')

      time = Time.now.strftime(' %H:%M:%S')

      ec = @unit.executions.where(status: 'active').count
      exes = ' ' + @c.yellow("ex#{ec}")

      ti = @unit.timers.where(status: 'active').count
      tis = ti > 0 ? ' ' + @c.yellow("ti#{ti}") : ''

      ta = nil
      if Dir.exist?(File.join(@root, 'var/tasks'))
        ta = Dir[File.join(@root, 'var/tasks/**/*.json')].count
      end
      tas = ta > 0 ? ' ' + @c.yellow("ta#{ta}") : ''

{'fraction_non_alphanumeric': 0.16448820234255646, 'fraction_numerical': 0.0022067560685791886, 'mean_word_length': 2.0138107416879794, 'pattern_counts': {'":': 0, '<': 39, '<?xml version=': 0, '>': 7, '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': '1037552', 'n_tokens_mistral': 2055, 'n_tokens_neox': 1962, 'n_words': 436}