<commit_before>module Nit
  class Status
    # Encapsulates the process of computing the file index from `git status`.
    class State # DISCUSS: alternative names: Screen, ScreenState, FileIndexes?
      # TODO: test me.
      def initialize(screen_state, ignored_files)
        @screen = Lines.new(screen_state)

        @files, @ignored = files_for(screen, ignored_files)
      end
      attr_reader :files, :ignored, :screen

      def evaluate(indexes)
        indexes.collect { |i| files[i] }.join(" ")
      end

    private
      def files_for(screen, ignored_files)
        files = screen.files

        ignored = [] # TODO: that must be implemented by Files.
        files.delete_if { |f| ignored_files.include?(f.path) ? ignored << f : false }

        [Files.new(files), Files.new(ignored)]
      end
    end

    def initialize(config)
      @config = config
    end

    def call(original=`git status`, *args)
      state = State.new(original, @config.ignored_files)

      process(state, *args)
    end

  private
    def process(state)
      files, screen, ignored = state.files, state.screen, state.ignored

      files.each_with_index do |file, i| # TODO: should we have redundant file patterns here? it is better readable, thou.
        ln = file.line

        if ln.match(screen.file_patterns[:modified])
          ln.sub!("#\tmodified:", "#\tmodified: [#{i}] ")
        elsif ln.match(screen.file_patterns[:new])
          ln.sub!("#\t", "#\t [#{i}] ")
        end
      end

      # TODO: should be pluggable:
      ignore_files(screen, ignored)
      bold_branch(screen)

      screen.to_s
    end

    def bold_branch(lines)
      lines.find(/# On branch (.+)/) do |ln, matches|
        line = "# On branch \033[1m#{matches[1]}\033[22m"
        ln.replace(line)
      end
    end

    def ignore_files(screen, ignored)
      return unless ignored.size > 0

      ignored.each { |f| f.line.delete  }
      screen << "#"
      screen << "#   Ignored files: #{ignored.size}"
    end
  end
end<commit_msg>use Files#index whereever possible.<commit_after>module Nit
  class Status
    # Encapsulates the process of computing the file index from `git status`.
    class State # DISCUSS: alternative names: Screen, ScreenState, FileIndexes?
      # TODO: test me.
      def initialize(screen_state, ignored_files)
        @screen = Lines.new(screen_state)

        @files, @ignored = files_for(screen, ignored_files)
      end
      attr_reader :files, :ignored, :screen

      def evaluate(indexes)
        indexes.collect { |i| files[i] }.join(" ")
      end

    private
      def files_for(screen, ignored_files)
        files = screen.files

        ignored = [] # TODO: that must be implemented by Files.
        files.delete_if { |f| ignored_files.include?(f.path) ? ignored << f : false }

        [Files.new(files), Files.new(ignored)]
      end
    end

    def initialize(config)
      @config = config
    end

    def call(original=`git status`, *args)
      state = State.new(original, @config.ignored_files)

      process(state, *args)
    end

  private
    def process(state)
      files, screen, ignored = state.files, state.screen, state.ignored

      files.each do |file| # TODO: should we have redundant file patterns here? it is better readable, thou.
        ln = file.line
        i  = files.index(file)

        if ln.match(screen.file_patterns[:modified])
          ln.sub!("#\tmodified:", "#\tmodified: [#{i}] ")
        elsif ln.match(screen.file_patterns[:new])
          ln.sub!("#\t", "#\t [#{i}] ")
        end
      end

      # TODO: should be pluggable:
      ignore_files(screen, ignored)
      bold_branch(screen)

      screen.to_s
    end

    def bold_branch(lines)
      lines.find(/# On branch (.+)/) do |ln, matches|
        line = "# On branch \033[1m#{matches[1]}\033[22m"
        ln.replace(line)
      end
    end

    def ignore_files(screen, ignored)
      return unless ignored.size > 0

      ignored.each { |f| f.line.delete  }
      screen << "#"
      screen << "#   Ignored files: #{ignored.size}"
    end
  end
end
{'fraction_non_alphanumeric': 0.12154561017363659, 'fraction_numerical': 0.005380288579114698, 'mean_word_length': 2.399833748960931, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 5, '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': '2529883', 'n_tokens_mistral': 1318, 'n_tokens_neox': 1254, 'n_words': 339}