<commit_before>package gui

import (
	"log"
	"runtime"
	"unicode"

	"github.com/felixangell/phi/cfg"
	"github.com/felixangell/strife"
	"github.com/veandco/go-sdl2/sdl"
)

// View is an array of buffers basically.
type View struct {
	BaseComponent

	conf           *cfg.TomlConfig
	buffers        map[int]*BufferPane
	focusedBuff    int
	commandPalette *CommandPalette
}

func NewView(width, height int, conf *cfg.TomlConfig) *View {
	view := &View{
		conf:    conf,
		buffers: map[int]*BufferPane{},
	}

	view.Translate(width, height)
	view.Resize(width, height)

	view.commandPalette = NewCommandPalette(*conf, view)
	view.UnfocusBuffers()

	return view
}

func (n *View) hidePalette() {
	p := n.commandPalette
	p.clearInput()
	p.SetFocus(false)

	// set focus to the buffer
	// that invoked the cmd palette
	if p.parentBuff != nil {
		p.parentBuff.SetFocus(true)
		n.focusedBuff = p.parentBuff.index
	}

	// remove focus from palette
	p.buff.SetFocus(false)
}

func (n *View) focusPalette(buff *Buffer) {
	p := n.commandPalette
	p.SetFocus(true)

	// focus the command palette
	p.buff.SetFocus(true)

	// remove focus from the buffer
	// that invoked the command palette
	p.parentBuff = buff
}

func (n *View) UnfocusBuffers() {
	// clear focus from buffers
	for _, buffPane := range n.buffers {
		buffPane.Buff.SetFocus(false)
	}
}

func sign(dir int) int {
	if dir > 0 {
		return 1
	} else if dir < 0 {
		return -1
	}
	return 0
}

func (n *View) removeBuffer(index int) {
	log.Println("Removing buffer index:", index)
	delete(n.buffers, index)

	// only resize the buffers if we have
	// some remaining in the window
	if len(n.buffers) > 0 {
		bufferWidth := n.w / len(n.buffers)

		// translate all the components accordingly.
		for i, buffPane := range n.buffers {
			buffPane.Buff.Resize(bufferWidth, n.h)
			buffPane.Buff.SetPosition(bufferWidth*i, 0)
		}
	}

}

func (n *View) ChangeFocus(dir int) {
	prevBuff, _ := n.buffers[n.focusedBuff]

	if dir == -1 {
		n.focusedBuff--
	} else if dir == 1 {
		n.focusedBuff++
	}

	if n.focusedBuff < 0 {
		n.focusedBuff = len(n.buffers) - 1
	} else if n.focusedBuff >= len(n.buffers) {
		n.focusedBuff = 0
	}

	if prevBuff != nil {
		prevBuff.Buff.SetFocus(false)
	}

	if buffPane, ok := n.buffers[n.focusedBuff]; ok {
		buffPane.Buff.SetFocus(true)
	}
}

func (n *View) getCurrentBuff() *Buffer {
	if buffPane, ok := n.buffers[n.focusedBuff]; ok {
		return buffPane.Buff
	}
	return nil
}

func (n *View) OnInit() {
}

