From bc464fe6f1fc99af269c20e87e7be38ec26de2b3 Mon Sep 17 00:00:00 2001 From: "Kartik K. Agaram" Date: Sun, 18 Sep 2022 01:11:23 -0700 Subject: [PATCH] start showing source menu file navigation state graphically I'm a bit leery of going down this road: - If there's a bug in how I render logs graphically that could be extremely misleading. Perhaps this suggests that the code to log things should be significantly simpler than the code that might be debugged. If writing the debug helper requires all my smarts I'm not smart enough to debug using the helper, etc. Given this idea, the fact that I'm copying production code into the logging helper is concerning. - There's a question of what code it's ok for logging helpers to depend on. This is an issue shared with tests. I often implicitly (and without meaning to) assume the presence of some well-tested helpers when writing tests. If those helpers ever break I can get into a rabbit hole of debugging. This problem might be even more insidious with logging helpers that will give me no indication when they break. Still and all, it's cool to see menus in my logs. Let's see if it's useful. --- commands.lua | 58 +++++++++++++++++++++++++++++++++++++++++++++++----- source.lua | 1 + 2 files changed, 54 insertions(+), 5 deletions(-) diff --git a/commands.lua b/commands.lua index 0913733..bf662ec 100644 --- a/commands.lua +++ b/commands.lua @@ -54,10 +54,10 @@ end function source.draw_file_navigator() if File_navigation.num_lines == nil then - File_navigation.num_lines = source.num_lines_for_file_navigator() + File_navigation.num_lines = source.num_lines_for_file_navigator(File_navigation.candidates) end App.color(Menu_background_color) - love.graphics.rectangle('fill', 0,Menu_status_bar_height, App.screen.width, File_navigation.num_lines * Editor_state.line_height) + love.graphics.rectangle('fill', 0,Menu_status_bar_height, App.screen.width, File_navigation.num_lines * Editor_state.line_height + --[[highlight padding]] 2) local x,y = 5, Menu_status_bar_height for i,filename in ipairs(File_navigation.candidates) do if filename == 'source' then @@ -71,10 +71,10 @@ function source.draw_file_navigator() end end -function source.num_lines_for_file_navigator() +function source.num_lines_for_file_navigator(candidates) local result = 1 local x = 5 - for i,filename in ipairs(File_navigation.candidates) do + for i,filename in ipairs(candidates) do local width = App.width(to_text(filename)) if x + width > App.screen.width - 5 then result = result+1 @@ -105,7 +105,7 @@ end function keychord_pressed_on_file_navigator(chord, key) log(2, 'file navigator: '..chord) - log(2, ('cursor initially at %d %s'):format(File_navigation.index, File_navigation.candidates[File_navigation.index])) + log(2, {name='file_navigator_state', files=File_navigation.candidates, index=File_navigation.index}) if chord == 'escape' then Show_file_navigator = false elseif chord == 'return' then @@ -127,6 +127,54 @@ function keychord_pressed_on_file_navigator(chord, key) end end +function log_render.file_navigator_state(o, x,y, w) + -- duplicate structure of source.draw_file_navigator + local num_lines = source.num_lines_for_file_navigator(o.files) + local h = num_lines * Editor_state.line_height + App.color(Menu_background_color) + love.graphics.rectangle('fill', x,y, w,h) + -- compute the x,y,width of the current index (in offsets from top left) + local x2,y2 = 0,0 + local width = 0 + for i,filename in ipairs(o.files) do + local filename_text = to_text(filename) + width = App.width(filename_text) + if x2 + width > App.screen.width - 5 then + y2 = y2 + Editor_state.line_height + x2 = 0 + end + if i == o.index then + break + end + x2 = x2 + width + 30 + end + -- figure out how much of the menu to display + local menu_xmin = math.max(0, x2-w/2) + local menu_xmax = math.min(App.screen.width, x2+w/2) + -- now selectively print out entries + local x3,y3 = 0,y -- x3 is relative, y3 is absolute + local width = 0 + for i,filename in ipairs(o.files) do + local filename_text = to_text(filename) + width = App.width(filename_text) + if x3 + width > App.screen.width - 5 then + y3 = y3 + Editor_state.line_height + x3 = 0 + end + if i == o.index then + App.color(Menu_highlight_color) + love.graphics.rectangle('fill', x + x3-menu_xmin - 5, y3-2, width+5*2, Editor_state.line_height+2*2) + end + if x3 >= menu_xmin and x3 + width < menu_xmax then + App.color(Menu_command_color) + App.screen.draw(filename_text, x + x3-menu_xmin, y3) + end + x3 = x3 + width + 30 + end + -- + return h+20 +end + function file_navigator_up() local y, x, width = file_coord(File_navigation.index) local index = file_index(y-Editor_state.line_height, x, width) diff --git a/source.lua b/source.lua index 3b16b9d..13a66cd 100644 --- a/source.lua +++ b/source.lua @@ -1,4 +1,5 @@ source = {} +log_render = {} Editor_state = {}