<commit_before>package main

import (
	"io/ioutil"
	"os"
	"os/signal"
	"syscall"

	"github.com/codegangsta/cli"
	"github.com/ejholmes/chip8"
	"github.com/nsf/termbox-go"
)

var cmdRun = cli.Command{
	Name:   "run",
	Usage:  "Run a chip8 program",
	Action: runRun,
}

func runRun(c *cli.Context) {
	if !c.Args().Present() {
		cli.ShowAppHelp(c)
		return
	}

	// Read program.
	program, err := ioutil.ReadFile(c.Args().First())
	must(err)

	// Initialize peripherals.
	d, err := chip8.NewTermboxDisplay(
		termbox.ColorDefault, // Foreground
		termbox.ColorDefault, // Background
	)
	defer d.Close()
	must(err)
	k := chip8.NewTermboxKeypad()

	// Initialize CPU.
	cpu, err := chip8.NewCPU(nil)
	must(err)
	cpu.Graphics.Display = d
	cpu.Keypad = k

	// Load program.
	_, err = cpu.LoadBytes(program)
	must(err)

	sig := make(chan os.Signal)
	signal.Notify(sig, os.Interrupt, syscall.SIGTERM)
	go func() {
		<-sig
		cpu.Stop()
	}()

	// Run it.
	err = cpu.Run()<commit_msg>Add --log and --clock flags.<commit_after>package main

import (
	"io/ioutil"
	"log"
	"os"
	"os/signal"
	"syscall"
	"time"

	"github.com/codegangsta/cli"
	"github.com/ejholmes/chip8"
	"github.com/nsf/termbox-go"
)

var cmdRun = cli.Command{
	Name:   "run",
	Usage:  "Run a chip8 program",
	Action: runRun,
	Flags: []cli.Flag{
		cli.StringFlag{
			Name:  "log",
			Usage: "If provided, specifies a log file to write debug output to.",
		},
		cli.IntFlag{
			Name:  "clock",
			Usage: "Clock speed, in hz, to run at.",
			Value: int(chip8.DefaultClockSpeed),
		},
	},
}

func runRun(c *cli.Context) {
	// Initialize peripherals.
	d, err := chip8.NewTermboxDisplay(
		termbox.ColorDefault, // Foreground
		termbox.ColorDefault, // Background
	)
	defer d.Close()
	must(err)
	k := chip8.NewTermboxKeypad()

	// Initialize CPU.
	cpu, err := chip8.NewCPU(&chip8.Options{
		ClockSpeed: time.Duration(c.Int("clock")),
	})
	must(err)
	cpu.Graphics.Display = d
	cpu.Keypad = k

	// If a log file is specified, create a logger and add it to the CPU.
	if fname := c.String("log"); fname != "" {
		f, err := os.Create(fname)
		must(err)

		cpu.Logger = log.New(f, "", 0)
	}

	if c.Args().Present() {
		// Read program.
		program, err := ioutil.ReadFile(c.Args().First())
		must(err)

		// Load program.
		_, err = cpu.LoadBytes(program)
		must(err)
	} else {
		_, err = cpu.Load(os.Stdin)
		must(err)
	}

	sig := make(chan os.Signal)
	signal.Notify(sig, os.Interrupt, syscall.SIGTERM)
	go func() {
		<-sig
		cpu.Stop()
	}()

	// Run it.
	err = cpu.Run()
{'fraction_non_alphanumeric': 0.17105263157894737, 'fraction_numerical': 0.00518341307814992, 'mean_word_length': 3.2168067226890757, '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': '5956018', 'n_tokens_mistral': 1080, 'n_tokens_neox': 1012, 'n_words': 221}