func (n *View) OnUpdate() bool {
	dirty := false

	CONTROL_DOWN = strife.KeyPressed(sdl.K_LCTRL) || strife.KeyPressed(sdl.K_RCTRL)
	SUPER_DOWN = strife.KeyPressed(sdl.K_LGUI) || strife.KeyPressed(sdl.K_RGUI)

	shortcutName := "ctrl"
	source := cfg.Shortcuts.Controls

	if strife.PollKeys() && (SUPER_DOWN || CONTROL_DOWN) {
		if runtime.GOOS == "darwin" {
			if SUPER_DOWN {
				source = cfg.Shortcuts.Supers
				shortcutName = "super"
			} else if CONTROL_DOWN {
				source = cfg.Shortcuts.Controls
				shortcutName = "control"
			}
		} else {
			source = cfg.Shortcuts.Supers
		}

		r := rune(strife.PopKey())

		if r == sdl.K_F12 {
			DEBUG_MODE = !DEBUG_MODE
		}

		left := 1073741904
		right := 1073741903
		up := 1073741906
		down := 1073741905

		// map to left/right/etc.
		// FIXME
		var key string
		switch int(r) {
		case left:
			key = "left"
		case right:
			key = "right"
		case up:
			key = "up"
		case down:<commit_msg>remove magic numbers<commit_after>package gui

import (
	"log"
	"runtime"
	"unicode"

	"github.com/felixangell/phi/cfg"
	"github.com/felixangell/strife"
	"github.com/veandco/go-sdl2/sdl"
)

// View is an array of buffers basically.
type View struct {
	BaseComponent

	conf           *cfg.TomlConfig
	buffers        map[int]*BufferPane
	focusedBuff    int
	commandPalette *CommandPalette
}

func NewView(width, height int, conf *cfg.TomlConfig) *View {
	view := &View{
		conf:    conf,
		buffers: map[int]*BufferPane{},
	}

	view.Translate(width, height)
	view.Resize(width, height)

	view.commandPalette = NewCommandPalette(*conf, view)
	view.UnfocusBuffers()

	return view
}

func (n *View) hidePalette() {
	p := n.commandPalette
	p.clearInput()
	p.SetFocus(false)

	// set focus to the buffer
	// that invoked the cmd palette
	if p.parentBuff != nil {
		p.parentBuff.SetFocus(true)
		n.focusedBuff = p.parentBuff.index
	}

	// remove focus from palette
	p.buff.SetFocus(false)
}

func (n *View) focusPalette(buff *Buffer) {
	p := n.commandPalette
	p.SetFocus(true)

	// focus the command palette
	p.buff.SetFocus(true)

	// remove focus from the buffer
	// that invoked the command palette
	p.parentBuff = buff
}

func (n *View) UnfocusBuffers() {
	// clear focus from buffers
	for _, buffPane := range n.buffers {
		buffPane.Buff.SetFocus(false)
	}
}

func sign(dir int) int {
	if dir > 0 {
		return 1
	} else if dir < 0 {
		return -1
	}
	return 0
}

func (n *View) removeBuffer(index int) {
	log.Println("Removing buffer index:", index)
	delete(n.buffers, index)

	// only resize the buffers if we have
	// some remaining in the window
	if len(n.buffers) > 0 {
		bufferWidth := n.w / len(n.buffers)

		// translate all the components accordingly.
		for i, buffPane := range n.buffers {
			buffPane.Buff.Resize(bufferWidth, n.h)
			buffPane.Buff.SetPosition(bufferWidth*i, 0)
		}
	}

}

func (n *View) ChangeFocus(dir int) {
	prevBuff, _ := n.buffers[n.focusedBuff]

	if dir == -1 {
		n.focusedBuff--
	} else if dir == 1 {
		n.focusedBuff++
	}

	if n.focusedBuff < 0 {
		n.focusedBuff = len(n.buffers) - 1
	} else if n.focusedBuff >= len(n.buffers) {
		n.focusedBuff = 0
	}

	if prevBuff != nil {
		prevBuff.Buff.SetFocus(false)
	}

	if buffPane, ok := n.buffers[n.focusedBuff]; ok {
		buffPane.Buff.SetFocus(true)
	}
}

func (n *View) getCurrentBuff() *Buffer {
	if buffPane, ok := n.buffers[n.focusedBuff]; ok {
		return buffPane.Buff
	}
	return nil
}

func (n *View) OnInit() {
}

func (n *View) OnUpdate() bool {
	dirty := false

	CONTROL_DOWN = strife.KeyPressed(sdl.K_LCTRL) || strife.KeyPressed(sdl.K_RCTRL)
	SUPER_DOWN = strife.KeyPressed(sdl.K_LGUI) || strife.KeyPressed(sdl.K_RGUI)

	shortcutName := "ctrl"
	source := cfg.Shortcuts.Controls

	if strife.PollKeys() && (SUPER_DOWN || CONTROL_DOWN) {
		if runtime.GOOS == "darwin" {
			if SUPER_DOWN {
				source = cfg.Shortcuts.Supers
				shortcutName = "super"
			} else if CONTROL_DOWN {
				source = cfg.Shortcuts.Controls
				shortcutName = "control"
			}
		} else {
			source = cfg.Shortcuts.Supers
		}

		r := rune(strife.PopKey())

		if r == sdl.K_F12 {
			DEBUG_MODE = !DEBUG_MODE
		}

		left := sdl.K_LEFT
		right := sdl.K_RIGHT
		up := sdl.K_UP
		down := sdl.K_DOWN

		// map to left/right/etc.
		// FIXME
		var key string
		switch int(r) {
		case left:
			key = "left"
		case right:
			key = "right"
		case up:
			key = "up"
		case down:
{'fraction_non_alphanumeric': 0.13241338813857897, 'fraction_numerical': 0.010275983558426306, 'mean_word_length': 3.2133580705009277, 'pattern_counts': {'":': 0, '<': 7, '<?xml version=': 0, '>': 9, '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': '5161658', 'n_tokens_mistral': 2881, 'n_tokens_neox': 2596, 'n_words': 625}