Merge lines.love

This commit is contained in:
Kartik K. Agaram 2023-12-29 14:29:18 -08:00
commit 6bd2086b51
6 changed files with 25 additions and 23 deletions

View File

@ -10,7 +10,7 @@ Margin_right = 25
edit = {} edit = {}
-- run in both tests and a real run -- run in both tests and a real run
function edit.initialize_state(top, left, right, font_height, line_height) -- currently always draws to bottom of screen function edit.initialize_state(top, left, right, font, font_height, line_height) -- currently always draws to bottom of screen
local result = { local result = {
lines = {{data=''}}, -- array of strings lines = {{data=''}}, -- array of strings
@ -47,6 +47,7 @@ function edit.initialize_state(top, left, right, font_height, line_height) -- c
cursor_x = 0, cursor_x = 0,
cursor_y = 0, cursor_y = 0,
font = font,
font_height = font_height, font_height = font_height,
line_height = line_height, line_height = line_height,
@ -67,7 +68,7 @@ function edit.initialize_state(top, left, right, font_height, line_height) -- c
search_backup = nil, -- stuff to restore when cancelling search search_backup = nil, -- stuff to restore when cancelling search
} }
return result return result
end -- App.initialize_state end -- edit.initialize_state
function edit.check_locs(State) function edit.check_locs(State)
-- if State is inconsistent (i.e. file changed by some other program), -- if State is inconsistent (i.e. file changed by some other program),
@ -99,6 +100,7 @@ end
-- return y drawn until -- return y drawn until
function edit.draw(State) function edit.draw(State)
love.graphics.setFont(State.font)
App.color(Text_color) App.color(Text_color)
assert(#State.lines == #State.line_cache, ('line_cache is out of date; %d elements when it should be %d'):format(#State.line_cache, #State.lines)) assert(#State.lines == #State.line_cache, ('line_cache is out of date; %d elements when it should be %d'):format(#State.line_cache, #State.lines))
assert(Text.le1(State.screen_top1, State.cursor1), ('screen_top (line=%d,pos=%d) is below cursor (line=%d,pos=%d)'):format(State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos)) assert(Text.le1(State.screen_top1, State.cursor1), ('screen_top (line=%d,pos=%d) is below cursor (line=%d,pos=%d)'):format(State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos))
@ -384,7 +386,7 @@ end
function edit.update_font_settings(State, font_height) function edit.update_font_settings(State, font_height)
State.font_height = font_height State.font_height = font_height
love.graphics.setFont(love.graphics.newFont(State.font_height)) State.font = love.graphics.newFont(State.font_height)
State.line_height = math.floor(font_height*1.3) State.line_height = math.floor(font_height*1.3)
end end
@ -401,7 +403,8 @@ function edit.initialize_test_state()
15, -- top margin 15, -- top margin
Test_margin_left, Test_margin_left,
App.screen.width - Test_margin_right, App.screen.width - Test_margin_right,
14, -- font height assuming default LÖVE font love.graphics.getFont(),
14,
15) -- line height 15) -- line height
end end

View File

@ -6,7 +6,7 @@
-- functions to render them into the log_render namespace. -- functions to render them into the log_render namespace.
function source.initialize_log_browser_side() function source.initialize_log_browser_side()
Log_browser_state = edit.initialize_state(Margin_top, Editor_state.right + Margin_right + Margin_left, (Editor_state.right+Margin_right)*2, Editor_state.font_height, Editor_state.line_height) Log_browser_state = edit.initialize_state(Margin_top, Editor_state.right + Margin_right + Margin_left, (Editor_state.right+Margin_right)*2, Editor_state.font, Editor_state.font_height, Editor_state.line_height)
Log_browser_state.filename = 'log' Log_browser_state.filename = 'log'
load_from_disk(Log_browser_state) -- TODO: pay no attention to Fold load_from_disk(Log_browser_state) -- TODO: pay no attention to Fold
log_browser.parse(Log_browser_state) log_browser.parse(Log_browser_state)

View File

