<commit_before>require "gosu"
require "grid_pattern_editor/base"
require "grid_pattern_editor/z_order"
require "grid_pattern_editor/board"

module GridPatternEditor
  class Window < Gosu::Window
    TEXTS = [
      "0",
      "1",
      "2",
    ]

    attr_reader :images, :texts

    def initialize(file_name=nil, width=800, height=600)
      super(width, height, false)
      self.caption = "GridPatternEditor"
      init_images
      @board = Board.new(self, width, height)
      @file_name = file_name
    end

    def update
    end

    def draw
      @board.draw
    end

    def button_down(id)
      case id
      when Gosu::MsLeft
        clicked_cell = @board.click
        return unless clicked_cell
        index = TEXTS.index(clicked_cell.text)
        index += 1
        index %= TEXTS.size
        clicked_cell.set_image_from_text(TEXTS[index])
      when Gosu::KbEnter, Gosu::KbReturn
        write_data
      when Gosu::KbR
        @board.reset
      when Gosu::KbEscape
        close
      end
    end

    def needs_cursor?
      true
    end

    private
    def init_images
      @images = {}
      TEXTS.each do |text|
        image_path = File.join(images_dir, "#{text}.png")
        next unless File.exist?(image_path)
        image = Gosu::Image.new(self, image_path, false)
        @images[text] = image
      end
    end

    def images_dir
      File.join(media_dir, "images")
    end

    def media_dir
      base_dir = File.expand_path(File.join(__FILE__, "..", "..", ".."))
      File.join(base_dir, "media")
    end

    def write_data
      if @file_name.nil?
        puts(@board.to_s)
      elsif File.exist?(@file_name)
        File.open(@file_name, "w") do |file|
          file.puts(@board.to_s)
        end
      else
        $stderr.puts("Warning: don't exists the file: #{@file_name}")
      end<commit_msg>Fix a typo in the warning message<commit_after>require "gosu"
require "grid_pattern_editor/base"
require "grid_pattern_editor/z_order"
require "grid_pattern_editor/board"

module GridPatternEditor
  class Window < Gosu::Window
    TEXTS = [
      "0",
      "1",
      "2",
    ]

    attr_reader :images, :texts

    def initialize(file_name=nil, width=800, height=600)
      super(width, height, false)
      self.caption = "GridPatternEditor"
      init_images
      @board = Board.new(self, width, height)
      @file_name = file_name
    end

    def update
    end

    def draw
      @board.draw
    end

    def button_down(id)
      case id
      when Gosu::MsLeft
        clicked_cell = @board.click
        return unless clicked_cell
        index = TEXTS.index(clicked_cell.text)
        index += 1
        index %= TEXTS.size
        clicked_cell.set_image_from_text(TEXTS[index])
      when Gosu::KbEnter, Gosu::KbReturn
        write_data
      when Gosu::KbR
        @board.reset
      when Gosu::KbEscape
        close
      end
    end

    def needs_cursor?
      true
    end

    private
    def init_images
      @images = {}
      TEXTS.each do |text|
        image_path = File.join(images_dir, "#{text}.png")
        next unless File.exist?(image_path)
        image = Gosu::Image.new(self, image_path, false)
        @images[text] = image
      end
    end

    def images_dir
      File.join(media_dir, "images")
    end

    def media_dir
      base_dir = File.expand_path(File.join(__FILE__, "..", "..", ".."))
      File.join(base_dir, "media")
    end

    def write_data
      if @file_name.nil?
        puts(@board.to_s)
      elsif File.exist?(@file_name)
        File.open(@file_name, "w") do |file|
          file.puts(@board.to_s)
        end
      else
        $stderr.puts("Warning: don't exist the file: #{@file_name}")
      end
{'fraction_non_alphanumeric': 0.10707559860102232, 'fraction_numerical': 0.0053806833467850415, 'mean_word_length': 2.164255319148936, 'pattern_counts': {'":': 0, '<': 5, '<?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': '4419705', 'n_tokens_mistral': 1270, 'n_tokens_neox': 1235, 'n_words': 269}