hoist and duplicate a conditional

I'm duplicating the bounds check when drawing cursor and search
highlight because they're separate concerns and require subtly different
logic.
This commit is contained in:
Kartik K. Agaram 2023-07-31 08:24:15 -07:00
parent 484b76f5c6
commit f7f42b0bef
2 changed files with 10 additions and 6 deletions

View File

@ -53,8 +53,8 @@ function Text.draw(State, line_index, y, startpos, hide_cursor)
end
if not hide_cursor and line_index == State.cursor1.line then
-- render search highlight or cursor
if pos <= State.cursor1.pos and pos + frag_len >= State.cursor1.pos then
if State.search_term then
if State.search_term then
if pos <= State.cursor1.pos and pos + frag_len >= State.cursor1.pos then
local data = State.lines[State.cursor1.line].data
local cursor_offset = Text.offset(data, State.cursor1.pos)
if data:sub(cursor_offset, cursor_offset+#State.search_term-1) == State.search_term then
@ -62,7 +62,9 @@ function Text.draw(State, line_index, y, startpos, hide_cursor)
App.color(Text_color)
love.graphics.print(State.search_term, State.left+lo_px,y)
end
elseif Focus == 'edit' then
end
elseif Focus == 'edit' then
if pos <= State.cursor1.pos and pos + frag_len >= State.cursor1.pos then
Text.draw_cursor(State, State.left+Text.x(screen_line, State.cursor1.pos-pos+1), y)
end
end

View File

@ -31,8 +31,8 @@ function Text.draw(State, line_index, y, startpos)
App.screen.print(screen_line, State.left,y)
if line_index == State.cursor1.line then
-- render search highlight or cursor
if pos <= State.cursor1.pos and pos + frag_len >= State.cursor1.pos then
if State.search_term then
if State.search_term then
if pos <= State.cursor1.pos and pos + frag_len >= State.cursor1.pos then
local data = State.lines[State.cursor1.line].data
local cursor_offset = Text.offset(data, State.cursor1.pos)
if data:sub(cursor_offset, cursor_offset+#State.search_term-1) == State.search_term then
@ -40,7 +40,9 @@ function Text.draw(State, line_index, y, startpos)
App.color(Text_color)
love.graphics.print(State.search_term, State.left+lo_px,y)
end
else
end
else
if pos <= State.cursor1.pos and pos + frag_len >= State.cursor1.pos then
Text.draw_cursor(State, State.left+Text.x(screen_line, State.cursor1.pos-pos+1), y)
end
end