@ -190,8 +190,8 @@ There's much more I could include here; check out [the LÖVE manual](https://lov
The text-editor widget includes extremely thorough automated tests to give you The text-editor widget includes extremely thorough automated tests to give you
early warning if you break something. early warning if you break something.
* `state = edit.initialize_state(top, left, right, font_height, line_height)` -- * `state = edit.initialize_state(top, left, right, font, font_height, line_height)`
returns an object that can be used to render an interactive editor widget -- returns an object that can be used to render an interactive editor widget
for text starting at `y=top` on the app window, between `x=left` and for text starting at `y=top` on the app window, between `x=left` and
`x=right`. Wraps long lines at word boundaries where possible, or in the `x=right`. Wraps long lines at word boundaries where possible, or in the
middle of words (no hyphenation yet) when it must. middle of words (no hyphenation yet) when it must.

10
run.lua
View File

@ -54,14 +54,14 @@ function print_and_log(s)
end end
function run.load_settings() function run.load_settings()
love.graphics.setFont(love.graphics.newFont(Settings.font_height)) local font = love.graphics.newFont(Settings.font_height)
-- set up desired window dimensions and make window resizable -- set up desired window dimensions and make window resizable
_, _, App.screen.flags = App.screen.size() _, _, App.screen.flags = App.screen.size()
App.screen.flags.resizable = true App.screen.flags.resizable = true
App.screen.width, App.screen.height = Settings.width, Settings.height App.screen.width, App.screen.height = Settings.width, Settings.height
App.screen.resize(App.screen.width, App.screen.height, App.screen.flags) App.screen.resize(App.screen.width, App.screen.height, App.screen.flags)
run.set_window_position_from_settings(Settings) run.set_window_position_from_settings(Settings)
Editor_state = edit.initialize_state(Margin_top, Margin_left, App.screen.width-Margin_right, Settings.font_height, math.floor(Settings.font_height*1.3)) Editor_state = edit.initialize_state(Margin_top, Margin_left, App.screen.width-Margin_right, font, Settings.font_height, math.floor(Settings.font_height*1.3))
Editor_state.filename = Settings.filename Editor_state.filename = Settings.filename
Editor_state.screen_top1 = Settings.screen_top Editor_state.screen_top1 = Settings.screen_top
Editor_state.cursor1 = Settings.cursor Editor_state.cursor1 = Settings.cursor
@ -79,11 +79,9 @@ end
function run.initialize_default_settings() function run.initialize_default_settings()
local font_height = 20 local font_height = 20
love.graphics.setFont(love.graphics.newFont(font_height)) local font = love.graphics.newFont(font_height)
run.initialize_window_geometry() run.initialize_window_geometry()
Editor_state = edit.initialize_state(Margin_top, Margin_left, App.screen.width-Margin_right) Editor_state = edit.initialize_state(Margin_top, Margin_left, App.screen.width-Margin_right, font, font_height, math.floor(font_height*1.3))
Editor_state.font_height = font_height
Editor_state.line_height = math.floor(font_height*1.3)
Settings = run.settings() Settings = run.settings()
end end

View File

@ -114,7 +114,7 @@ end
function source.load_settings() function source.load_settings()
local settings = Settings.source local settings = Settings.source
love.graphics.setFont(love.graphics.newFont(settings.font_height)) local font = love.graphics.newFont(settings.font_height)
-- set up desired window dimensions and make window resizable -- set up desired window dimensions and make window resizable
_, _, App.screen.flags = App.screen.size() _, _, App.screen.flags = App.screen.size()
App.screen.flags.resizable = true App.screen.flags.resizable = true
@ -126,7 +126,7 @@ function source.load_settings()
if Show_log_browser_side then if Show_log_browser_side then
right = App.screen.width/2 - Margin_right right = App.screen.width/2 - Margin_right
end end
Editor_state = edit.initialize_state(Margin_top, Margin_left + Line_number_width*App.width('m'), right, settings.font_height, math.floor(settings.font_height*1.3)) Editor_state = edit.initialize_state(Margin_top, Margin_left + Line_number_width*App.width('m'), right, font, settings.font_height, math.floor(settings.font_height*1.3))
Editor_state.filename = settings.filename Editor_state.filename = settings.filename
Editor_state.filename = basename(Editor_state.filename) -- migrate settings that used full paths; we now support only relative paths within the app Editor_state.filename = basename(Editor_state.filename) -- migrate settings that used full paths; we now support only relative paths within the app
if settings.cursors then if settings.cursors then
@ -152,12 +152,10 @@ end
function source.initialize_default_settings() function source.initialize_default_settings()
local font_height = 20 local font_height = 20
love.graphics.setFont(love.graphics.newFont(font_height)) local font = love.graphics.newFont(font_height)
source.initialize_window_geometry() source.initialize_window_geometry()
Editor_state = edit.initialize_state(Margin_top, Margin_left + Line_number_width*App.width('m'), App.screen.width-Margin_right) Editor_state = edit.initialize_state(Margin_top, Margin_left + Line_number_width*App.width('m'), App.screen.width-Margin_right, font, font_height, math.floor(font_height*1.3))
Editor_state.filename = 'run.lua' Editor_state.filename = 'run.lua'
Editor_state.font_height = font_height
Editor_state.line_height = math.floor(font_height*1.3)
end end
function source.initialize_window_geometry() function source.initialize_window_geometry()

