<commit_before>package main

import (
	"strings"

	"github.com/jroimartin/gocui"
)

func editor(v *gocui.View, key gocui.Key, ch rune, mod gocui.Modifier) {
	state.HideHelp = true

	if state.Mode == modeEscape {
		escEditor(v, key, ch, mod)
		return
	}

	if ch != 0 && mod == 0 {
		v.EditWrite(ch)
	}

	switch key {
	case gocui.KeyEsc:
		state.Mode = modeEscape
		state.KeepAutoscrolling = true

	// Space, backspace, Del
	case gocui.KeySpace:
		v.EditWrite(' ')
	case gocui.KeyBackspace, gocui.KeyBackspace2:
		v.EditDelete(true)
		moveAhead(v)
	case gocui.KeyDelete:
		v.EditDelete(false)

	// Cursor movement
	case gocui.KeyArrowLeft:
		v.MoveCursor(-1, 0, false)
		moveAhead(v)
	case gocui.KeyArrowRight:
		x, _ := v.Cursor()
		x2, _ := v.Origin()
		x += x2
		buf := v.Buffer()
		// I don't know really how this works, this was mostly obtained through trial
		// and error. Anyway, this system impedes going on a newline by moving right.
		// This is usually possible because once you write something to the buffer
		// it automatically adds " \n", which is two characters. Sooo yeah.
		if buf != "" && len(buf) > (x+2) {
			v.MoveCursor(1, 0, false)
		}

	// Insert/Overwrite
	case gocui.KeyInsert:
		if state.Mode == modeInsert {
			state.Mode = modeOverwrite
		} else {
			state.Mode = modeInsert
		}
		v.Overwrite = state.Mode == modeOverwrite

	// History browse
	case gocui.KeyArrowDown:
		n := state.BrowseActions(-1)
		setText(v, n)
	case gocui.KeyArrowUp:
		n := state.BrowseActions(1)
		setText(v, n)

	case gocui.KeyEnter:
		buf := v.Buffer()
		v.Clear()
		v.SetCursor(0, 0)

		if buf != "" {
			buf = buf[:len(buf)-2]
		}
		if strings.TrimSpace(buf) != "" {
			state.PushAction(buf)
			state.ActionIndex = -1
		}

		enterActions[state.Mode](buf)
	}
}

func setText(v *gocui.View, text string) {
	v.Clear()
	// Why are we doing this? Because normally when you write a line
	// gocui adds " \n" at the end of it. Whe clearing and adding, though,
	// the space isn't added back.
	v.Write([]byte(text + " "))
	v.SetCursor(len(text), 0)
}

// moveAhead displays the next 10 characters when moving backwards,
// in order to see where we're moving or what we're deleting.
func moveAhead(v *gocui.View) {
	cX, _ := v.Cursor()
	oX, _ := v.Origin()
	if cX < 10 && oX > 0 {
		newOX := oX - 10
		forward := 10<commit_msg>Remove hacks about buffer being 2 bytes longer than it is<commit_after>package main

import (
	"strings"

	"github.com/jroimartin/gocui"
)

func editor(v *gocui.View, key gocui.Key, ch rune, mod gocui.Modifier) {
	state.HideHelp = true

	if state.Mode == modeEscape {
		escEditor(v, key, ch, mod)
		return
	}

	if ch != 0 && mod == 0 {
		v.EditWrite(ch)
	}

	switch key {
	case gocui.KeyEsc:
		state.Mode = modeEscape
		state.KeepAutoscrolling = true

	// Space, backspace, Del
	case gocui.KeySpace:
		v.EditWrite(' ')
	case gocui.KeyBackspace, gocui.KeyBackspace2:
		v.EditDelete(true)
		moveAhead(v)
	case gocui.KeyDelete:
		v.EditDelete(false)

	// Cursor movement
	case gocui.KeyArrowLeft:
		v.MoveCursor(-1, 0, false)
		moveAhead(v)
	case gocui.KeyArrowRight:
		x, _ := v.Cursor()
		x2, _ := v.Origin()
		x += x2
		buf := v.Buffer()
		// I don't know really how this works, this was mostly obtained through trial
		// and error. Anyway, this system impedes going on a newline by moving right.
		// This is usually possible because once you write something to the buffer
		// it automatically adds " \n", which is two characters. Sooo yeah.
		if buf != "" && len(buf) > (x+2) {
			v.MoveCursor(1, 0, false)
		}

	// Insert/Overwrite
	case gocui.KeyInsert:
		if state.Mode == modeInsert {
			state.Mode = modeOverwrite
		} else {
			state.Mode = modeInsert
		}
		v.Overwrite = state.Mode == modeOverwrite

	// History browse
	case gocui.KeyArrowDown:
		n := state.BrowseActions(-1)
		setText(v, n)
	case gocui.KeyArrowUp:
		n := state.BrowseActions(1)
		setText(v, n)

	case gocui.KeyEnter:
		buf := v.Buffer()
		v.Clear()
		v.SetCursor(0, 0)

		if buf != "" {
			buf = buf[:len(buf)-1]
		}
		if strings.TrimSpace(buf) != "" {
			state.PushAction(buf)
			state.ActionIndex = -1
		}

		enterActions[state.Mode](buf)
	}
}

func setText(v *gocui.View, text string) {
	v.Clear()
	v.Write([]byte(text))
	v.SetCursor(len(text), 0)
}

// moveAhead displays the next 10 characters when moving backwards,
// in order to see where we're moving or what we're deleting.
func moveAhead(v *gocui.View) {
	cX, _ := v.Cursor()
	oX, _ := v.Origin()
	if cX < 10 && oX > 0 {
		newOX := oX - 10
		forward := 10
{'fraction_non_alphanumeric': 0.1363435148295706, 'fraction_numerical': 0.011730854360336431, 'mean_word_length': 3.1458715596330276, 'pattern_counts': {'":': 0, '<': 5, '<?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': '5720307', 'n_tokens_mistral': 1887, 'n_tokens_neox': 1724, 'n_words': 471}