<commit_before>local Class = require 'lib.hump.class'
local Text = getClass 'pud.ui.Text'
local KeyboardEvent = getClass 'pud.event.KeyboardEvent'

local string_len = string.len
local string_sub = string.sub
local format = string.format

-- TextEntry
-- A text frame that gathers keyboard input.
local TextEntry = Class{name='TextEntry',
	inherits=Text,
	function(self, ...)
		Text.construct(self, ...)

		InputEvents:register(self, KeyboardEvent)
	end
}

-- destructor
function TextEntry:destroy()
	self._isEnteringText = nil

	-- Frame will unregister all InputEvents
	Text.destroy(self)
end

-- override Frame:onRelease()
function TextEntry:onRelease(button, mods)
	if self._pressed then
		if 'l' == button then
			self._isEnteringText = not self._isEnteringText
			if self._isEnteringText then
				self:showCursor()
			else
				self:hideCursor()
			end
		end
	end
end

-- override Frame:onHoverIn()
function TextEntry:onHoverIn(x, y)
	if self._isEnteringText then return end
	Text.onHoverIn(self, x, y)
end

-- override Frame:onHoverOut()
function TextEntry:onHoverOut(x, y)
	if self._isEnteringText then return end
	Text.onHoverOut(self, x, y)
end

-- override Frame:switchToNormalStyle()
function TextEntry:switchToNormalStyle()
	if self._isEnteringText then return end
	Text.switchToNormalStyle(self)
end

-- override Frame:switchToHoverStyle()
function TextEntry:switchToHoverStyle()
	if self._isEnteringText then return end
	Text.switchToHoverStyle(self)
end

-- capture text input if in editing mode
function TextEntry:KeyboardEvent(e)
	if not self._isEnteringText then return end

	if self._text then
		-- copy the entire text
		local text = {}
		local numLines = #self._text
		for i=1,numLines do text[i] = self._text[i] end
		
		local lineNum = numLines

		if lineNum < 1 then lineNum = 1 end
		local line = text[lineNum] or ''

		local key = e:getKey()
		switch(key) {
			backspace = function()
				line = string_sub(line, 1, -2)
				if string.len(line) < 1 then
					text[lineNum] = nil
					lineNum = lineNum - 1
					if lineNum < 1 then lineNum = 1 end
					line = text[lineNum] or ''
				end
			end,

			['return'] = function()
				local nextLine = lineNum + 1
				if nextLine > self._maxLines then nextLine = self._maxLines end
				text[nextLine] = text[nextLine] or ''
			end,

			escape = function()
				self._isEnteringText = false
				self:hideCursor()
				self:_handleMouseRelease(love.mouse.getPosition())
			end,

			default = function()
				local unicode = e:getUnicode()
				if unicode then
					line = format('%s%s', line, unicode)
				end<commit_msg>change TextEntry to stop entering text on Return if one line<commit_after>local Class = require 'lib.hump.class'
local Text = getClass 'pud.ui.Text'
local KeyboardEvent = getClass 'pud.event.KeyboardEvent'

local string_len = string.len
local string_sub = string.sub
local format = string.format

-- TextEntry
-- A text frame that gathers keyboard input.
local TextEntry = Class{name='TextEntry',
	inherits=Text,
	function(self, ...)
		Text.construct(self, ...)

		InputEvents:register(self, KeyboardEvent)
	end
}

-- destructor
function TextEntry:destroy()
	self._isEnteringText = nil

	-- Frame will unregister all InputEvents
	Text.destroy(self)
end

-- override Frame:onRelease()
function TextEntry:onRelease(button, mods)
	if self._pressed then
		if 'l' == button then
			self._isEnteringText = not self._isEnteringText
			if self._isEnteringText then
				self:showCursor()
			else
				self:hideCursor()
			end
		end
	end
end

-- override Frame:onHoverIn()
function TextEntry:onHoverIn(x, y)
	if self._isEnteringText then return end
	Text.onHoverIn(self, x, y)
end

-- override Frame:onHoverOut()
function TextEntry:onHoverOut(x, y)
	if self._isEnteringText then return end
	Text.onHoverOut(self, x, y)
end

-- override Frame:switchToNormalStyle()
function TextEntry:switchToNormalStyle()
	if self._isEnteringText then return end
	Text.switchToNormalStyle(self)
end

-- override Frame:switchToHoverStyle()
function TextEntry:switchToHoverStyle()
	if self._isEnteringText then return end
	Text.switchToHoverStyle(self)
end

-- capture text input if in editing mode
function TextEntry:KeyboardEvent(e)
	if not self._isEnteringText then return end

	if self._text then
		-- copy the entire text
		local text = {}
		local numLines = #self._text
		for i=1,numLines do text[i] = self._text[i] end
		
		local lineNum = numLines

		if lineNum < 1 then lineNum = 1 end
		local line = text[lineNum] or ''

		local _stopEntering = function()
			self._isEnteringText = false
			self:hideCursor()
			self:_handleMouseRelease(love.mouse.getPosition())
		end

		local _nextLine = function()
			if self._maxLines == 1 then
				_stopEntering()
			else
				local nextLine = lineNum + 1
				if nextLine > self._maxLines then nextLine = self._maxLines end
				text[nextLine] = text[nextLine] or ''
			end
		end

		local key = e:getKey()
		switch(key) {
			backspace = function()
				line = string_sub(line, 1, -2)
				if string.len(line) < 1 then
					text[lineNum] = nil
					lineNum = lineNum - 1
					if lineNum < 1 then lineNum = 1 end
					line = text[lineNum] or ''
				end
			end,

			['return'] = _nextLine,
			kpenter = _nextLine,
			escape = _stopEntering,

			default = function()
				local unicode = e:getUnicode()
				if unicode then
					line = format('%s%s', line, unicode)
				end
{'fraction_non_alphanumeric': 0.09942174967356836, 'fraction_numerical': 0.00391717963066592, 'mean_word_length': 3.7662222222222224, 'pattern_counts': {'":': 0, '<': 9, '<?xml version=': 0, '>': 5, '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': '2444785', 'n_tokens_mistral': 1993, 'n_tokens_neox': 1747, 'n_words': 504}