View File

@ -25,7 +25,7 @@ Same_point_distance = 4 -- pixel distance at which two points are considered th
edit = {} edit = {}
-- run in both tests and a real run -- run in both tests and a real run
function edit.initialize_state(top, left, right, font_height, line_height) -- currently always draws to bottom of screen function edit.initialize_state(top, left, right, font, font_height, line_height) -- currently always draws to bottom of screen
local result = { local result = {
-- a line is either text or a drawing -- a line is either text or a drawing
-- a text is a table with: -- a text is a table with:
@ -85,6 +85,7 @@ function edit.initialize_state(top, left, right, font_height, line_height) -- c
current_drawing_mode = 'line', current_drawing_mode = 'line',
previous_drawing_mode = nil, -- extra state for some ephemeral modes like moving/deleting/naming points previous_drawing_mode = nil, -- extra state for some ephemeral modes like moving/deleting/naming points
font = font,
font_height = font_height, font_height = font_height,
line_height = line_height, line_height = line_height,
@ -105,7 +106,7 @@ function edit.initialize_state(top, left, right, font_height, line_height) -- c
search_backup = nil, -- stuff to restore when cancelling search search_backup = nil, -- stuff to restore when cancelling search
} }
return result return result
end -- App.initialize_state end -- edit.initialize_state
function edit.check_locs(State) function edit.check_locs(State)
-- if State is inconsistent (i.e. file changed by some other program), -- if State is inconsistent (i.e. file changed by some other program),
@ -157,6 +158,7 @@ end
function edit.draw(State, hide_cursor, show_line_numbers) function edit.draw(State, hide_cursor, show_line_numbers)
State.button_handlers = {} State.button_handlers = {}
love.graphics.setFont(State.font)
App.color(Text_color) App.color(Text_color)
assert(#State.lines == #State.line_cache, ('line_cache is out of date; %d elements when it should be %d'):format(#State.line_cache, #State.lines)) assert(#State.lines == #State.line_cache, ('line_cache is out of date; %d elements when it should be %d'):format(#State.line_cache, #State.lines))
assert(Text.le1(State.screen_top1, State.cursor1), ('screen_top (line=%d,pos=%d) is below cursor (line=%d,pos=%d)'):format(State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos)) assert(Text.le1(State.screen_top1, State.cursor1), ('screen_top (line=%d,pos=%d) is below cursor (line=%d,pos=%d)'):format(State.screen_top1.line, State.screen_top1.pos, State.cursor1.line, State.cursor1.pos))
@ -552,7 +554,7 @@ end
function edit.update_font_settings(State, font_height) function edit.update_font_settings(State, font_height)
State.font_height = font_height State.font_height = font_height
love.graphics.setFont(love.graphics.newFont(State.font_height)) State.font = love.graphics.newFont(State.font_height)
State.line_height = math.floor(font_height*1.3) State.line_height = math.floor(font_height*1.3)
end end
@ -569,7 +571,8 @@ function edit.initialize_test_state()
15, -- top margin 15, -- top margin
Test_margin_left, Test_margin_left,
App.screen.width - Test_margin_right, App.screen.width - Test_margin_right,
14, -- font height assuming default LÖVE font love.graphics.getFont(),
14,
15) -- line height 15) -- line height
end end