From f7f42b0befc3fbe6c425cfa8bad18192c2beb926 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Mon, 31 Jul 2023 08:24:15 -0700 Subject: 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. --- source_text.lua | 8 +++++--- text.lua | 8 +++++--- 2 files changed, 10 insertions(+), 6 deletions(-) diff --git a/source_text.lua b/source_text.lua index b3d4963..9e3a9ac 100644 --- a/source_text.lua +++ b/source_text.lua @@ -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 diff --git a/text.lua b/text.lua index 9762365..8da0586 100644 --- a/text.lua +++ b/text.lua @@ -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 -- cgit 1.4.1-2-gfad0