<commit_before>require 'curses'
module Sudoku
  class SudokuSolver < Thor
    desc "read", "Reads a sudoku and prints it"
    method_option :file, aliases: "-f", desc: "Read from a file and print", required: false
    def read(*args)
      string = read_file(*args)
      grid = Grid.parse(string)
      puts grid.to_s
    end

    desc "solve", "Reads a sudoku and solves it"
    method_option :file, aliases: "-f", desc: "Read from a file and solve", required: false
    def solve(*args)
      if path = options[:file]
        begin
          file = File.open(path)
          grid = Grid.parse(file.read)
          solver = Solver.new(grid, &print_method)
          result = solver.solve
          window.clear
          window << "Solving took: #{solver.total_time} seconds\n"
          window << "Sudoku:\n"
          window << solver.solved_grids.first
        rescue Exception => e
          window.clear
          window << "Something went wrong, #{e.message}"
        end
      end
      window.getch
      window.close
    end

    private

    def parse_multiple(file)
      content = file.read
      content.split("=\n").map {|grid| Grid.parse(grid)}
    end

    def read_file(*args)
      path = options[:file]
      file = File.open(path)
      file.read
    end

    def window
      @window ||= init_window
    end

    def init_window
      win = Curses::Window.new(100,100,0,0)
      win.refresh
      win
    end

    def print_method
      ->(grid) { window.clear; window << grid.to_s; window.refresh }
    end
  end
end
<commit_msg>Amend command line utility to work with new callback strategy<commit_after>require 'curses'
module Sudoku
  class SudokuSolver < Thor
    desc "read", "Reads a sudoku and prints it"
    method_option :file, aliases: "-f", desc: "Read from a file and print", required: false
    def read(*args)
      string = read_file(*args)
      grid = Grid.parse(string)
      puts grid.to_s
    end

    desc "solve", "Reads a sudoku and solves it"
    method_option :file, aliases: "-f", desc: "Read from a file and solve", required: false
    def solve(*args)
      if path = options[:file]
        begin
          file = File.open(path)
          grid = Grid.parse(file.read)
          solver = Solver.new(grid)
          result = solver.solve(&print_method)
          window.clear
          window << "Solving took: #{solver.total_time} seconds\n"
          if result
            window << "Solved sudoku:\n"
            window << solver.solved_grids.first.to_s
          else
            window << "Could not find an answer"
          end
        rescue Exception => e
          window.clear
          window << "Something went wrong, #{e.message}"
        end
      end
      window.getch
      window.close
    end

    private

    def parse_multiple(file)
      content = file.read
      content.split("=\n").map {|grid| Grid.parse(grid)}
    end

    def read_file(*args)
      path = options[:file]
      file = File.open(path)
      file.read
    end

    def window
      @window ||= init_window
    end

    def init_window
      win = Curses::Window.new(100,100,0,0)
      win.refresh
      win
    end

    def print_method
      ->(grid) { window.clear; window << grid.to_s; window.refresh }
    end
  end
end

{'fraction_non_alphanumeric': 0.10526315789473684, 'fraction_numerical': 0.004895960832313341, 'mean_word_length': 2.049440298507463, 'pattern_counts': {'":': 0, '<': 27, '<?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': '4120555', 'n_tokens_mistral': 1025, 'n_tokens_neox': 1004, 'n_words': 291}