<commit_before>// Copyright 2014 <NAME>.  All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package chip8

import "github.com/nsf/termbox-go"

const (
	GraphicsWidth  = 64 // Pixels
	GraphicsHeight = 32 // Pixels
)

// Display represents the output display for the CHIP-8 graphics array.
type Display interface {
	// Turn on the display and do any initialization.
	Init() error

	// Turn off the display and cleanup.
	Close()

	// Render should render the current graphics array to the display.
	Render(*Graphics) error
}

// Graphics represents the graphics array for the CHIP-8.
type Graphics struct {
	// The raw pixels of the graphics array.
	Pixels [GraphicsWidth * GraphicsHeight]byte

	// The display to render to. The nil value is the DefaultDisplay.
	Display
}

// DrawSprite draws a sprite to the graphics array starting at coording x, y.
// If there is a collision, WriteSprite returns true.
func (g *Graphics) WriteSprite(sprite []byte, x, y byte) (collision bool) {
	n := len(sprite)

	for yl := 0; yl < n; yl++ {
		// A row of sprite data.
		r := sprite[yl]

		for xl := 0; xl < 8; xl++ {
			// This represents a mask for the bit that we
			// care about for this coordinate.
			i := 0x80 >> byte(xl)

			// The X position for this pixel
			xp := uint16(x) + uint16(xl)
			if xp >= GraphicsWidth {
				xp = xp - GraphicsWidth
			}

			// The Y position for this pixel
			yp := uint16(y) + uint16(yl)
			if yp >= GraphicsHeight {
				yp = yp - GraphicsHeight
			}

			if g.Set(xp, yp, (r&byte(i)) == byte(i)) {
				collision = true
			}
		}
	}

	return
}

// Clear clears the display.
func (g *Graphics) Clear() {
	g.EachPixel(func(_, _ uint16, addr int) {
		g.Pixels[addr] = 0
	})
}

// Draw draws the graphics array to the Display.
func (g *Graphics) Draw() error {
	return g.display().Render(g)
}

// EachPixel yields each pixel in the graphics array to fn.
func (g *Graphics) EachPixel(fn func(x, y uint16, addr int)) {
	for y := 0; y < GraphicsHeight-1; y++ {
		for x := 0; x < GraphicsWidth-1; x++ {
			a := y*GraphicsWidth + x
			fn(uint16(x), uint16(y), a)
		}
	}
}

// Set sets the value of the pixel at the coordinate.
func (g *Graphics) Set(x, y uint16, set bool) (collision bool) {
	a := x + y*GraphicsWidth

	if g.Pixels[a] == 0x01 {
		collision = true
	}

	var v byte
	if set {
		v = 0x01
	}

	g.Pixels[a] = g.Pixels[a] ^ v

	return
}

func (g *Graphics) display() Display {
	if g.Display == nil {
		return DefaultDisplay
	}

	return g.Display
}

var (
	fg = termbox.ColorBlack
	bg = termbox.ColorDefault
)

// display is an implementation of the Display interface that renders
// the graphics array to the terminal.
type display struct{}

func (d *display) Init() error {
	if err := termbox.Init(); err != nil {
		return err
	}

	termbox.HideCursor()

	if err := termbox.Clear(bg, bg); err != nil {
		return err
	}

	return termbox.Flush()
}

func (d *display) Close() {
	termbox.Close()
}

func (d *display) Render(g *Graphics) error {
	g.EachPixel(func(x, y uint16, addr int) {
		v := ' '

		if g.Pixels[addr] == 0x01 {
			v = '*'
		}

		termbox.SetCell(
			int(x),
			int(y),
			v,
			fg,
			bg,
		)
	})

	return termbox.Flush()
}
<commit_msg>Cleanup.<commit_after>// Copyright 2014 <NAME>.  All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

package chip8

import "github.com/nsf/termbox-go"

