<commit_before>require 'yaml'

#
# This module reasons at the entries level
# (list of entry)
#

module Pwss

  def self.get search_string, entries
    id = choose_entry search_string, entries

    entries[id]["password"]
  end


  def self.update search_string, entries, length, alnum
    id = choose_entry search_string, entries

    password = Cipher.check_or_generate "<PASSWORD> entry", length, alnum

    entries[id]["password"] = password
    entries[id]["updated_at"] = Date.today

    return entries, password
  end
    

  def self.destroy search_string, entries
    id = choose_entry search_string, entries

    entries.delete_at(id) if id != -1
    entries
  end
  

  def self.list entries
    index = 0 
    entries.each do |element|
      print_entry index, element
      index += 1
    end
  end

  private

  #
  # Let the user select an entry from data
  # (data is a YAML string with an array of entries)
  #
  def self.choose_entry search_string, entries
    # here we have a nuisance: we want the user to choose one entry
    # by relative id (e.g. the third found), but we need to return
    # the absolute id... so we just keep track of the real ids with an array
    # we ask the user the index of the array

    index = 0
    found = Array.new
    entries.each do |entry|
      if entry["title"].downcase.include?(search_string.downcase)
        print_entry found.size, entry
        found << index
      end
      index += 1
    end

    if found.size == 0 then
      printf "No entry matches the search criteria.\n"
      exit -1
    end

    if found.size > 1 then
      printf "\nVarious matches.\nSelect entry by ID (0..#{found.size-1}) or -1 to exit: "
      id = STDIN.gets.chomp.to_i
      while (id < -1 or id >= found.size)
        printf "Select entry by ID (0..#{found.size-1}) or -1 to exit: "
        id = STDIN.gets.chomp.to_i
      end
      if id == -1 then
        exit -1
      end
    else
      id = 0
    end

    found[id]
  end
  
  #
  # Print entry
  #
  def self.print_entry id, element
    puts "\n---\nENTRY ID: #{id}"
    # we need to duplicate, because deletion in place will remove
    # passwords from entries (and, frankly, we need them)<commit_msg>we are more cautious with updates and always require the user to confirm the entry (even if there is only one match)<commit_after>require 'yaml'

#
# This module reasons at the entries level
# (list of entry)
#

module Pwss

  def self.get search_string, entries
    id = choose_entry search_string, entries

    entries[id]["password"]
  end


  def self.update search_string, entries, length, alnum
    id = choose_entry search_string, entries

    password = <PASSWORD>_or_generate "new password for entry", length, alnum

    entries[id]["password"] = password
    entries[id]["updated_at"] = Date.today

    return entries, password
  end
    

  def self.destroy search_string, entries
    id = choose_entry search_string, entries

    entries.delete_at(id) if id != -1
    entries
  end
  

  def self.list entries
    index = 0 
    entries.each do |element|
      print_entry index, element
      index += 1
    end
  end

  private

  #
  # Let the user select an entry from data
  # (data is a YAML string with an array of entries)
  #
  def self.choose_entry search_string, entries, confirm_even_if_one = false
    # here we have a nuisance: we want the user to choose one entry
    # by relative id (e.g. the third found), but we need to return
    # the absolute id (to update the right entry in the safe)
    # ... so we just keep track of the real ids with an array
    #     and we ask the user the index of the array

    index = 0
    found = Array.new
    entries.each do |entry|
      if entry["title"].downcase.include?(search_string.downcase)
        print_entry found.size, entry
        found << index
      end
      index += 1
    end

    if found.size == 0 then
      printf "No entry matches the search criteria.\n"
      exit -1
    end

    if found.size > 1 or confirm_even_if_one then
      printf "\nVarious matches.\n" if found.size > 1
      printf "Select entry by ID (0..#{found.size-1}) or -1 to exit: "
      id = STDIN.gets.chomp.to_i
      while (id < -1 or id >= found.size)
        printf "Select entry by ID (0..#{found.size-1}) or -1 to exit: "
        id = STDIN.gets.chomp.to_i
      end
      if id == -1 then
        exit -1
      end
    else
      id = 0
    end

    found[id]
  end
  
  #
  # Print entry
  #
  def self.print_entry id, element
    puts "\n---\nENTRY ID: #{id}"
    # we need to duplicate, because deletion in place will remove
    # passwords from entries (and, frankly, we need them)
{'fraction_non_alphanumeric': 0.08619210977701544, 'fraction_numerical': 0.007933104631217838, 'mean_word_length': 2.360951008645533, 'pattern_counts': {'":': 0, '<': 11, '<?xml version=': 0, '>': 10, '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': '4347244', 'n_tokens_mistral': 1490, 'n_tokens_neox': 1427, 'n_words': 556}