<commit_before>module Imap::Backup
  module Configuration; end

  class Configuration::FolderChooser
    attr_reader :account

    def initialize(account)
      @account = account
    end

    def run
      if connection.nil?
        Imap::Backup.logger.warn "Connection failed"
        highline.ask "Press a key "
        return
      end

      if imap_folders.nil?
        Imap::Backup.logger.warn "Unable to get folder list"
        highline.ask "Press a key "
        return
      end

      remove_missing

      catch :done do
        loop do
          Kernel.system("clear")
          show_menu
        end
      end
    end

    private

    def show_menu
      highline.choose do |menu|
        menu.header = "Add/remove folders"
        menu.index = :number
        add_folders menu
        menu.choice("return to the account menu") { throw :done }
        menu.hidden("quit") { throw :done }
      end
    end

    def add_folders(menu)
      imap_folders.each do |folder|
        mark = selected?(folder) ? "+" : "-"
        menu.choice("#{mark} #{folder}") do
          toggle_selection folder
        end
      end
    end

    def selected?(folder_name)
      config_folders = account.folders
      return false if config_folders.nil?

      config_folders.find { |f| f[:name] == folder_name }
    end

    def remove_missing
      removed = []
      config_folders = []
      account.folders.each do |f|
        found = imap_folders.find { |folder| folder == f[:name] }
        if found
          config_folders << f
        else
          removed << f[:name]
        end
      end

      return if removed.empty?

      account.folders = config_folders

      Kernel.puts <<~MESSAGE
        The following folders have been removed: #{removed.join(', ')}
      MESSAGE

      highline.ask "Press a key "
    end

    def toggle_selection(folder_name)
      if selected?(folder_name)
        new_list = account.folders.filter { |f| f[:name] != folder_name }
        account.folders = new_list
      else
        existing = account.folders || []
        account.folders = existing + [{name: folder_name}]
      end<commit_msg>Maintain Ruby 2.5 compatibility<commit_after>module Imap::Backup
  module Configuration; end

  class Configuration::FolderChooser
    attr_reader :account

    def initialize(account)
      @account = account
    end

    def run
      if connection.nil?
        Imap::Backup.logger.warn "Connection failed"
        highline.ask "Press a key "
        return
      end

      if imap_folders.nil?
        Imap::Backup.logger.warn "Unable to get folder list"
        highline.ask "Press a key "
        return
      end

      remove_missing

      catch :done do
        loop do
          Kernel.system("clear")
          show_menu
        end
      end
    end

    private

    def show_menu
      highline.choose do |menu|
        menu.header = "Add/remove folders"
        menu.index = :number
        add_folders menu
        menu.choice("return to the account menu") { throw :done }
        menu.hidden("quit") { throw :done }
      end
    end

    def add_folders(menu)
      imap_folders.each do |folder|
        mark = selected?(folder) ? "+" : "-"
        menu.choice("#{mark} #{folder}") do
          toggle_selection folder
        end
      end
    end

    def selected?(folder_name)
      config_folders = account.folders
      return false if config_folders.nil?

      config_folders.find { |f| f[:name] == folder_name }
    end

    def remove_missing
      removed = []
      config_folders = []
      account.folders.each do |f|
        found = imap_folders.find { |folder| folder == f[:name] }
        if found
          config_folders << f
        else
          removed << f[:name]
        end
      end

      return if removed.empty?

      account.folders = config_folders

      Kernel.puts <<~MESSAGE
        The following folders have been removed: #{removed.join(', ')}
      MESSAGE

      highline.ask "Press a key "
    end

    def toggle_selection(folder_name)
      if selected?(folder_name)
        new_list = account.folders.select { |f| f[:name] != folder_name }
        account.folders = new_list
      else
        existing = account.folders || []
        account.folders = existing + [{name: folder_name}]
      end
{'fraction_non_alphanumeric': 0.09093023255813953, 'fraction_numerical': 0.00046511627906976747, 'mean_word_length': 1.9278420694349898, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 3, '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': '1781954', 'n_tokens_mistral': 1277, 'n_tokens_neox': 1243, 'n_words': 332}