const (
	GraphicsWidth  = 64 // Pixels
	GraphicsHeight = 32 // Pixels
)

// Display represents the output display for the CHIP-8 graphics array.
type Display interface {
	// Turn on the display and do any initialization.
	Init() error

	// Turn off the display and cleanup.
	Close()

	// Render should render the current graphics array to the display.
	Render(*Graphics) error
}

// Graphics represents the graphics array for the CHIP-8.
type Graphics struct {
	// The raw pixels of the graphics array.
	Pixels [GraphicsWidth * GraphicsHeight]byte

	// The display to render to. The nil value is the DefaultDisplay.
	Display
}

// DrawSprite draws a sprite to the graphics array starting at coording x, y.
// If there is a collision, WriteSprite returns true.
func (g *Graphics) WriteSprite(sprite []byte, x, y byte) (collision bool) {
	n := len(sprite)

	for yl := 0; yl < n; yl++ {
		// A row of sprite data.
		r := sprite[yl]

		for xl := 0; xl < 8; xl++ {
			// This represents a mask for the bit that we
			// care about for this coordinate.
			i := 0x80 >> byte(xl)

			// Whether the bit is set or not.
			set := (r & byte(i)) == byte(i)

			// The X position for this pixel
			xp := uint16(x) + uint16(xl)
			if xp >= GraphicsWidth {
				xp = xp - GraphicsWidth
			}

			// The Y position for this pixel
			yp := uint16(y) + uint16(yl)
			if yp >= GraphicsHeight {
				yp = yp - GraphicsHeight
			}

			if g.Set(xp, yp, set) {
				collision = true
			}
		}
	}

	return
}

// Clear clears the display.
func (g *Graphics) Clear() {
	g.EachPixel(func(_, _ uint16, addr int) {
		g.Pixels[addr] = 0
	})
}

// Draw draws the graphics array to the Display.
func (g *Graphics) Draw() error {
	return g.display().Render(g)
}

// EachPixel yields each pixel in the graphics array to fn.
func (g *Graphics) EachPixel(fn func(x, y uint16, addr int)) {
	for y := 0; y < GraphicsHeight-1; y++ {
		for x := 0; x < GraphicsWidth-1; x++ {
			a := y*GraphicsWidth + x
			fn(uint16(x), uint16(y), a)
		}
	}
}

// Set sets the value of the pixel at the coordinate.
func (g *Graphics) Set(x, y uint16, set bool) (collision bool) {
	a := x + y*GraphicsWidth

	if g.Pixels[a] == 0x01 {
		collision = true
	}

	var v byte
	if set {
		v = 0x01
	}

	g.Pixels[a] = g.Pixels[a] ^ v

	return
}

func (g *Graphics) display() Display {
	if g.Display == nil {
		return DefaultDisplay
	}

	return g.Display
}

var (
	fg = termbox.ColorBlack
	bg = termbox.ColorDefault
)

// display is an implementation of the Display interface that renders
// the graphics array to the terminal.
type display struct{}

func (d *display) Init() error {
	if err := termbox.Init(); err != nil {
		return err
	}

	termbox.HideCursor()

	if err := termbox.Clear(bg, bg); err != nil {
		return err
	}

	return termbox.Flush()
}

func (d *display) Close() {
	termbox.Close()
}

func (d *display) Render(g *Graphics) error {
	g.EachPixel(func(x, y uint16, addr int) {
		v := ' '

		if g.Pixels[addr] == 0x01 {
			v = '*'
		}

		termbox.SetCell(
			int(x),
			int(y),
			v,
			fg,
			bg,
		)
	})

	return termbox.Flush()
}

{'fraction_non_alphanumeric': 0.12734768666972057, 'fraction_numerical': 0.015574896930829134, 'mean_word_length': 2.855208946439082, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, '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': '3454564', 'n_tokens_mistral': 2552, 'n_tokens_neox': 2288, 'n_words': 834}